Search in sources :

Example 1 with GeocoderException

use of org.opennms.features.geocoder.GeocoderException in project opennms by OpenNMS.

the class GoogleGeocoderService method ensureInitialized.

public void ensureInitialized() throws GeocoderException {
    if (m_geocoder == null) {
        final HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        if (notEmpty(m_clientId) && notEmpty(m_clientKey)) {
            try {
                LOG.info("Initializing Google Geocoder using Client ID and Key.");
                m_geocoder = new AdvancedGeoCoder(httpClient, m_clientId, m_clientKey);
            } catch (final InvalidKeyException e) {
                throw new GeocoderException("Unable to initialize Google Geocoder.", e);
            }
        }
        if (m_geocoder == null) {
            LOG.info("Initializing Google Geocoder using default configuration.");
            m_geocoder = new AdvancedGeoCoder(httpClient);
        }
        /* Configure proxying, if necessary... */
        final String httpProxyHost = System.getProperty("http.proxyHost");
        final Integer httpProxyPort = Integer.getInteger("http.proxyPort");
        if (httpProxyHost != null && httpProxyPort != null) {
            LOG.info("Proxy configuration found, using {}:{} as HTTP proxy.", httpProxyHost, httpProxyPort);
            httpClient.getHostConfiguration().setProxy(httpProxyHost, httpProxyPort);
        } else {
            LOG.info("No proxy configuration found.");
        }
        /* Limit retries... */
        httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, true));
        httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout);
        LOG.info("Google Geocoder initialized.");
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AdvancedGeoCoder(com.google.code.geocoder.AdvancedGeoCoder) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) InvalidKeyException(java.security.InvalidKeyException) GeocoderException(org.opennms.features.geocoder.GeocoderException) TemporaryGeocoderException(org.opennms.features.geocoder.TemporaryGeocoderException)

Example 2 with GeocoderException

use of org.opennms.features.geocoder.GeocoderException in project opennms by OpenNMS.

the class GoogleGeocoderService method getCoordinates.

@Override
public synchronized Coordinates getCoordinates(final String address) throws GeocoderException {
    ensureInitialized();
    final GeocoderRequest request = new GeocoderRequestBuilder().setAddress(address).setLanguage("en").getGeocoderRequest();
    GeocodeResponse response;
    try {
        response = m_geocoder.geocode(request);
    } catch (IOException e) {
        // Makes the assumption that IO related exceptions are temporary, which is suitable for most scenarios
        throw new TemporaryGeocoderException("Failed to get coordinates for " + address + " using the Google Geocoder.", e);
    }
    switch(response.getStatus()) {
        case OK:
            return new GoogleCoordinates(response.getResults().get(0));
        case OVER_QUERY_LIMIT:
            throw new TemporaryGeocoderException("Failed to get coordinates for " + address + " using the Google Geocoder.  You have exceeded the daily usage limit.");
        case ERROR:
        case INVALID_REQUEST:
        case REQUEST_DENIED:
        case UNKNOWN_ERROR:
        case ZERO_RESULTS:
        default:
            throw new GeocoderException("Failed to get coordinates for " + address + " using Google Geocoder.  Response was: " + response.getStatus().toString());
    }
}
Also used : GeocoderRequestBuilder(com.google.code.geocoder.GeocoderRequestBuilder) TemporaryGeocoderException(org.opennms.features.geocoder.TemporaryGeocoderException) GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) IOException(java.io.IOException) GeocoderException(org.opennms.features.geocoder.GeocoderException) TemporaryGeocoderException(org.opennms.features.geocoder.TemporaryGeocoderException) GeocoderRequest(com.google.code.geocoder.model.GeocoderRequest)

Example 3 with GeocoderException

use of org.opennms.features.geocoder.GeocoderException in project opennms by OpenNMS.

the class NominatimGeocoderService method getCoordinates.

@Override
public Coordinates getCoordinates(final String address) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(address));
    method.addHeader("User-Agent", "OpenNMS-NominatimGeocoderService/1.0");
    if (m_referer != null && !"".equals(m_referer)) {
        method.addHeader("Referer", m_referer);
    }
    InputStream responseStream = null;
    CloseableHttpResponse response = null;
    try {
        response = m_clientWrapper.execute(method);
        final StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new GeocoderException("Nominatim returned a non-OK response code: " + statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        responseStream = response.getEntity().getContent();
        final ElementTree tree = ElementTree.fromStream(responseStream);
        if (tree == null) {
            throw new GeocoderException("an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
        }
        final List<ElementTree> places = tree.findAll("//place");
        if (places.size() > 1) {
            m_log.warn("More than one location returned for query: {}", address);
        } else if (places.size() == 0) {
            throw new GeocoderException("Nominatim returned an OK status code, but no places");
        }
        final ElementTree place = places.get(0);
        final Float longitude = Float.valueOf(place.getAttribute("lon"));
        final Float latitude = Float.valueOf(place.getAttribute("lat"));
        return new Coordinates(longitude, latitude);
    } catch (final GeocoderException e) {
        throw e;
    } catch (final Throwable e) {
        throw new GeocoderException("unable to get lon/lat from Nominatim", e);
    } finally {
        IOUtils.closeQuietly(responseStream);
        m_clientWrapper.close(response);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Coordinates(org.opennms.features.geocoder.Coordinates) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GeocoderException(org.opennms.features.geocoder.GeocoderException) ElementTree(net.simon04.jelementtree.ElementTree)

Aggregations

GeocoderException (org.opennms.features.geocoder.GeocoderException)3 TemporaryGeocoderException (org.opennms.features.geocoder.TemporaryGeocoderException)2 AdvancedGeoCoder (com.google.code.geocoder.AdvancedGeoCoder)1 GeocoderRequestBuilder (com.google.code.geocoder.GeocoderRequestBuilder)1 GeocodeResponse (com.google.code.geocoder.model.GeocodeResponse)1 GeocoderRequest (com.google.code.geocoder.model.GeocoderRequest)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidKeyException (java.security.InvalidKeyException)1 ElementTree (net.simon04.jelementtree.ElementTree)1 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)1 StatusLine (org.apache.http.StatusLine)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 Coordinates (org.opennms.features.geocoder.Coordinates)1