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:34:41 UTC
[camel] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 5f6940f CAMEL-11891 - incase of emptybody unmarshalling needed
5f6940f is described below
commit 5f6940ffe45a23749cca9a1f55136bbf6a1423e3
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 | 10 +++++++---
4 files changed, 51 insertions(+), 3 deletions(-)
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 30cf511..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
@@ -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 -->
--
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].