[
https://issues.apache.org/jira/browse/CAMEL-12415?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16419399#comment-16419399
]
ASF GitHub Bot commented on CAMEL-12415:
----------------------------------------
IIlllII closed pull request #2276: CAMEL-12415 - camel-jaxb, fix options
combination: encoding,filterNonXmlChars
URL: https://github.com/apache/camel/pull/2276
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/FallbackTypeConverter.java
b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
index 8283af650a4..24e51ed537f 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
@@ -295,11 +295,13 @@ protected void doStop() throws Exception {
Marshaller marshaller = context.createMarshaller();
Writer buffer = new StringWriter();
+
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 +316,7 @@ protected void doStop() throws Exception {
}
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 7af3d727702..49e0df3c2cc 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,13 +36,18 @@
NonXmlCharFilterer nonXmlCharFilterer = new NonXmlCharFilterer();
private XMLStreamWriter writer;
+ private String encoding;
/**
* @param writer
* target writer to wrap.
+ * @param encoding
+ * the encoding to write in header
+ *
*/
- public FilteringXmlStreamWriter(XMLStreamWriter writer) {
+ public FilteringXmlStreamWriter(XMLStreamWriter writer, String encoding) {
this.writer = writer;
+ this.encoding = encoding != null ? encoding.toUpperCase() : null;
}
/**
@@ -181,7 +186,11 @@ public void writeProcessingInstruction(String target)
throws XMLStreamException
}
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 +198,11 @@ public void writeStartDocument(String encoding, String
version) throws XMLStream
}
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 ea74d37dad5..0500210261c 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
@@ -205,18 +205,8 @@ void doMarshal(Exchange exchange, Object graph,
OutputStream stream, Marshaller
// only marshal if its possible
if (introspector.isElement(element)) {
- if (asXmlStreamWriter(exchange)) {
- XMLStreamWriter writer =
typeConverter.convertTo(XMLStreamWriter.class, stream);
- if (needFiltering(exchange)) {
- writer = new FilteringXmlStreamWriter(writer);
- }
- if (xmlStreamWriterWrapper != null) {
- writer = xmlStreamWriterWrapper.wrapWriter(writer);
- }
- marshaller.marshal(element, writer);
- } else {
- marshaller.marshal(element, stream);
- }
+ XMLStreamWriter writer = getWriter(exchange, stream);
+ performWrite(exchange, stream, writer, marshaller, element);
return;
} else if (objectFactory && element != null) {
Method objectFactoryMethod =
JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass());
@@ -225,18 +215,8 @@ void doMarshal(Exchange exchange, Object graph,
OutputStream stream, Marshaller
Object instance =
objectFactoryMethod.getDeclaringClass().newInstance();
if (instance != null) {
Object toMarshall =
objectFactoryMethod.invoke(instance, element);
- if (asXmlStreamWriter(exchange)) {
- XMLStreamWriter writer =
typeConverter.convertTo(XMLStreamWriter.class, stream);
- if (needFiltering(exchange)) {
- writer = new FilteringXmlStreamWriter(writer);
- }
- if (xmlStreamWriterWrapper != null) {
- writer =
xmlStreamWriterWrapper.wrapWriter(writer);
- }
- marshaller.marshal(toMarshall, writer);
- } else {
- marshaller.marshal(toMarshall, stream);
- }
+ XMLStreamWriter writer = getWriter(exchange, stream);
+ performWrite(exchange, stream, writer, marshaller,
toMarshall);
return;
}
} catch (Exception e) {
@@ -258,6 +238,27 @@ void doMarshal(Exchange exchange, Object graph,
OutputStream stream, Marshaller
}
}
+ private void performWrite(Exchange exchange, OutputStream stream,
XMLStreamWriter writer, Marshaller marshaller, Object toMarshall) throws
JAXBException {
+ if (asXmlStreamWriter(exchange)) {
+ marshaller.marshal(toMarshall, writer);
+ } else {
+ marshaller.marshal(toMarshall, stream);
+ }
+ }
+
+ private XMLStreamWriter getWriter(Exchange exchange, OutputStream stream) {
+ XMLStreamWriter writer =
typeConverter.convertTo(XMLStreamWriter.class, exchange, stream);
+ if (needFiltering(exchange)) {
+ String charset = exchange.getProperty(Exchange.CHARSET_NAME,
String.class);
+ writer = new FilteringXmlStreamWriter(writer, charset);
+ }
+ if (xmlStreamWriterWrapper != null) {
+ writer = xmlStreamWriterWrapper.wrapWriter(writer);
+ }
+ return writer;
+ }
+
+
private boolean asXmlStreamWriter(Exchange exchange) {
return needFiltering(exchange) || (xmlStreamWriterWrapper != null);
}
diff --git
a/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriterTest.java
b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriterTest.java
index b8b38016783..59d164bc5f9 100644
---
a/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriterTest.java
+++
b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriterTest.java
@@ -42,7 +42,7 @@
@Before
public void setUp() {
- filteringXmlStreamWriter = new
FilteringXmlStreamWriter(xmlStreamWriterMock);
+ filteringXmlStreamWriter = new
FilteringXmlStreamWriter(xmlStreamWriterMock, null);
filteringXmlStreamWriter.nonXmlCharFilterer = nonXmlCharFiltererMock;
when(nonXmlCharFiltererMock.filter("value")).thenReturn("filteredValue");
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 00000000000..3adb9c3b252
--- /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");
+ }
+ };
+ }
+
+}
----------------------------------------------------------------
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]
> Camel-jaxb option "encoding" with option "filterNonXmlChars" generate wrong
> data
> --------------------------------------------------------------------------------
>
> Key: CAMEL-12415
> URL: https://issues.apache.org/jira/browse/CAMEL-12415
> Project: Camel
> Issue Type: Bug
> Components: camel-jaxb
> Affects Versions: 2.21.0
> Environment: OS X 13.3, Java 8
> Reporter: Jonas Waage
> Priority: Minor
>
> When using the jaxb-component to marshal.
> The properties:
> * Encoding
> * FilterNonXmlChars
> do not work together correctly.
> FilterNonXmlChars will ignore the set encoding, and make the output bytes
> UTF-8 encoded.
> I would either expect this to just work, and by that I mean bytes should be
> encoded as the set encoding, or minimally fail during startup of the route
> with an exception explaining that these properties does not work together.
> I'd really prefer the first, since I want to use the functionality of both.
> I have provided a patch which makes this work.I have done a small refactoring
> to not have to duplicate more code. I can rewrite this if it is a problem.
> Below is a test which will reproduce the problem.
> {code:java}
> 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");
> }
> };
> }
> {code}
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)