[
https://issues.apache.org/jira/browse/CAMEL-12062?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16281524#comment-16281524
]
ASF GitHub Bot commented on CAMEL-12062:
----------------------------------------
oscerd closed pull request #2130: CAMEL-12062: Propagate encoding in charset
property from jaxb component
URL: https://github.com/apache/camel/pull/2130
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 b78d5169c08..0782e2197c5 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 void marshal(Exchange exchange, Object graph,
OutputStream stream) throws
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 00000000000..b045821f005
--- /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");
+ }
+ };
+ }
+
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Jaxb component does not communicate charset when explicitly set
> ---------------------------------------------------------------
>
> Key: CAMEL-12062
> URL: https://issues.apache.org/jira/browse/CAMEL-12062
> Project: Camel
> Issue Type: Bug
> Components: camel-jaxb
> Affects Versions: 2.20.1
> Reporter: Jonas Waage
> Assignee: Andrea Cosentino
> Priority: Minor
> Fix For: 2.21.0
>
>
> This test will cause a UnmappableCharacterException, since jaxb does not
> communicate the encoding of the bytes it outputs causing the file endpoint to
> assume it is UTF-8.
> This can be mitigated by adding an explicit reader in the route, but it would
> be nice if it worked OOTB.
> {code:java}
> /**
> * @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");
> }
> };
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)