From a677b50d3e027ac4f41b2ef9e40678d88b045e0c Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Wed, 16 Dec 2015 08:30:35 +0100 Subject: [PATCH] CAMEL-9427: camel-jetty - Should also support rest-dsl with api-doc --- .../component/jetty/JettyHttpComponent.java | 23 ++++++- components/camel-jetty9/pom.xml | 5 ++ .../jetty/rest/RestApiJettyTest.java | 65 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestApiJettyTest.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 bcaea1bf0bdd7..6ecc9b59fee3c 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.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 @@ * * @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 CONNECTORS = new HashMap(); @@ -1004,6 +1005,18 @@ public void setProxyPort(Integer proxyPort) { @Override public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map 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 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 parameters, boolean api) throws Exception { String path = basePath; if (uriTemplate != null) { @@ -1066,7 +1079,13 @@ public Consumer createConsumer(CamelContext camelContext, Processor processor, S 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 diff --git a/components/camel-jetty9/pom.xml b/components/camel-jetty9/pom.xml index 88f4b486435a7..d568e18f7ed75 100644 --- a/components/camel-jetty9/pom.xml +++ b/components/camel-jetty9/pom.xml @@ -126,6 +126,11 @@ test + + org.apache.camel + camel-swagger-java + test + org.apache.camel camel-jackson 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 0000000000000..eb79aa20c770f --- /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 + *

+ * 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; + +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"); + } + }; + } + +}