CAMEL-6078: Fixed issue in camel-http4 when using different port numbers 
together with default port number. Thanks to Rene Avontuur for the patch.


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

Branch: refs/heads/camel-2.11.x
Commit: b37d6c1bdf4a23beadf3b6a24a962c29e4059885
Parents: 7ca957f
Author: Claus Ibsen <[email protected]>
Authored: Thu May 23 11:31:17 2013 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Thu May 23 11:31:41 2013 +0200

----------------------------------------------------------------------
 .../camel/component/http4/HttpComponent.java       |   20 ++-
 .../component/http4/HttpDefaultPortNumberTest.java |  130 +++++++++++++++
 2 files changed, 147 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b37d6c1b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
 
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
index 6f9600b..9d39b8a 100644
--- 
a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
+++ 
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
@@ -226,10 +226,10 @@ public class HttpComponent extends 
HeaderFilterStrategyComponent {
         }
         // restructure uri to be based on the parameters left as we dont want 
to include the Camel internal options
         // build up the http uri
-        URI httpUri = URISupport.createRemainingURI(new URI(httpUriAddress), 
parameters);
+        URI uriHttpUriAddress = new URI(httpUriAddress);
 
         // validate http uri that end-user did not duplicate the http part 
that can be a common error
-        String part = httpUri.getSchemeSpecificPart();
+        String part = uriHttpUriAddress.getSchemeSpecificPart();
         if (part != null) {
             part = part.toLowerCase();
             if (part.startsWith("//http//") || part.startsWith("//https//") || 
part.startsWith("//http://";) || part.startsWith("//https://";)) {
@@ -237,6 +237,21 @@ public class HttpComponent extends 
HeaderFilterStrategyComponent {
                         "The uri part is not configured correctly. You have 
duplicated the http(s) protocol.");
             }
         }
+
+        // determine the portnumber (special case: default portnumber)
+        int port = getPort(uriHttpUriAddress);
+
+        // we can not change the port of an URI, we must create a new one with 
an explicit port value
+        URI httpUri = URISupport.createRemainingURI(
+                new URI(uriHttpUriAddress.getScheme(),
+                        uriHttpUriAddress.getUserInfo(),
+                        uriHttpUriAddress.getHost(),
+                        port,
+                        uriHttpUriAddress.getPath(),
+                        uriHttpUriAddress.getQuery(),
+                        uriHttpUriAddress.getFragment()),
+                        parameters);
+
         endpoint.setHttpUri(httpUri);
         if (headerFilterStrategy != null) {
             endpoint.setHeaderFilterStrategy(headerFilterStrategy);
@@ -258,7 +273,6 @@ public class HttpComponent extends 
HeaderFilterStrategyComponent {
             endpoint.setHttpContext(httpContext);
         }
         // register port on schema registry
-        int port = getPort(httpUri);
         registerPort(secure, x509HostnameVerifier, port, sslContextParameters);
 
         return endpoint;

http://git-wip-us.apache.org/repos/asf/camel/blob/b37d6c1b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpDefaultPortNumberTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpDefaultPortNumberTest.java
 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpDefaultPortNumberTest.java
new file mode 100644
index 0000000..f08cfd9
--- /dev/null
+++ 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpDefaultPortNumberTest.java
@@ -0,0 +1,130 @@
+/**
+ * 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;
+
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http4.handler.BasicValidationHandler;
+import org.apache.http.conn.HttpHostConnectException;
+import org.apache.http.localserver.LocalTestServer;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * @version
+ */
+@Ignore("We cannot run this test as default port 80 is not allows on most 
boxes")
+public class HttpDefaultPortNumberTest extends BaseHttpTest {
+
+    @Test
+    public void testHttpConnectionWithTwoRoutesAndOneWithDefaultPort() throws 
Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("http4://" + getHostName() + "/search");
+                from("direct:dummy")
+                        .to("http4://" + getHostName() + ":" + getPort() + 
"/search");
+            }
+        });
+
+        context.start();
+        Exchange exchange = template.request("direct:start", null);
+
+        // note: the default portnumber will appear in the error message
+        assertRefused(exchange, ":80");
+    }
+
+    @Test
+    public void testHttpConnectionWithTwoRoutesAndAllPortsSpecified() throws 
Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("http4://" + getHostName() + ":80/search");
+                from("direct:dummy")
+                        .to("http4://" + getHostName() + ":" + getPort() + 
"/search");
+            }
+        });
+
+        context.start();
+        Exchange exchange = template.request("direct:start", null);
+
+        //specifying the defaultportnumber helps
+        assertRefused(exchange, ":80");
+    }
+
+    @Test
+    public void testHttpConnectionRefusedStoppedServer() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("http4://" + getHostName() + "/search");
+                from("direct:dummy")
+                        .to("http4://" + getHostName() + ":" + getPort() + 
"/search");
+            }
+        });
+
+        context.start();
+        localServer.stop();
+
+        Exchange exchange = template.request("direct:start", null);
+
+        assertRefused(exchange, ":80");
+    }
+
+    @Test
+    public void testHttpConnectionRefusedRunningServer() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("http4://" + getHostName() + "/search");
+            }
+        });
+
+        context.start();
+
+        //server is runnning, but connecting to other port
+        Exchange exchange = template.request("direct:start", null);
+
+        assertRefused(exchange, ":80");
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return true;
+    }
+
+    @Override
+    protected void registerHandler(LocalTestServer server) {
+        server.register("/search", new BasicValidationHandler("GET", null, 
null, getExpectedContent()));
+    }
+
+    private void assertRefused(Exchange exchange, String portExt) {
+        Map<String, Object> headers = exchange.getOut().getHeaders();
+        //no http response:
+        assertNull(headers.get(Exchange.HTTP_RESPONSE_CODE));
+        //and got an exception:
+        assertIsInstanceOf(HttpHostConnectException.class, 
exchange.getException());
+        //with message:
+        assertEquals("Connection to http://127.0.0.1"; + portExt + " refused", 
exchange.getException().getMessage());
+    }
+}
\ No newline at end of file

Reply via email to