You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/07/18 06:49:32 UTC

[camel] 02/07: CAMEL-12658 - camel-weather: Freegeoip service is no longer avaiable, we need to switch to apilayer IPstack

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f5583a1a05bbdc2f0c2f01baa3064f54b5a9c9ed
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Tue Jul 17 16:27:04 2018 +0200

    CAMEL-12658 - camel-weather: Freegeoip service is no longer avaiable, we need to switch to apilayer IPstack
---
 .../src/main/docs/weather-component.adoc           | 15 ++++++++--
 .../camel/component/weather/WeatherComponent.java  | 32 +++++++++++++++++++++-
 .../component/weather/WeatherConfiguration.java    | 30 +++++++++++++++++++-
 .../geolocation/FreeGeoIpGeoLocationProvider.java  | 16 +++++++++--
 .../weather/CurrentWeatherConsumerHtmlTest.java    |  2 +-
 .../weather/CurrentWeatherMadridProducerTest.java  |  2 +-
 .../springboot/WeatherComponentConfiguration.java  | 25 +++++++++++++++++
 7 files changed, 114 insertions(+), 8 deletions(-)

diff --git a/components/camel-weather/src/main/docs/weather-component.adoc b/components/camel-weather/src/main/docs/weather-component.adoc
index dc0c155..d25f407 100644
--- a/components/camel-weather/src/main/docs/weather-component.adoc
+++ b/components/camel-weather/src/main/docs/weather-component.adoc
@@ -42,7 +42,17 @@ definition of the weather endpoint using the appid paramĀ !
 
 
 // component options: START
-The Weather component has no options.
+The Weather component supports 3 options, which are listed below.
+
+
+
+[width="100%",cols="2,5,^1,2",options="header"]
+|===
+| Name | Description | Default | Type
+| *geolocationAccessKey* (common) | The geolocation service now needs an accessKey to be used |  | String
+| *geolocationRequestHost IP* (common) | The geolocation service now needs to specify the IP associated to the accessKey you're using |  | String
+| *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean
+|===
 // component options: END
 
 
@@ -66,7 +76,7 @@ with the following path and query parameters:
 |===
 
 
