You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/08/26 16:02:34 UTC
[06/23] camel git commit: CAMEL-10164: swagger component for making
rest calls with swagger schema validation and facade to actual HTTP client in
use
CAMEL-10164: swagger component for making rest calls with swagger schema validation and facade to actual HTTP client in use
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/adde56ac
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/adde56ac
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/adde56ac
Branch: refs/heads/master
Commit: adde56ac56144ff1ec0e0b7425ceacf1d8c3ad98
Parents: 14352b1
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Aug 24 12:27:27 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Aug 26 16:53:31 2016 +0200
----------------------------------------------------------------------
.../apache/camel/spi/RestConsumerFactory.java | 20 ++-
.../apache/camel/spi/RestProducerFactory.java | 51 +++++++
.../SwaggerComponentAutoConfiguration.java | 52 +++++++
.../SwaggerComponentConfiguration.java | 67 +++++++++
.../main/resources/META-INF/spring.factories | 19 +++
.../swagger/component/SwaggerComponent.java | 16 +-
.../swagger/component/SwaggerEndpoint.java | 24 ++-
.../swagger/component/SwaggerProducer.java | 147 +++++++++++++++----
.../component/DummyRestProducerFactory.java | 45 ++++++
.../component/SwaggerComponentGetTest.java | 9 ++
.../camel/swagger/component/SwaggerGetTest.java | 13 ++
11 files changed, 418 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java
index bbb5f15..4ab546a 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java
@@ -26,6 +26,9 @@ import org.apache.camel.Processor;
* Allows SPI to plugin a {@link RestConsumerFactory} that creates the Camel {@link Consumer} responsible
* for handling incoming HTTP requests from clients that request to access REST services which has been created using
* the <a href="http://camel.apache.org/rest-dsl">rest-dsl</a>.
+ *
+ * @see RestApiConsumerFactory
+ * @see RestApiProcessorFactory
*/
public interface RestConsumerFactory {
@@ -34,14 +37,15 @@ public interface RestConsumerFactory {
* href="http://camel.apache.org/event-driven-consumer.html">Event
* Driven Consumer</a>, which consumes messages from the endpoint using the given processor
*
- * @param camelContext the camel context
- * @param processor the processor
- * @param verb HTTP verb such as GET, POST
- * @param basePath base path
- * @param uriTemplate uri template
- * @param consumes media-types for what this REST service consume as input (accept-type), is <tt>null</tt> or <tt>*/*</tt> for anything
- * @param produces media-types for what this REST service produces as output, can be <tt>null</tt>
- * @param parameters additional parameters
+ * @param camelContext the camel context
+ * @param processor the processor
+ * @param verb HTTP verb such as GET, POST
+ * @param basePath base path
+ * @param uriTemplate uri template
+ * @param consumes media-types for what this REST service consume as input (accept-type), is <tt>null</tt> or <tt>*/*</tt> for anything
+ * @param produces media-types for what this REST service produces as output, can be <tt>null</tt>
+ * @param configuration REST configuration
+ * @param parameters additional parameters
* @return a newly created REST consumer
* @throws Exception can be thrown
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/camel-core/src/main/java/org/apache/camel/spi/RestProducerFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestProducerFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RestProducerFactory.java
new file mode 100644
index 0000000..5da1261
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/spi/RestProducerFactory.java
@@ -0,0 +1,51 @@
+/**
+ * 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.spi;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+
+/**
+ * Allows SPI to plugin a {@link RestProducerFactory} that creates the Camel {@link Producer} responsible
+ * for performing HTTP requests to call a remote REST service.
+ */
+public interface RestProducerFactory {
+
+ /**
+ * Creates a new REST producer.
+ *
+ * @param camelContext the camel context
+ * @param exchange the exchange
+ * @param scheme scheme to use such as http or https
+ * @param host host (incl port) of the REST service
+ * @param verb HTTP verb such as GET, POST
+ * @param basePath base path
+ * @param uriTemplate uri template
+ * @param queryParameters query parameters
+ * @param consumes media-types for what the REST service consume as input (accept-type), is <tt>null</tt> or <tt>*/*</tt> for anything
+ * @param produces media-types for what the REST service produces as output, can be <tt>null</tt>
+ * @param parameters additional parameters
+ * @return a newly created REST producer
+ * @throws Exception can be thrown
+ */
+ Producer createProducer(CamelContext camelContext, Exchange exchange, String scheme, String host,
+ String verb, String basePath, String uriTemplate, String queryParameters,
+ String consumes, String produces, Map<String, Object> parameters) throws Exception;
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentAutoConfiguration.java b/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentAutoConfiguration.java
new file mode 100644
index 0000000..af238a2
--- /dev/null
+++ b/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentAutoConfiguration.java
@@ -0,0 +1,52 @@
+/**
+ * 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.swagger.component.springboot;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.camel.CamelContext;
+import org.apache.camel.swagger.component.SwaggerComponent;
+import org.apache.camel.util.IntrospectionSupport;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Generated by camel-package-maven-plugin - do not edit this file!
+ */
+@Configuration
+@EnableConfigurationProperties(SwaggerComponentConfiguration.class)
+public class SwaggerComponentAutoConfiguration {
+
+ @Bean(name = "swagger-component")
+ @ConditionalOnClass(CamelContext.class)
+ @ConditionalOnMissingBean(SwaggerComponent.class)
+ public SwaggerComponent configureSwaggerComponent(
+ CamelContext camelContext,
+ SwaggerComponentConfiguration configuration) throws Exception {
+ SwaggerComponent component = new SwaggerComponent();
+ component.setCamelContext(camelContext);
+ Map<String, Object> parameters = new HashMap<>();
+ IntrospectionSupport.getProperties(configuration, parameters, null,
+ false);
+ IntrospectionSupport.setProperties(camelContext,
+ camelContext.getTypeConverter(), component, parameters);
+ return component;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentConfiguration.java b/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentConfiguration.java
new file mode 100644
index 0000000..b31b00d
--- /dev/null
+++ b/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentConfiguration.java
@@ -0,0 +1,67 @@
+/**
+ * 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.swagger.component.springboot;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * Camel Swagger Java support
+ *
+ * Generated by camel-package-maven-plugin - do not edit this file!
+ */
+@ConfigurationProperties(prefix = "camel.component.swagger")
+public class SwaggerComponentConfiguration {
+
+ /**
+ * The swagger schema to use in json format. The schema is loaded as a
+ * resource from the classpath or file system.
+ */
+ private String schema;
+ /**
+ * The camel component to use as HTTP client for calling the REST service.
+ * The default value is: http
+ */
+ private String componentName;
+ /**
+ * Host and port of HTTP service to use (override host in swagger schema)
+ */
+ private String host;
+
+ public String getSchema() {
+ return schema;
+ }
+
+ public void setSchema(String schema) {
+ this.schema = schema;
+ }
+
+ public String getComponentName() {
+ return componentName;
+ }
+
+ public void setComponentName(String componentName) {
+ this.componentName = componentName;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components-starter/camel-swagger-java-starter/src/main/resources/META-INF/spring.factories
----------------------------------------------------------------------
diff --git a/components-starter/camel-swagger-java-starter/src/main/resources/META-INF/spring.factories b/components-starter/camel-swagger-java-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000..d313eab
--- /dev/null
+++ b/components-starter/camel-swagger-java-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.apache.camel.swagger.component.springboot.SwaggerComponentAutoConfiguration
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java
index a439379..bf012f4 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java
@@ -25,6 +25,7 @@ public class SwaggerComponent extends UriEndpointComponent {
private String componentName = "http";
private String schema;
+ private String host;
public SwaggerComponent() {
super(SwaggerEndpoint.class);
@@ -59,10 +60,6 @@ public class SwaggerComponent extends UriEndpointComponent {
}
endpoint.setPath(path);
- setProperties(endpoint, parameters);
- // any leftover parameters should be kept as additional uri parameters
-
-
return endpoint;
}
@@ -90,4 +87,15 @@ public class SwaggerComponent extends UriEndpointComponent {
public void setComponentName(String componentName) {
this.componentName = componentName;
}
+
+ public String getHost() {
+ return host;
+ }
+
+ /**
+ * Host and port of HTTP service to use (override host in swagger schema)
+ */
+ public void setHost(String host) {
+ this.host = host;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java
index c5f7cb5..df1dc02 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java
@@ -44,7 +44,7 @@ public class SwaggerEndpoint extends DefaultEndpoint {
private transient Swagger swagger;
- @UriPath
+ @UriPath(enums = "http,https")
private String schema;
@UriPath(enums = "get,put,post,head,delete,patch,options") @Metadata(required = "true")
private String verb;
@@ -52,6 +52,8 @@ public class SwaggerEndpoint extends DefaultEndpoint {
private String path;
@UriParam
private String componentName;
+ @UriParam
+ private String host;
public SwaggerEndpoint(String endpointUri, Component component) {
super(endpointUri, component);
@@ -78,6 +80,9 @@ public class SwaggerEndpoint extends DefaultEndpoint {
return schema;
}
+ /**
+ * Scheme to use when calling the REST service such as http or https
+ */
public void setSchema(String schema) {
this.schema = schema;
}
@@ -86,6 +91,9 @@ public class SwaggerEndpoint extends DefaultEndpoint {
return verb;
}
+ /**
+ * Verb of the HTTP service such as get,post,put etc.
+ */
public void setVerb(String verb) {
this.verb = verb;
}
@@ -94,6 +102,9 @@ public class SwaggerEndpoint extends DefaultEndpoint {
return path;
}
+ /**
+ * Uri template (context-path) of HTTP service to call
+ */
public void setPath(String path) {
this.path = path;
}
@@ -110,6 +121,17 @@ public class SwaggerEndpoint extends DefaultEndpoint {
this.componentName = componentName;
}
+ public String getHost() {
+ return host;
+ }
+
+ /**
+ * Host and port of HTTP service to use (override host in swagger schema)
+ */
+ public void setHost(String host) {
+ this.host = host;
+ }
+
@Override
protected void doStart() throws Exception {
super.doStart();
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java
index 9af31be..019bdd3 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java
@@ -17,17 +17,26 @@
package org.apache.camel.swagger.component;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.Component;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
+import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.NoSuchHeaderException;
+import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultAsyncProducer;
-import org.apache.camel.util.StringHelper;
+import org.apache.camel.spi.RestProducerFactory;
+import org.apache.camel.util.AsyncProcessorConverterHelper;
+import org.apache.camel.util.CollectionStringBuffer;
import org.apache.camel.util.URISupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -36,8 +45,6 @@ public class SwaggerProducer extends DefaultAsyncProducer {
private static final Logger LOG = LoggerFactory.getLogger(SwaggerProducer.class);
- // TODO: delegate to actual producer
-
private Swagger swagger;
public SwaggerProducer(Endpoint endpoint) {
@@ -54,59 +61,54 @@ public class SwaggerProducer extends DefaultAsyncProducer {
String verb = getEndpoint().getVerb();
String path = getEndpoint().getPath();
- Operation op = getSwaggerOperation(verb, path);
- if (op == null) {
+ Operation operation = getSwaggerOperation(verb, path);
+ if (operation == null) {
exchange.setException(new IllegalArgumentException("Swagger schema does not contain operation for " + verb + ":" + path));
callback.done(true);
return true;
}
try {
- // build context path to use for actual HTTP call
- // replace path parameters with value from header
- String contextPath = path;
Map<String, Object> query = new LinkedHashMap<>();
- for (Parameter param : op.getParameters()) {
- if ("path".equals(param.getIn())) {
- String name = param.getName();
- if (name != null) {
- String value = exchange.getIn().getHeader(name, String.class);
- if (value != null) {
- String key = "{" + name + "}";
- contextPath = StringHelper.replaceAll(contextPath, key, value);
- }
- }
- } else if ("query".equals(param.getIn())) {
+ for (Parameter param : operation.getParameters()) {
+ if ("query".equals(param.getIn())) {
String name = param.getName();
if (name != null) {
String value = exchange.getIn().getHeader(name, String.class);
if (value != null) {
query.put(name, value);
+ } else if (param.getRequired()) {
+ // the parameter is required but there is no header with the value
+ exchange.setException(new NoSuchHeaderException(exchange, name, String.class));
+ callback.done(true);
+ return true;
}
}
}
}
+
+ // build as query string
+ String options = null;
if (!query.isEmpty()) {
- String options = URISupport.createQueryString(query);
- contextPath = contextPath + "?" + options;
+ options = URISupport.createQueryString(query);
}
- LOG.debug("Using context-path: {}", contextPath);
+ // TODO: bind to consumes context-type
+ // TODO: if binding is turned on/off/auto etc
+ // TODO: build dynamic uri for component (toD, headers)
+ // create http producer to use for calling the remote HTTP service
+ // TODO: create the producer once and reuse (create HTTP_XXX headers for dynamic values)
+ Producer producer = createHttpProducer(exchange, operation, verb, path, options);
+ if (producer != null) {
+ AsyncProcessor async = AsyncProcessorConverterHelper.convert(producer);
+ return async.process(exchange, callback);
+ }
} catch (Throwable e) {
exchange.setException(e);
- callback.done(true);
- return true;
}
- // TODO: bind to consumes context-type
- // TODO: if binding is turned on/off/auto etc
- // TODO: use the component and build uri with verb/path
- // TODO: build dynamic uri for component (toD, headers)
-
- exchange.getIn().setBody("Hello Donald Duck");
-
- // do some binding first
+ // some error or there was no producer, so we are done
callback.done(true);
return true;
}
@@ -144,4 +146,85 @@ public class SwaggerProducer extends DefaultAsyncProducer {
public void setSwagger(Swagger swagger) {
this.swagger = swagger;
}
+
+ protected Producer createHttpProducer(Exchange exchange, Operation operation, String verb, String path, String queryParameters) throws Exception {
+ RestProducerFactory factory = null;
+ String cname = null;
+ if (getEndpoint().getComponentName() != null) {
+ Object comp = getEndpoint().getCamelContext().getRegistry().lookupByName(getEndpoint().getComponentName());
+ if (comp != null && comp instanceof RestProducerFactory) {
+ factory = (RestProducerFactory) comp;
+ } else {
+ comp = getEndpoint().getCamelContext().getComponent(getEndpoint().getComponentName());
+ if (comp != null && comp instanceof RestProducerFactory) {
+ factory = (RestProducerFactory) comp;
+ }
+ }
+
+ if (factory == null) {
+ if (comp != null) {
+ throw new IllegalArgumentException("Component " + getEndpoint().getComponentName() + " is not a RestProducerFactory");
+ } else {
+ throw new NoSuchBeanException(getEndpoint().getComponentName(), RestProducerFactory.class.getName());
+ }
+ }
+ cname = getEndpoint().getComponentName();
+ }
+
+ // try all components
+ if (factory == null) {
+ for (String name : getEndpoint().getCamelContext().getComponentNames()) {
+ Component comp = getEndpoint().getCamelContext().getComponent(name);
+ if (comp != null && comp instanceof RestProducerFactory) {
+ factory = (RestProducerFactory) comp;
+ cname = name;
+ break;
+ }
+ }
+ }
+
+ // lookup in registry
+ if (factory == null) {
+ Set<RestProducerFactory> factories = getEndpoint().getCamelContext().getRegistry().findByType(RestProducerFactory.class);
+ if (factories != null && factories.size() == 1) {
+ factory = factories.iterator().next();
+ }
+ }
+
+ if (factory != null) {
+
+ CollectionStringBuffer produces = new CollectionStringBuffer(",");
+ List<String> list = operation.getProduces();
+ if (list == null) {
+ list = swagger.getProduces();
+ }
+ if (list != null) {
+ for (String s : list) {
+ produces.append(s);
+ }
+ }
+ CollectionStringBuffer consumes = new CollectionStringBuffer(",");
+ list = operation.getConsumes();
+ if (list == null) {
+ list = swagger.getConsumes();
+ }
+ if (list != null) {
+ for (String s : list) {
+ consumes.append(s);
+ }
+ }
+
+ // TODO: allow to chose scheme if there is multiple
+ String scheme = swagger.getSchemes() != null && swagger.getSchemes().size() == 1 ? swagger.getSchemes().get(0).toValue() : "http";
+ String host = getEndpoint().getHost() != null ? getEndpoint().getHost() : swagger.getHost();
+ String basePath = swagger.getBasePath();
+ String uriTemplate = path;
+
+ return factory.createProducer(getEndpoint().getCamelContext(), exchange, scheme, host, verb, basePath, uriTemplate, queryParameters,
+ (consumes.isEmpty() ? "" : consumes.toString()), (produces.isEmpty() ? "" : produces.toString()), null);
+
+ } else {
+ throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/DummyRestProducerFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/DummyRestProducerFactory.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/DummyRestProducerFactory.java
new file mode 100644
index 0000000..d960dbe
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/DummyRestProducerFactory.java
@@ -0,0 +1,45 @@
+/**
+ * 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.swagger.component;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.spi.RestProducerFactory;
+
+public class DummyRestProducerFactory implements RestProducerFactory {
+
+ @Override
+ public Producer createProducer(CamelContext camelContext, Exchange exchange, String scheme, String host,
+ String verb, String basePath, final String uriTemplate, final String queryParameters,
+ String consumes, String produces, Map<String, Object> parameters) throws Exception {
+
+ return new DefaultProducer(null) {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ // for testing purpose, check if we have {name} in template
+ if (uriTemplate.contains("{name}")) {
+ String name = exchange.getIn().getHeader("name", String.class);
+ exchange.getIn().setBody("Hello " + name);
+ }
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java
index 451b3cb..ffe80a3 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java
@@ -18,11 +18,19 @@ package org.apache.camel.swagger.component;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class SwaggerComponentGetTest extends CamelTestSupport {
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("dummy", new DummyRestProducerFactory());
+ return jndi;
+ }
+
@Test
public void testSwaggerGet() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Hello Donald Duck");
@@ -38,6 +46,7 @@ public class SwaggerComponentGetTest extends CamelTestSupport {
@Override
public void configure() throws Exception {
SwaggerComponent sc = new SwaggerComponent();
+ sc.setComponentName("dummy");
sc.setSchema("hello-api.json");
context.addComponent("swagger", sc);
http://git-wip-us.apache.org/repos/asf/camel/blob/adde56ac/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java
index eb29281..f4b6937 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java
@@ -18,11 +18,19 @@ package org.apache.camel.swagger.component;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class SwaggerGetTest extends CamelTestSupport {
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("dummy", new DummyRestProducerFactory());
+ return jndi;
+ }
+
@Test
public void testSwaggerGet() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Hello Donald Duck");
@@ -37,6 +45,11 @@ public class SwaggerGetTest extends CamelTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
+ SwaggerComponent sc = new SwaggerComponent();
+ sc.setComponentName("dummy");
+
+ context.addComponent("swagger", sc);
+
from("direct:start")
.to("swagger:hello-api.json:get:hello/hi/{name}")
.to("mock:result");