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 2010/07/16 06:40:43 UTC

svn commit: r964673 - in /camel/trunk/components/camel-http4/src: main/java/org/apache/camel/component/http4/ test/java/org/apache/camel/component/http4/

Author: ningjiang
Date: Fri Jul 16 04:40:42 2010
New Revision: 964673

URL: http://svn.apache.org/viewvc?rev=964673&view=rev
Log:
CAMEL-2945, CAMEL-2950 merged the change of camel-http into camel-http4

Added:
    camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java   (with props)
Modified:
    camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
    camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyTest.java
    camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java

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=964673&r1=964672&r2=964673&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 Fri Jul 16 04:40:42 2010
@@ -17,6 +17,7 @@
 package org.apache.camel.component.http4;
 
 import java.net.URI;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.camel.Endpoint;
@@ -131,6 +132,11 @@ public class HttpComponent extends Heade
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        String addressUri = uri;
+        if (!uri.startsWith("http4:") && !uri.startsWith("https4:")) {
+            addressUri = remaining;
+        }
+        Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
         // http client can be configured from URI options
         HttpParams clientParams = configureHttpParams(parameters);
 
@@ -149,11 +155,10 @@ public class HttpComponent extends Heade
         validateParameters(uri, parameters, "httpClient.");
         // create the configurer to use for this endpoint
         HttpClientConfigurer configurer = createHttpClientConfigurer(parameters);
-
+        URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), CastUtils.cast(httpClientParameters));
         // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
