camel git commit: CAMEL-10164: Add support for binding in rest to
Repository: camel Updated Branches: refs/heads/master 7f7ce39d2 -> 8129b1a78 CAMEL-10164: Add support for binding in rest to Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8129b1a7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8129b1a7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8129b1a7 Branch: refs/heads/master Commit: 8129b1a784b307d2a0b576c8f46c046c7739d762 Parents: 7f7ce39 Author: Claus IbsenAuthored: Tue Sep 6 10:07:53 2016 +0200 Committer: Claus Ibsen Committed: Tue Sep 6 10:07:53 2016 +0200 -- .../rest/RestProducerBindingProcessor.java | 36 1 file changed, 22 insertions(+), 14 deletions(-) -- http://git-wip-us.apache.org/repos/asf/camel/blob/8129b1a7/camel-core/src/main/java/org/apache/camel/component/rest/RestProducerBindingProcessor.java -- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducerBindingProcessor.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducerBindingProcessor.java index b1d879e..db838a1 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducerBindingProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducerBindingProcessor.java @@ -62,28 +62,24 @@ public class RestProducerBindingProcessor extends DelegateAsyncProcessor { this.camelContext = camelContext; -if (jsonDataFormat != null) { +if (outJsonDataFormat != null) { this.jsonUnmarshal = new UnmarshalProcessor(outJsonDataFormat); } else { this.jsonUnmarshal = null; } -if (outJsonDataFormat != null) { +if (jsonDataFormat != null) { this.jsonMarshal = new MarshalProcessor(jsonDataFormat); -} else if (jsonDataFormat != null) { -this.jsonMarshal = new MarshalProcessor(outJsonDataFormat); } else { this.jsonMarshal = null; } -if (xmlDataFormat != null) { +if (outXmlDataFormat != null) { this.xmlUnmarshal = new UnmarshalProcessor(outXmlDataFormat); } else { this.xmlUnmarshal = null; } -if (outXmlDataFormat != null) { +if (xmlDataFormat != null) { this.xmlMarshal = new MarshalProcessor(xmlDataFormat); -} else if (xmlDataFormat != null) { -this.xmlMarshal = new MarshalProcessor(outXmlDataFormat); } else { this.xmlMarshal = null; } @@ -139,18 +135,30 @@ public class RestProducerBindingProcessor extends DelegateAsyncProcessor { } // we only need to perform before binding if the message body is POJO based -// if its convertable to stream based then its not POJO based -InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, exchange, body); -if (is != null) { -exchange.getIn().setBody(is); +if (body instanceof String || body instanceof byte[]) { +// the body is text based and thus not POJO so no binding needed if (outType != null) { // wrap callback to add reverse operation if we know the output type from the REST service callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false); } // okay now we can continue routing to the producer return getProcessor().process(exchange, callback); +} else { +// if its convertable to stream based then its not POJO based +InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, exchange, body); +if (is != null) { +exchange.getIn().setBody(is); +if (outType != null) { +// wrap callback to add reverse operation if we know the output type from the REST service +callback = new RestProducerBindingUnmarshalCallback(exchange, callback, jsonMarshal, xmlMarshal, false); +} +// okay now we can continue routing to the producer +return getProcessor().process(exchange, callback); +} } +// assume body is POJO based and binding needed + String contentType = ExchangeHelper.getContentType(exchange); if (contentType != null) { isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml"); @@ -306,8 +314,8 @@ public class RestProducerBindingProcessor extends DelegateAsyncProcessor { return; } -// is there any
[3/4] camel git commit: CAMEL-10164: Add support for binding in rest to
CAMEL-10164: Add support for binding in rest to Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/29cd3cdb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/29cd3cdb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/29cd3cdb Branch: refs/heads/master Commit: 29cd3cdbd23a8b7f94ac8818e8df5ce50f066d88 Parents: 737b49c Author: Claus IbsenAuthored: Mon Sep 5 16:18:26 2016 +0200 Committer: Claus Ibsen Committed: Mon Sep 5 16:26:13 2016 +0200 -- camel-core/src/main/docs/rest-component.adoc| 48 +- .../rest/RestConsumerBindingProcessor.java | 461 +++ .../camel/component/rest/RestEndpoint.java | 8 +- .../camel/component/rest/RestProducer.java | 76 ++- .../rest/RestProducerBindingProcessor.java | 124 ++--- .../camel/model/rest/RestBindingDefinition.java | 2 +- .../binding/RestConsumerBindingProcessor.java | 460 -- .../JettyRestProducerPojoInOutTest.java | 8 +- .../src/test/resources/log4j2.properties| 2 +- 9 files changed, 658 insertions(+), 531 deletions(-) -- http://git-wip-us.apache.org/repos/asf/camel/blob/29cd3cdb/camel-core/src/main/docs/rest-component.adoc -- diff --git a/camel-core/src/main/docs/rest-component.adoc b/camel-core/src/main/docs/rest-component.adoc index 2413940..910f271 100644 --- a/camel-core/src/main/docs/rest-component.adoc +++ b/camel-core/src/main/docs/rest-component.adoc @@ -54,9 +54,9 @@ The REST component supports 17 endpoint options which are listed below: | inType | common | | String | To declare the incoming POJO binding type as a FQN class name | outType | common | | String | To declare the outgoing POJO binding type as a FQN class name | produces | common | | String | Media type such as: 'text/xml' or 'application/json' this REST service returns. +| routeId | common | | String | Name of the route this REST services creates | bridgeErrorHandler | consumer | false | boolean | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN/ERROR level and ignored. | description | consumer | | String | Human description to document this REST service -| routeId | consumer | | String | Name of the route this REST services creates | exceptionHandler | consumer (advanced) | | ExceptionHandler | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN/ERROR level and ignored. | apiDoc | producer | | String | The swagger api doc resource to use. The resource is loaded from classpath by default and must be in JSon format. | host | producer | | String | Host and port of HTTP service to use (override host in swagger schema) @@ -162,6 +162,52 @@ to use http4 you can do: +[[Rest-Producer-Binding]] +Rest producer binding +^ + +The REST producer supports binding using JSon or XML like the rest-dsl does. + +For example to use jetty with json binding mode turned on you can configure this in the rest configuration: + +[source,java] + + restConfiguration().component("jetty").host("localhost").port(8080).bindingMode(RestBindingMode.json); + + from("direct:start") +.to("rest:post:user"); + + +Then when calling the REST service using rest producer it will automatic bind any POJOs to json before calling the REST service: + +[source,java] + + UserPojo user = new UserPojo(); + user.setId(123); + user.setName("Donald Duck"); + + template.sendBody("direct:start", user); + + +In the example above we send a POJO instance `UserPojo` as the message body. And because we have turned on JSon binding +in the rest configuration, then the POJO will be marshalled from POJO to JSon before calling the REST service. + +However if you want to also perform binding for the response message (eg what the REST service send back as response) you +would need to configure the `outType` option to specify what is the classname of the POJO to unmarshal from JSon to POJO. + +For example if the REST service returns a JSon payload that binds to
[2/4] camel git commit: CAMEL-10164: Add support for binding in rest to
CAMEL-10164: Add support for binding in rest to Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/737b49ca Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/737b49ca Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/737b49ca Branch: refs/heads/master Commit: 737b49ca80147fb7956e1188ae74b26ea8782eda Parents: e097a5d Author: Claus IbsenAuthored: Mon Sep 5 15:14:34 2016 +0200 Committer: Claus Ibsen Committed: Mon Sep 5 16:26:13 2016 +0200 -- .../camel/component/rest/RestComponent.java | 1 - .../camel/component/rest/RestEndpoint.java | 4 +- .../camel/component/rest/RestProducer.java | 155 ++- .../rest/RestProducerBindingProcessor.java | 356 ++ .../camel/model/rest/RestBindingDefinition.java | 6 +- .../processor/binding/RestBindingProcessor.java | 460 --- .../binding/RestConsumerBindingProcessor.java | 460 +++ .../JettyRestProducerPojoInOutTest.java | 107 + .../src/test/resources/log4j2.properties| 2 +- 9 files changed, 1080 insertions(+), 471 deletions(-) -- http://git-wip-us.apache.org/repos/asf/camel/blob/737b49ca/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 4fb3d4e..5384b5f 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 @@ -22,7 +22,6 @@ import org.apache.camel.Endpoint; import org.apache.camel.impl.UriEndpointComponent; import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.URISupport; /** * Rest component. http://git-wip-us.apache.org/repos/asf/camel/blob/737b49ca/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java -- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java index 671e27f..04b4ced 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java @@ -29,7 +29,6 @@ import org.apache.camel.Producer; import org.apache.camel.impl.DefaultEndpoint; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.Metadata; -import org.apache.camel.spi.RestApiProcessorFactory; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.spi.RestProducerFactory; @@ -326,7 +325,8 @@ public class RestEndpoint extends DefaultEndpoint { } else { producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters); } -return new RestProducer(this, producer); +RestConfiguration config = getCamelContext().getRestConfiguration(cname, true); +return new RestProducer(this, producer, config); } 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/737b49ca/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java -- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java index 9508ba5..97976d6 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java @@ -17,16 +17,21 @@ package org.apache.camel.component.rest; import java.net.URLDecoder; +import java.util.HashMap; import java.util.Map; import org.apache.camel.AsyncCallback; import org.apache.camel.AsyncProcessor; +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.DefaultAsyncProducer; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.RestConfiguration; import org.apache.camel.tools.apt.helper.CollectionStringBuffer; import org.apache.camel.util.AsyncProcessorConverterHelper; +import org.apache.camel.util.EndpointHelper;