You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by on...@apache.org on 2017/10/24 07:51:38 UTC

[camel] branch camel-2.19.x updated: CAMEL-11891 - incase of emptybody unmarshalling needed

This is an automated email from the ASF dual-hosted git repository.

onders pushed a commit to branch camel-2.19.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.19.x by this push:
     new 4c3fce9  CAMEL-11891 - incase of emptybody unmarshalling needed
4c3fce9 is described below

commit 4c3fce9f6ac00c01e2366677a731b0f64125a1e6
Author: onders86 <on...@gmail.com>
AuthorDate: Wed Oct 18 17:10:35 2017 +0300

    CAMEL-11891 - incase of emptybody unmarshalling needed
---
 .../camel/dataformat/xmljson/XmlJsonDataFormat.java      | 16 ++++++++++++++++
 .../dataformat/xmljson/SpringXmlJsonDataFormatTest.java  | 15 +++++++++++++++
 .../camel/dataformat/xmljson/XmlJsonDataFormatTest.java  | 13 +++++++++++++
 .../dataformat/xmljson/SpringXmlJsonDataFormatTest.xml   |  6 ++++++
 4 files changed, 50 insertions(+)

diff --git a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
index 6acf6cf..8d38db1 100644
--- a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
+++ b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
@@ -29,10 +29,12 @@ import net.sf.json.JSON;
 import net.sf.json.JSONSerializer;
 import net.sf.json.xml.XMLSerializer;
 import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.DataFormatName;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat}) using 
@@ -190,12 +192,26 @@ public class XmlJsonDataFormat extends ServiceSupport implements DataFormat, Dat
     public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
         Object inBody = exchange.getIn().getBody();
         JSON toConvert;
+        
+        if (inBody == null) {
+            return null;
+        }
+        
+        if (inBody instanceof StreamCache) {
+            long length = ((StreamCache) inBody).length();
+            if (length <= 0) {
+                return inBody;
+            }
+        }
         // if the incoming object is already a JSON object, process as-is,
         // otherwise parse it as a String
         if (inBody instanceof JSON) {
             toConvert = (JSON) inBody;
         } else {
             String jsonString = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, inBody);
+            if (ObjectHelper.isEmpty(jsonString)) {
+                return null;
+            }
             toConvert = JSONSerializer.toJSON(jsonString);
         }
 
diff --git a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.java b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.java
index 3c3a319..11d64b1 100644
--- a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.java
+++ b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.java
@@ -16,13 +16,17 @@
  */
 package org.apache.camel.dataformat.xmljson;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 
+import javax.xml.transform.stream.StreamSource;
+
 import org.w3c.dom.Document;
 
 import net.sf.json.JSONObject;
 import net.sf.json.JSONSerializer;
 
+import org.apache.camel.StreamCache;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
 
@@ -66,6 +70,17 @@ public class SpringXmlJsonDataFormatTest extends CamelSpringTestSupport {
         mockJSON.assertIsSatisfied();
         mockXML.assertIsSatisfied();
     }
+    
+    @Test
+    public void testEmptyBodyToJson() throws Exception {
+        MockEndpoint mockJSON = getMockEndpoint("mock:emptyBody2Xml");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isInstanceOf(StreamCache.class);
+
+        StreamSource in = context.getTypeConverter().convertTo(StreamSource.class, new ByteArrayInputStream("".getBytes()));
+        template.requestBody("direct:emptyBody2Unmarshal", in);
+        mockJSON.assertIsSatisfied();
+    }
 
     @Test
     public void testSomeOptionsToXML() throws Exception {
diff --git a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormatTest.java b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormatTest.java
index f7c0258..8632a8b 100644
--- a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormatTest.java
+++ b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormatTest.java
@@ -207,6 +207,16 @@ public class XmlJsonDataFormatTest extends AbstractJsonTestSupport {
         assertTrue("Expected a JSON array with string elements: 1, 2, 3, 4", array.containsAll(Arrays.asList("1", "2", "3", "4")));
         mockJSON.assertIsSatisfied();
     }
+    
+    @Test
+    public void testEmptyBodyToJson() throws Exception {
+        MockEndpoint mockJSON = getMockEndpoint("mock:null2xml");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isNull();
+
+        template.requestBody("direct:unmarshalNull2Xml", "");
+        mockJSON.assertIsSatisfied();
+    }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -219,6 +229,9 @@ public class XmlJsonDataFormatTest extends AbstractJsonTestSupport {
                 from("direct:marshal").marshal(format).to("mock:json");
                 // from JSON to XML
                 from("direct:unmarshal").unmarshal(format).to("mock:xml");
+                
+                // test null body to xml
+                from("direct:unmarshalNull2Xml").unmarshal(format).to("mock:null2xml");
 
                 // from XML to JSON - inline dataformat
                 from("direct:marshalInline").marshal().xmljson().to("mock:jsonInline");
diff --git a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.xml b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.xml
index fc38ae3..696aae5 100644
--- a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.xml
+++ b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/SpringXmlJsonDataFormatTest.xml
@@ -61,6 +61,12 @@
             <unmarshal ref="xmljsonWithOptions"/>
             <to uri="mock:xmlWithOptions"/>
         </route>
+        
+        <route streamCache="true">
+            <from uri="direct:emptyBody2Unmarshal"/>
+            <unmarshal ref="xmljson"/>
+            <to uri="mock:emptyBody2Xml"/>
+        </route>
 
     </camelContext>
     <!-- END SNIPPET: e1 -->

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].