camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1074153 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/util/ components/camel-http/src/main/java/org/apache/camel/component/http/ components/camel-http/src/main/java/org/apache/...
Date Thu, 24 Feb 2011 13:57:41 GMT
Author: davsclaus
Date: Thu Feb 24 13:57:40 2011
New Revision: 1074153

URL: http://svn.apache.org/viewvc?rev=1074153&view=rev
Log:
CAMEL-3715: Fixed double encoding of uri with http/http4 producers.

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java
    camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
    camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
    camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpQueryTest.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java Thu Feb 24
13:57:40 2011
@@ -92,8 +92,22 @@ public final class URISupport {
      * Creates a URI with the given query
      */
     public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException
{
-        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
uri.getPath(),
-                       query, uri.getFragment());
+        ObjectHelper.notNull(uri, "uri");
+
+        // assemble string as new uri and replace parameters with the query instead
+        String s = uri.toString();
+        String before = ObjectHelper.before(s, "?");
+        if (before != null) {
+            s = before;
+        }
+        if (query != null) {
+            s = s + "?" + query;
+        }
+        if (uri.getFragment() != null) {
+            s = s + "#" + uri.getFragment();
+        }
+
+        return new URI(s);
     }
 
     public static String stripPrefix(String value, String prefix) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java Thu Feb
24 13:57:40 2011
@@ -18,6 +18,7 @@ package org.apache.camel.util;
 
 import java.net.URI;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.camel.ContextTestSupport;
@@ -125,4 +126,23 @@ public class URISupportTest extends Cont
         assertEquals("123", params.get("bar"));
     }
 
+    public void testCreateRemainingURIEncoding() throws Exception {
+        // the uri is already encoded, but we create a new one with new query parameters
+        String uri = "http://localhost:23271/myapp/mytest?columns=name%2Ctotalsens%2Cupsens&username=apiuser";
+
+        // these are the parameters which is tricky to encode
+        Map map = new LinkedHashMap();
+        map.put("foo", "abc def");
+        map.put("bar", "123,456");
+        map.put("name", "S\u00F8ren"); // danish letter
+
+        System.out.println(map.get("name"));
+
+        // create new uri with the parameters
+        URI out = URISupport.createRemainingURI(new URI(uri), map);
+        assertNotNull(out);
+        assertEquals("http://localhost:23271/myapp/mytest?foo=abc+def&bar=123%2C456&name=S%C3%B8ren",
out.toString());
+        assertEquals("http://localhost:23271/myapp/mytest?foo=abc+def&bar=123%2C456&name=S%C3%B8ren",
out.toASCIIString());
+    }
+
 }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
