camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject git commit: Polished the code base of the new Camel Weather component.
Date Mon, 27 May 2013 19:33:11 GMT
Updated Branches:
  refs/heads/master 1c7ed3d5f -> eb1fa8294


Polished the code base of the new Camel Weather component.

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

Branch: refs/heads/master
Commit: eb1fa8294925d8fb598831256a7411af2781aa36
Parents: 1c7ed3d
Author: Babak Vahdat <bvahdat@apache.org>
Authored: Mon May 27 21:33:06 2013 +0200
Committer: Babak Vahdat <bvahdat@apache.org>
Committed: Mon May 27 21:33:06 2013 +0200

----------------------------------------------------------------------
 .../camel/component/weather/WeatherComponent.java  |    6 +-
 .../component/weather/WeatherConfiguration.java    |  100 ++++++---------
 .../camel/component/weather/WeatherConstants.java  |   30 +++++
 .../camel/component/weather/WeatherConsumer.java   |   44 +++----
 .../camel/component/weather/WeatherEndpoint.java   |    5 +-
 .../camel/component/weather/WeatherUnits.java      |   26 ++++
 .../component/weather/BaseWeatherConsumerTest.java |   16 +--
 .../weather/CurrentWeatherConsumerTest.java        |    3 +-
 .../weather/CurrentWeatherMadridConsumerTest.java  |    2 +
 .../Forecast7WeatherMadridConsumerTest.java        |    5 +-
 10 files changed, 126 insertions(+), 111 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherComponent.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherComponent.java
