You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by al...@apache.org on 2018/04/07 15:42:41 UTC

[camel] branch camel-2.21.x updated: CAMEL-12415 - camel-jaxb, fix options combination: encoding, filterNonXmlChars

This is an automated email from the ASF dual-hosted git repository.

aldettinger pushed a commit to branch camel-2.21.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.21.x by this push:
     new c68bdb1  CAMEL-12415 - camel-jaxb, fix options combination: encoding,filterNonXmlChars
c68bdb1 is described below

commit c68bdb19a8b8d2b85e8c37507c7889915724fcb8
Author: Jonas Waage <jo...@gmail.com>
AuthorDate: Thu Mar 29 18:33:44 2018 +0200

    CAMEL-12415 - camel-jaxb, fix options combination: encoding,filterNonXmlChars
    
    (cherry picked from commit 608dd28f88a4b983e0abb0fa21c7ab68fc809345)
---
 .../converter/jaxb/FallbackTypeConverter.java      |  7 +-
 .../converter/jaxb/FilteringXmlStreamWriter.java   | 25 ++++++-
 .../camel/converter/jaxb/JaxbDataFormat.java       | 14 ++--
 .../ExplicitEncodingAndXMLCharFilteringTest.java   | 85 ++++++++++++++++++++++
 4 files changed, 119 insertions(+), 12 deletions(-)

diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
index 8283af6..b2ec41b 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
@@ -298,8 +298,9 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert
             if (isPrettyPrint()) {
                 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
             }