Thu Feb 24 13:57:40 2011
@@ -232,7 +232,7 @@ public class HttpComponent extends Heade
         String part = httpUri.getSchemeSpecificPart();
         if (part != null) {
             part = part.toLowerCase();
-            if (part.startsWith("//http//") || part.startsWith("//https//")) {
+            if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://")
|| part.startsWith("//https://")) {
                 throw new ResolveEndpointFailedException(uri,
                         "The uri part is not configured correctly. You have duplicated the
http(s) protocol.");
             }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java
Thu Feb 24 13:57:40 2011
@@ -26,7 +26,7 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.component.http.HttpConstants;
 import org.apache.camel.component.http.HttpConverter;
 import org.apache.camel.component.http.HttpEndpoint;
@@ -156,6 +156,13 @@ public final class HttpHelper {
             uri = endpoint.getHttpUri().toASCIIString();
         }
 
+        // resolve placeholders in uri
+        try {
+            uri = exchange.getContext().resolvePropertyPlaceholders(uri);
+        } catch (Exception e) {
+            throw new RuntimeExchangeException("Cannot resolve property placeholders with
uri: " + uri, exchange, e);
+        }
+
         // append HTTP_PATH to HTTP_URI if it is provided in the header
         String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
         if (path != null) {
@@ -179,11 +186,10 @@ public final class HttpHelper {
                             path = path.substring(1);
                         }
                     } else {
-                        throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH
header, due to: cannot find the right HTTP_BASE_URI");
+                        throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH
header, due to: cannot find the right HTTP_BASE_URI", exchange);
                     }
                 } catch (Throwable t) {
-                    throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH
header, due to: "
-                                                    + t.getMessage(), t);
+                    throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH
header, due to: " + t.getMessage(), exchange, t);
                 }
 
             }

Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
(original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
Thu Feb 24 13:57:40 2011
@@ -183,7 +183,7 @@ public class HttpComponent extends Heade
         String part = httpUri.getSchemeSpecificPart();
         if (part != null) {
             part = part.toLowerCase();
-            if (part.startsWith("//http//") || part.startsWith("//https//")) {
+            if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://")
|| part.startsWith("//https://")) {
                 throw new ResolveEndpointFailedException(uri,
                         "The uri part is not configured correctly. You have duplicated the
http(s) protocol.");
             }

Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
(original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
Thu Feb 24 13:57:40 2011
@@ -26,7 +26,7 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.component.http4.HttpConstants;
 import org.apache.camel.component.http4.HttpConverter;
 import org.apache.camel.component.http4.HttpEndpoint;
@@ -158,6 +158,13 @@ public final class HttpHelper {
             uri = endpoint.getHttpUri().toASCIIString();
         }
 
+        // resolve placeholders in uri
+        try {
+            uri = exchange.getContext().resolvePropertyPlaceholders(uri);
+        } catch (Exception e) {
+            throw new RuntimeExchangeException("Cannot resolve property placeholders with
uri: " + uri, exchange, e);
+        }
+
         // append HTTP_PATH to HTTP_URI if it is provided in the header
         String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
         if (path != null) {
@@ -181,11 +188,10 @@ public final class HttpHelper {
                             path = path.substring(1);
                         }
                     } else {
-                        throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH
header, due to: cannot find the right HTTP_BASE_URI");
+                        throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH
header, due to: cannot find the right HTTP_BASE_URI", exchange);
                     }
                 } catch (Throwable t) {
-                    throw new RuntimeCamelException("Cannot analyze the Exchange.HTTP_PATH
header, due to: "
-                            + t.getMessage(), t);
+                    throw new RuntimeExchangeException("Cannot analyze the Exchange.HTTP_PATH
header, due to: " + t.getMessage(), exchange, t);
                 }
 
             }

Modified: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpQueryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpQueryTest.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpQueryTest.java
(original)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpQueryTest.java
Thu Feb 24 13:57:40 2011
@@ -22,7 +22,6 @@ import org.apache.camel.component.http4.
 import org.apache.http.localserver.LocalTestServer;
 import org.junit.Test;
 
-
 /**
  *
  * @version 
@@ -54,7 +53,6 @@ public class HttpQueryTest extends BaseH
     public void httpQueryWithEscapedCharacter() throws Exception {
         Exchange exchange = template.request("http4://" + getHostName() + ":" + getPort()
+ "/test/?my=%40%20camel", new Processor() {
             public void process(Exchange exchange) throws Exception {
-                
             }
         });
 
@@ -64,6 +62,6 @@ public class HttpQueryTest extends BaseH
     @Override
     protected void registerHandler(LocalTestServer server) {
         server.register("/", new BasicValidationHandler("GET", "hl=en&q=camel", null,
getExpectedContent()));
-        server.register("/test/", new BasicValidationHandler("GET", "my=%40+camel", null,
getExpectedContent()));
+        server.register("/test/", new BasicValidationHandler("GET", "my=@+camel", null, getExpectedContent()));
     }
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java?rev=1074153&r1=1074152&r2=1074153&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
(original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
Thu Feb 24 13:57:40 2011
@@ -66,13 +66,13 @@ public class HttpClientRouteTest extends
         assertTrue("Should be more than one header but was: " + headers, headers.size() >
0);
         
         // should get the Content-Length
-        assertNotNull("Should get the content-lenghth ", headers.get("Content-Length"));
+        assertNotNull("Should get the content-length", headers.get("Content-Length"));
     }
     
     @Test
     public void testHttpRouteWithQuery() throws Exception {
         MockEndpoint mockEndpoint = getMockEndpoint("mock:a");
-        mockEndpoint.expectedBodiesReceived("%40 query");        
+        mockEndpoint.expectedBodiesReceived("@ query");
         
         template.sendBody("direct:start3", null);
         mockEndpoint.assertIsSatisfied();        

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java?rev=1074153&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java
(added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java
Thu Feb 24 13:57:40 2011
@@ -0,0 +1,68 @@
+/**
+ * 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.jetty;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class HttpEndpointUriEncodingIssueTest extends BaseJettyTest {
+
+    @Test
+    public void testEndpointUriEncodingIssue() throws Exception {
+        String uri = "http://localhost:{{port}}/myapp/mytest?columns=totalsens,upsens&username=apiuser";
+        String out = template.requestBody(uri, null, String.class);
+
+        assertEquals("We got totalsens,upsens columns", out);
+    }
+
+    @Test
+    public void testEndpointUriWithDanishCharEncodingIssue() throws Exception {
+        String uri = "http://localhost:{{port}}/myapp/mytest?columns=claus,s\u00F8ren&username=apiuser";
+        String out = template.requestBody(uri, null, String.class);
+
+        assertEquals("We got claus,s\u00F8ren columns", out);
+    }
+
+    @Test
+    public void testEndpointHeaderUriEncodingIssue() throws Exception {
+        String uri = "http://localhost:{{port}}/myapp/mytest?columns=totalsens,upsens&username=apiuser";
+        String out = template.requestBodyAndHeader("http://localhost/dummy", null, Exchange.HTTP_URI,
uri, String.class);
+
+        assertEquals("We got totalsens,upsens columns", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("jetty:http://localhost:{{port}}/myapp/mytest").process(new Processor()
{
+                    public void process(Exchange exchange) throws Exception {
+                        String columns = exchange.getIn().getHeader("columns", String.class);
+                        exchange.getOut().setBody("We got " + columns + " columns");
+                    }
+                });
+            }
+        };
+    }
+
+}



Mime
View raw message