Repository: camel Updated Branches: refs/heads/master cae408e0d -> 0d84fee97
CAMEL-7619: Rest DSL - adding support for xml/json binding using Camel's data formats. Work in progress. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0d84fee9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0d84fee9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0d84fee9 Branch: refs/heads/master Commit: 0d84fee974281eae649d55be32a03e936fca9358 Parents: cae408e Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Jul 25 14:30:21 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Jul 25 14:30:21 2014 +0200 ---------------------------------------------------------------------- .../processor/binding/RestBindingProcessor.java | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0d84fee9/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java index f911f70..9d53959 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java @@ -126,11 +126,12 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess isJson = consumes != null && consumes.toLowerCase(Locale.US).contains("json"); } - if (!isXml && !isJson) { + // if we do not know explicit if its json or xml, then need to check the message body to be sure what it is + if (!isXml && !isJson || isXml && isJson) { // read the content into memory so we can determine if its xml or json String body = MessageHelper.extractBodyAsString(exchange.getIn()); if (body != null) { - isXml = body.startsWith("<") || body.contains("xml"); + isXml = body.startsWith("<"); isJson = !isXml; } } @@ -231,11 +232,12 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess // need to prepare exchange first ExchangeHelper.prepareOutToIn(exchange); - if (!isXml && !isJson) { + // if we do not know explicit if its json or xml, then need to check the message body to be sure what it is + if (!isXml && !isJson || isXml && isJson) { // read the content into memory so we can determine if its xml or json String body = MessageHelper.extractBodyAsString(exchange.getIn()); if (body != null) { - isXml = body.startsWith("<") || body.contains("xml"); + isXml = body.startsWith("<"); isJson = !isXml; } } @@ -249,6 +251,19 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess xmlMmarshal.process(exchange); } else if (isJson && jsonMmarshal != null) { jsonMmarshal.process(exchange); + } else { + // we could not bind + if (bindingMode.equals("auto")) { + // okay for auto we do not mind if we could not bind + return; + } else { + if (bindingMode.contains("xml")) { + exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange)); + } else { + exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange)); + } + } + return; } } catch (Throwable e) { exchange.setException(e);