Repository: camel
Updated Branches:
  refs/heads/camel-2.17.x b0d37a924 -> 091855d68


CAMEL-10370: Conversion to CxfPayload throws Exception for Non-XML payload

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/091855d6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/091855d6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/091855d6

Branch: refs/heads/camel-2.17.x
Commit: 091855d68cd1ff7f16217f54c5752c16893b624d
Parents: b0d37a9
Author: Stephan Siano <stephan.si...@sap.com>
Authored: Tue Oct 4 16:25:20 2016 +0200
Committer: Stephan Siano <stephan.si...@sap.com>
Committed: Wed Oct 5 12:16:00 2016 +0200

----------------------------------------------------------------------
 .../cxf/converter/CxfPayloadConverter.java      | 96 +++++++++++---------
 .../cxf/converter/CxfPayloadConverterTest.java  | 21 +++++
 2 files changed, 73 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/091855d6/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 1da3fff..5801a7b 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
@@ -36,6 +36,7 @@ import org.w3c.dom.NodeList;
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.apache.camel.FallbackConverter;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.StreamCache;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.component.cxf.CxfPayload;
@@ -123,54 +124,61 @@ public final class CxfPayloadConverter {
         // use fallback type converter, so we can probably convert into
         // CxfPayloads from other types
         if (type.isAssignableFrom(CxfPayload.class)) {
-            if (!value.getClass().isArray()) {
-                Source src = null;
-                // many of the common format that can have a Source created 
directly
-                if (value instanceof InputStream) {
-                    src = new StreamSource((InputStream) value);
-                } else if (value instanceof Reader) {
-                    src = new StreamSource((Reader) value);
-                } else if (value instanceof String) {
-                    src = new StreamSource(new StringReader((String) value));
-                } else if (value instanceof Node) {
-                    src = new DOMSource((Node) value);
-                } else if (value instanceof Source) {
-                    src = (Source) value;
-                }
-                if (src == null) {
-                    // assuming staxsource is preferred, otherwise use the one 
preferred
-                    TypeConverter tc = 
registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
-                    if (tc == null) {
-                        tc = registry.lookup(Source.class, value.getClass());
+            try {
+                if (!value.getClass().isArray()) {
+                    Source src = null;
+                    // many of the common format that can have a Source created
+                    // directly
+                    if (value instanceof InputStream) {
+                        src = new StreamSource((InputStream)value);
+                    } else if (value instanceof Reader) {
+                        src = new StreamSource((Reader)value);
+                    } else if (value instanceof String) {
+                        src = new StreamSource(new 
StringReader((String)value));
+                    } else if (value instanceof Node) {
+                        src = new DOMSource((Node)value);
+                    } else if (value instanceof Source) {
+                        src = (Source)value;
+                    }
+                    if (src == null) {
+                        // assuming staxsource is preferred, otherwise use the
+                        // one preferred
+                        TypeConverter tc = 
registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
+                        if (tc == null) {
+                            tc = registry.lookup(Source.class, 
value.getClass());
+                        }
+                        if (tc != null) {
+                            src = tc.convertTo(Source.class, exchange, value);
+                        }
                     }
-                    if (tc != null) {
-                        src = tc.convertTo(Source.class, exchange, value);
+                    if (src != null) {
+                        return (T)sourceToCxfPayload(src, exchange);
                     }
                 }
-                if (src != null) {
-                    return (T) sourceToCxfPayload(src, exchange);
+                TypeConverter tc = registry.lookup(NodeList.class, 
value.getClass());
+                if (tc != null) {
+                    NodeList nodeList = tc.convertTo(NodeList.class, exchange, 
value);
+                    return (T)nodeListToCxfPayload(nodeList, exchange);
                 }
-            }
-            TypeConverter tc = registry.lookup(NodeList.class, 
value.getClass());
-            if (tc != null) {
-                NodeList nodeList = tc.convertTo(NodeList.class, exchange, 
value);
-                return (T) nodeListToCxfPayload(nodeList, exchange);
-            }
-            tc = registry.lookup(Document.class, value.getClass());
-            if (tc != null) {
-                Document document = tc.convertTo(Document.class, exchange, 
value);
-                return (T) documentToCxfPayload(document, exchange);
-            }
-            // maybe we can convert via an InputStream
-            CxfPayload<?> p;
-            p = convertVia(InputStream.class, exchange, value, registry);
-            if (p != null) {
-                return (T) p;
-            }
-            // String is the converter of last resort
-            p = convertVia(String.class, exchange, value, registry);
-            if (p != null) {
-                return (T) p;
+                tc = registry.lookup(Document.class, value.getClass());
+                if (tc != null) {
+                    Document document = tc.convertTo(Document.class, exchange, 
value);
+                    return (T)documentToCxfPayload(document, exchange);
+                }
+                // maybe we can convert via an InputStream
+                CxfPayload<?> p;
+                p = convertVia(InputStream.class, exchange, value, registry);
+                if (p != null) {
+                    return (T)p;
+                }
+                // String is the converter of last resort
+                p = convertVia(String.class, exchange, value, registry);
+                if (p != null) {
+                    return (T)p;
+                }
+            } catch (RuntimeCamelException e) {
+                // the internal conversion to XML can throw an exception if 
the content is not XML
+                // ignore this and return Void.TYPE to indicate that we cannot 
convert this
             }
             // no we could not do it currently
             return (T) Void.TYPE;

http://git-wip-us.apache.org/repos/asf/camel/blob/091855d6/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 8e2d46c..7ec06a2 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
@@ -114,6 +114,27 @@ public class CxfPayloadConverterTest extends 
ExchangeTestSupport {
     }
 
     @Test
+    public void testByteArrayToCxfPayload() {
+        // convert to byte array
+        exchange.getIn().setBody(inputStream);
+        byte[] bytes = exchange.getIn().getBody(byte[].class);
+        assertNotNull(bytes);
+        exchange.getIn().setBody(bytes);
+        // use default type converter
+        CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class);
+        assertTrue(payload instanceof CxfPayload);
+        assertEquals("Get a wrong size of body", 1, 
payload.getBodySources().size());
+        assertEquals("Get a wrong size of body", 1, payload.getBody().size());
+    }
+
+    @Test
+    public void testInvalidByteArrayToCxfPayload() {
+        exchange.getIn().setBody("NON-XML-Payload".getBytes());
+        CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class);
+        assertNull(payload);
+    }
+
+    @Test
     public void testFromCxfPayload() {
         exchange.getIn().setBody(payload);
         InputStream inputStream = exchange.getIn().getBody(InputStream.class);

Reply via email to