camel git commit: CAMEL-9442: Query string gets decoded when bridging netty-http to http4

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

camel git commit: CAMEL-9442: Query string gets decoded when bridging netty-http to http4

gertv-2
Repository: camel
Updated Branches:
  refs/heads/camel-2.16.x 1d48df6f8 -> 10d50677e


CAMEL-9442: Query string gets decoded when bridging netty-http to http4


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

Branch: refs/heads/camel-2.16.x
Commit: 10d50677e6dbe6f8fbbb98cc1a8b473a97eaa43d
Parents: 1d48df6
Author: Gert Vanthienen <[hidden email]>
Authored: Tue Dec 22 14:09:36 2015 +0100
Committer: Gert Vanthienen <[hidden email]>
Committed: Tue Dec 22 14:56:53 2015 +0100

----------------------------------------------------------------------
 .../apache/camel/http/common/HttpHelper.java    |  8 +++-
 .../component/http4/HttpBridgeEndpointTest.java | 29 ++++++++++++-
 .../handler/BasicRawQueryValidationHandler.java | 45 ++++++++++++++++++++
 .../http4/handler/BasicValidationHandler.java   | 22 ++++++----
 4 files changed, 93 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/10d50677/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index c9b8d7b..e18198a 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -311,8 +311,12 @@ public final class HttpHelper {
      */
     public static URI createURI(Exchange exchange, String url, HttpCommonEndpoint endpoint) throws URISyntaxException {
         URI uri = new URI(url);
-        // is a query string provided in the endpoint URI or in a header (header overrules endpoint)
-        String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
+        // is a query string provided in the endpoint URI or in a header
+        // (header overrules endpoint, raw query header overrules query header)
+        String queryString = exchange.getIn().getHeader(Exchange.HTTP_RAW_QUERY, String.class);
+        if (queryString == null) {
+            queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
+        }
         if (queryString == null) {
             queryString = endpoint.getHttpUri().getRawQuery();
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/10d50677/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBridgeEndpointTest.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBridgeEndpointTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBridgeEndpointTest.java
index 862091b..5ecb67f 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBridgeEndpointTest.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBridgeEndpointTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.http4;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.component.http4.handler.BasicRawQueryValidationHandler;
 import org.apache.camel.component.http4.handler.BasicValidationHandler;
 import org.apache.http.impl.bootstrap.HttpServer;
 import org.apache.http.impl.bootstrap.ServerBootstrap;
@@ -42,7 +43,8 @@ public class HttpBridgeEndpointTest extends BaseHttpTest {
                 setResponseFactory(getHttpResponseFactory()).
                 setExpectationVerifier(getHttpExpectationVerifier()).
                 setSslContext(getSSLContext()).
-                registerHandler("/", new BasicValidationHandler("GET", null, null, getExpectedContent())).create();
+                registerHandler("/", new BasicValidationHandler("GET", null, null, getExpectedContent())).
+                registerHandler("/query", new BasicRawQueryValidationHandler("GET", "x=%3B", null, getExpectedContent())).create();
         localServer.start();
 
         super.setUp();
@@ -79,4 +81,29 @@ public class HttpBridgeEndpointTest extends BaseHttpTest {
 
         assertExchange(exchange);
     }
+
+    @Test
+    public void bridgeEndpointWithQuery() throws Exception {
+        Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/query?bridgeEndpoint=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_URI, "http://host:8080/");
+                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "x=%3B");
+            }
+        });
+
+        assertExchange(exchange);
+    }
+
+    @Test
+    public void bridgeEndpointWithRawQueryAndQuery() throws Exception {
+        Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/query?bridgeEndpoint=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_URI, "http://host:8080/");
+                exchange.getIn().setHeader(Exchange.HTTP_RAW_QUERY, "x=%3B");
+                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "x=;");
+            }
+        });
+
+        assertExchange(exchange);
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/10d50677/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicRawQueryValidationHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicRawQueryValidationHandler.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicRawQueryValidationHandler.java
new file mode 100644
index 0000000..e482188
--- /dev/null
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicRawQueryValidationHandler.java
@@ -0,0 +1,45 @@
+/**
+ * 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.http4.handler;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.http.HttpRequest;
+
+/**
+ * Similar to {@link BasicValidationHandler} but validates the raw query instead.
+ */
+public class BasicRawQueryValidationHandler extends BasicValidationHandler {
+
+    public BasicRawQueryValidationHandler(String expectedMethod, String expectedQuery, Object expectedContent, String responseContent) {
+        super(expectedMethod, expectedQuery, expectedContent, responseContent);
+    }
+
+    protected boolean validateQuery(HttpRequest request) throws IOException {
+        try {
+            String query = new URI(request.getRequestLine().getUri()).getRawQuery();
+            if (expectedQuery != null && !expectedQuery.equals(query)) {
+                return false;
+            }
+        } catch (URISyntaxException e) {
+            throw new IOException(e);
+        }
+        return true;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/10d50677/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicValidationHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicValidationHandler.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicValidationHandler.java
index a89955f..70c1ce9 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicValidationHandler.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/handler/BasicValidationHandler.java
@@ -58,14 +58,9 @@ public class BasicValidationHandler implements HttpRequestHandler {
             return;
         }
 
-        try {
-            String query = new URI(request.getRequestLine().getUri()).getQuery();            
-            if (expectedQuery != null && !expectedQuery.equals(query)) {
-                response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
-                return;
-            }
-        } catch (URISyntaxException e) {
-            throw new IOException(e);
+        if (!validateQuery(request)) {
+            response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
+            return;
         }
 
         if (expectedContent != null) {
@@ -84,4 +79,15 @@ public class BasicValidationHandler implements HttpRequestHandler {
         }
     }
 
+    protected boolean validateQuery(HttpRequest request) throws IOException {
+        try {
+            String query = new URI(request.getRequestLine().getUri()).getQuery();
+            if (expectedQuery != null && !expectedQuery.equals(query)) {
+                return false;
+            }
+        } catch (URISyntaxException e) {
+            throw new IOException(e);
+        }
+        return true;
+    }
 }
\ No newline at end of file