CAMEL-9427: camel-jetty - Should also support rest-dsl with api-doc
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/18de9265 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/18de9265 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/18de9265 Branch: refs/heads/camel-2.16.x Commit: 18de9265cff952e1e17db27020a5137648486fbc Parents: d5fddde Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Dec 16 08:30:35 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Dec 16 08:56:10 2015 +0100 ---------------------------------------------------------------------- .../component/jetty/JettyHttpComponent.java | 23 ++++++- components/camel-jetty9/pom.xml | 5 ++ .../component/jetty/rest/RestApiJettyTest.java | 65 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/18de9265/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java index bcaea1b..6ecc9b5 100644 --- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java +++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java @@ -52,6 +52,7 @@ import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.spi.ManagementAgent; import org.apache.camel.spi.ManagementStrategy; import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RestApiConsumerFactory; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.util.FileUtil; @@ -93,7 +94,7 @@ import org.slf4j.LoggerFactory; * * @version */ -public abstract class JettyHttpComponent extends HttpCommonComponent implements RestConsumerFactory { +public abstract class JettyHttpComponent extends HttpCommonComponent implements RestConsumerFactory, RestApiConsumerFactory { public static final String TMP_DIR = "CamelJettyTempDir"; protected static final HashMap<String, ConnectorRef> CONNECTORS = new HashMap<String, ConnectorRef>(); @@ -1004,6 +1005,18 @@ public abstract class JettyHttpComponent extends HttpCommonComponent implements @Override public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { + return doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration, parameters, false); + } + + @Override + public Consumer createApiConsumer(CamelContext camelContext, Processor processor, String contextPath, + RestConfiguration configuration, Map<String, Object> parameters) throws Exception { + // reuse the createConsumer method we already have. The api need to use GET and match on uri prefix + return doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration, parameters, true); + } + + Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, + String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception { String path = basePath; if (uriTemplate != null) { @@ -1066,7 +1079,13 @@ public abstract class JettyHttpComponent extends HttpCommonComponent implements String query = URISupport.createQueryString(map); - String url = "jetty:%s://%s:%s/%s?httpMethodRestrict=%s"; + String url; + if (api) { + url = "jetty:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s"; + } else { + url = "jetty:%s://%s:%s/%s?httpMethodRestrict=%s"; + } + // must use upper case for restrict String restrict = verb.toUpperCase(Locale.US); // get the endpoint http://git-wip-us.apache.org/repos/asf/camel/blob/18de9265/components/camel-jetty9/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/pom.xml b/components/camel-jetty9/pom.xml index b89918b..b4247aa 100644 --- a/components/camel-jetty9/pom.xml +++ b/components/camel-jetty9/pom.xml @@ -128,6 +128,11 @@ <!-- for testing rest-dsl --> <dependency> <groupId>org.apache.camel</groupId> + <artifactId>camel-swagger-java</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> <artifactId>camel-jackson</artifactId> <scope>test</scope> </dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/18de9265/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java new file mode 100644 index 0000000..eb79aa2 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.java @@ -0,0 +1,65 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.model.rest.RestParamType; +import org.junit.Test; + +public class RestApiJettyTest extends BaseJettyTest { + + @Override + protected boolean useJmx() { + return true; + } + + @Test + public void testApi() throws Exception { + String out = template.requestBody("jetty:http://localhost:{{port}}/api-doc", null, String.class); + assertNotNull(out); + assertTrue(out.contains("\"version\" : \"1.2.3\"")); + assertTrue(out.contains("\"title\" : \"The hello rest thing\"")); + assertTrue(out.contains("\"/hello/bye/{name}\"")); + assertTrue(out.contains("\"/hello/hi/{name}\"")); + assertTrue(out.contains("\"summary\" : \"To update the greeting message\"")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()).apiContextPath("/api-doc") + .apiProperty("cors", "true").apiProperty("api.title", "The hello rest thing").apiProperty("api.version", "1.2.3"); + + rest("/hello").consumes("application/json").produces("application/json") + .get("/hi/{name}").description("Saying hi") + .param().name("name").type(RestParamType.path).dataType("string").description("Who is it").endParam() + .to("log:hi") + .get("/bye/{name}").description("Saying bye") + .param().name("name").type(RestParamType.path).dataType("string").description("Who is it").endParam() + .responseMessage().code(200).message("A reply message").endResponseMessage() + .to("log:bye") + .post("/bye").description("To update the greeting message").consumes("application/xml").produces("application/xml") + .param().name("greeting").type(RestParamType.body).dataType("string").description("Message to use as greeting").endParam() + .to("log:bye"); + } + }; + } + +}