index d21ad1d..3ddae29 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherComponent.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherComponent.java
@@ -23,13 +23,12 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.impl.UriEndpointComponent;
 
 /**
- * An <a href="http://camel.apache.org/weather.html">Weather Component</a>.
+ * A <a href="http://camel.apache.org/weather.html">Weather Component</a>.
  * <p/>
  * Camel uses <a href="http://openweathermap.org/api#weather">Open Weather</a>
to get the information.
  */
 public class WeatherComponent extends UriEndpointComponent {
 
-
     public WeatherComponent() {
         super(WeatherEndpoint.class);
     }
@@ -38,8 +37,9 @@ public class WeatherComponent extends UriEndpointComponent {
         super(context, WeatherEndpoint.class);
     }
 
+    @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
-        WeatherConfiguration configuration = new WeatherConfiguration();
+        WeatherConfiguration configuration = new WeatherConfiguration(this);
 
         // and then override from parameters
         setProperties(configuration, parameters);

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java
index 321bc6f..8a932f4 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.component.weather;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Scanner;
 
@@ -26,6 +23,10 @@ import org.apache.camel.spi.UriParam;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
 
+import static org.apache.camel.component.weather.WeatherUnits.METRIC;
+import static org.apache.camel.util.ObjectHelper.isEmpty;
+import static org.apache.camel.util.ObjectHelper.notNull;
+
 public class WeatherConfiguration {
 
     @UriParam
@@ -33,42 +34,36 @@ public class WeatherConfiguration {
     @UriParam
     private String period = "";
     @UriParam
-    private String units = "metric";
+    private WeatherUnits units = METRIC;
+    private WeatherComponent component;
 
+    public WeatherConfiguration(WeatherComponent component) {
+        this.component = notNull(component, "component");
+    }
 
     public String getPeriod() {
         return period;
     }
 
     public void setPeriod(String period) {
-        if (period != null) {
-            int result = 0;
-            try {
-                result = new Scanner(period).useDelimiter("\\D+").nextInt();
-            } catch (Throwable e) {
-            }
-            if (result != 0) {
-                this.period = "" + result;
-            } else {
-                this.period = "";
-            }
+        notNull(period, "period");
+        int result = 0;
+        try {
+            result = new Scanner(period).useDelimiter("\\D+").nextInt();
+        } catch (Throwable e) {
+            // ignore and fallback the period to be an empty string
+        }
+        if (result != 0) {
+            this.period = "" + result;
         }
     }
 
-    public String getUnits() {
+    public WeatherUnits getUnits() {
         return units;
     }
 
-
-    public void setUnits(String units) {
-        if (units != null) {
-            units = units.trim();
-            if (units.equalsIgnoreCase("IMPERIAL")) {
-                this.units = "imperial";
-            } else {
-                this.units = "metric";
-            }
-        }
+    public void setUnits(WeatherUnits units) {
+        this.units = notNull(units, "units");
     }
 
     public String getLocation() {
@@ -79,56 +74,37 @@ public class WeatherConfiguration {
         this.location = location;
     }
 
-    public String getQuery() {
+    public String getQuery() throws Exception {
         String result = "http://api.openweathermap.org/data/2.5/";
         String location = "";
-        if (getLocation() == null || getLocation().isEmpty()) {
+        if (isEmpty(getLocation())) {
             location = getGeoLocation();
         } else {
-            //assuming the location is a town,country
+            // assuming the location is a town or country
             location = "q=" + getLocation();
         }
-        if (getPeriod() != null && getPeriod().isEmpty()) {
 
+        if (isEmpty(getPeriod())) {
             result += "weather?" + location;
-
         } else {
             result += "forecast/daily?" + location + "&cnt=" + getPeriod();
         }
-        result += "&units=" + this.units;
+        result += "&units=" + units.name().toLowerCase();
+
         return result;
     }
 
-    private String getGeoLocation() {
-        final String LATITUDE = "latitude";
-        final String LONGITUDE = "longitude";
-        String result = "";
-
-        try {
-            String urlStr = "http://freegeoip.net/json/";
-            URL url = new URL(urlStr);
-            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
-            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
-            String inputLine;
-            String temp = "";
-            while ((inputLine = in.readLine()) != null) {
-                temp += inputLine;
-            }
-            in.close();
-            urlConnection.disconnect();
-
-            if (temp != null && !temp.isEmpty()) {
-                ObjectMapper mapper = new ObjectMapper();
-                JsonNode node = mapper.readValue(temp, JsonNode.class);
-                JsonNode latitudeNode = node.get(LATITUDE);
-                JsonNode longitudeNode = node.get(LONGITUDE);
-                if (latitudeNode != null && longitudeNode != null) {
-                    result = "lat=" + latitudeNode.toString() + "&lon=" + longitudeNode.toString();
-                }
-            }
-        } catch (Exception e) {
-            //this is going to fail if using this offline
+    private String getGeoLocation() throws Exception {
+        String geoLocation = component.getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
new URL("http://freegeoip.net/json/"));
+        if (isEmpty(geoLocation)) {
+            throw new IllegalStateException("Retrieved an unexpected value: '" + geoLocation
+ "' for the geographical location");
         }
-        return result;
+
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode node = mapper.readValue(geoLocation, JsonNode.class);
+        JsonNode latitudeNode = notNull(node.get("latitude"), "latitude");
+        JsonNode longitudeNode = notNull(node.get("longitude"), "longitude");
+
+        return "lat=" + latitudeNode.toString() + "&lon=" + longitudeNode.toString();
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java
new file mode 100644
index 0000000..a386580
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java
@@ -0,0 +1,30 @@
+/**
+ * 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.weather;
+
+/**
+ * The Weather constants
+ */
+public final class WeatherConstants {
+
+    public static final String WEATHER_QUERY = "CamelWeatherQuery";
+
+    private WeatherConstants() {
+        // utility class
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java
index 219a29c..d8a906f 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java
@@ -16,51 +16,39 @@
  */
 package org.apache.camel.component.weather;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
 import java.net.URL;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.ScheduledPollConsumer;
+import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class WeatherConsumer extends ScheduledPollConsumer {
+    public static final long DEFAULT_CONSUMER_DELAY = 60 * 60 * 1000L;
     private static final transient Logger LOG = LoggerFactory.getLogger(WeatherConsumer.class);
-    public static final long DEFAULT_CONSUMER_DELAY = 60 * 60 *1000L;
-    private final WeatherEndpoint endpoint;
-    private String query;
-
+    private final String query;
 
     public WeatherConsumer(WeatherEndpoint endpoint, Processor processor, String query) {
         super(endpoint, processor);
-        this.endpoint = endpoint;
         this.query = query;
     }
 
+    @Override
     protected int poll() throws Exception {
-        LOG.debug("Executing Weather Query " + this.query);
-        URL url = new URL(this.query);
-        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
-        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
-        String inputLine;
-        String answer = "";
-        while ((inputLine = in.readLine()) != null) {
-            answer += inputLine;
-        }
-        in.close();
-        urlConnection.disconnect();
-        LOG.debug("result = " + answer);
-        if (answer != null && !answer.isEmpty()) {
-            Exchange exchange = endpoint.createExchange();
-            exchange.getIn().setBody(answer);
-            exchange.setProperty("query", this.query);
-            getProcessor().process(exchange);
-            return 1;
-        } else {
-            return 0;
+        LOG.debug("Going to execute the query '{}'", query);
+        String answer = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
new URL(query));
+        LOG.debug("Got back the answer '{}'", answer);
+        if (ObjectHelper.isEmpty(answer)) {
+            throw new IllegalStateException("Got the empty string '" + answer + "' as the
result of the query '" + query + "'");
         }
+
+        Exchange exchange = getEndpoint().createExchange();
+        exchange.getIn().setBody(answer);
+        exchange.getIn().setHeader(WeatherConstants.WEATHER_QUERY, query);
+        getProcessor().process(exchange);
+
+        return 1;
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherEndpoint.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherEndpoint.java
index 3bf5831..a23cd42 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherEndpoint.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherEndpoint.java
@@ -23,9 +23,8 @@ import org.apache.camel.impl.DefaultPollingEndpoint;
 import org.apache.camel.spi.UriParam;
 
 /**
- *
+ * Weather Endpoint
  */
-
 public class WeatherEndpoint extends DefaultPollingEndpoint {
     @UriParam
     private WeatherConfiguration configuration;
@@ -45,8 +44,8 @@ public class WeatherEndpoint extends DefaultPollingEndpoint {
         return true;
     }
 
+    @Override
     public Consumer createConsumer(Processor processor) throws Exception {
-
         WeatherConsumer answer = new WeatherConsumer(this, processor, this.configuration.getQuery());
 
         // ScheduledPollConsumer default delay is 500 millis and that is too often for polling
a feed,

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
new file mode 100644
index 0000000..ef7888f
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
@@ -0,0 +1,26 @@
+/**
+ * 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.weather;
+
+/**
+ * The possible units for temperature measurement
+ */
+public enum WeatherUnits {
+
+    IMPERIAL, METRIC;
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java
b/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java
index a6e0906..8b8754e 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java
@@ -18,7 +18,6 @@ package org.apache.camel.component.weather;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
@@ -28,7 +27,8 @@ public abstract class BaseWeatherConsumerTest extends CamelTestSupport {
     @Test
     public void testGrabbingListOfEntries() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMinimumMessageCount(1);
+        // as the default delay option is one hour long, we expect exactly one message exchange
+        mock.expectedMessageCount(1);
         mock.assertIsSatisfied();
 
         Exchange exchange = mock.getExchanges().get(0);
@@ -36,16 +36,8 @@ public abstract class BaseWeatherConsumerTest extends CamelTestSupport
{
         Message in = exchange.getIn();
         assertNotNull(in);
         assertNotNull(in.getBody());
-        assertIsInstanceOf(String.class,in.getBody());
-        assertStringContains(in.getBody().toString(),"temp");
-    }
-
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() throws Exception {
-                from("weather:foo").to("mock:result");
-            }
-        };
+        String body = assertIsInstanceOf(String.class, in.getBody());
+        assertStringContains(body, "temp");
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
index c7d553d..79d7973 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
@@ -20,9 +20,10 @@ import org.apache.camel.builder.RouteBuilder;
 
 public class CurrentWeatherConsumerTest extends BaseWeatherConsumerTest {
 
-
+    @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
+            @Override
             public void configure() throws Exception {
                 from("weather:foo").to("mock:result");
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridConsumerTest.java
b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridConsumerTest.java
index c0a0969..acdde0c 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridConsumerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridConsumerTest.java
@@ -20,8 +20,10 @@ import org.apache.camel.builder.RouteBuilder;
 
 public class CurrentWeatherMadridConsumerTest extends BaseWeatherConsumerTest {
 
+    @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
+            @Override
             public void configure() throws Exception {
                 from("weather:foo?location=Madrid,Spain").to("mock:result");
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb1fa829/components/camel-weather/src/test/java/org/apache/camel/component/weather/Forecast7WeatherMadridConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/Forecast7WeatherMadridConsumerTest.java
b/components/camel-weather/src/test/java/org/apache/camel/component/weather/Forecast7WeatherMadridConsumerTest.java
index daa5afe..3d1e5c7 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/Forecast7WeatherMadridConsumerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/Forecast7WeatherMadridConsumerTest.java
@@ -20,11 +20,12 @@ import org.apache.camel.builder.RouteBuilder;
 
 public class Forecast7WeatherMadridConsumerTest extends BaseWeatherConsumerTest {
 
-
+    @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
+            @Override
             public void configure() throws Exception {
-                from("weather:foo?location=Madrid,Spain&period=7 days").to("mock:result");
+                from("weather:foo?location=Madrid,Spain&period=7 days&units=IMPERIAL").to("mock:result");
             }
         };
     }


Mime
View raw message