Updated Branches: refs/heads/master 4a2dd39db -> ea902873b
Added producer to camel-weather Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ea902873 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ea902873 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ea902873 Branch: refs/heads/master Commit: ea902873b9426a6a13f9b02d5c4cb74ac1ae1d59 Parents: 4a2dd39 Author: Claus Ibsen <[email protected]> Authored: Thu Jun 6 11:21:46 2013 +0200 Committer: Claus Ibsen <[email protected]> Committed: Thu Jun 6 11:21:46 2013 +0200 ---------------------------------------------------------------------- .../component/weather/WeatherConfiguration.java | 9 +- .../camel/component/weather/WeatherConstants.java | 1 + .../camel/component/weather/WeatherEndpoint.java | 24 +++-- .../camel/component/weather/WeatherProducer.java | 58 +++++++++++ .../weather/CurrentWeatherMadridProducerTest.java | 77 +++++++++++++++ 5 files changed, 156 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ea902873/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 85bc3a3..f0d6499 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 @@ -86,14 +86,17 @@ public class WeatherConfiguration { } public String getQuery() throws Exception { + return getQuery(getLocation()); + } + + public String getQuery(String location) throws Exception { String answer = "http://api.openweathermap.org/data/2.5/"; - String location; - if (isEmpty(getLocation())) { + if (isEmpty(location)) { location = getGeoLocation(); } else { // assuming the location is a town or country - location = "q=" + getLocation(); + location = "q=" + location; } if (isEmpty(getPeriod())) { http://git-wip-us.apache.org/repos/asf/camel/blob/ea902873/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 index fc81632..c0e888a 100644 --- 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 @@ -21,6 +21,7 @@ package org.apache.camel.component.weather; */ public final class WeatherConstants { + public static final String WEATHER_LOCATION = "CamelWeatherLocation"; public static final String WEATHER_QUERY = "CamelWeatherQuery"; private WeatherConstants() { http://git-wip-us.apache.org/repos/asf/camel/blob/ea902873/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 b70a4fa..3015ef3 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 @@ -35,16 +35,6 @@ public class WeatherEndpoint extends DefaultPollingEndpoint { } @Override - public Producer createProducer() throws Exception { - throw new UnsupportedOperationException("Making Weather is not implemented - yet."); - } - - @Override - public boolean isSingleton() { - return true; - } - - @Override public Consumer createConsumer(Processor processor) throws Exception { WeatherConsumer answer = new WeatherConsumer(this, processor, this.configuration.getQuery()); @@ -54,4 +44,18 @@ public class WeatherEndpoint extends DefaultPollingEndpoint { configureConsumer(answer); return answer; } + + @Override + public Producer createProducer() throws Exception { + return new WeatherProducer(this, configuration.getQuery()); + } + + @Override + public boolean isSingleton() { + return true; + } + + public WeatherConfiguration getConfiguration() { + return configuration; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/ea902873/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java new file mode 100644 index 0000000..afe1b85 --- /dev/null +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java @@ -0,0 +1,58 @@ +/** + * 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; + +import java.net.URL; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; + +public class WeatherProducer extends DefaultProducer { + + private final String query; + + public WeatherProducer(WeatherEndpoint endpoint, String query) { + super(endpoint); + this.query = query; + } + + @Override + public WeatherEndpoint getEndpoint() { + return (WeatherEndpoint) super.getEndpoint(); + } + + @Override + public void process(Exchange exchange) throws Exception { + String q = query; + String location = exchange.getIn().getHeader(WeatherConstants.WEATHER_LOCATION, String.class); + if (location != null) { + q = getEndpoint().getConfiguration().getQuery(location); + } + + log.debug("Going to execute the Weather query {}", q); + String weather = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, new URL(q)); + log.debug("Got back the Weather information {}", weather); + + if (ObjectHelper.isEmpty(weather)) { + throw new IllegalStateException("Got the unexpected value '" + weather + "' as the result of the query '" + q + "'"); + } + + exchange.getIn().setBody(weather); + exchange.getIn().setHeader(WeatherConstants.WEATHER_QUERY, q); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ea902873/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..ec17d00 --- /dev/null +++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherMadridProducerTest.java @@ -0,0 +1,77 @@ +/** + * 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; + +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.junit.Test; + +public class CurrentWeatherMadridProducerTest extends BaseWeatherConsumerTest { + + @Test + public void testGrabbingListOfEntries() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + // as the default delay option is one hour long, we expect exactly one message exchange + mock.expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + mock.assertIsSatisfied(); + + Exchange exchange = mock.getExchanges().get(0); + assertNotNull(exchange); + Message in = exchange.getIn(); + assertNotNull(in); + String weather = assertIsInstanceOf(String.class, in.getBody()); + + checkWeatherContent(weather); + } + + @Test + public void testHeaderOverride() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + // as the default delay option is one hour long, we expect exactly one message exchange + mock.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", WeatherConstants.WEATHER_LOCATION, "Paris,France"); + + mock.assertIsSatisfied(); + + Exchange exchange = mock.getExchanges().get(0); + assertNotNull(exchange); + Message in = exchange.getIn(); + assertNotNull(in); + String weather = assertIsInstanceOf(String.class, in.getBody()); + + checkWeatherContent(weather); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("weather:foo?location=Madrid,Spain") + .to("mock:result"); + } + }; + } + +}
