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/26ae7724 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/26ae7724 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/26ae7724 Branch: refs/heads/master Commit: 26ae7724e1877645b1e36c7c3575760e0f83120a Parents: bdbeb4a Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Sep 22 21:14:50 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Sep 23 07:51:05 2015 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestApiEndpoint.java | 39 ++++++++++++++++++-- .../model/rest/RestConfigurationDefinition.java | 25 +++++++++++++ .../camel/spi/RestApiConsumerFactory.java | 2 + .../org/apache/camel/spi/RestConfiguration.java | 19 ++++++++++ .../netty4/http/rest/RestApiNettyTest.java | 9 ----- .../services/org/apache/camel/component/sql | 18 --------- .../services/org/apache/camel/rest/swagger | 18 +++++++++ 7 files changed, 100 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/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 56fe965..7ceb16b 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 @@ -16,15 +16,18 @@ */ package org.apache.camel.component.rest; +import java.io.IOException; import java.util.Map; import java.util.Set; +import org.apache.camel.CamelContext; import org.apache.camel.Component; import org.apache.camel.Consumer; import org.apache.camel.NoSuchBeanException; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RestApiConsumerFactory; import org.apache.camel.spi.RestApiProcessorFactory; @@ -36,10 +39,15 @@ import org.apache.camel.spi.UriPath; @UriEndpoint(scheme = "rest-api", title = "REST API", syntax = "rest-api:path", consumerOnly = true, label = "core,rest") public class RestApiEndpoint extends DefaultEndpoint { + public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/rest/"; + private FactoryFinder factoryFinder; + @UriPath @Metadata(required = "true") private String path; @UriParam private String componentName; + @UriParam + private String apiComponentName; private Map<String, Object> parameters; @@ -77,6 +85,17 @@ public class RestApiEndpoint extends DefaultEndpoint { this.componentName = componentName; } + public String getApiComponentName() { + return apiComponentName; + } + + /** + * The Camel Rest API component to use for generating the API of the REST services, such as swagger. + */ + public void setApiComponentName(String apiComponentName) { + this.apiComponentName = apiComponentName; + } + public Map<String, Object> getParameters() { return parameters; } @@ -88,19 +107,33 @@ public class RestApiEndpoint extends DefaultEndpoint { this.parameters = parameters; } + private Class<?> findApiProcessorFactory(String name, CamelContext context) throws ClassNotFoundException, IOException { + if (factoryFinder == null) { + factoryFinder = context.getFactoryFinder(RESOURCE_PATH); + } + return factoryFinder.findClass(name); + } + @Override public Producer createProducer() throws Exception { RestApiProcessorFactory factory = null; + RestConfiguration config = getCamelContext().getRestConfiguration(componentName, true); + // lookup in registry Set<RestApiProcessorFactory> factories = getCamelContext().getRegistry().findByType(RestApiProcessorFactory.class); if (factories != null && factories.size() == 1) { factory = factories.iterator().next(); } - if (factory != null) { + // lookup on classpath using factory finder + String name = apiComponentName != null ? apiComponentName : config.getApiComponent(); + Object instance = getCamelContext().getFactoryFinder(RESOURCE_PATH).newInstance(name); + if (instance instanceof RestApiProcessorFactory) { + factory = (RestApiProcessorFactory) instance; + } - RestConfiguration config = getCamelContext().getRestConfiguration(componentName, true); + if (factory != null) { // calculate the url to the rest API service String path = getPath(); @@ -111,7 +144,7 @@ public class RestApiEndpoint extends DefaultEndpoint { Processor processor = factory.createApiProcessor(getCamelContext(), path, config, getParameters()); return new RestApiProducer(this, processor); } else { - throw new IllegalStateException("Cannot find RestApiProcessorFactory in Registry"); + throw new IllegalStateException("Cannot find RestApiProcessorFactory in Registry or classpath"); } } http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/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 2c5906d..90a6138 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 @@ -42,6 +42,9 @@ public class RestConfigurationDefinition { @XmlAttribute private String component; + @XmlAttribute @Metadata(defaultValue = "swagger") + private String apiComponent; + @XmlAttribute private String scheme; @@ -107,6 +110,17 @@ public class RestConfigurationDefinition { this.component = component; } + public String getApiComponent() { + return apiComponent; + } + + /** + * The name of the Camel component to use as the REST API (such as swagger) + */ + public void setApiComponent(String apiComponent) { + this.apiComponent = apiComponent; + } + public String getScheme() { return scheme; } @@ -341,6 +355,14 @@ public class RestConfigurationDefinition { } /** + * To use a specific Camel rest API component + */ + public RestConfigurationDefinition apiComponent(String componentId) { + setApiComponent(componentId); + return this; + } + + /** * To use a specific scheme such as http/https */ public RestConfigurationDefinition scheme(String scheme) { @@ -531,6 +553,9 @@ public class RestConfigurationDefinition { if (component != null) { answer.setComponent(CamelContextHelper.parseText(context, component)); } + if (apiComponent != null) { + answer.setApiComponent(CamelContextHelper.parseText(context, apiComponent)); + } if (scheme != null) { answer.setScheme(CamelContextHelper.parseText(context, scheme)); } http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/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 c000d93..b051f29 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,6 +24,8 @@ 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/26ae7724/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 768ad42..55a45d6 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 @@ -38,6 +38,7 @@ public class RestConfiguration { } private String component; + private String apiComponent; private String scheme; private String host; private int port; @@ -75,6 +76,24 @@ public class RestConfiguration { } /** + * Gets the name of the Camel component to use as the REST API (such as swagger) + * + * @return the component name, or <tt>null</tt> to let Camel use the default name <tt>swagger</tt> + */ + public String getApiComponent() { + return apiComponent; + } + + /** + * Sets the name of the Camel component to use as the REST API (such as swagger) + * + * @param apiComponent the name of the component (such as swagger) + */ + public void setApiComponent(String apiComponent) { + this.apiComponent = apiComponent; + } + + /** * Gets the hostname to use by the REST consumer * * @return the hostname, or <tt>null</tt> to use default hostname http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/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 99d1665..41c22dc 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 @@ -18,21 +18,12 @@ package org.apache.camel.component.netty4.http.rest; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.netty4.http.BaseNettyTest; -import org.apache.camel.impl.JndiRegistry; import org.apache.camel.model.rest.RestParamType; -import org.apache.camel.swagger.SwaggerRestApiProcessorFactory; import org.junit.Test; public class RestApiNettyTest extends BaseNettyTest { @Override - protected JndiRegistry createRegistry() throws Exception { - JndiRegistry jndi = super.createRegistry(); - jndi.bind("SwaggerRestApiProcessorFactory", new SwaggerRestApiProcessorFactory()); - return jndi; - } - - @Override protected boolean useJmx() { return true; } http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/components/camel-sql/src/main/resources/META-INF/services/org/apache/camel/component/sql ---------------------------------------------------------------------- diff --git a/components/camel-sql/src/main/resources/META-INF/services/org/apache/camel/component/sql b/components/camel-sql/src/main/resources/META-INF/services/org/apache/camel/component/sql deleted file mode 100755 index 3b9a254..0000000 --- a/components/camel-sql/src/main/resources/META-INF/services/org/apache/camel/component/sql +++ /dev/null @@ -1,18 +0,0 @@ -# -# 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. -# - -class=org.apache.camel.component.sql.SqlComponent http://git-wip-us.apache.org/repos/asf/camel/blob/26ae7724/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/rest/swagger ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/rest/swagger b/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/rest/swagger new file mode 100755 index 0000000..999809c --- /dev/null +++ b/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/rest/swagger @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.swagger.SwaggerRestApiProcessorFactory