-            if (exchange != null && exchange.getProperty(Exchange.CHARSET_NAME, String.class) != null) {
-                marshaller.setProperty(Marshaller.JAXB_ENCODING, exchange.getProperty(Exchange.CHARSET_NAME, String.class));
+            String charset = exchange != null ? exchange.getProperty(Exchange.CHARSET_NAME, String.class) : null;
+            if (charset != null) {
+                marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
             }
             Object toMarshall = value;
             if (objectFactoryMethod != null) {
@@ -314,7 +315,7 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert
             }
             if (needFiltering(exchange)) {
                 XMLStreamWriter writer = parentTypeConverter.convertTo(XMLStreamWriter.class, buffer);
-                FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer);
+                FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer, charset);
                 marshaller.marshal(toMarshall, filteringWriter);
             } else {
                 marshaller.marshal(toMarshall, buffer);
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriter.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriter.java
index 7af3d72..3a82b14 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriter.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriter.java
@@ -36,6 +36,7 @@ public class FilteringXmlStreamWriter implements XMLStreamWriter {
     NonXmlCharFilterer nonXmlCharFilterer = new NonXmlCharFilterer();
 
     private XMLStreamWriter writer;
+    private String encoding;
 
     /**
      * @param writer
@@ -46,6 +47,18 @@ public class FilteringXmlStreamWriter implements XMLStreamWriter {
     }
 
     /**
+     * @param writer
+     *            target writer to wrap.
+     * @param encoding
+     *            the encoding to write in the xml prolog.
+     *
+     */
+    public FilteringXmlStreamWriter(XMLStreamWriter writer, String encoding) {
+        this.writer = writer;
+        this.encoding = encoding;
+    }
+
+    /**
      * This method applies filtering before delegating call to {@link #writer}.
      */
     public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
@@ -181,7 +194,11 @@ public class FilteringXmlStreamWriter implements XMLStreamWriter {
     }
 
     public void writeStartDocument() throws XMLStreamException {
-        writer.writeStartDocument();
+        if (encoding != null) {
+            this.writeStartDocument(encoding, null);
+        } else {
+            writer.writeStartDocument();
+        }
     }
 
     public void writeStartDocument(String encoding, String version) throws XMLStreamException {
@@ -189,7 +206,11 @@ public class FilteringXmlStreamWriter implements XMLStreamWriter {
     }
 
     public void writeStartDocument(String version) throws XMLStreamException {
-        writer.writeStartDocument(version);
+        if (encoding != null) {
+            this.writeStartDocument(encoding, version);
+        } else {
+            writer.writeStartDocument(version);
+        }
     }
 
     public void writeStartElement(String prefix, String localName, String namespaceURI)
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
index ea74d37..35a208a 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
@@ -168,7 +168,7 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
                     marshaller.setProperty(property.getKey(), property.getValue());
                 }
             }
-            doMarshal(exchange, graph, stream, marshaller);
+            doMarshal(exchange, graph, stream, marshaller, charset);
 
             if (contentTypeHeader) {
                 if (exchange.hasOut()) {
@@ -182,7 +182,7 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
         }
     }
 
-    void doMarshal(Exchange exchange, Object graph, OutputStream stream, Marshaller marshaller) throws Exception {
+    void doMarshal(Exchange exchange, Object graph, OutputStream stream, Marshaller marshaller, String charset) throws Exception {
 
         Object element = graph;
         QName partNamespaceOnDataFormat = getPartNamespace();
@@ -206,9 +206,9 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
         // only marshal if its possible
         if (introspector.isElement(element)) {
             if (asXmlStreamWriter(exchange)) {
-                XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
+                XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, exchange, stream);
                 if (needFiltering(exchange)) {
-                    writer = new FilteringXmlStreamWriter(writer);
+                    writer = new FilteringXmlStreamWriter(writer, charset);
                 }
                 if (xmlStreamWriterWrapper != null) {
                     writer = xmlStreamWriterWrapper.wrapWriter(writer);
@@ -226,9 +226,9 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
                     if (instance != null) {
                         Object toMarshall = objectFactoryMethod.invoke(instance, element);
                         if (asXmlStreamWriter(exchange)) {
-                            XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
+                            XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, exchange, stream);
                             if (needFiltering(exchange)) {
-                                writer = new FilteringXmlStreamWriter(writer);
+                                writer = new FilteringXmlStreamWriter(writer, charset);
                             }
                             if (xmlStreamWriterWrapper != null) {
                                 writer = xmlStreamWriterWrapper.wrapWriter(writer);
@@ -257,7 +257,7 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
             throw new InvalidPayloadException(exchange, JAXBElement.class);
         }
     }
-
+    
     private boolean asXmlStreamWriter(Exchange exchange) {
         return needFiltering(exchange) || (xmlStreamWriterWrapper != null);
     }
diff --git a/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitEncodingAndXMLCharFilteringTest.java b/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitEncodingAndXMLCharFilteringTest.java
new file mode 100644
index 0000000..3adb9c3
--- /dev/null
+++ b/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitEncodingAndXMLCharFilteringTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (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
+ *
+ * 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.
+ */
+package org.apache.camel.example;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.jaxb.JaxbDataFormat;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+
+/**
+ * @version 
+ */
+public class ExplicitEncodingAndXMLCharFilteringTest extends CamelTestSupport {
+
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/charset");
+        super.setUp();
+    }
+
+    @Test
+    public void testIsoAndCharacterFiltering() throws Exception {
+        PurchaseOrder order = new PurchaseOrder();
+        //Data containing characters ÆØÅæøå that differ in utf-8 and iso + a spouting whale
+        String name = "\u00c6\u00d8\u00C5\u00e6\u00f8\u00e5\uD83D\uDC33\uFFFD";
+        String expected = "\u00c6\u00d8\u00C5\u00e6\u00f8\u00e5  \uFFFD"; //Spouting whale has become spaces
+        order.setName(name);
+        order.setAmount(123.45);
+        order.setPrice(2.22);
+
+        MockEndpoint result = getMockEndpoint("mock:file");
+        result.expectedFileExists("target/charset/output.xml");
+
+        template.sendBody("direct:start", order);
+        assertMockEndpointsSatisfied();
+
+        JAXBContext jaxbContext = JAXBContext.newInstance("org.apache.camel.example");
+        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+        InputStream inputStream = new FileInputStream("target/charset/output.xml");
+        Reader reader = new InputStreamReader(inputStream, "ISO-8859-1");
+        PurchaseOrder obj = (PurchaseOrder) unmarshaller.unmarshal(reader);
+        assertEquals(expected, obj.getName());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                JaxbDataFormat jaxb = new JaxbDataFormat("org.apache.camel.example");
+                jaxb.setFilterNonXmlChars(true);
+                jaxb.setEncoding("iso-8859-1");
+
+                from("direct:start")
+                        .marshal(jaxb)
+                        .to("file:target/charset/?fileName=output.xml&charset=iso-8859-1");
+            }
+        };
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
aldettinger@apache.org.