CAMEL-9807: Fixed CXF converter to not return null on empty payload in some use cases that could cause Camel to consider this as a miss type converter combo. Thanks to Joerg Kessler for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7e4f0b90 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7e4f0b90 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7e4f0b90 Branch: refs/heads/camel-2.16.x Commit: 7e4f0b90ee64629dbd90a1168c51ed8495dff989 Parents: ac741fd Author: Claus Ibsen <[email protected]> Authored: Mon Apr 4 14:33:29 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Mon Apr 4 14:34:23 2016 +0200 ---------------------------------------------------------------------- .../cxf/converter/CxfPayloadConverter.java | 38 ++++++++------- .../cxf/converter/CxfPayloadConverterTest.java | 50 ++++++++++++++++---- 2 files changed, 61 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7e4f0b90/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java index 1267103..74e0d10 100644 --- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java +++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java @@ -21,7 +21,6 @@ import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; import java.util.List; - import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Source; @@ -79,7 +78,7 @@ public final class CxfPayloadConverter { } return new CxfPayload<T>(headers, body); } - + @Converter public static <T> CxfPayload<T> sourceToCxfPayload(Source src, Exchange exchange) { List<T> headers = new ArrayList<T>(); @@ -92,21 +91,21 @@ public final class CxfPayloadConverter { public static <T> NodeList cxfPayloadToNodeList(CxfPayload<T> payload, Exchange exchange) { return new NodeListWrapper(payload.getBody()); } - + @Converter public static <T> Node cxfPayLoadToNode(CxfPayload<T> payload, Exchange exchange) { List<Element> payloadBodyElements = payload.getBody(); - + if (payloadBodyElements.size() > 0) { return payloadBodyElements.get(0); } return null; } - + @Converter public static <T> Source cxfPayLoadToSource(CxfPayload<T> payload, Exchange exchange) { List<Source> payloadBody = payload.getBodySources(); - + if (payloadBody.size() > 0) { return payloadBody.get(0); } @@ -128,15 +127,15 @@ public final class CxfPayloadConverter { Source src = null; // many of the common format that can have a Source created directly if (value instanceof InputStream) { - src = new StreamSource((InputStream)value); + src = new StreamSource((InputStream) value); } else if (value instanceof Reader) { - src = new StreamSource((Reader)value); + src = new StreamSource((Reader) value); } else if (value instanceof String) { - src = new StreamSource(new StringReader((String)value)); + src = new StreamSource(new StringReader((String) value)); } else if (value instanceof Node) { - src = new DOMSource((Node)value); + src = new DOMSource((Node) value); } else if (value instanceof Source) { - src = (Source)value; + src = (Source) value; } if (src == null) { // assuming staxsource is preferred, otherwise use the one preferred @@ -179,8 +178,8 @@ public final class CxfPayloadConverter { // Convert a CxfPayload into something else if (CxfPayload.class.isAssignableFrom(value.getClass())) { CxfPayload<?> payload = (CxfPayload<?>) value; - - if (payload.getBodySources().size() == 1) { + int size = payload.getBodySources().size(); + if (size == 1) { if (type.isAssignableFrom(Document.class)) { Source s = payload.getBodySources().get(0); Document d; @@ -205,16 +204,16 @@ public final class CxfPayloadConverter { } else if (s instanceof StAXSource) { r = ((StAXSource) s).getXMLStreamReader(); } - if (r != null) { + if (r != null) { s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap())); } } T t = tc.convertTo(type, s); return t; - } + } } TypeConverter tc = registry.lookup(type, NodeList.class); - if (tc != null) { + if (tc != null) { Object result = tc.convertTo(type, cxfPayloadToNodeList((CxfPayload<?>) value, exchange)); if (result == null) { // no we could not do it currently, and we just abort the convert here @@ -222,7 +221,7 @@ public final class CxfPayloadConverter { } else { return (T) result; } - + } // we cannot convert a node list, so we try the first item from the // node list @@ -235,6 +234,11 @@ public final class CxfPayloadConverter { // no we could not do it currently return (T) Void.TYPE; } + } else { + if (size == 0) { + // empty size so we cannot convert + return (T) Void.TYPE; + } } } return null; http://git-wip-us.apache.org/repos/asf/camel/blob/7e4f0b90/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java index 17ffc16..5bf636f 100644 --- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CxfPayloadConverterTest.java @@ -26,23 +26,25 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; + import org.apache.camel.StreamCache; import org.apache.camel.component.cxf.CxfPayload; import org.apache.camel.test.junit4.ExchangeTestSupport; import org.junit.Before; import org.junit.Test; - public class CxfPayloadConverterTest extends ExchangeTestSupport { private Document document; private CxfPayload<String[]> payload; private CxfPayload<String[]> emptyPayload; private FileInputStream inputStream; + @Override public boolean isCreateCamelContextPerClass() { return true; @@ -79,12 +81,12 @@ public class CxfPayloadConverterTest extends ExchangeTestSupport { assertNotNull(payload); assertEquals("Get a wrong size of body", 1, payload.getBody().size()); } - + @Test public void testCxfPayloadToNodeList() { NodeList nodeList = CxfPayloadConverter.cxfPayloadToNodeList(payload, exchange); assertNotNull(nodeList); - assertEquals("Get a worng size of nodeList", 1, nodeList.getLength()); + assertEquals("Get a worng size of nodeList", 1, nodeList.getLength()); } @Test @@ -103,14 +105,13 @@ public class CxfPayloadConverterTest extends ExchangeTestSupport { assertEquals("Get a wrong size of body", 1, payload.getBodySources().size()); assertEquals("Get a wrong size of body", 1, payload.getBody().size()); assertEquals("expects stream source", "streamsource", payload.getBodySources().get(0).getClass().getSimpleName().toLowerCase()); - } - + @Test public void testFromCxfPayload() { exchange.getIn().setBody(payload); InputStream inputStream = exchange.getIn().getBody(InputStream.class); - assertTrue(inputStream instanceof InputStream); + assertTrue(inputStream instanceof InputStream); } @Test @@ -119,7 +120,7 @@ public class CxfPayloadConverterTest extends ExchangeTestSupport { exchange.getIn().setBody(payload); Node node = exchange.getIn().getBody(Node.class); assertNotNull(node); - + // do the empty conversion exchange.getIn().setBody(emptyPayload); node = exchange.getIn().getBody(Node.class); @@ -129,15 +130,44 @@ public class CxfPayloadConverterTest extends ExchangeTestSupport { exchange.getIn().setBody(payload); node = exchange.getIn().getBody(Node.class); assertNotNull(node); - + // To make sure we always get the element here Element root = (Element) node; assertEquals("root element name", "root", root.getNodeName()); assertEquals("root element namespace", "http://www.test.org/foo", root.getNamespaceURI()); Element bar = (Element) root.getElementsByTagName("bar").item(0); assertEquals("child element name", "bar", bar.getNodeName()); - assertEquals("child element namespace", "http://www.test.org/foo", - bar.getNamespaceURI()); + assertEquals("child element namespace", "http://www.test.org/foo", bar.getNamespaceURI()); + } + + @Test + public void testEmptySaxPayload() { + exchange.getIn().setBody(emptyPayload); + Object out = exchange.getIn().getBody(SAXSource.class); + assertNull("Should not be able to convert an empty payload", out); + } + + @Test + public void testEmptySaxAgainPayload() { + // do the empty + exchange.getIn().setBody(emptyPayload); + Object out = exchange.getIn().getBody(SAXSource.class); + assertNull("Should not be able to convert an empty payload", out); + + // do the working + exchange.getIn().setBody(payload); + out = exchange.getIn().getBody(SAXSource.class); + assertNotNull("Should be able to convert a non-empty payload", out); + + // do the empty one again + exchange.getIn().setBody(emptyPayload); + out = exchange.getIn().getBody(SAXSource.class); + assertNull("Should not be able to convert an empty payload", out); + + // do the working + exchange.getIn().setBody(payload); + out = exchange.getIn().getBody(SAXSource.class); + assertNotNull("Should be able to convert a non-empty payload", out); } }
