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 2016/05/25 15:07:13 UTC
[3/4] camel git commit: CAMEL-9273:Support all free api methods
CAMEL-9273:Support all free api methods
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cd6c3d71
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cd6c3d71
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cd6c3d71
Branch: refs/heads/master
Commit: cd6c3d715edc99d543d56fa0a6737f40e04e9509
Parents: ced55f7
Author: Arno Noordover <ar...@noordover.net>
Authored: Mon May 23 21:42:12 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed May 25 16:54:34 2016 +0200
----------------------------------------------------------------------
.../camel/component/weather/WeatherApi.java | 23 +++
.../component/weather/WeatherConfiguration.java | 53 +++++++
.../camel/component/weather/WeatherQuery.java | 86 +++++++++--
.../component/weather/WeatherQueryTest.java | 153 +++++++++++++++++++
4 files changed, 304 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/cd6c3d71/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherApi.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherApi.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherApi.java
new file mode 100644
index 0000000..1b90899
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherApi.java
@@ -0,0 +1,23 @@
+/**
+ * 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;
+
+public enum WeatherApi {
+
+ Current, Station, Hourly, Daily
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/cd6c3d71/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 c569ef3..df8f9df 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,6 +16,7 @@
*/
package org.apache.camel.component.weather;
+import java.util.List;
import java.util.Scanner;
import org.apache.camel.component.weather.geolocation.FreeGeoIpGeoLocationProvider;
@@ -62,6 +63,14 @@ public class WeatherConfiguration {
private WeatherLanguage language = en;
@UriParam
private String headerName;
+ @UriParam
+ private String zip;
+ @UriParam
+ private List<String> ids;
+ @UriParam
+ private Integer cnt;
+ @UriParam
+ private WeatherApi weatherApi;
@UriParam(label = "proxy")
private String proxyHost;
@@ -342,4 +351,48 @@ public class WeatherConfiguration {
public void setProxyAuthHost(String proxyAuthHost) {
this.proxyAuthHost = proxyAuthHost;
}
+
+ public String getZip() {
+ return zip;
+ }
+
+ /**
+ * Zip-code, e.g. 94040,us
+ */
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ public List<String> getIds() {
+ return ids;
+ }
+
+ /**
+ * List of id's of city/stations
+ */
+ public void setIds(List<String> ids) {
+ this.ids = ids;
+ }
+
+ public Integer getCnt() {
+ return cnt;
+ }
+
+ /**
+ * Number of results to be found
+ */
+ public void setCnt(Integer cnt) {
+ this.cnt = cnt;
+ }
+
+ public WeatherApi getWeatherApi() {
+ return weatherApi;
+ }
+
+ /**
+ * The API to be use (current, forecast/3 hour, forecast daily, station
+ */
+ public void setWeatherApi(WeatherApi weatherApi) {
+ this.weatherApi = weatherApi;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/cd6c3d71/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
index 45af6f0..090a88a 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.weather;
import org.apache.camel.component.weather.geolocation.GeoLocation;
import org.apache.camel.component.weather.geolocation.GeoLocationProvider;
+import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
@@ -47,10 +48,12 @@ public class WeatherQuery {
public String getQuery(String location) throws Exception {
String answer = "http://api.openweathermap.org/data/2.5/";
+ boolean point = false;
if (weatherConfiguration.getLat() != null && weatherConfiguration.getLon() != null
&& weatherConfiguration.getRightLon() == null && weatherConfiguration.getTopLat() == null) {
- location = "lat=" + weatherConfiguration.getLat() + "&lon=" + weatherConfiguration.getLon();
+ location = createLatLonQueryString();
+ point = true;
} else if (weatherConfiguration.getLat() != null && weatherConfiguration.getLon() != null
&& weatherConfiguration.getRightLon() != null && weatherConfiguration.getTopLat() != null) {
location = "bbox=" + weatherConfiguration.getLon() + ","
@@ -58,8 +61,15 @@ public class WeatherQuery {
+ weatherConfiguration.getRightLon() + ","
+ weatherConfiguration.getTopLat() + ","
+ weatherConfiguration.getZoom() + "&cluster=yes";
+ } else if (!isEmpty(weatherConfiguration.getZip())) {
+ location = "zip=" + weatherConfiguration.getZip();
+ } else if (weatherConfiguration.getIds() != null && weatherConfiguration.getIds().size() > 0) {
+ location = "id=" + String.join(",", weatherConfiguration.getIds());
} else if (isEmpty(location) || "current".equals(location)) {
- location = getCurrentGeoLocation();
+ GeoLocation geoLocation = getCurrentGeoLocation();
+ weatherConfiguration.setLat(geoLocation.getLatitude());
+ weatherConfiguration.setLon(geoLocation.getLongitude());
+ location = createLatLonQueryString();
} else {
// assuming the location is a town or country
location = "q=" + location;
@@ -67,12 +77,13 @@ public class WeatherQuery {
location = location + "&lang=" + weatherConfiguration.getLanguage();
- if (weatherConfiguration.getTopLat() != null && weatherConfiguration.getRightLon() != null) {
- answer += "box/city?" + location;
- } else if (isEmpty(weatherConfiguration.getPeriod())) {
- answer += "weather?" + location;
- } else {
- answer += "forecast/daily?" + location + "&cnt=" + weatherConfiguration.getPeriod();
+ String context = createContext();
+ answer += context + location;
+
+ if (!isEmpty(weatherConfiguration.getPeriod())) {
+ answer += "&cnt=" + weatherConfiguration.getPeriod();
+ } else if (weatherConfiguration.getCnt() != null) {
+ answer += "&cnt=" + weatherConfiguration.getCnt();
}
// append the desired measurement unit if not the default (which is metric)
@@ -93,9 +104,62 @@ public class WeatherQuery {
}
- String getCurrentGeoLocation() throws Exception {
- GeoLocation geoLocation = geoLocationProvider.getCurrentGeoLocation();
- return "lat=" + geoLocation.getLatitude() + "&lon=" + geoLocation.getLongitude();
+ private String createContext() {
+ String answer;
+ if (isBoxedQuery()) {
+ if (weatherConfiguration.getWeatherApi() == WeatherApi.Station) {
+ answer = "box/station?";
+ } else {
+ answer = "box/city?";
+ }
+ } else if (isGeoLocation() && weatherConfiguration.getCnt() != null) {
+ if (weatherConfiguration.getWeatherApi() == WeatherApi.Station) {
+ answer = "station/find?";
+ } else {
+ answer = "find?";
+ }
+ } else if (weatherConfiguration.getIds() != null && weatherConfiguration.getIds().size() > 0) {
+ if (weatherConfiguration.getIds().size() == 1) {
+ if (!isEmpty(weatherConfiguration.getPeriod())) {
+ if (weatherConfiguration.getWeatherApi() == WeatherApi.Hourly) {
+ answer = "forecast?";
+ } else {
+ answer = "forecast/daily?";
+ }
+ } else if (weatherConfiguration.getWeatherApi() == WeatherApi.Station) {
+ answer = "station?";
+ } else {
+ answer = "weather?";
+ }
+ } else {
+ answer = "group?";
+ }
+ } else if (isEmpty(weatherConfiguration.getPeriod())) {
+ answer = "weather?";
+ } else {
+ if (weatherConfiguration.getWeatherApi() == WeatherApi.Hourly) {
+ answer = "forecast?";
+ } else {
+ answer = "forecast/daily?";
+ }
+ }
+ return answer;
+ }
+
+ private boolean isGeoLocation() {
+ return weatherConfiguration.getLat() != null && weatherConfiguration.getLon() != null;
+ }
+
+ private String createLatLonQueryString() {
+ return "lat=" + weatherConfiguration.getLat() + "&lon=" + weatherConfiguration.getLon();
+ }
+
+ private boolean isBoxedQuery() {
+ return weatherConfiguration.getTopLat() != null && weatherConfiguration.getRightLon() != null;
+ }
+
+ GeoLocation getCurrentGeoLocation() throws Exception {
+ return geoLocationProvider.getCurrentGeoLocation();
}
void setGeoLocationProvider(GeoLocationProvider geoLocationProvider) {
http://git-wip-us.apache.org/repos/asf/camel/blob/cd6c3d71/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
index ae39322..af9dcec 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.component.weather;
+import java.util.Arrays;
+import java.util.List;
+
import org.apache.camel.component.weather.geolocation.GeoLocation;
import org.apache.camel.component.weather.geolocation.GeoLocationProvider;
import org.junit.Before;
@@ -64,6 +67,23 @@ public class WeatherQueryTest {
}
@Test
+ public void testBoxedStationQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setLon("4");
+ weatherConfiguration.setLat("52");
+ weatherConfiguration.setRightLon("6");
+ weatherConfiguration.setTopLat("54");
+ weatherConfiguration.setZoom(8);
+ weatherConfiguration.setUnits(WeatherUnits.METRIC);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setWeatherApi(WeatherApi.Station);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/box/station?bbox=4,52,6,54,8&cluster=yes&lang=en&units=metric&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
public void testLatLonQuery() throws Exception {
WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
weatherConfiguration.setLon("4");
@@ -78,6 +98,124 @@ public class WeatherQueryTest {
}
@Test
+ public void testZipQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setZip("2493CJ,nl");
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/weather?zip=2493CJ,nl&lang=nl&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testSingleIdQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ List<String> ids = Arrays.asList("524901");
+ weatherConfiguration.setIds(ids);
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/weather?id=524901&lang=nl&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testSingleIdDailyForecastQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ List<String> ids = Arrays.asList("524901");
+ weatherConfiguration.setIds(ids);
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setPeriod("20");
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/forecast/daily?id=524901&lang=nl&cnt=20&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testSingleIdHourlyForecastQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ List<String> ids = Arrays.asList("524901");
+ weatherConfiguration.setIds(ids);
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setWeatherApi(WeatherApi.Hourly);
+ weatherConfiguration.setPeriod("20");
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/forecast?id=524901&lang=nl&cnt=20&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testSingleIdStationQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ List<String> ids = Arrays.asList("52");
+ weatherConfiguration.setIds(ids);
+ weatherConfiguration.setMode(WeatherMode.JSON);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setWeatherApi(WeatherApi.Station);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/station?id=52&lang=nl&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testMultiIdQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ List<String> ids = Arrays.asList("524901", "703448");
+ weatherConfiguration.setIds(ids);
+ weatherConfiguration.setMode(WeatherMode.JSON);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/group?id=524901,703448&lang=nl&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testFindInCircleQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setLat(LATITUDE);
+ weatherConfiguration.setLon(LONGITUDE);
+ weatherConfiguration.setCnt(25);
+ weatherConfiguration.setMode(WeatherMode.JSON);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/find?lat=51.98&lon=4.13&lang=nl&cnt=25&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testFindStationInCircleQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setLat(LATITUDE);
+ weatherConfiguration.setLon(LONGITUDE);
+ weatherConfiguration.setCnt(25);
+ weatherConfiguration.setMode(WeatherMode.JSON);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setWeatherApi(WeatherApi.Station);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/station/find?lat=51.98&lon=4.13&lang=nl&cnt=25&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
public void testCurrentLocationQuery() throws Exception {
WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
weatherConfiguration.setMode(WeatherMode.XML);
@@ -92,6 +230,21 @@ public class WeatherQueryTest {
}
@Test
+ public void testCurrentLocationHourlyQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setPeriod("3");
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setUnits(WeatherUnits.IMPERIAL);
+ weatherConfiguration.setAppid(APPID);
+ weatherConfiguration.setWeatherApi(WeatherApi.Hourly);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/forecast?lat=51.98&lon=4.13&lang=nl&cnt=3&units=imperial&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
public void testCurrentLocationQuery2() throws Exception {
WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
weatherConfiguration.setMode(WeatherMode.XML);