-==== Query Parameters (43 parameters):
+==== Query Parameters (44 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -115,6 +125,7 @@ with the following path and query parameters:
 | *proxyAuthUsername* (proxy) | Username for proxy authentication |  | String
 | *proxyHost* (proxy) | The proxy host name |  | String
 | *proxyPort* (proxy) | The proxy port number |  | Integer
+| *geolocationAccessKey* (security) | The geolocation service now needs an accessKey to be used |  | String
 |===
 // endpoint options: END
 
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 557af0f..a49cd3a 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
@@ -25,6 +25,7 @@ import org.apache.camel.component.weather.http.AuthenticationMethod;
 import org.apache.camel.component.weather.http.CompositeHttpConfigurer;
 import org.apache.camel.component.weather.http.HttpClientConfigurer;
 import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.spi.Metadata;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpConnectionManager;
@@ -38,6 +39,9 @@ import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 public class WeatherComponent extends UriEndpointComponent {
 
     private HttpClient httpClient;
+    private String geolocationAccessKey;
+    private String geolocationRequestHostIP;
+
 
     public WeatherComponent() {
         super(WeatherEndpoint.class);
@@ -49,12 +53,14 @@ public class WeatherComponent extends UriEndpointComponent {
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        WeatherConfiguration configuration = new WeatherConfiguration(this);
+    	WeatherConfiguration configuration = new WeatherConfiguration(this);
 
         // and then override from parameters
         setProperties(configuration, parameters);
 
         httpClient = createHttpClient(configuration);
+        geolocationAccessKey = configuration.getGeolocationAccessKey();
+        geolocationRequestHostIP = configuration.getGeolocationRequestHostIP();
         WeatherEndpoint endpoint = new WeatherEndpoint(uri, this, configuration);
         return endpoint;
     }
@@ -129,4 +135,28 @@ public class WeatherComponent extends UriEndpointComponent {
     public HttpClient getHttpClient() {
         return httpClient;
     }
+
+	public String getGeolocationAccessKey() {
+		return geolocationAccessKey;
+	}
+	
+    /**
+     * The geolocation service now needs an accessKey to be used
+     */
+	public void setGeolocationAccessKey(String geolocationAccessKey) {
+		this.geolocationAccessKey = geolocationAccessKey;
+	}
+
+	public String getGeolocationRequestHostIP() {
+		return geolocationRequestHostIP;
+	}
+
+    /**
+     * The geolocation service now needs to specify the IP associated to the accessKey you're using
+     */
+	public void setGeolocationRequestHostIP(String geolocationRequestHostIP) {
+		this.geolocationRequestHostIP = geolocationRequestHostIP;
+	}
+	
+
 }
\ No newline at end of file
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 36feb71..92e90c6 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
@@ -21,6 +21,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Scanner;
 
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.weather.geolocation.FreeGeoIpGeoLocationProvider;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
@@ -90,11 +91,15 @@ public class WeatherConfiguration {
     private String proxyAuthHost;
     @UriParam(label = "advanced")
     private HttpConnectionManager httpConnectionManager;
+    @UriParam(label = "security")
+    private String geolocationAccessKey;
+    @Metadata(label = "security")
+    private String geolocationRequestHostIP;
 
     public WeatherConfiguration(WeatherComponent component) {
         this.component = notNull(component, "component");
         weatherQuery = new WeatherQuery(this);
-        FreeGeoIpGeoLocationProvider geoLocationProvider = new FreeGeoIpGeoLocationProvider(component);
+        FreeGeoIpGeoLocationProvider geoLocationProvider = new FreeGeoIpGeoLocationProvider(component, geolocationAccessKey);
         weatherQuery.setGeoLocationProvider(geoLocationProvider);
     }
 
@@ -407,4 +412,27 @@ public class WeatherConfiguration {
     public void setWeatherApi(WeatherApi weatherApi) {
         this.weatherApi = weatherApi;
     }
+
+	public String getGeolocationAccessKey() {
+		return geolocationAccessKey;
+	}
+
+
+    /**
+     * The geolocation service now needs an accessKey to be used
+     */
+	public void setGeolocationAccessKey(String geolocationAccessKey) {
+		this.geolocationAccessKey = geolocationAccessKey;
+	}
+
+	public String getGeolocationRequestHostIP() {
+		return geolocationRequestHostIP;
+	}
+	
+    /**
+     * The geolocation service now needs to specify the IP associated to the accessKey you're using
+     */
+	public void setGeolocationRequestHostIP(String geolocationRequestHostIP) {
+		this.geolocationRequestHostIP = geolocationRequestHostIP;
+	}
 }
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
index 6c41bf3..e0ae56b 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.weather.geolocation;
 import org.apache.camel.component.weather.WeatherComponent;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
@@ -30,14 +31,25 @@ public class FreeGeoIpGeoLocationProvider implements GeoLocationProvider {
 
     private final WeatherComponent component;
 
-    public FreeGeoIpGeoLocationProvider(WeatherComponent component) {
+    public FreeGeoIpGeoLocationProvider(WeatherComponent component, String accessKey) {
         this.component = component;
     }
 
     @Override
     public GeoLocation getCurrentGeoLocation() throws Exception {
         HttpClient httpClient = component.getHttpClient();
-        GetMethod getMethod = new GetMethod("https://freegeoip.net/json/");
+        if (isEmpty(component.getGeolocationAccessKey())) {
+            throw new IllegalStateException("The geolocation service requires a mandatory geolocationAccessKey");
+        }
+        if (isEmpty(component.getGeolocationRequestHostIP())) {
+            throw new IllegalStateException("The geolocation service requires a mandatory geolocationRequestHostIP");
+        }
+        GetMethod getMethod = new GetMethod("http://api.ipstack.com/" + component.getGeolocationRequestHostIP());
+        getMethod.setQueryString(new NameValuePair[] { 
+        	    new NameValuePair("access_key", component.getGeolocationAccessKey()),
+        	    new NameValuePair("legacy", "1"),
+        	    new NameValuePair("output", "json")
+        	}); 
         try {
             int statusCode = httpClient.executeMethod(getMethod);
             if (statusCode != HttpStatus.SC_OK) {
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java
index ebb0ec5..8e5317d 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java
@@ -35,7 +35,7 @@ public class CurrentWeatherConsumerHtmlTest extends BaseWeatherConsumerTest {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("weather:foo?mode=HTML&appid=9162755b2efa555823cfe0451d7fff38").to("mock:result");
+                from("weather:foo?mode=HTML&appid=9162755b2efa555823cfe0451d7fff38&geolocationAccessKey=test&geolocationRequestHostIP=test").to("mock:result");
             }
         };
     }
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java
index 61b7dc6..787dff9 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java
@@ -89,7 +89,7 @@ public class CurrentWeatherMadridProducerTest extends BaseWeatherConsumerTest {
                 
                 /* The Camel Route uses the apache-camel appid to access the openweathermap service */
                 from("direct:start")
-                    .to("weather:foo?location=Madrid,Spain&appid=9162755b2efa555823cfe0451d7fff38")
+                    .to("weather:foo?location=Madrid,Spain&appid=9162755b2efa555823cfe0451d7fff38&geolocationAccessKey=test&geolocationRequestHostIP=test")
                     .to("mock:result");
             }
         };
diff --git a/platforms/spring-boot/components-starter/camel-weather-starter/src/main/java/org/apache/camel/component/weather/springboot/WeatherComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-weather-starter/src/main/java/org/apache/camel/component/weather/springboot/WeatherComponentConfiguration.java
index 8cf8bb1..1d0ce50 100644
--- a/platforms/spring-boot/components-starter/camel-weather-starter/src/main/java/org/apache/camel/component/weather/springboot/WeatherComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-weather-starter/src/main/java/org/apache/camel/component/weather/springboot/WeatherComponentConfiguration.java
@@ -32,12 +32,37 @@ public class WeatherComponentConfiguration
             ComponentConfigurationPropertiesCommon {
 
     /**
+     * The geolocation service now needs an accessKey to be used
+     */
+    private String geolocationAccessKey;
+    /**
+     * The geolocation service now needs to specify the IP associated to the
+     * accessKey you're using
+     */
+    private String geolocationRequestHostIP;
+    /**
      * Whether the component should resolve property placeholders on itself when
      * starting. Only properties which are of String type can use property
      * placeholders.
      */
     private Boolean resolvePropertyPlaceholders = true;
 
+    public String getGeolocationAccessKey() {
+        return geolocationAccessKey;
+    }
+
+    public void setGeolocationAccessKey(String geolocationAccessKey) {
+        this.geolocationAccessKey = geolocationAccessKey;
+    }
+
+    public String getGeolocationRequestHostIP() {
+        return geolocationRequestHostIP;
+    }
+
+    public void setGeolocationRequestHostIP(String geolocationRequestHostIP) {
+        this.geolocationRequestHostIP = geolocationRequestHostIP;
+    }
+
     public Boolean getResolvePropertyPlaceholders() {
         return resolvePropertyPlaceholders;
     }