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/fbebccdb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fbebccdb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fbebccdb Branch: refs/heads/master Commit: fbebccdb9463870f245443e639fc27db82cbae59 Parents: 30ee1a8 Author: Claus Ibsen <[email protected]> Authored: Fri Aug 26 12:50:47 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 26 16:53:31 2016 +0200 ---------------------------------------------------------------------- .../component/restlet/RestletComponent.java | 3 + .../component/undertow/UndertowComponent.java | 36 +++++++++++- .../component/undertow/UndertowHelper.java | 5 +- .../rest/RestUndertowProducerGetTest.java | 59 ++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fbebccdb/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java index 81469b0..4a29ff0 100644 --- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java +++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java @@ -832,6 +832,9 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements R setProperties(camelContext, endpoint, parameters); } + // the endpoint must be started before creating the producer + ServiceHelper.startService(endpoint); + return endpoint.createProducer(); } http://git-wip-us.apache.org/repos/asf/camel/blob/fbebccdb/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java index deeb6f3..98cf710 100644 --- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java +++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java @@ -37,16 +37,19 @@ import org.apache.camel.CamelContext; import org.apache.camel.Consumer; import org.apache.camel.Endpoint; import org.apache.camel.Processor; +import org.apache.camel.Producer; import org.apache.camel.component.undertow.handlers.HttpCamelHandler; import org.apache.camel.component.undertow.handlers.NotFoundHandler; import org.apache.camel.impl.UriEndpointComponent; import org.apache.camel.spi.RestApiConsumerFactory; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; +import org.apache.camel.spi.RestProducerFactory; import org.apache.camel.util.FileUtil; import org.apache.camel.util.HostUtils; 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; @@ -56,7 +59,7 @@ import org.slf4j.LoggerFactory; /** * Represents the component that manages {@link UndertowEndpoint}. */ -public class UndertowComponent extends UriEndpointComponent implements RestConsumerFactory, RestApiConsumerFactory { +public class UndertowComponent extends UriEndpointComponent implements RestConsumerFactory, RestApiConsumerFactory, RestProducerFactory { private static final Logger LOG = LoggerFactory.getLogger(UndertowEndpoint.class); private UndertowHttpBinding undertowHttpBinding; @@ -231,6 +234,37 @@ public class UndertowComponent extends UriEndpointComponent implements RestConsu } @Override + public Producer createProducer(CamelContext camelContext, String host, + String verb, String basePath, String uriTemplate, + String consumes, String produces, Map<String, Object> parameters) throws Exception { + + // avoid leading slash + basePath = FileUtil.stripLeadingSeparator(basePath); + uriTemplate = FileUtil.stripLeadingSeparator(uriTemplate); + + // restlet method must be in upper-case + String restletMethod = verb.toUpperCase(Locale.US); + + // get the endpoint + String url; + if (uriTemplate != null) { + url = String.format("undertow:%s/%s/%s?restletMethods=%s", host, basePath, uriTemplate, restletMethod); + } else { + url = String.format("undertow:%s/%s?restletMethods=%s", host, basePath, restletMethod); + } + + UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class); + if (parameters != null && !parameters.isEmpty()) { + setProperties(camelContext, endpoint, parameters); + } + + // the endpoint must be started before creating the producer + ServiceHelper.startService(endpoint); + + return endpoint.createProducer(); + } + + @Override protected void doStart() throws Exception { super.doStart(); http://git-wip-us.apache.org/repos/asf/camel/blob/fbebccdb/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHelper.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHelper.java index afbe2a2..f9eda6f 100644 --- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHelper.java +++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHelper.java @@ -45,7 +45,10 @@ public final class UndertowHelper { * @return the URL to invoke */ public static String createURL(Exchange exchange, UndertowEndpoint endpoint) { - String uri = uri = endpoint.getHttpURI().toASCIIString(); + String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); + if (uri == null) { + uri = endpoint.getHttpURI().toASCIIString(); + } // resolve placeholders in uri try { http://git-wip-us.apache.org/repos/asf/camel/blob/fbebccdb/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowProducerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowProducerGetTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowProducerGetTest.java new file mode 100644 index 0000000..5d6ca9e --- /dev/null +++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowProducerGetTest.java @@ -0,0 +1,59 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.undertow.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.undertow.BaseUndertowTest; +import org.junit.Test; + +public class RestUndertowProducerGetTest extends BaseUndertowTest { + + @Test + public void testUndertowProducerGet() 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 { + // configure to use restlet on localhost with the given port + restConfiguration().component("undertow").host("localhost").port(getPort()); + + 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"); + } + }); + } + }; + } + +}
