You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/10/30 14:17:55 UTC

[2/2] git commit: CAMEL-6872: Do not decode Content-Type header by deafult.

CAMEL-6872: Do not decode Content-Type header by deafult.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/379ca058
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/379ca058
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/379ca058

Branch: refs/heads/camel-2.12.x
Commit: 379ca058a406f6a0cecfc95c88f412a6a6b6ccf1
Parents: 7db4d7e
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Oct 30 14:17:23 2013 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Oct 30 14:18:41 2013 +0100

----------------------------------------------------------------------
 .../netty/http/DefaultNettyHttpBinding.java     | 30 +++++++++++++++++---
 .../netty/http/NettyHttpContentTypeTest.java    | 16 +++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/379ca058/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index 84d315b..f6c6c45 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.ObjectOutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
@@ -153,7 +154,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding {
             Iterator<?> it = ObjectHelper.createIterator(values);
             while (it.hasNext()) {
                 Object extracted = it.next();
-                Object decoded = configuration.isUrlDecodeHeaders() ? URLDecoder.decode(extracted.toString(), "UTF-8") : extracted.toString();
+                Object decoded = shouldUrlDecodeHeader(configuration, name, extracted, "UTF-8");
                 LOG.trace("HTTP-header: {}", extracted);
                 if (headerFilterStrategy != null
                         && !headerFilterStrategy.applyFilterToExternalHeaders(name, decoded, exchange)) {
@@ -173,7 +174,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding {
                 Iterator<?> it = ObjectHelper.createIterator(values);
                 while (it.hasNext()) {
                     Object extracted = it.next();
-                    Object decoded = configuration.isUrlDecodeHeaders() ? URLDecoder.decode(extracted.toString(), "UTF-8") : extracted.toString();
+                    Object decoded = shouldUrlDecodeHeader(configuration, name, extracted, "UTF-8");
                     LOG.trace("URI-Parameter: {}", extracted);
                     if (headerFilterStrategy != null
                             && !headerFilterStrategy.applyFilterToExternalHeaders(name, decoded, exchange)) {
@@ -195,8 +196,8 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding {
                 for (String param : body.split("&")) {
                     String[] pair = param.split("=", 2);
                     if (pair.length == 2) {
-                        String name = configuration.isUrlDecodeHeaders() ? URLDecoder.decode(pair[0], charset) : pair[0];
-                        String value = configuration.isUrlDecodeHeaders() ? URLDecoder.decode(pair[1], charset) : pair[1];
+                        String name = shouldUrlDecodeHeader(configuration, "", pair[0], charset);
+                        String value = shouldUrlDecodeHeader(configuration, name, pair[1], charset);
                         if (headerFilterStrategy != null
                                 && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
                             NettyHttpHelper.appendHeader(headers, name, value);
@@ -210,6 +211,27 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding {
 
     }
 
+    /**
+     * Decodes the header if needed to, or returns the header value as is.
+     *
+     * @param configuration  the configuration
+     * @param headerName     the header name
+     * @param value          the current header value
+     * @param charset        the charset to use for decoding
+     * @return  the decoded value (if decoded was needed) or a <tt>toString</tt> representation of the value.
+     * @throws UnsupportedEncodingException is thrown if error decoding.
+     */
+    protected String shouldUrlDecodeHeader(NettyHttpConfiguration configuration, String headerName, Object value, String charset) throws UnsupportedEncodingException {
+        // do not decode Content-Type
+        if (Exchange.CONTENT_TYPE.equals(headerName)) {
+            return value.toString();
+        } else if (configuration.isUrlDecodeHeaders()) {
+            return URLDecoder.decode(value.toString(), charset);
+        } else {
+            return value.toString();
+        }
+    }
+
     @Override
     public Message toCamelMessage(HttpResponse response, Exchange exchange, NettyHttpConfiguration configuration) throws Exception {
         LOG.trace("toCamelMessage: {}", response);

http://git-wip-us.apache.org/repos/asf/camel/blob/379ca058/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java
index 7828856..8499c52 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java
@@ -56,6 +56,22 @@ public class NettyHttpContentTypeTest extends BaseNettyTest {
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testContentTypeWithActionAndPlus() throws Exception {
+        getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/soap+xml;charset=\"utf-8\";action=\"http://somewhere.com/foo\"");
+        getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_CHARACTER_ENCODING, "utf-8");
+        getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URL, "http://0.0.0.0:" + getPort() + "/foo");
+        getMockEndpoint("mock:input").expectedPropertyReceived(Exchange.CHARSET_NAME, "utf-8");
+
+        byte[] data = "Hello World".getBytes(Charset.forName("utf-8"));
+        String out = template.requestBodyAndHeader("netty-http:http://0.0.0.0:{{port}}/foo", data,
+                "content-type", "application/soap+xml;charset=\"utf-8\";action=\"http://somewhere.com/foo\"", String.class);
+        assertEquals("Bye World", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {