Skip to content

Commit

Permalink
CAMEL-9427: camel-jetty - Should also support rest-dsl with api-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Dec 16, 2015
1 parent 1e871d7 commit a677b50
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, ConnectorRef> CONNECTORS = new HashMap<String, ConnectorRef>();
Expand Down Expand Up @@ -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<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) {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions components/camel-jetty9/pom.xml
Expand Up @@ -126,6 +126,11 @@
<scope>test</scope>
</dependency>
<!-- 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>
Expand Down
@@ -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");
}
};
}

}

0 comments on commit a677b50

Please sign in to comment.