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/83a097e7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/83a097e7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/83a097e7 Branch: refs/heads/master Commit: 83a097e7889654c27396f1bc10282bcf3a410307 Parents: 19b1f5c Author: Claus Ibsen <[email protected]> Authored: Thu Aug 25 14:23:30 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 26 16:53:31 2016 +0200 ---------------------------------------------------------------------- camel-core/readme.adoc | 2 +- .../SwaggerComponentAutoConfiguration.java | 52 ---- .../SwaggerComponentConfiguration.java | 67 ----- .../main/resources/META-INF/spring.factories | 19 -- ...ttySwaggerRestProducerGetQueryParamTest.java | 60 ---- .../JettySwaggerRestProducerGetTest.java | 62 ---- .../swagger/component/SwaggerComponent.java | 107 ------- .../swagger/component/SwaggerEndpoint.java | 175 ------------ .../swagger/component/SwaggerProducer.java | 282 ------------------- .../services/org/apache/camel/component/swagger | 18 -- .../component/DummyRestProducerFactory.java | 56 ---- .../swagger/component/RestSwaggerGetTest.java | 57 ---- .../component/RestSwaggerGetUriParamTest.java | 56 ---- .../component/SwaggerComponentGetTest.java | 63 ----- .../camel/swagger/component/SwaggerGetTest.java | 62 ---- .../component/SwaggerGetUriParamTest.java | 62 ---- .../producer/DummyRestProducerFactory.java | 56 ++++ .../swagger/producer/RestSwaggerGetTest.java | 56 ++++ .../producer/RestSwaggerGetUriParamTest.java | 56 ++++ components/readme.adoc | 3 - docs/user-manual/en/SUMMARY.md | 1 - 21 files changed, 169 insertions(+), 1203 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/camel-core/readme.adoc ---------------------------------------------------------------------- diff --git a/camel-core/readme.adoc b/camel-core/readme.adoc index e7160f0..a552c5f 100644 --- a/camel-core/readme.adoc +++ b/camel-core/readme.adoc @@ -56,7 +56,7 @@ Components `ref:name` | The ref component is used for lookup of existing endpoints bound in the Registry. | link:src/main/docs/rest-component.adoc[REST] (camel-core) + -`rest:method:path:uriTemplate` | The rest component is used for hosting REST services which has been defined using the rest-dsl in Camel. +`rest:method:path:uriTemplate` | The rest component is used for either hosting REST services (consumer) or calling external REST services (producer). | link:src/main/docs/rest-api-component.adoc[REST API] (camel-core) + `rest-api:path/contextIdPattern` | The rest-api component is used for providing Swagger API of the REST services which has been defined using the rest-dsl in Camel. http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index af238a2..0000000 --- a/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentAutoConfiguration.java +++ /dev/null @@ -1,52 +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. - */ -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/83a097e7/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 deleted file mode 100644 index 7fdc34c..0000000 --- a/components-starter/camel-swagger-java-starter/src/main/java/org/apache/camel/swagger/component/springboot/SwaggerComponentConfiguration.java +++ /dev/null @@ -1,67 +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. - */ -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 camel component to use as HTTP client for calling the REST service. - * The default value is: http - */ - private String componentName; - /** - * The swagger api doc resource to use. The resource is loaded from - * classpath by default and must be in JSon format. - */ - private String apiDoc; - /** - * Host and port of HTTP service to use (override host in swagger schema) - */ - private String host; - - public String getComponentName() { - return componentName; - } - - public void setComponentName(String componentName) { - this.componentName = componentName; - } - - public String getApiDoc() { - return apiDoc; - } - - public void setApiDoc(String apiDoc) { - this.apiDoc = apiDoc; - } - - 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/83a097e7/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 deleted file mode 100644 index d313eab..0000000 --- a/components-starter/camel-swagger-java-starter/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,19 +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. -# - -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.apache.camel.swagger.component.springboot.SwaggerComponentAutoConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetQueryParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetQueryParamTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetQueryParamTest.java deleted file mode 100644 index 7ee98f6..0000000 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetQueryParamTest.java +++ /dev/null @@ -1,60 +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 - * <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.producer; - -import org.apache.camel.RoutesBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.jetty.BaseJettyTest; -import org.apache.camel.swagger.component.SwaggerComponent; -import org.junit.Ignore; -import org.junit.Test; - -@Ignore -@Deprecated -public class JettySwaggerRestProducerGetQueryParamTest extends BaseJettyTest { - - @Test - public void testSwaggerGet() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Bye Donald Duck"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - SwaggerComponent sc = new SwaggerComponent(); - sc.setComponentName("jetty"); - context.addComponent("swagger", sc); - - String host = "localhost:" + getPort(); - - from("direct:start") - .toF("swagger:get:bye?host=%s&apiDoc=%s", host, "hello-api.json") - .to("mock:result"); - - from("jetty:http://localhost:{{port}}/api/bye/?matchOnUriPrefix=true") - .transform().simple("Bye ${header.name}"); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetTest.java deleted file mode 100644 index 3438390..0000000 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettySwaggerRestProducerGetTest.java +++ /dev/null @@ -1,62 +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 - * <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.producer; - -import org.apache.camel.RoutesBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.jetty.BaseJettyTest; -import org.apache.camel.swagger.component.SwaggerComponent; -import org.junit.Ignore; -import org.junit.Test; - -@Ignore -@Deprecated -public class JettySwaggerRestProducerGetTest extends BaseJettyTest { - - @Test - public void testSwaggerGet() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Hello Donald Duck"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - String host = "localhost:" + getPort(); - - SwaggerComponent sc = new SwaggerComponent(); - sc.setComponentName("jetty"); - sc.setHost(host); - sc.setApiDoc("hello-api.json"); - context.addComponent("swagger", sc); - - from("direct:start") - .to("swagger:get:hello/hi/{name}") - .to("mock:result"); - - from("jetty:http://localhost:{{port}}/api/hello/hi/?matchOnUriPrefix=true") - .transform().constant("Hello Donald Duck"); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index 6634c13..0000000 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerComponent.java +++ /dev/null @@ -1,107 +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. - */ -package org.apache.camel.swagger.component; - -import java.util.Map; - -import org.apache.camel.Endpoint; -import org.apache.camel.impl.UriEndpointComponent; -import org.apache.camel.util.URISupport; - -@Deprecated -public class SwaggerComponent extends UriEndpointComponent { - - private String componentName = "http"; - private String apiDoc; - private String host; - - // TODO: we could move this to rest component in camel-core - // and have its producer support using a swagger schema and use a factory to lookup - // the code in this component that creates the producer - - public SwaggerComponent() { - super(SwaggerEndpoint.class); - } - - @Override - protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - SwaggerEndpoint endpoint = new SwaggerEndpoint(uri, this); - endpoint.setComponentName(componentName); - endpoint.setApiDoc(apiDoc); - endpoint.setHost(host); - - String verb; - String path; - String[] parts = remaining.split(":"); - if (parts.length == 2) { - verb = parts[0]; - path = parts[1]; - } else { - throw new IllegalArgumentException("Invalid syntax. Expected swagger:verb:path?options"); - } - - endpoint.setVerb(verb); - // path must start with leading slash - if (!path.startsWith("/")) { - path = "/" + path; - } - endpoint.setPath(path); - - setProperties(endpoint, parameters); - - // the rest is URI parameters on path - String query = URISupport.createQueryString(parameters); - endpoint.setQueryParameters(query); - - return endpoint; - } - - public String getComponentName() { - return componentName; - } - - /** - * The camel component to use as HTTP client for calling the REST service. - * The default value is: http - */ - public void setComponentName(String componentName) { - this.componentName = componentName; - } - - public String getApiDoc() { - return apiDoc; - } - - /** - * The swagger api doc resource to use. - * The resource is loaded from classpath by default and must be in JSon format. - */ - public void setApiDoc(String apiDoc) { - this.apiDoc = apiDoc; - } - - 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/83a097e7/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 deleted file mode 100644 index 375b22d..0000000 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerEndpoint.java +++ /dev/null @@ -1,175 +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. - */ -package org.apache.camel.swagger.component; - -import java.io.InputStream; - -import io.swagger.models.Swagger; -import io.swagger.parser.SwaggerParser; -import org.apache.camel.Component; -import org.apache.camel.Consumer; -import org.apache.camel.Processor; -import org.apache.camel.Producer; -import org.apache.camel.impl.DefaultEndpoint; -import org.apache.camel.spi.Metadata; -import org.apache.camel.spi.UriEndpoint; -import org.apache.camel.spi.UriParam; -import org.apache.camel.spi.UriPath; -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.apache.camel.util.ResourceHelper.resolveMandatoryResourceAsInputStream; - -@Deprecated -@UriEndpoint(scheme = "swagger", title = "Swagger", syntax = "swagger:verb:path", - producerOnly = true, label = "rest", lenientProperties = true) -public class SwaggerEndpoint extends DefaultEndpoint { - - private static final Logger LOG = LoggerFactory.getLogger(SwaggerEndpoint.class); - - private transient Swagger swagger; - - @UriPath(enums = "get,put,post,head,delete,patch,options") @Metadata(required = "true") - private String verb; - @UriPath @Metadata(required = "true") - private String path; - @UriParam - private String componentName; - @UriParam - private String apiDoc; - @UriParam - private String host; - @UriParam(multiValue = true) - private String queryParameters; - - public SwaggerEndpoint(String endpointUri, Component component) { - super(endpointUri, component); - } - - @Override - public Producer createProducer() throws Exception { - SwaggerProducer answer = new SwaggerProducer(this); - answer.setSwagger(swagger); - return answer; - } - - @Override - public Consumer createConsumer(Processor processor) throws Exception { - throw new UnsupportedOperationException("Consumer not supported"); - } - - @Override - public boolean isLenientProperties() { - return true; - } - - @Override - public boolean isSingleton() { - return true; - } - - public String getVerb() { - return verb; - } - - /** - * Verb of the HTTP service such as get,post,put etc. - */ - public void setVerb(String verb) { - this.verb = verb; - } - - public String getPath() { - return path; - } - - /** - * Uri template (context-path) of HTTP service to call - */ - public void setPath(String path) { - this.path = path; - } - - public String getQueryParameters() { - return queryParameters; - } - - /** - * Query parameters for the HTTP service to call - */ - public void setQueryParameters(String queryParameters) { - this.queryParameters = queryParameters; - } - - public String getComponentName() { - return componentName; - } - - /** - * The camel component to use as HTTP client for calling the REST service. - * The default value is: http - */ - public void setComponentName(String componentName) { - this.componentName = componentName; - } - - public String getApiDoc() { - return apiDoc; - } - - /** - * The swagger api doc resource to use. - * The resource is loaded from classpath by default and must be in JSon format. - */ - public void setApiDoc(String apiDoc) { - this.apiDoc = apiDoc; - } - - public String getHost() { - return host; - } - - /** - * Host and port of HTTP service to use (override host in swagger api-doc) - */ - public void setHost(String host) { - this.host = host; - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - - // load json model - if (apiDoc == null) { - throw new IllegalArgumentException("The swagger api-doc must be configured using the apiDoc option"); - } - - InputStream is = resolveMandatoryResourceAsInputStream(getCamelContext(), apiDoc); - try { - SwaggerParser parser = new SwaggerParser(); - String json = getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, is); - LOG.debug("Loaded swagger api-doc:\n{}", json); - swagger = parser.parse(json); - } finally { - IOHelper.close(is); - } - - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index 0422dd9..0000000 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/component/SwaggerProducer.java +++ /dev/null @@ -1,282 +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. - */ -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.spi.RestProducerFactory; -import org.apache.camel.util.AsyncProcessorConverterHelper; -import org.apache.camel.util.CollectionStringBuffer; -import org.apache.camel.util.FileUtil; -import org.apache.camel.util.ServiceHelper; -import org.apache.camel.util.StringHelper; -import org.apache.camel.util.URISupport; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Deprecated -public class SwaggerProducer extends DefaultAsyncProducer { - - private static final Logger LOG = LoggerFactory.getLogger(SwaggerProducer.class); - - private Swagger swagger; - private Operation operation; - private AsyncProcessor producer; - - public SwaggerProducer(Endpoint endpoint) { - super(endpoint); - } - - @Override - public SwaggerEndpoint getEndpoint() { - return (SwaggerEndpoint) super.getEndpoint(); - } - - @Override - public boolean process(Exchange exchange, AsyncCallback callback) { - // TODO: bind to consumes context-type - // TODO: if binding is turned on/off/auto etc - - try { - if (producer != null) { - prepareExchange(exchange); - return producer.process(exchange, callback); - } - } catch (Throwable e) { - exchange.setException(e); - } - - // some error or there was no producer, so we are done - callback.done(true); - return true; - } - - protected void prepareExchange(Exchange exchange) throws Exception { - boolean hasPath = false; - boolean hasQuery = false; - - // uri template with path parameters resolved - String resolvedUriTemplate = getEndpoint().getPath(); - // for query parameters - Map<String, Object> query = new LinkedHashMap<>(); - 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) { - hasQuery = true; - // we need to remove the header as they are sent as query instead - // TODO: we could use a header filter strategy to skip these headers - exchange.getIn().removeHeader(param.getName()); - query.put(name, value); - } else if (param.getRequired()) { - throw new NoSuchHeaderException(exchange, name, String.class); - } - } - } else if ("path".equals(param.getIn())) { - String value = exchange.getIn().getHeader(param.getName(), String.class); - if (value != null) { - hasPath = true; - // we need to remove the header as they are sent as path instead - // TODO: we could use a header filter strategy to skip these headers - exchange.getIn().removeHeader(param.getName()); - String token = "{" + param.getName() + "}"; - resolvedUriTemplate = StringHelper.replaceAll(resolvedUriTemplate, token, value); - } else if (param.getRequired()) { - // the parameter is required but we do not have a header - throw new NoSuchHeaderException(exchange, param.getName(), String.class); - } - } - } - - if (hasQuery) { - String queryParameters = URISupport.createQueryString(query); - exchange.getIn().setHeader(Exchange.HTTP_QUERY, queryParameters); - } - - if (hasPath) { - 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(); - basePath = FileUtil.stripLeadingSeparator(basePath); - resolvedUriTemplate = FileUtil.stripLeadingSeparator(resolvedUriTemplate); - // if so us a header for the dynamic uri template so we reuse same endpoint but the header overrides the actual url to use - String overrideUri = String.format("%s://%s/%s/%s", scheme, host, basePath, resolvedUriTemplate); - exchange.getIn().setHeader(Exchange.HTTP_URI, overrideUri); - } - } - - public Swagger getSwagger() { - return swagger; - } - - public void setSwagger(Swagger swagger) { - this.swagger = swagger; - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - - String verb = getEndpoint().getVerb(); - String path = getEndpoint().getPath(); - - operation = getSwaggerOperation(verb, path); - if (operation == null) { - throw new IllegalArgumentException("Swagger schema does not contain operation for " + verb + ":" + path); - } - - Producer processor = createHttpProducer(operation, verb, path); - if (processor != null) { - producer = AsyncProcessorConverterHelper.convert(processor); - } - ServiceHelper.startService(producer); - } - - @Override - protected void doStop() throws Exception { - super.doStop(); - - ServiceHelper.stopService(producer); - } - - private Operation getSwaggerOperation(String verb, String path) { - Path modelPath = swagger.getPath(path); - if (modelPath == null) { - return null; - } - - // get,put,post,head,delete,patch,options - Operation op = null; - if ("get".equals(verb)) { - op = modelPath.getGet(); - } else if ("put".equals(verb)) { - op = modelPath.getPut(); - } else if ("post".equals(verb)) { - op = modelPath.getPost(); - } else if ("head".equals(verb)) { - op = modelPath.getHead(); - } else if ("delete".equals(verb)) { - op = modelPath.getDelete(); - } else if ("patch".equals(verb)) { - op = modelPath.getPatch(); - } else if ("options".equals(verb)) { - op = modelPath.getOptions(); - } - return op; - } - - private Producer createHttpProducer(Operation operation, String verb, String path) throws Exception { - - LOG.debug("Using Swagger operation: {} with {} {}", operation, verb, path); - - 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) { - LOG.debug("Using RestProducerFactory: {}", factory); - - 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); - } - } - - String host = getEndpoint().getHost() != null ? getEndpoint().getHost() : swagger.getHost(); - String basePath = swagger.getBasePath(); - String uriTemplate = path; - - return factory.createProducer(getEndpoint().getCamelContext(), host, verb, basePath, uriTemplate, - (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/83a097e7/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/component/swagger ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/component/swagger b/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/component/swagger deleted file mode 100644 index 909e061..0000000 --- a/components/camel-swagger-java/src/main/resources/META-INF/services/org/apache/camel/component/swagger +++ /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.swagger.component.SwaggerComponent http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index 6688e6f..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/DummyRestProducerFactory.java +++ /dev/null @@ -1,56 +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. - */ -package org.apache.camel.swagger.component; - -import java.util.Map; - -import org.apache.camel.CamelContext; -import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; -import org.apache.camel.Producer; -import org.apache.camel.impl.DefaultProducer; -import org.apache.camel.spi.RestProducerFactory; -import org.apache.camel.util.ObjectHelper; - -public class DummyRestProducerFactory implements RestProducerFactory { - - @Override - public Producer createProducer(CamelContext camelContext, String host, - String verb, String basePath, final String uriTemplate, - String consumes, String produces, Map<String, Object> parameters) throws Exception { - - // use a dummy endpoint - Endpoint endpoint = camelContext.getEndpoint("stub:dummy"); - - return new DefaultProducer(endpoint) { - @Override - public void process(Exchange exchange) throws Exception { - String query = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); - if (query != null) { - String name = ObjectHelper.after(query, "name="); - exchange.getIn().setBody("Bye " + name); - } - String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); - if (uri != null) { - int pos = uri.lastIndexOf('/'); - String name = uri.substring(pos + 1); - exchange.getIn().setBody("Hello " + name); - } - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetTest.java deleted file mode 100644 index 05fca79..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetTest.java +++ /dev/null @@ -1,57 +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. - */ -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.Ignore; -import org.junit.Test; - -public class RestSwaggerGetTest 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"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - restConfiguration().producerComponent("dummy"); - - from("direct:start") - .to("rest:get:hello/hi/{name}?apiDoc=hello-api.json") - .to("mock:result"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetUriParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetUriParamTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetUriParamTest.java deleted file mode 100644 index a5783bb..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/RestSwaggerGetUriParamTest.java +++ /dev/null @@ -1,56 +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. - */ -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 RestSwaggerGetUriParamTest 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("Bye Donald+Duck"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - restConfiguration().producerComponent("dummy"); - - from("direct:start") - .to("rest:get:bye?name={name}&apiDoc=hello-api.json") - .to("mock:result"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index 6608be5..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerComponentGetTest.java +++ /dev/null @@ -1,63 +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. - */ -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.Ignore; -import org.junit.Test; - -@Deprecated -@Ignore -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"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - SwaggerComponent sc = new SwaggerComponent(); - sc.setComponentName("dummy"); - sc.setApiDoc("hello-api.json"); - - context.addComponent("swagger", sc); - - from("direct:start") - .to("swagger:get:hello/hi/{name}") - .to("mock:result"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/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 deleted file mode 100644 index 74c404a..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetTest.java +++ /dev/null @@ -1,62 +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. - */ -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.Ignore; -import org.junit.Test; - -@Deprecated -@Ignore -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"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - 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:get:hello/hi/{name}?apiDoc=hello-api.json") - .to("mock:result"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetUriParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetUriParamTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetUriParamTest.java deleted file mode 100644 index 780790c..0000000 --- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/component/SwaggerGetUriParamTest.java +++ /dev/null @@ -1,62 +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. - */ -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.Ignore; -import org.junit.Test; - -@Deprecated -@Ignore -public class SwaggerGetUriParamTest 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("Bye Donald+Duck"); - - template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RoutesBuilder createRouteBuilder() throws Exception { - 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:get:bye?name={name}&apiDoc=hello-api.json") - .to("mock:result"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/DummyRestProducerFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/DummyRestProducerFactory.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/DummyRestProducerFactory.java new file mode 100644 index 0000000..cd3903f --- /dev/null +++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/DummyRestProducerFactory.java @@ -0,0 +1,56 @@ +/** + * 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.producer; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.spi.RestProducerFactory; +import org.apache.camel.util.ObjectHelper; + +public class DummyRestProducerFactory implements RestProducerFactory { + + @Override + public Producer createProducer(CamelContext camelContext, String host, + String verb, String basePath, final String uriTemplate, + String consumes, String produces, Map<String, Object> parameters) throws Exception { + + // use a dummy endpoint + Endpoint endpoint = camelContext.getEndpoint("stub:dummy"); + + return new DefaultProducer(endpoint) { + @Override + public void process(Exchange exchange) throws Exception { + String query = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); + if (query != null) { + String name = ObjectHelper.after(query, "name="); + exchange.getIn().setBody("Bye " + name); + } + String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); + if (uri != null) { + int pos = uri.lastIndexOf('/'); + String name = uri.substring(pos + 1); + exchange.getIn().setBody("Hello " + name); + } + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetTest.java new file mode 100644 index 0000000..a91c8d8 --- /dev/null +++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetTest.java @@ -0,0 +1,56 @@ +/** + * 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.producer; + +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 RestSwaggerGetTest 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"); + + template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().producerComponent("dummy"); + + from("direct:start") + .to("rest:get:hello/hi/{name}?apiDoc=hello-api.json") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetUriParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetUriParamTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetUriParamTest.java new file mode 100644 index 0000000..7bd7fe7 --- /dev/null +++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/producer/RestSwaggerGetUriParamTest.java @@ -0,0 +1,56 @@ +/** + * 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.producer; + +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 RestSwaggerGetUriParamTest 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("Bye Donald+Duck"); + + template.sendBodyAndHeader("direct:start", null, "name", "Donald Duck"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().producerComponent("dummy"); + + from("direct:start") + .to("rest:get:bye?name={name}&apiDoc=hello-api.json") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/components/readme.adoc ---------------------------------------------------------------------- diff --git a/components/readme.adoc b/components/readme.adoc index 3534035..6b5ec75 100644 --- a/components/readme.adoc +++ b/components/readme.adoc @@ -501,9 +501,6 @@ Components | link:camel-stringtemplate/src/main/docs/string-template-component.adoc[String Template] (camel-stringtemplate) + `string-template:resourceUri` | Transforms the message using a String template. -| link:camel-swagger-java/src/main/docs/swagger-component.adoc[Swagger] (camel-swagger-java) + -`swagger:verb:path` | Camel Swagger Java support - | link:camel-telegram/src/main/docs/telegram-component.adoc[Telegram] (camel-telegram) + `telegram:type/authorizationToken` | The Camel endpoint for a telegram bot. http://git-wip-us.apache.org/repos/asf/camel/blob/83a097e7/docs/user-manual/en/SUMMARY.md ---------------------------------------------------------------------- diff --git a/docs/user-manual/en/SUMMARY.md b/docs/user-manual/en/SUMMARY.md index 5dca599..b3eda45 100644 --- a/docs/user-manual/en/SUMMARY.md +++ b/docs/user-manual/en/SUMMARY.md @@ -289,7 +289,6 @@ * [Stomp](stomp-component.adoc) * [Stream](stream-component.adoc) * [String Template](string-template-component.adoc) - * [Swagger](swagger-component.adoc) * [Telegram](telegram-component.adoc) * [Twitter](twitter-component.adoc) * [Undertow](undertow-component.adoc)
