Skip to content

Commit

Permalink
Refinement for #55
Browse files Browse the repository at this point in the history
@ansell suggested putting reader.close into finally, which should prevent too many resources being left open.
  • Loading branch information
nickdos committed Nov 14, 2018
1 parent 62828d3 commit 9054a72
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions grails-app/controllers/au/org/ala/bie/ExternalSiteController.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* Copyright (C) 2018 Atlas of Living Australia
* All Rights Reserved.
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/

package au.org.ala.bie

import grails.converters.JSON
Expand Down Expand Up @@ -107,22 +120,27 @@ class ExternalSiteController {
*/
def proxyAutocomplete = {
def url = ( "${grailsApplication.config.getProperty("bie.index.url")}/search/auto.json" + params.toQueryString() ).toURL()
HttpURLConnection connection = (HttpURLConnection) url.openConnection()
connection.setRequestMethod("GET")
connection.connect()
StringBuilder content = new StringBuilder()
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))
String line
// read from the connection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n")
BufferedReader bufferedReader

try {
HttpURLConnection connection = url.openConnection()
connection.setRequestMethod("GET")
connection.connect()
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))
String line
// read from the connection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n")
}
response.setContentType(connection.getContentType())
response.status = connection.getResponseCode()
render content.toString() //render url.getText()
} catch (Exception e) {
// will bubble up to Grails and trigger an error page
log.error "${e.message}", e
} finally {
bufferedReader.close()
}
bufferedReader.close()

response.setContentType(connection.getContentType())
response.status = connection.getResponseCode()

render content.toString() //render url.getText()
}
}

0 comments on commit 9054a72

Please sign in to comment.