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/bf49e363 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bf49e363 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bf49e363 Branch: refs/heads/master Commit: bf49e3631b0e1b1ba60286674baef2aedfbea4ec Parents: b05fd50 Author: Claus Ibsen <[email protected]> Authored: Fri Aug 26 16:26:53 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Aug 26 16:53:31 2016 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestComponent.java | 4 ++ .../model/rest/RestConfigurationDefinition.java | 61 ++++++++++++++++---- .../org/apache/camel/spi/RestConfiguration.java | 22 +++++++ .../producer/JettyRestProducerApiDocTest.java | 4 +- .../JettyRestProducerInvalidApiDocTest.java | 4 +- .../producer/RestSwaggerGetUriParamTest.java | 4 +- 6 files changed, 81 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java index f8245c6..4fb3d4e 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java @@ -101,6 +101,10 @@ public class RestComponent extends UriEndpointComponent { } answer.setComponentName(name); } + // if no explicit producer api was given, then fallback and use default configured + if (answer.getApiDoc() == null && getCamelContext().getRestConfiguration() != null) { + answer.setApiDoc(getCamelContext().getRestConfiguration().getProducerApiDoc()); + } return answer; } http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java index dec2901..fe5327e 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java @@ -42,10 +42,10 @@ public class RestConfigurationDefinition { @XmlAttribute private String component; - @XmlAttribute @Metadata(defaultValue = "swagger") + @XmlAttribute @Metadata(label = "consumer", defaultValue = "swagger") private String apiComponent; - @XmlAttribute + @XmlAttribute @Metadata(label = "producer") private String producerComponent; @XmlAttribute @@ -57,22 +57,25 @@ public class RestConfigurationDefinition { @XmlAttribute private String port; - @XmlAttribute + @XmlAttribute @Metadata(label = "producer") + private String producerApiDoc; + + @XmlAttribute @Metadata(label = "consumer") private String contextPath; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private String apiContextPath; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private String apiContextRouteId; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private String apiContextIdPattern; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private Boolean apiContextListing; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private RestHostNameResolver hostNameResolver; @XmlAttribute @Metadata(defaultValue = "off") @@ -81,7 +84,7 @@ public class RestConfigurationDefinition { @XmlAttribute private Boolean skipBindingOnErrorCode; - @XmlAttribute + @XmlAttribute @Metadata(label = "consumer") private Boolean enableCORS; @XmlAttribute @@ -96,16 +99,16 @@ public class RestConfigurationDefinition { @XmlElement(name = "endpointProperty") private List<RestPropertyDefinition> endpointProperties = new ArrayList<RestPropertyDefinition>(); - @XmlElement(name = "consumerProperty") + @XmlElement(name = "consumerProperty") @Metadata(label = "consumer") private List<RestPropertyDefinition> consumerProperties = new ArrayList<RestPropertyDefinition>(); @XmlElement(name = "dataFormatProperty") private List<RestPropertyDefinition> dataFormatProperties = new ArrayList<RestPropertyDefinition>(); - @XmlElement(name = "apiProperty") + @XmlElement(name = "apiProperty") @Metadata(label = "consumer") private List<RestPropertyDefinition> apiProperties = new ArrayList<RestPropertyDefinition>(); - @XmlElement(name = "corsHeaders") + @XmlElement(name = "corsHeaders") @Metadata(label = "consumer") private List<RestPropertyDefinition> corsHeaders = new ArrayList<RestPropertyDefinition>(); public String getComponent() { @@ -185,6 +188,23 @@ public class RestConfigurationDefinition { this.port = port; } + public String getProducerApiDoc() { + return producerApiDoc; + } + + /** + * Sets the location of the api document (swagger api) the REST producer will use + * to validate the REST uri and query parameters are valid accordingly to the api document. + * This requires adding camel-swagger-java to the classpath, and any miss configuration + * will let Camel fail on startup and report the error(s). + * <p/> + * The location of the api document is loaded from classpath by default, but you can use + * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url. + */ + public void setProducerApiDoc(String producerApiDoc) { + this.producerApiDoc = producerApiDoc; + } + public String getContextPath() { return contextPath; } @@ -472,6 +492,20 @@ public class RestConfigurationDefinition { } /** + * Sets the location of the api document (swagger api) the REST producer will use + * to validate the REST uri and query parameters are valid accordingly to the api document. + * This requires adding camel-swagger-java to the classpath, and any miss configuration + * will let Camel fail on startup and report the error(s). + * <p/> + * The location of the api document is loaded from classpath by default, but you can use + * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url. + */ + public RestConfigurationDefinition producerApiDoc(String apiDoc) { + setProducerApiDoc(apiDoc); + return this; + } + + /** * Sets a leading context-path the REST services will be using. * <p/> * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application @@ -681,6 +715,9 @@ public class RestConfigurationDefinition { if (port != null) { answer.setPort(CamelContextHelper.parseInteger(context, port)); } + if (producerApiDoc != null) { + answer.setProducerApiDoc(CamelContextHelper.parseText(context, producerApiDoc)); + } if (apiContextPath != null) { answer.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath)); } http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java index 0a71ba6..c90ba8d 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java @@ -40,6 +40,7 @@ public class RestConfiguration { private String component; private String apiComponent; private String producerComponent; + private String producerApiDoc; private String scheme; private String host; private int port; @@ -116,6 +117,27 @@ public class RestConfiguration { } /** + * Gets the location of the api document (swagger api) the REST producer will use + * to validate the REST uri and query parameters are valid accordingly to the api document. + */ + public String getProducerApiDoc() { + return producerApiDoc; + } + + /** + * Sets the location of the api document (swagger api) the REST producer will use + * to validate the REST uri and query parameters are valid accordingly to the api document. + * This requires adding camel-swagger-java to the classpath, and any miss configuration + * will let Camel fail on startup and report the error(s). + * <p/> + * The location of the api document is loaded from classpath by default, but you can use + * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url. + */ + public void setProducerApiDoc(String producerApiDoc) { + this.producerApiDoc = producerApiDoc; + } + + /** * Gets the hostname to use by the REST consumer * * @return the hostname, or <tt>null</tt> to use default hostname http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerApiDocTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerApiDocTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerApiDocTest.java index ede12e5..c3f0824 100644 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerApiDocTest.java +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerApiDocTest.java @@ -34,10 +34,10 @@ public class JettyRestProducerApiDocTest extends BaseJettyTest { @Override public void configure() throws Exception { // configure to use localhost with the given port - restConfiguration().component("jetty").host("localhost").port(getPort()); + restConfiguration().component("jetty").host("localhost").port(getPort()).producerApiDoc("hello-api.json"); from("direct:start") - .to("rest:get:api/hello/hi/{name}?apiDoc=hello-api.json"); + .to("rest:get:api:hello/hi/{name}"); // use the rest DSL to define the rest services rest("/api/") http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java index 9711dab..7cc3353 100644 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java @@ -55,10 +55,10 @@ public class JettyRestProducerInvalidApiDocTest extends CamelTestSupport { @Override public void configure() throws Exception { // configure to use localhost with the given port - restConfiguration().component("jetty").host("localhost"); + restConfiguration().component("jetty").host("localhost").producerApiDoc("hello-api.json"); from("direct:start") - .to("rest:get:api/bye/?unknown={name}&apiDoc=hello-api.json"); + .to("rest:get:api/bye/?unknown={name}"); } }); http://git-wip-us.apache.org/repos/asf/camel/blob/bf49e363/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 index 7bd7fe7..53fe043 100644 --- 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 @@ -45,10 +45,10 @@ public class RestSwaggerGetUriParamTest extends CamelTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - restConfiguration().producerComponent("dummy"); + restConfiguration().producerComponent("dummy").producerApiDoc("hello-api.json"); from("direct:start") - .to("rest:get:bye?name={name}&apiDoc=hello-api.json") + .to("rest:get:bye?name={name}") .to("mock:result"); } };
