/* * (non-Javadoc) * @see org.jasig.portlet.weather.dao.IWeatherDao#find(java.lang.String) */ public Collection<Location> find(String location) { final String url = FIND_URL .replace("@KEY@", key) .replace("@QUERY@", QuietUrlCodec.encode(location, Constants.URL_ENCODING)); HttpMethod getMethod = new GetMethod(url); InputStream inputStream = null; try { // Execute the method. int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { final String statusText = getMethod.getStatusText(); throw new DataRetrievalFailureException( "get of '" + url + "' failed with status '" + statusCode + "' due to '" + statusText + "'"); } // Read the response body inputStream = getMethod.getResponseBodyAsStream(); List<Location> locations = deserializeSearchResults(inputStream); return locations; } catch (HttpException e) { throw new RuntimeException( "http protocol exception while getting data from weather service from: " + url, e); } catch (IOException e) { throw new RuntimeException( "IO exception while getting data from weather service from: " + url, e); } catch (JAXBException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } finally { // try to close the inputstream IOUtils.closeQuietly(inputStream); // release the connection getMethod.releaseConnection(); } }
protected Object getAndDeserialize(String url, TemperatureUnit unit) { HttpMethod getMethod = new GetMethod(url); InputStream inputStream = null; try { // Execute the method. int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { final String statusText = getMethod.getStatusText(); throw new DataRetrievalFailureException( "get of '" + url + "' failed with status '" + statusCode + "' due to '" + statusText + "'"); } // Read the response body inputStream = getMethod.getResponseBodyAsStream(); Weather weather = deserializeWeatherResult(inputStream, unit); return weather; } catch (HttpException e) { throw new RuntimeException( "http protocol exception while getting data from weather service from: " + url, e); } catch (IOException e) { throw new RuntimeException( "IO exception while getting data from weather service from: " + url, e); } catch (JAXBException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } catch (ParseException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } finally { // try to close the inputstream IOUtils.closeQuietly(inputStream); // release the connection getMethod.releaseConnection(); } }