camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-11891) XML2JSON Data Format and empty requests
Date Tue, 24 Oct 2017 07:35:00 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-11891?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16216458#comment-16216458
] 

ASF GitHub Bot commented on CAMEL-11891:
----------------------------------------

onders86 closed pull request #2048: CAMEL-11891 - incase of empty body unmarshalling needed
URL: https://github.com/apache/camel/pull/2048
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 6acf6cffa21..8d38db158fd 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.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 void marshal(Exchange exchange, Object graph, OutputStream stream)
throws
     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 3c3a319feb1..11d64b1630d 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 void testMarshalAndUnmarshal() throws Exception {
         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 f7c0258aa67..8632a8b2bf3 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 void testXmlArraysToJson() throws Exception {
         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 void configure() throws Exception {
                 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 30cf5116af9..696aae5c2a4 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
@@ -1,6 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-
     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.
@@ -8,14 +7,13 @@
     (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
+    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.
-
 -->
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -63,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 -->


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> XML2JSON Data Format and empty requests
> ---------------------------------------
>
>                 Key: CAMEL-11891
>                 URL: https://issues.apache.org/jira/browse/CAMEL-11891
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-xmljson
>    Affects Versions: 2.19.3
>            Reporter: Richard Groote
>            Assignee: ├ľnder Sezgin
>             Fix For: 2.19.4, 2.20.1
>
>
> It would be nice if the XmlJsonDataFormat can handle empty body.
> In our project we upgraded from 2.14.0 to 2.19.3. 
> The below code no longer works in 2.19.3 when a GET request is made to a servlet component.
In our situation the body contains a stream cache and the xml2json will fail.
> A possible solution is to check the http method before doing the unmarshalling.
>       <camel:choice>
>         <camel:when>
>           <camel:simple>${body} != null</camel:simple>
>           <camel:unmarshal ref="xml2json"/>
>         </camel:when>
>       </camel:choice>
> The current solution in our project is a extension of the XmlJsonDataFormat.
> In the extension the method unmarshall is overriden
> {code:java}
> 	@Override
> 	public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
>         Object inBody = exchange.getIn().getBody();
>         
>         if(inBody == null) {
>         		return null;
>         }
>         
>         if(inBody instanceof StreamCache) {
>         		long length = ((StreamCache) inBody).length();
>         		if(length <= 0) {
>         			return null;
>         		}
>         } else {
> 	        if(!(inBody instanceof JSON)) {
> 	        	 	String jsonString = exchange.getContext().getTypeConverter().convertTo(String.class,
exchange, inBody);
> 	        	 	if(StringUtils.isEmpty(jsonString)) {
> 	        	 		return null;
> 	        	 	}
> 	        }
>         }
>         
>         
>         
>         return super.unmarshal(exchange, stream);
> 	}
> {code}
> 	



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message