Repository: camel Updated Branches: refs/heads/master 0feb25f39 -> c91dcd3f1
CAMEL-8545: camel-swagger-java to run outside servlet - work in progress Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c91dcd3f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c91dcd3f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c91dcd3f Branch: refs/heads/master Commit: c91dcd3f192072d7765c4c56e084ae6d97fe895e Parents: 0feb25f Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Sep 23 17:50:17 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Sep 23 17:53:53 2015 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestApiEndpoint.java | 7 +++-- .../model/rest/RestConfigurationDefinition.java | 27 ++++++++++++++++++ .../camel/spi/RestApiConsumerFactory.java | 2 -- .../camel/spi/RestApiProcessorFactory.java | 2 +- .../org/apache/camel/spi/RestConfiguration.java | 13 +++++++++ .../rest/DummyRestProcessorFactory.java | 2 +- .../netty4/http/rest/RestApiNettyTest.java | 9 +----- .../src/test/resources/log4j.properties | 6 ++-- .../camel/swagger/RestSwaggerProcessor.java | 29 +++++++++++++------- .../swagger/SwaggerRestApiProcessorFactory.java | 4 +-- .../camel/example/cdi/UserRouteBuilder.java | 4 +-- 11 files changed, 74 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java index e2a0178..6e1d01b 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestApiEndpoint.java @@ -175,7 +175,10 @@ public class RestApiEndpoint extends DefaultEndpoint { path = "/" + path; } - Processor processor = factory.createApiProcessor(getCamelContext(), path, getContextIdPattern(), config, getParameters()); + // whether listing of the context id's is enabled or not + boolean contextIdListing = config.isApiContextListing(); + + Processor processor = factory.createApiProcessor(getCamelContext(), path, getContextIdPattern(), contextIdListing, config, getParameters()); return new RestApiProducer(this, processor); } else { throw new IllegalStateException("Cannot find RestApiProcessorFactory in Registry or classpath"); @@ -242,7 +245,7 @@ public class RestApiEndpoint extends DefaultEndpoint { // TODO: is this needed? // there may be an optional context path configured to help Camel calculate the correct urls for the REST services - // this may be needed when using camel-serlvet where we cannot get the actual context-path or port number of the servlet engine + // this may be needed when using camel-servlet where we cannot get the actual context-path or port number of the servlet engine // during init of the servlet /* String contextPath = config.getApiContextPath(); if (contextPath != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java index 0467f21..2854eb2 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java @@ -64,6 +64,9 @@ public class RestConfigurationDefinition { private String apiContextIdPattern; @XmlAttribute + private Boolean apiContextListing; + + @XmlAttribute private RestHostNameResolver hostNameResolver; @XmlAttribute @Metadata(defaultValue = "auto") @@ -209,6 +212,18 @@ public class RestConfigurationDefinition { this.apiContextIdPattern = apiContextIdPattern; } + public Boolean getApiContextListing() { + return apiContextListing; + } + + /** + * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover + * these contexts, if <tt>false</tt> then only the current CamelContext is in use. + */ + public void setApiContextListing(Boolean apiContextListing) { + this.apiContextListing = apiContextListing; + } + public RestHostNameResolver getHostNameResolver() { return hostNameResolver; } @@ -435,6 +450,15 @@ public class RestConfigurationDefinition { } /** + * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover + * these contexts, if <tt>false</tt> then only the current CamelContext is in use. + */ + public RestConfigurationDefinition apiContextListing(boolean listing) { + setApiContextListing(listing); + return this; + } + + /** * Sets a leading context-path the REST services will be using. * <p/> * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application @@ -605,6 +629,9 @@ public class RestConfigurationDefinition { answer.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern)); } } + if (apiContextListing != null) { + answer.setApiContextListing(apiContextListing); + } if (contextPath != null) { answer.setContextPath(CamelContextHelper.parseText(context, contextPath)); } http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/main/java/org/apache/camel/spi/RestApiConsumerFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestApiConsumerFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RestApiConsumerFactory.java index b051f29..c000d93 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestApiConsumerFactory.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestApiConsumerFactory.java @@ -24,8 +24,6 @@ import org.apache.camel.Processor; public interface RestApiConsumerFactory { - // TODO: merge this method to RestConsumerFactory - /** * Creates a new REST API <a * href="http://camel.apache.org/event-driven-consumer.html">Event http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java index 8f5210d..d65fbec 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java @@ -35,7 +35,7 @@ public interface RestApiProcessorFactory { * @return a newly created REST API provider * @throws Exception can be thrown */ - Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, + Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, RestConfiguration configuration, Map<String, Object> parameters) throws Exception; } http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java index c8cbcff..89c98ef 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java @@ -45,6 +45,7 @@ public class RestConfiguration { private String contextPath; private String apiContextPath; private String apiContextIdPattern; + private boolean apiContextListing; private RestHostNameResolver restHostNameResolver = RestHostNameResolver.localHostName; private RestBindingMode bindingMode = RestBindingMode.off; private boolean skipBindingOnErrorCode = true; @@ -201,6 +202,18 @@ public class RestConfiguration { this.apiContextIdPattern = apiContextIdPattern; } + public boolean isApiContextListing() { + return apiContextListing; + } + + /** + * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover + * these contexts, if <tt>false</tt> then only the current CamelContext is in use. + */ + public void setApiContextListing(boolean apiContextListing) { + this.apiContextListing = apiContextListing; + } + /** * Gets the resolver to use for resolving hostname * http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java b/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java index f2d2948..497910c 100644 --- a/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java +++ b/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java @@ -27,7 +27,7 @@ import org.apache.camel.spi.RestConfiguration; public class DummyRestProcessorFactory implements RestApiProcessorFactory { @Override - public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, + public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { return new Processor() { @Override http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestApiNettyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestApiNettyTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestApiNettyTest.java index 41c22dc..9d6fe12 100644 --- a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestApiNettyTest.java +++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestApiNettyTest.java @@ -30,14 +30,7 @@ public class RestApiNettyTest extends BaseNettyTest { @Test public void testApi() throws Exception { - String out = template.requestBody("netty4-http:http://localhost:{{port}}/api-doc/", null, String.class); - assertNotNull(out); - log.info(out); - - String id = context.getName(); - assertTrue(out.contains("{\"name\": \"" + id + "\"}")); - - out = template.requestBody("netty4-http:http://localhost:{{port}}/api-doc/" + id, null, String.class); + String out = template.requestBody("netty4-http:http://localhost:{{port}}/api-doc", null, String.class); assertNotNull(out); log.info(out); http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/components/camel-netty4-http/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-netty4-http/src/test/resources/log4j.properties b/components/camel-netty4-http/src/test/resources/log4j.properties index 6b57733..a9d61c7 100644 --- a/components/camel-netty4-http/src/test/resources/log4j.properties +++ b/components/camel-netty4-http/src/test/resources/log4j.properties @@ -21,10 +21,10 @@ log4j.rootLogger=INFO, file # uncomment the following to enable camel debugging -#log4j.logger.org.apache.camel.component.netty=TRACE -#log4j.logger.org.apache.camel.component.netty.http=DEBUG +#log4j.logger.org.apache.camel.component.netty4=TRACE +#log4j.logger.org.apache.camel.component.netty4.http=DEBUG #log4j.logger.org.apache.camel=DEBUG -#log4j.logger.org.jboss.netty=TRACE +#log4j.logger.io.netty=TRACE # CONSOLE appender not used by default log4j.appender.out=org.apache.log4j.ConsoleAppender http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java index fd04d87..5a34355 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java @@ -23,6 +23,7 @@ import io.swagger.jaxrs.config.BeanConfig; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.util.EndpointHelper; +import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,10 +33,12 @@ public class RestSwaggerProcessor implements Processor { private final BeanConfig swaggerConfig; private final RestSwaggerSupport support; private final String contextIdPattern; + private final boolean contextIdListing; @SuppressWarnings("unchecked") - public RestSwaggerProcessor(String contextIdPattern, Map<String, Object> parameters) { + public RestSwaggerProcessor(String contextIdPattern, boolean contextIdListing, Map<String, Object> parameters) { this.contextIdPattern = contextIdPattern; + this.contextIdListing = contextIdListing; this.support = new RestSwaggerSupport(); this.swaggerConfig = new BeanConfig(); @@ -55,17 +58,23 @@ public class RestSwaggerProcessor implements Processor { try { // render list of camel contexts as root - if (route == null || route.equals("") || route.equals("/")) { + if (contextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/"))) { support.renderCamelContexts(adapter, contextId, contextIdPattern); } else { - // first part is the camel context - if (route.startsWith("/")) { - route = route.substring(1); - } - // the remainder is the route part - String name = route.split("/")[0]; - if (route.startsWith(contextId)) { - route = route.substring(name.length()); + String name; + if (ObjectHelper.isNotEmpty(route)) { + // first part is the camel context + if (route.startsWith("/")) { + route = route.substring(1); + } + // the remainder is the route part + name = route.split("/")[0]; + if (route.startsWith(contextId)) { + route = route.substring(name.length()); + } + } else { + // listing not enabled then get current camel context as the name + name = exchange.getContext().getName(); } boolean match = true; http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java index dba4f89..45eff06 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java @@ -27,7 +27,7 @@ import org.apache.camel.spi.RestConfiguration; public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory { @Override - public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, + public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { Map<String, Object> options = new HashMap<String, Object>(parameters); @@ -50,6 +50,6 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory { String path = configuration.getContextPath(); options.put("base.path", path); - return new RestSwaggerProcessor(contextIdPattern, options); + return new RestSwaggerProcessor(contextIdPattern, contextIdListing, options); } } http://git-wip-us.apache.org/repos/asf/camel/blob/c91dcd3f/examples/camel-example-swagger-cdi/src/main/java/org/apache/camel/example/cdi/UserRouteBuilder.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-swagger-cdi/src/main/java/org/apache/camel/example/cdi/UserRouteBuilder.java b/examples/camel-example-swagger-cdi/src/main/java/org/apache/camel/example/cdi/UserRouteBuilder.java index f037444..e30d978 100644 --- a/examples/camel-example-swagger-cdi/src/main/java/org/apache/camel/example/cdi/UserRouteBuilder.java +++ b/examples/camel-example-swagger-cdi/src/main/java/org/apache/camel/example/cdi/UserRouteBuilder.java @@ -39,8 +39,8 @@ public class UserRouteBuilder extends RouteBuilder { .dataFormatProperty("prettyPrint", "true") // setup context path and port number that netty will use .contextPath("/rest").port(8080) - // add swagger api-doc out of the box, and only allow the docs for this CamelContext (#name#) - .apiContextPath("/api-doc").apiContextIdPattern("#name#") + // add swagger api-doc out of the box + .apiContextPath("/api-doc") .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3") // and enable CORS .apiProperty("cors", "true");