You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/06/06 07:54:01 UTC

git commit: CAMEL-6431: First spike of camel-geocoder component.

Updated Branches:
  refs/heads/master 825db82a0 -> c382286c6


CAMEL-6431: First spike of camel-geocoder component.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c382286c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c382286c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c382286c

Branch: refs/heads/master
Commit: c382286c6411c80aa5a53c456b5f152f1ba79e13
Parents: 825db82
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jun 6 07:48:54 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jun 6 07:48:54 2013 +0200

----------------------------------------------------------------------
 components/camel-geocoder/pom.xml                  |    5 +
 .../component/geocoder/GeoCoderConstants.java      |    3 +
 .../camel/component/geocoder/GeoCoderEndpoint.java |    9 ++
 .../camel/component/geocoder/GeoCoderProducer.java |   93 +++++++++++++--
 .../geocoder/GeoCoderComponentAddressTest.java     |    2 +-
 .../geocoder/GeoCoderCurrentAddressTest.java       |   49 ++++++++
 .../geocoder/GeoCoderLatLngComponentTest.java      |    4 +-
 7 files changed, 155 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/pom.xml b/components/camel-geocoder/pom.xml
index 7a46f40..2d09ae7 100644
--- a/components/camel-geocoder/pom.xml
+++ b/components/camel-geocoder/pom.xml
@@ -45,6 +45,11 @@
       <artifactId>geocoder-java</artifactId>
       <version>${geocoder-java-version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.jackson</groupId>
+      <artifactId>jackson-mapper-asl</artifactId>
+      <version>${jackson-version}</version>
+    </dependency>
 
     <!-- testing -->
     <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderConstants.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderConstants.java
index 0f4a810..461ac54 100644
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderConstants.java
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderConstants.java
@@ -21,6 +21,9 @@ public final class GeoCoderConstants {
     public static final String ADDRESS = "CamelGeoCoderAddress";
     public static final String LATLNG = "CamelGeoCoderLatlng";
     public static final String STATUS = "CamelGeoCoderStatus";
+    public static final String REGION_CODE = "CamelGeoCoderRegionCode";
+    public static final String REGION_NAME = "CamelGeoCoderRegionName";
+    public static final String CITY = "CamelGeoCoderCity";
     public static final String COUNTRY_LONG = "CamelGeoCoderCountryLong";
     public static final String COUNTRY_SHORT = "CamelGeoCoderCountryShort";
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
index 5ed6ad6..9613d48 100644
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
@@ -29,6 +29,7 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
     private String address;
     private String latlng;
     private String language = "en";
+    private boolean headersOnly;
 
     public GeoCoderEndpoint() {
     }
@@ -72,4 +73,12 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
     public void setLatlng(String latlng) {
         this.latlng = latlng;
     }
+
+    public boolean isHeadersOnly() {
+        return headersOnly;
+    }
+
+    public void setHeadersOnly(boolean headersOnly) {
+        this.headersOnly = headersOnly;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
index 70878a4..435bc7c 100644
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.geocoder;
 
 import java.math.BigDecimal;
+import java.net.URL;
 
 import com.google.code.geocoder.Geocoder;
 import com.google.code.geocoder.model.GeocodeResponse;
@@ -28,9 +29,14 @@ import com.google.code.geocoder.model.LatLng;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.camel.util.ObjectHelper.isEmpty;
+import static org.apache.camel.util.ObjectHelper.notNull;
+
 /**
  * The GeoCoder producer.
  */
@@ -73,20 +79,76 @@ public class GeoCoderProducer extends DefaultProducer {
                 extractGeoResult(res, exchange);
             }
         } else if (address != null) {
-            GeocoderRequest req = new GeocoderRequest(address, endpoint.getLanguage());
-            LOG.debug("Geocode for address {}", address);
-            GeocodeResponse res = geocoder.geocode(req);
-            LOG.debug("Geocode response {}", res);
 
-            if (res != null) {
-                extractGeoResult(res, exchange);
+            // 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);
+                }
             }
         }
     }
 
