You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/12/07 09:01:32 UTC

[camel] branch master updated: CAMEL-12062 Propagate encoding in property

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

acosentino 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 3bf8a1b  CAMEL-12062 Propagate encoding in property
3bf8a1b is described below

commit 3bf8a1b85976a58a15e9e0b7a2786ee32e3121af
Author: Jonas Waage <jo...@gmail.com>
AuthorDate: Tue Dec 5 00:34:55 2017 +0100

    CAMEL-12062 Propagate encoding in property
---
 .../camel/converter/jaxb/JaxbDataFormat.java       |  4 ++
 .../camel/example/ExplicitFileEncodingTest.java    | 77 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)

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 b78d516..0782e21 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
@@ -136,6 +136,10 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
             String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
             if (charset == null) {
                 charset = encoding;
+                //Propagate the encoding of the exchange
+                if (charset != null) {
+                    exchange.setProperty(Exchange.CHARSET_NAME, charset);
+                }
             }
             if (charset != null) {
                 marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
diff --git a/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitFileEncodingTest.java b/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitFileEncodingTest.java
new file mode 100644
index 0000000..b045821
--- /dev/null
+++ b/components/camel-jaxb/src/test/java/org/apache/camel/example/ExplicitFileEncodingTest.java
@@ -0,0 +1,77 @@
+/**
+ * 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.File;
+
+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 ExplicitFileEncodingTest extends CamelTestSupport {
+
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/charset");
+        super.setUp();
+    }
+
+    @Test
+    public void testISOFileEncoding() throws Exception {
+        PurchaseOrder order = new PurchaseOrder();
+        //Data containing characters ÆØÅæøå that differ in utf-8 and iso
+        String name = "\u00c6\u00d8\u00C5\u00e6\u00f8\u00e5";
+        order.setName(name);
+        order.setAmount(123.45);
+        order.setPrice(2.22);
+
+        MockEndpoint result = getMockEndpoint("mock:file");
+        result.expectedFileExists("target/charset/output.txt");
+
+        template.sendBody("direct:start", order);
+        assertMockEndpointsSatisfied();
+
+        JAXBContext jaxbContext = JAXBContext.newInstance("org.apache.camel.example");
+        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+        PurchaseOrder obj = (PurchaseOrder)unmarshaller.unmarshal(new File("target/charset/output.txt"));
+        assertEquals(obj.getName(), name);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                JaxbDataFormat jaxb = new JaxbDataFormat("org.apache.camel.example");
+                jaxb.setEncoding("iso-8859-1");
+
+                from("direct:start")
+                        .marshal(jaxb)
+                        .to("file:target/charset/?fileName=output.txt&charset=iso-8859-1");
+            }
+        };
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].