-        URI httpUri = URISupport.createRemainingURI(new URI(uri), CastUtils.cast(parameters));
-        uri = httpUri.toString();
-
+        URI httpUri = URISupport.createRemainingURI(new URI(addressUri), CastUtils.cast(parameters));
+        
         // validate http uri that end-user did not duplicate the http part that can be a common error
         String part = httpUri.getSchemeSpecificPart();
         if (part != null) {
@@ -170,7 +175,7 @@ public class HttpComponent extends Heade
         registerPort(secure, port);
 
         // create the endpoint
-        HttpEndpoint endpoint = new HttpEndpoint(uri, this, httpUri, clientParams, clientConnectionManager, configurer);
+        HttpEndpoint endpoint = new HttpEndpoint(endpointUri.toString(), this, httpUri, clientParams, clientConnectionManager, configurer);
         setEndpointHeaderFilterStrategy(endpoint);
 
         // prefer to use endpoint configured over component configured
@@ -202,9 +207,9 @@ public class HttpComponent extends Heade
     private static int getPort(URI uri) {
         int port = uri.getPort();
         if (port < 0) {
-            if ("http4".equals(uri.getScheme())) {
+            if ("http4".equals(uri.getScheme()) || "http".equals(uri.getScheme())) {
                 port = 80;
-            } else if ("https4".equals(uri.getScheme())) {
+            } else if ("https4".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
                 port = 443;
             } else {
                 throw new IllegalArgumentException("Unknown scheme, cannot determine port number for uri: " + uri);

Modified: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyTest.java?rev=964673&r1=964672&r2=964673&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyTest.java (original)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyTest.java Fri Jul 16 04:40:42 2010
@@ -34,13 +34,21 @@ import org.junit.Test;
  * @version $Revision$
  */
 public class HttpBodyTest extends BaseHttpTest {
-
+    private String protocolString = "http4://";
     // default content encoding of the local test server
     private String charset = "ISO-8859-1";
+    
+    public String getProtocolString() {
+        return protocolString;
+    }
+    
+    public void setProtocolString(String protocol) {
+        protocolString = protocol;
+    }
 
     @Test
     public void httpPostWithStringBody() throws Exception {
-        Exchange exchange = template.request("http4://" + getHostName() + ":" + getPort() + "/", new Processor() {
+        Exchange exchange = template.request(getProtocolString() + getHostName() + ":" + getPort() + "/", new Processor() {
             public void process(Exchange exchange) throws Exception {
                 // without this property, camel use the os default encoding
                 // to create the byte array for the StringRequestEntity
@@ -54,7 +62,7 @@ public class HttpBodyTest extends BaseHt
 
     @Test
     public void httpPostWithByteArrayBody() throws Exception {
-        Exchange exchange = template.request("http4://" + getHostName() + ":" + getPort() + "/", new Processor() {
+        Exchange exchange = template.request(getProtocolString() + getHostName() + ":" + getPort() + "/", new Processor() {
             public void process(Exchange exchange) throws Exception {
                 exchange.getIn().setBody(getBody().getBytes(charset));
             }
@@ -65,7 +73,7 @@ public class HttpBodyTest extends BaseHt
 
     @Test
     public void httpPostWithInputStreamBody() throws Exception {
-        Exchange exchange = template.request("http4://" + getHostName() + ":" + getPort() + "/", new Processor() {
+        Exchange exchange = template.request(getProtocolString() + getHostName() + ":" + getPort() + "/", new Processor() {
             public void process(Exchange exchange) throws Exception {
                 exchange.getIn().setBody(new ByteArrayInputStream(getBody().getBytes(charset)));
             }
@@ -80,7 +88,7 @@ public class HttpBodyTest extends BaseHt
         expectedHeaders.put("Content-Type", "image/jpeg");
         localServer.register("/", new HeaderValidationHandler("POST", null, null, getExpectedContent(), expectedHeaders));
 
-        Exchange exchange = template.send("http4://" + getHostName() + ":" + getPort() + "/", new Processor() {
+        Exchange exchange = template.send(getProtocolString() + getHostName() + ":" + getPort() + "/", new Processor() {
             public void process(Exchange exchange) throws Exception {
                 exchange.getIn().setBody(new File("src/test/data/logo.jpeg"));
                 exchange.getIn().setHeader("Content-Type", "image/jpeg");

Added: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java?rev=964673&view=auto
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java (added)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java Fri Jul 16 04:40:42 2010
@@ -0,0 +1,38 @@
+/**
+ * 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 org.apache.camel.CamelContext;
+import org.junit.Before;
+
+public class HttpBodyWithOtherProtocalNameTest extends HttpBodyTest {
+    
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        setProtocolString("newHttp://http://");
+    }
+    
+    public CamelContext createCamelContext() throws Exception {
+        CamelContext answer = super.createCamelContext();
+        // register the a new HttpComponent with different protocol name
+        answer.addComponent("newHttp", new HttpComponent());
+        return answer;
+    }
+
+}

Propchange: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java?rev=964673&r1=964672&r2=964673&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java (original)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java Fri Jul 16 04:40:42 2010
@@ -29,6 +29,7 @@ import org.apache.commons.codec.DecoderE
 import org.apache.commons.codec.binary.Base64;
 import org.apache.http.Header;
 import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
@@ -36,6 +37,8 @@ import org.apache.http.HttpResponseInter
 import org.apache.http.HttpStatus;
 import org.apache.http.ProtocolException;
 import org.apache.http.auth.AUTH;
+import org.apache.http.client.HttpClient;
+import org.apache.http.conn.params.ConnRoutePNames;
 import org.apache.http.localserver.LocalTestServer;
 import org.apache.http.protocol.BasicHttpProcessor;
 import org.apache.http.protocol.HTTP;
@@ -78,6 +81,30 @@ public class HttpProxyServerTest extends
 
         super.tearDown();
     }
+    
+    @Test
+    public void testDifferentHttpProxyConfigured() throws Exception {
+        HttpEndpoint http1 = context.getEndpoint("http4://www.google.com?proxyHost=myproxy&proxyPort=1234", HttpEndpoint.class);
+        HttpEndpoint http2 = context.getEndpoint("http4://www.google.com?proxyHost=myotherproxy&proxyPort=2345", HttpEndpoint.class);
+        HttpEndpoint http3 = context.getEndpoint("http4://www.google.com?test=parameter", HttpEndpoint.class);
+        
+        HttpClient client1 = http1.createHttpClient();
+        HttpHost proxy1 = (HttpHost)client1.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);
+        assertEquals("myproxy", proxy1.getHostName());
+        assertEquals(1234, proxy1.getPort());
+        
+        HttpClient client2 = http2.createHttpClient();
+        HttpHost proxy2 = (HttpHost)client2.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);
+        assertEquals("myotherproxy", proxy2.getHostName());
+        assertEquals(2345, proxy2.getPort());
+
+        //As the endpointUri is recreated, so the parameter could be in different place
+        assertTrue("Get a wrong endpoint uri of http1", http1.getEndpointUri().indexOf("proxyPort=1234") > 0);
+        assertTrue("Get a wrong endpoint uri of http2", http2.getEndpointUri().indexOf("proxyHost=myotherproxy") > 0);
+        assertEquals("Get a wrong endpoint uri", "http4://www.google.com?test=parameter", http3.getEndpointUri());
+       
+        assertEquals("Should get the same EndpointKey", http1.getEndpointKey(), http2.getEndpointKey());
+    }
 
     @Test
     public void httpGetWithProxyAndWithoutUser() throws Exception {