+    protected void processCurrentLocation(Exchange exchange) throws Exception {
+        LOG.debug("Geocode for current address");
+        String json = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, new URL("http://freegeoip.net/json/"));
+        if (isEmpty(json)) {
+            throw new IllegalStateException("Got the unexpected value '" + json + "' for the geolocation");
+        }
+        LOG.debug("Geocode response {}", json);
+
+        exchange.getIn().setHeader(GeoCoderConstants.STATUS, GeocoderStatus.OK);
+
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode node = mapper.readValue(json, JsonNode.class);
+
+        JsonNode latitudeNode = notNull(node.get("latitude"), "latitude");
+        JsonNode longitudeNode = notNull(node.get("longitude"), "longitude");
+        String resLatlng = latitudeNode.asText() + "," + longitudeNode.asText();
+        exchange.getIn().setHeader(GeoCoderConstants.LATLNG, resLatlng);
+
+        JsonNode country_code = node.get("country_code");
+        JsonNode country_name = node.get("country_name");
+        if (country_code != null) {
+            exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_SHORT, country_code.asText());
+        }
+        if (country_name != null) {
+            exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_LONG, country_name.asText());
+        }
+
+        JsonNode region_code = node.get("region_code");
+        JsonNode region_name = node.get("region_name");
+        if (region_code != null) {
+            exchange.getIn().setHeader(GeoCoderConstants.REGION_CODE, region_code.asText());
+        }
+        if (region_name != null) {
+            exchange.getIn().setHeader(GeoCoderConstants.REGION_NAME, region_name.asText());
+        }
+
+        JsonNode city = node.get("city");
+        if (city != null) {
+            exchange.getIn().setHeader(GeoCoderConstants.CITY, city.asText());
+        }
+
+        // should we include body
+        if (!endpoint.isHeadersOnly()) {
+            exchange.getIn().setBody(json);
+        }
+    }
+
     protected void extractGeoResult(GeocodeResponse res, Exchange exchange) {
         exchange.getIn().setHeader(GeoCoderConstants.STATUS, res.getStatus());
-        exchange.getIn().setBody(res);
+        // should we include body
+        if (!endpoint.isHeadersOnly()) {
+            exchange.getIn().setBody(res);
+        }
 
         if (res.getStatus() == GeocoderStatus.OK) {
             exchange.getIn().setHeader(GeoCoderConstants.ADDRESS, res.getResults().get(0).getFormattedAddress());
@@ -101,12 +163,16 @@ public class GeoCoderProducer extends DefaultProducer {
                 exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_SHORT, country.getShortName());
                 exchange.getIn().setHeader(GeoCoderConstants.COUNTRY_LONG, country.getLongName());
             }
+
+            GeocoderAddressComponent city = getCity(res);
+            if (city != null) {
+                exchange.getIn().setHeader(GeoCoderConstants.CITY, city.getLongName());
+            }
         }
     }
 
     private static GeocoderAddressComponent getCountry(GeocodeResponse res) {
         for (GeocoderResult result : res.getResults()) {
-            // look for "country" in types
             for (String type : result.getTypes()) {
                 if ("country".equals(type)) {
                     return result.getAddressComponents().get(0);
@@ -116,4 +182,15 @@ public class GeoCoderProducer extends DefaultProducer {
         return null;
     }
 
+    private static GeocoderAddressComponent getCity(GeocodeResponse res) {
+        for (GeocoderResult result : res.getResults()) {
+            for (String type : result.getTypes()) {
+                if ("locality".equals(type)) {
+                    return result.getAddressComponents().get(0);
+                }
+            }
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderComponentAddressTest.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderComponentAddressTest.java b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderComponentAddressTest.java
index f5f0952..37f10a8 100644
--- a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderComponentAddressTest.java
+++ b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderComponentAddressTest.java
@@ -41,7 +41,7 @@ public class GeoCoderComponentAddressTest extends CamelTestSupport {
                 from("direct:start")
                   .to("geocoder:address:Paris, France")
                   .to("log:result")
-                  .log("Location ${header.CamelGeocoderAddress} is at lat/lng: ${header.CamelGeocoderLatlng}")
+                  .log("Location ${header.CamelGeocoderAddress} is at lat/lng: ${header.CamelGeocoderLatlng} in city ${header.CamelGeocoderCity}")
                   .to("mock:result");
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderCurrentAddressTest.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderCurrentAddressTest.java b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderCurrentAddressTest.java
new file mode 100644
index 0000000..6f351c1
--- /dev/null
+++ b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderCurrentAddressTest.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.geocoder;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class GeoCoderCurrentAddressTest extends CamelTestSupport {
+
+    @Test
+    public void testGeoCoder() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello");
+        mock.message(0).header(GeoCoderConstants.LATLNG).isNotNull();
+
+        template.sendBody("direct:start", "Hello");
+        
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                  .to("geocoder:address:current?headersOnly=true")
+                  .to("log:result")
+                  .log("You are at ${header.CamelGeoCoderLatlng} in city ${header.CamelGeoCoderCity} in country ${header.CamelGeoCoderCountryLong}")
+                  .to("mock:result");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c382286c/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderLatLngComponentTest.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderLatLngComponentTest.java b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderLatLngComponentTest.java
index 9761246..ed6b631 100644
--- a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderLatLngComponentTest.java
+++ b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderLatLngComponentTest.java
@@ -28,6 +28,7 @@ public class GeoCoderLatLngComponentTest extends CamelTestSupport {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
         mock.expectedHeaderReceived(GeoCoderConstants.COUNTRY_SHORT, "US");
+        mock.expectedHeaderReceived(GeoCoderConstants.CITY, "New York");
 
         // the address header overrides the endpoint configuration
         template.sendBody("direct:start", "Hello");
@@ -42,7 +43,8 @@ public class GeoCoderLatLngComponentTest extends CamelTestSupport {
                 from("direct:start")
                   .to("geocoder:latlng:40.714224,-73.961452")
                   .to("log:result")
-                  .log("Location ${header.CamelGeocoderAddress} is at lat/lng: ${header.CamelGeocoderLatlng} and in country ${header.CamelGeoCoderCountryShort}")
+                  .log("Location ${header.CamelGeocoderAddress} is at lat/lng: ${header.CamelGeocoderLatlng}"
+                          + " and in city ${header.CamelGeoCoderCity} in country ${header.CamelGeoCoderCountryLong}")
                   .to("mock:result");
             }
         };