CAMEL-10164: swagger component for making rest calls with swagger schema validation and facade to actual HTTP client in use
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/098a67e4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/098a67e4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/098a67e4 Branch: refs/heads/master Commit: 098a67e4b08689742310d15a2fab5294caf7a9b9 Parents: bf49e36 Author: Claus Ibsen <[email protected]> Authored: Fri Aug 26 16:41:26 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 26 16:53:31 2016 +0200 ---------------------------------------------------------------------- .../camel/component/http/HttpComponent.java | 42 +++++++++++++- .../camel/component/http4/HttpComponent.java | 44 +++++++++++++- components/camel-jetty9/pom.xml | 5 ++ .../rest/producer/Http4RestProducerGetTest.java | 60 ++++++++++++++++++++ .../rest/producer/HttpRestProducerGetTest.java | 60 ++++++++++++++++++++ 5 files changed, 207 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/098a67e4/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java index 9caa8cc..c6a37b1 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java @@ -23,16 +23,22 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; +import org.apache.camel.Producer; import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpCommonComponent; import org.apache.camel.http.common.HttpConfiguration; +import org.apache.camel.http.common.HttpRestHeaderFilterStrategy; import org.apache.camel.http.common.UrlRewrite; import org.apache.camel.spi.HeaderFilterStrategy; +import org.apache.camel.spi.RestProducerFactory; import org.apache.camel.util.CollectionHelper; +import org.apache.camel.util.FileUtil; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ServiceHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.apache.commons.httpclient.HttpConnectionManager; @@ -45,7 +51,7 @@ import org.apache.commons.httpclient.params.HttpConnectionManagerParams; * * @version */ -public class HttpComponent extends HttpCommonComponent { +public class HttpComponent extends HttpCommonComponent implements RestProducerFactory { protected HttpClientConfigurer httpClientConfigurer; protected HttpConnectionManager httpConnectionManager; @@ -291,7 +297,39 @@ public class HttpComponent extends HttpCommonComponent { HttpConnectionManager connectionManager, HttpClientConfigurer configurer) throws URISyntaxException { return new HttpEndpoint(uri, component, clientParams, connectionManager, configurer); } - + + @Override + public Producer createProducer(CamelContext camelContext, String host, + String verb, String basePath, String uriTemplate, String queryParameters, + String consumes, String produces, Map<String, Object> parameters) throws Exception { + + // avoid leading slash + basePath = FileUtil.stripLeadingSeparator(basePath); + uriTemplate = FileUtil.stripLeadingSeparator(uriTemplate); + + // get the endpoint + String url; + if (uriTemplate != null) { + // http is already prefixed in base path + url = String.format("%s/%s/%s", host, basePath, uriTemplate); + } else { + // http is already prefixed in base path + url = String.format("%s/%s", host, basePath); + } + + HttpEndpoint endpoint = camelContext.getEndpoint(url, HttpEndpoint.class); + if (parameters != null && !parameters.isEmpty()) { + setProperties(camelContext, endpoint, parameters); + } + String path = uriTemplate != null ? uriTemplate : basePath; + endpoint.setHeaderFilterStrategy(new HttpRestHeaderFilterStrategy(path, queryParameters)); + + // the endpoint must be started before creating the producer + ServiceHelper.startService(endpoint); + + return endpoint.createProducer(); + } + public HttpClientConfigurer getHttpClientConfigurer() { return httpClientConfigurer; } http://git-wip-us.apache.org/repos/asf/camel/blob/098a67e4/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java index d59c60d..b8468d0 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java @@ -23,16 +23,22 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; +import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; +import org.apache.camel.Producer; import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpCommonComponent; import org.apache.camel.http.common.HttpConfiguration; import org.apache.camel.http.common.HttpHelper; +import org.apache.camel.http.common.HttpRestHeaderFilterStrategy; import org.apache.camel.http.common.UrlRewrite; import org.apache.camel.spi.HeaderFilterStrategy; +import org.apache.camel.spi.RestProducerFactory; +import org.apache.camel.util.FileUtil; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ServiceHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.apache.camel.util.jsse.SSLContextParameters; @@ -59,7 +65,7 @@ import org.slf4j.LoggerFactory; * * @version */ -public class HttpComponent extends HttpCommonComponent { +public class HttpComponent extends HttpCommonComponent implements RestProducerFactory { private static final Logger LOG = LoggerFactory.getLogger(HttpComponent.class); @@ -317,13 +323,47 @@ public class HttpComponent extends HttpCommonComponent { return answer; } - @Override protected boolean useIntrospectionOnEndpoint() { return false; } + + @Override + public Producer createProducer(CamelContext camelContext, String host, + String verb, String basePath, String uriTemplate, String queryParameters, + String consumes, String produces, Map<String, Object> parameters) throws Exception { + + // avoid leading slash + basePath = FileUtil.stripLeadingSeparator(basePath); + uriTemplate = FileUtil.stripLeadingSeparator(uriTemplate); + + // replace http with http4 in the host part + host = host.replace("http", "http4"); + + // get the endpoint + String url; + if (uriTemplate != null) { + + url = String.format("%s/%s/%s", host, basePath, uriTemplate); + } else { + url = String.format("%s/%s", host, basePath); + } + + HttpEndpoint endpoint = camelContext.getEndpoint(url, HttpEndpoint.class); + if (parameters != null && !parameters.isEmpty()) { + setProperties(camelContext, endpoint, parameters); + } + String path = uriTemplate != null ? uriTemplate : basePath; + endpoint.setHeaderFilterStrategy(new HttpRestHeaderFilterStrategy(path, queryParameters)); + + // the endpoint must be started before creating the producer + ServiceHelper.startService(endpoint); + + return endpoint.createProducer(); + } + public HttpClientConfigurer getHttpClientConfigurer() { return httpClientConfigurer; } http://git-wip-us.apache.org/repos/asf/camel/blob/098a67e4/components/camel-jetty9/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/pom.xml b/components/camel-jetty9/pom.xml index fbb24f2..472066c 100644 --- a/components/camel-jetty9/pom.xml +++ b/components/camel-jetty9/pom.xml @@ -110,6 +110,11 @@ <artifactId>camel-http</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http4</artifactId> + <scope>test</scope> + </dependency> <!-- for testing rest-dsl --> <dependency> <groupId>org.apache.camel</groupId> http://git-wip-us.apache.org/repos/asf/camel/blob/098a67e4/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/Http4RestProducerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/Http4RestProducerGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/Http4RestProducerGetTest.java new file mode 100644 index 0000000..c46a93a --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/Http4RestProducerGetTest.java @@ -0,0 +1,60 @@ +/** + * 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.jetty.rest.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.junit.Test; + +public class Http4RestProducerGetTest extends BaseJettyTest { + + @Test + public void testHttp4ProducerGet() throws Exception { + String out = fluentTemplate.withHeader("id", "123").to("direct:start").request(String.class); + assertEquals("123;Donald Duck", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()) + // use camel-http4 as rest client + .producerComponent("http4"); + + from("direct:start") + .to("rest:get:users/{id}/basic"); + + // use the rest DSL to define the rest services + rest("/users/") + .get("{id}/basic") + .route() + .to("mock:input") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + String id = exchange.getIn().getHeader("id", String.class); + exchange.getOut().setBody(id + ";Donald Duck"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/098a67e4/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/HttpRestProducerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/HttpRestProducerGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/HttpRestProducerGetTest.java new file mode 100644 index 0000000..00f94c8 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/HttpRestProducerGetTest.java @@ -0,0 +1,60 @@ +/** + * 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.jetty.rest.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.junit.Test; + +public class HttpRestProducerGetTest extends BaseJettyTest { + + @Test + public void testHttpProducerGet() throws Exception { + String out = fluentTemplate.withHeader("id", "123").to("direct:start").request(String.class); + assertEquals("123;Donald Duck", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()) + // use camel-http as rest client + .producerComponent("http"); + + from("direct:start") + .to("rest:get:users/{id}/basic"); + + // use the rest DSL to define the rest services + rest("/users/") + .get("{id}/basic") + .route() + .to("mock:input") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + String id = exchange.getIn().getHeader("id", String.class); + exchange.getOut().setBody(id + ";Donald Duck"); + } + }); + } + }; + } + +}
