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.");
}
}
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());
}
}
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);
}
}
Aggregations