Repository: camel
Updated Branches:
  refs/heads/master 541a3ed8c -> 3c0e75bb0


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/3c0e75bb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3c0e75bb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3c0e75bb

Branch: refs/heads/master
Commit: 3c0e75bb031c8228968ef7d78bf2072dddffdda3
Parents: 541a3ed
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Jul 23 18:48:59 2014 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Jul 24 08:23:45 2014 +0200

----------------------------------------------------------------------
 .../processor/binding/BindingException.java     | 30 ++++++++++++++++++++
 .../processor/binding/RestBindingProcessor.java | 29 ++++++++-----------
 .../restlet/RestRestletBindingModeJsonTest.java | 17 +++++++++++
 .../restlet/RestRestletBindingModeXmlTest.java  | 17 +++++++++++
 4 files changed, 76 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3c0e75bb/camel-core/src/main/java/org/apache/camel/processor/binding/BindingException.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/processor/binding/BindingException.java
 
b/camel-core/src/main/java/org/apache/camel/processor/binding/BindingException.java
new file mode 100644
index 0000000..7887c50
--- /dev/null
+++ 
b/camel-core/src/main/java/org/apache/camel/processor/binding/BindingException.java
@@ -0,0 +1,30 @@
+/**
+ * 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.processor.binding;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+
+/**
+ * An exception if a binding is not possible, such as binding to json or xml 
cannot be done.
+ */
+public class BindingException extends CamelExchangeException {
+
+    public BindingException(String message, Exchange exchange) {
+        super(message, exchange);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0e75bb/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 4ca249c..630210b 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
@@ -62,6 +62,9 @@ public class RestBindingProcessor extends ServiceSupport 
implements AsyncProcess
         AsyncProcessorHelper.process(this, exchange);
     }
 
+    // TODO: consumes/produces can be a list of media types, and prioritized 
1st to last.
+    // TODO: parsing body should only be done if really needed
+
     @Override
     public boolean process(Exchange exchange, final AsyncCallback callback) {
         if (bindingMode == null || "off".equals(bindingMode)) {
@@ -92,11 +95,17 @@ public class RestBindingProcessor extends ServiceSupport 
implements AsyncProcess
             isJson = consumes != null && 
consumes.toLowerCase(Locale.US).contains("json");
         }
 
+        if (!isXml && !isJson) {
+            // read the content into memory so we can determine if its xml or 
json
+            String body = MessageHelper.extractBodyAsString(exchange.getIn());
+            isXml = body.startsWith("<") || body.contains("xml");
+            isJson = !isXml;
+        }
+
         // only allow xml/json if the binding mode allows that
         isXml &= bindingMode.equals("auto") || bindingMode.contains("xml");
         isJson &= bindingMode.equals("auto") || bindingMode.contains("json");
 
-        // check the header first if its xml or json
         if (isXml && xmlUnmarshal != null) {
             // add reverse operation
             exchange.addOnCompletion(new 
RestBindingMarshalOnCompletion(jsonMmarshal, xmlMmarshal));
@@ -107,20 +116,6 @@ public class RestBindingProcessor extends ServiceSupport 
implements AsyncProcess
             return jsonUnmarshal.process(exchange, callback);
         }
 
-        // read the content into memory so we can determine if its xml or json
-        String body = MessageHelper.extractBodyAsString(exchange.getIn());
-        isXml = body.startsWith("<") || body.contains("xml");
-
-        if (isXml && xmlUnmarshal != null) {
-            // add reverse operation
-            exchange.addOnCompletion(new 
RestBindingMarshalOnCompletion(jsonMmarshal, xmlMmarshal));
-            return xmlUnmarshal.process(exchange, callback);
-        } else if (jsonUnmarshal != null) {
-            // add reverse operation
-            exchange.addOnCompletion(new 
RestBindingMarshalOnCompletion(jsonMmarshal, xmlMmarshal));
-            return jsonUnmarshal.process(exchange, callback);
-        }
-
         // we could not bind
         if (bindingMode.equals("auto")) {
             // okay for auto we do not mind if we could not bind
@@ -128,9 +123,9 @@ public class RestBindingProcessor extends ServiceSupport 
implements AsyncProcess
             return true;
         } else {
             if (bindingMode.contains("xml")) {
-                exchange.setException(new IllegalArgumentException("Cannot 
bind to xml as message body is not xml compatible"));
+                exchange.setException(new BindingException("Cannot bind to xml 
as message body is not xml compatible", exchange));
             } else {
-                exchange.setException(new IllegalArgumentException("Cannot 
bind to json as message body is not json compatible"));
+                exchange.setException(new BindingException("Cannot bind to 
json as message body is not json compatible", exchange));
             }
             callback.done(true);
             return true;

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0e75bb/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java
 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java
index 8b29f13..f03c1b7 100644
--- 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java
+++ 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java
@@ -43,6 +43,23 @@ public class RestRestletBindingModeJsonTest extends 
RestletTestSupport {
         assertEquals("Donald Duck", user.getName());
     }
 
+    @Test
+    public void testBindingModeWrong() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:input");
+        mock.expectedMessageCount(0);
+
+        // we bind to json, but send in xml, which is not possible
+        String body = "<user name=\"Donald Duck\" id=\"123\"></user>";
+        try {
+            template.sendBody("http://localhost:"; + portNum + "/users/new", 
body);
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            // expected
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0e75bb/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java
 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java
index d9c28d8..a698ab8 100644
--- 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java
+++ 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java
@@ -43,6 +43,23 @@ public class RestRestletBindingModeXmlTest extends 
RestletTestSupport {
         assertEquals("Donald Duck", user.getName());
     }
 
+    @Test
+    public void testBindingModeWrong() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:input");
+        mock.expectedMessageCount(0);
+
+        // we bind to xml, but send in json, which is not possible
+        String body = "{\"id\": 123, \"name\": \"Donald Duck\"}";
+        try {
+            template.sendBody("http://localhost:"; + portNum + "/users/new", 
body);
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            // expected
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

Reply via email to