Search in sources :

Example 1 with GeocodeResponse

use of com.google.code.geocoder.model.GeocodeResponse 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 2 with GeocodeResponse

use of com.google.code.geocoder.model.GeocodeResponse in project camel by apache.

the class GeoCoderProducer method process.

public void process(Exchange exchange) throws Exception {
    // header take precedence
    String address = exchange.getIn().getHeader(GeoCoderConstants.ADDRESS, String.class);
    if (address == null) {
        address = endpoint.getAddress();
    }
    String latlng = exchange.getIn().getHeader(GeoCoderConstants.LATLNG, String.class);
    if (latlng == null) {
        latlng = endpoint.getLatlng();
    }
    if (latlng != null) {
        GeocoderRequest req = new GeocoderRequest();
        req.setLanguage(endpoint.getLanguage());
        String lat = ObjectHelper.before(latlng, ",");
        String lng = ObjectHelper.after(latlng, ",");
        req.setLocation(new LatLng(lat, lng));
        LOG.debug("Geocode for lat/lng {}", latlng);
        GeocodeResponse res = geocoder.geocode(req);
        LOG.debug("Geocode response {}", res);
        if (res != null) {
            extractGeoResult(res, exchange);
        }
    } else if (address != null) {
        // is it current address
        if ("current".equals(address)) {
            processCurrentLocation(exchange);
        } else {
            LOG.debug("Geocode for address {}", address);
            GeocoderRequest req = new GeocoderRequest(address, endpoint.getLanguage());
            GeocodeResponse res = geocoder.geocode(req);
            LOG.debug("Geocode response {}", res);
            if (res != null) {
                extractGeoResult(res, exchange);
            }
        }
    }
}
Also used : GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) LatLng(com.google.code.geocoder.model.LatLng) GeocoderRequest(com.google.code.geocoder.model.GeocoderRequest)

Example 3 with GeocodeResponse

use of com.google.code.geocoder.model.GeocodeResponse in project wildfly-camel by wildfly-extras.

the class GeocoderIntegrationTest method testGeocoderComponent.

@Test
public void testGeocoderComponent() throws Exception {
    HttpClientConfigurer configurer = new GeocoderHttpClientConfigurer();
    initialContext.bind("httpClientConfigurer", configurer);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("geocoder:address:London, England?httpClientConfigurer=#httpClientConfigurer");
        }
    });
    camelctx.start();
    try {
        // Geocoder API is sometimes flaky so retry the request if necessary
        GeocodeResponse result = null;
        int count = 0;
        while (count < 5) {
            ProducerTemplate template = camelctx.createProducerTemplate();
            result = template.requestBody("direct:start", null, GeocodeResponse.class);
            Assert.assertNotNull("Geocoder response is null", result);
            // Skip further testing if we exceeded the API rate limit
            GeocoderStatus status = result.getStatus();
            Assume.assumeFalse("Geocoder API rate limit exceeded", status.equals(OVER_QUERY_LIMIT));
            if (status.equals(OK)) {
                break;
            }
            Thread.sleep(1000);
            count++;
        }
        List<GeocoderResult> results = result.getResults();
        Assert.assertNotNull("Geocoder results is null", result);
        Assert.assertEquals("Expected 1 GeocoderResult to be returned", 1, results.size());
        LatLng location = results.get(0).getGeometry().getLocation();
        Assert.assertEquals("51.5073509", location.getLat().toPlainString());
        Assert.assertEquals("-0.1277583", location.getLng().toPlainString());
    } finally {
        camelctx.stop();
        initialContext.unbind("httpClientConfigurer");
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) GeocoderStatus(com.google.code.geocoder.model.GeocoderStatus) HttpClientConfigurer(org.apache.camel.component.geocoder.http.HttpClientConfigurer) GeocoderHttpClientConfigurer(org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) GeocoderHttpClientConfigurer(org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer) GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) LatLng(com.google.code.geocoder.model.LatLng) GeocoderResult(com.google.code.geocoder.model.GeocoderResult) Test(org.junit.Test)

Example 4 with GeocodeResponse

use of com.google.code.geocoder.model.GeocodeResponse in project camel by apache.

the class GeoCoderProxyTest method testGeoCoder.

@Test
public void testGeoCoder() throws Exception {
    GeoCoderEndpoint endpoint = context.getEndpoint("geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=3128&proxyAuthMethod=Basic&proxyAuthUsername=proxy&proxyAuthPassword=proxy", GeoCoderEndpoint.class);
    Geocoder geocoder = endpoint.createGeocoder();
    GeocoderRequest req = new GeocoderRequest();
    req.setLocation(new LatLng("45.4643", "9.1895"));
    GeocodeResponse res = geocoder.geocode(req);
    log.info("Response {} ", res);
}
Also used : GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) LatLng(com.google.code.geocoder.model.LatLng) Geocoder(com.google.code.geocoder.Geocoder) GeocoderRequest(com.google.code.geocoder.model.GeocoderRequest) Test(org.junit.Test)

Aggregations

GeocodeResponse (com.google.code.geocoder.model.GeocodeResponse)4 GeocoderRequest (com.google.code.geocoder.model.GeocoderRequest)3 LatLng (com.google.code.geocoder.model.LatLng)3 Test (org.junit.Test)2 Geocoder (com.google.code.geocoder.Geocoder)1 GeocoderRequestBuilder (com.google.code.geocoder.GeocoderRequestBuilder)1 GeocoderResult (com.google.code.geocoder.model.GeocoderResult)1 GeocoderStatus (com.google.code.geocoder.model.GeocoderStatus)1 IOException (java.io.IOException)1 CamelContext (org.apache.camel.CamelContext)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 HttpClientConfigurer (org.apache.camel.component.geocoder.http.HttpClientConfigurer)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 GeocoderException (org.opennms.features.geocoder.GeocoderException)1 TemporaryGeocoderException (org.opennms.features.geocoder.TemporaryGeocoderException)1 GeocoderHttpClientConfigurer (org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer)1