Repository: camel Updated Branches: refs/heads/camel-2.16.x 8ff5709fa -> cbf6335d5
CAMEL-9774: CXFPayload may lose CDATA sections under stream caching Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cbf6335d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cbf6335d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cbf6335d Branch: refs/heads/camel-2.16.x Commit: cbf6335d569f558f912f8a0d2a549279828fa136 Parents: 8ff5709 Author: Akitoshi Yoshida <a...@apache.org> Authored: Wed Mar 30 10:09:18 2016 +0200 Committer: Akitoshi Yoshida <a...@apache.org> Committed: Wed Mar 30 10:54:52 2016 +0200 ---------------------------------------------------------------------- .../cxf/converter/CachedCxfPayload.java | 26 ++++++++------------ .../cxf/converter/CachedCxfPayloadTest.java | 25 ++++++++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cbf6335d/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CachedCxfPayload.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CachedCxfPayload.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CachedCxfPayload.java index b480a73..b2b4db6 100644 --- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CachedCxfPayload.java +++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CachedCxfPayload.java @@ -22,14 +22,11 @@ import java.util.ArrayList; import java.util.ListIterator; import java.util.Map; +import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stax.StAXSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.Document; import org.apache.camel.Exchange; import org.apache.camel.StreamCache; @@ -38,18 +35,17 @@ import org.apache.camel.converter.jaxp.XmlConverter; import org.apache.camel.converter.stream.CachedOutputStream; import org.apache.camel.converter.stream.StreamSourceCache; import org.apache.cxf.staxutils.StaxSource; +import org.apache.cxf.staxutils.StaxUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CachedCxfPayload<T> extends CxfPayload<T> implements StreamCache { private static final Logger LOG = LoggerFactory.getLogger(CachedCxfPayload.class); - private final XmlConverter xml; public CachedCxfPayload(CxfPayload<T> orig, Exchange exchange, XmlConverter xml) { super(orig.getHeaders(), new ArrayList<Source>(orig.getBodySources()), orig.getNsMap()); ListIterator<Source> li = getBodySources().listIterator(); - this.xml = xml; while (li.hasNext()) { Source source = li.next(); XMLStreamReader reader = null; @@ -67,22 +63,22 @@ public class CachedCxfPayload<T> extends CxfPayload<T> implements StreamCache { if (reader != null) { Map<String, String> nsmap = getNsMap(); if (nsmap != null && !(reader instanceof DelegatingXMLStreamReader)) { - source = new StAXSource(new DelegatingXMLStreamReader(reader, nsmap)); + reader = new DelegatingXMLStreamReader(reader, nsmap); } CachedOutputStream cos = new CachedOutputStream(exchange); - StreamResult sr = new StreamResult(cos); try { - xml.toResult(source, sr); + StaxUtils.copy(reader, cos); li.set(new StreamSourceCache(cos.newStreamCache())); - } catch (TransformerException e) { + } catch (XMLStreamException e) { LOG.error("Transformation failed ", e); } catch (IOException e) { LOG.error("Cannot Create StreamSourceCache ", e); } + } else if (!(source instanceof DOMSource)) { - Document document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, source); + DOMSource document = exchange.getContext().getTypeConverter().convertTo(DOMSource.class, exchange, source); if (document != null) { - li.set(new DOMSource(document)); + li.set(document); } } } @@ -91,7 +87,6 @@ public class CachedCxfPayload<T> extends CxfPayload<T> implements StreamCache { private CachedCxfPayload(CachedCxfPayload<T> orig, Exchange exchange) throws IOException { super(orig.getHeaders(), new ArrayList<Source>(orig.getBodySources()), orig.getNsMap()); ListIterator<Source> li = getBodySources().listIterator(); - this.xml = orig.xml; while (li.hasNext()) { Source source = li.next(); if (source instanceof StreamCache) { @@ -119,10 +114,9 @@ public class CachedCxfPayload<T> extends CxfPayload<T> implements StreamCache { if (body instanceof StreamCache) { ((StreamCache) body).writeTo(os); } else { - StreamResult sr = new StreamResult(os); try { - xml.toResult(body, sr); - } catch (TransformerException e) { + StaxUtils.copy(body, os); + } catch (XMLStreamException e) { throw new IOException("Transformation failed", e); } } http://git-wip-us.apache.org/repos/asf/camel/blob/cbf6335d/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CachedCxfPayloadTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CachedCxfPayloadTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CachedCxfPayloadTest.java index 96cee5e..ac9c30e 100644 --- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CachedCxfPayloadTest.java +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/CachedCxfPayloadTest.java @@ -36,41 +36,44 @@ import org.apache.cxf.staxutils.StaxUtils; import org.junit.Test; public class CachedCxfPayloadTest extends ExchangeTestSupport { - private static final String PAYLOAD = "<foo>bar</foo>"; + private static final String PAYLOAD = "<foo>bar<![CDATA[ & a cdata section ]]></foo>"; + private static final String PAYLOAD_AMPED = "<foo>bar & a cdata section </foo>"; @Test public void testCachedCxfPayloadSAXSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException { SAXSource source = context.getTypeConverter().mandatoryConvertTo(SAXSource.class, PAYLOAD); - doTest(source); + // this conversion uses org.apache.camel.converter.jaxp.XmlConverter.toDOMNodeFromSAX which uses Transformer + // to convert SAXSource to DOM. This conversion preserves the content but loses its original representation. + doTest(source, PAYLOAD_AMPED); } @Test public void testCachedCxfPayloadStAXSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException { StAXSource source = context.getTypeConverter().mandatoryConvertTo(StAXSource.class, PAYLOAD); - doTest(source); + doTest(source, PAYLOAD); } @Test public void testCachedCxfPayloadStaxSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException { XMLStreamReader streamReader = StaxUtils.createXMLStreamReader(new StreamSource(new StringReader(PAYLOAD))); StaxSource source = new StaxSource(streamReader); - doTest(source); + doTest(source, PAYLOAD); } @Test public void testCachedCxfPayloadDOMSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException { DOMSource source = context.getTypeConverter().mandatoryConvertTo(DOMSource.class, PAYLOAD); - doTest(source); + doTest(source, PAYLOAD); } @Test public void testCachedCxfPayloadStreamSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException { StreamSource source = context.getTypeConverter().mandatoryConvertTo(StreamSource.class, PAYLOAD); - doTest(source); + doTest(source, PAYLOAD); } @SuppressWarnings({ "unchecked", "rawtypes" }) - private void doTest(Object source) throws IOException { + private void doTest(Object source, String payload) throws IOException { CxfPayload<?> originalPayload = context.getTypeConverter().convertTo(CxfPayload.class, source); CachedCxfPayload<?> cache = new CachedCxfPayload(originalPayload, exchange, new XmlConverter()); @@ -80,7 +83,7 @@ public class CachedCxfPayloadTest extends ExchangeTestSupport { cache.writeTo(bos); String s = context.getTypeConverter().convertTo(String.class, bos); - assertEquals(PAYLOAD, s); + assertEquals(payload, s); cache.reset(); @@ -89,15 +92,15 @@ public class CachedCxfPayloadTest extends ExchangeTestSupport { clone.writeTo(bos); s = context.getTypeConverter().convertTo(String.class, bos); - assertEquals(PAYLOAD, s); + assertEquals(payload, s); cache.reset(); clone.reset(); s = context.getTypeConverter().convertTo(String.class, cache); - assertEquals(PAYLOAD, s); + assertEquals(payload, s); s = context.getTypeConverter().convertTo(String.class, clone); - assertEquals(PAYLOAD, s); + assertEquals(payload, s); } }