[camel] branch master updated (e68abbf -> 40e7ee2)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated (e68abbf -> 40e7ee2)

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

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from e68abbf  Regen
     new cf86cdd  CAMEL-14126: Added unit test and made camel-stax accept more input data. Thanks to tomahi for sample project.
     new 40e7ee2  CAMEL-14126: camel-stax - Make sure to close input stream which can help on windows etc.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../component/stax/StAXJAXBIteratorExpression.java | 25 ++++++++------
 .../component/stax/IssueWithWrongEncodingTest.java | 38 +++++++++++++---------
 .../camel/component/stax/model/Product.java}       | 20 +++++-------
 .../src/test/resources/products_with_non_utf8.xml  |  9 +++++
 .../test/resources/products_with_valid_utf8.xml}   | 13 +++++---
 5 files changed, 63 insertions(+), 42 deletions(-)
 copy core/camel-core/src/test/java/org/apache/camel/component/file/FileMoveAndMoveFailedIssueTest.java => components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java (54%)
 copy components/{camel-jackson/src/test/java/org/apache/camel/component/jackson/TestOtherPojo.java => camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java} (77%)
 create mode 100755 components/camel-stax/src/test/resources/products_with_non_utf8.xml
 copy components/{camel-attachments/src/test/data/message1.xml => camel-stax/src/test/resources/products_with_valid_utf8.xml} (84%)
 mode change 100644 => 100755

Reply | Threaded
Open this post in threaded view
|

[camel] 01/02: CAMEL-14126: Added unit test and made camel-stax accept more input data. Thanks to tomahi for sample project.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cf86cdde44e4c4d558c816d607ce11f8a3022b2f
Author: Claus Ibsen <[hidden email]>
AuthorDate: Wed Nov 13 05:53:20 2019 +0100

    CAMEL-14126: Added unit test and made camel-stax accept more input data. Thanks to tomahi for sample project.
---
 .../component/stax/StAXJAXBIteratorExpression.java |  8 +--
 .../component/stax/IssueWithWrongEncodingTest.java | 72 ++++++++++++++++++++++
 .../apache/camel/component/stax/model/Product.java | 36 +++++++++++
 .../src/test/resources/products_with_non_utf8.xml  |  9 +++
 .../test/resources/products_with_valid_utf8.xml    | 27 ++++++++
 5 files changed, 147 insertions(+), 5 deletions(-)

diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
index 38d7b70..62f1fb4 100644
--- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
+++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
@@ -115,13 +115,11 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
     @SuppressWarnings("unchecked")
     public Object evaluate(Exchange exchange) {
         try {
-            XMLEventReader reader;
-            if (isNamespaceAware) {
-                reader = exchange.getIn().getMandatoryBody(XMLEventReader.class);
-            } else {
+            XMLEventReader reader = exchange.getContext().getTypeConverter().tryConvertTo(XMLEventReader.class, exchange, exchange.getIn().getBody());
+            if (reader == null) {
                 InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
                 XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
-                xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
+                xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, isNamespaceAware);
                 reader = xmlInputFactory.createXMLEventReader(inputStream);
             }
 
diff --git a/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java b/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java
new file mode 100644
index 0000000..b4f2337
--- /dev/null
+++ b/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.component.stax;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.stax.model.Product;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.component.stax.StAXBuilder.stax;
+
+public class IssueWithWrongEncodingTest extends CamelTestSupport {
+
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/encoding");
+        super.setUp();
+    }
+
+    @Test
+    public void testOkEncoding() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        File file = new File("src/test/resources/products_with_valid_utf8.xml");
+        template.sendBodyAndHeader("file:target/encoding", file, Exchange.FILE_NAME, "products_with_valid_utf8.xml");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testInvalidEncoding() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:result").expectedFileExists("target/encoding/error/products_with_non_utf8.xml");
+
+        File file = new File("src/test/resources/products_with_non_utf8.xml");
+        template.sendBodyAndHeader("file:target/encoding", file, Exchange.FILE_NAME, "products_with_non_utf8.xml");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/encoding?moveFailed=error")
+                    .split(stax(Product.class))
+                    .to("mock:result");
+            }
+        };
+    }
+}
diff --git a/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java b/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java
new file mode 100644
index 0000000..f5ddc1e
--- /dev/null
+++ b/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java
@@ -0,0 +1,36 @@
+/*
+ * 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.component.stax.model;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlType(name = "product")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Product {
+
+    private String name;
+
+    public String getName() {
+        return this.name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}
diff --git a/components/camel-stax/src/test/resources/products_with_non_utf8.xml b/components/camel-stax/src/test/resources/products_with_non_utf8.xml
new file mode 100755
index 0000000..d8ae660
--- /dev/null
+++ b/components/camel-stax/src/test/resources/products_with_non_utf8.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<products>
+    <product>
+        <name>first product ��</name>
+    </product>
+    <product>
+        <name>second product</name>
+    </product>
+</products>
\ No newline at end of file
diff --git a/components/camel-stax/src/test/resources/products_with_valid_utf8.xml b/components/camel-stax/src/test/resources/products_with_valid_utf8.xml
new file mode 100755
index 0000000..465908b
--- /dev/null
+++ b/components/camel-stax/src/test/resources/products_with_valid_utf8.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<products>
+    <product>
+        <name>first product</name>
+    </product>
+    <product>
+        <name>second product</name>
+    </product>
+</products>
\ No newline at end of file

Reply | Threaded
Open this post in threaded view
|

[camel] 02/02: CAMEL-14126: camel-stax - Make sure to close input stream which can help on windows etc.

davsclaus-2
In reply to this post by davsclaus-2
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 40e7ee28f2be6407c180ba354d5f2266d6d37347
Author: Claus Ibsen <[hidden email]>
AuthorDate: Wed Nov 13 06:04:25 2019 +0100

    CAMEL-14126: camel-stax - Make sure to close input stream which can help on windows etc.
---
 .../component/stax/StAXJAXBIteratorExpression.java      | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
index 62f1fb4..11af16a 100644
--- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
+++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
@@ -35,6 +35,7 @@ import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.LRUCacheFactory;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 
 import static org.apache.camel.component.stax.StAXUtil.getTagName;
@@ -115,9 +116,10 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
     @SuppressWarnings("unchecked")
     public Object evaluate(Exchange exchange) {
         try {
+            InputStream inputStream = null;
             XMLEventReader reader = exchange.getContext().getTypeConverter().tryConvertTo(XMLEventReader.class, exchange, exchange.getIn().getBody());
             if (reader == null) {
-                InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
+                inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
                 XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
                 xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, isNamespaceAware);
                 reader = xmlInputFactory.createXMLEventReader(inputStream);
@@ -127,7 +129,7 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
             if (clazz == null && handledName != null) {
                 clazz = (Class<T>) exchange.getContext().getClassResolver().resolveMandatoryClass(handledName);
             }
-            return createIterator(reader, clazz);
+            return createIterator(reader, clazz, inputStream);
         } catch (InvalidPayloadException e) {
             exchange.setException(e);
             return null;
@@ -143,8 +145,8 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
         }
     }
 
-    private Iterator<T> createIterator(XMLEventReader reader, Class<T> clazz) throws JAXBException {
-        return new StAXJAXBIterator<>(clazz, reader);
+    private Iterator<T> createIterator(XMLEventReader reader, Class<T> clazz, InputStream inputStream) throws JAXBException {
+        return new StAXJAXBIterator<>(clazz, reader, inputStream);
     }
 
     /**
@@ -153,14 +155,16 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
     static class StAXJAXBIterator<T> implements Iterator<T>, Closeable {
 
         private final XMLEventReader reader;
+        private final InputStream inputStream;
         private final Class<T> clazz;
         private final String name;
         private final Unmarshaller unmarshaller;
         private T element;
 
-        StAXJAXBIterator(Class<T> clazz, XMLEventReader reader) throws JAXBException {
+        StAXJAXBIterator(Class<T> clazz, XMLEventReader reader, InputStream inputStream) throws JAXBException {
             this.clazz = clazz;
             this.reader = reader;
+            this.inputStream = inputStream;
 
             name = getTagName(clazz);
             JAXBContext jaxb = jaxbContext(clazz);
@@ -221,6 +225,9 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
 
         @Override
         public void close() throws IOException {
+            if (inputStream != null) {
+                IOHelper.close(inputStream);
+            }
             try {
                 reader.close();
             } catch (XMLStreamException e) {