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 2009/05/26 15:14:50 UTC

svn commit: r778689 - in /camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http: CompositeHttpConfigurer.java HttpComponent.java ProxyHttpClientConfigurer.java

Author: ningjiang
Date: Tue May 26 13:14:50 2009
New Revision: 778689

URL: http://svn.apache.org/viewvc?rev=778689&view=rev
Log:
CAMEL-1599 applied patch with thanks to Jonathan, also introduced a CompositeHttpConfigurer to hold the multi-configurer

Added:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java   (with props)
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java   (with props)
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java

Added: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java?rev=778689&view=auto
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java (added)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java Tue May 26 13:14:50 2009
@@ -0,0 +1,56 @@
+/**
+ * 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.http;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.httpclient.HttpClient;
+
+public class CompositeHttpConfigurer implements HttpClientConfigurer {
+    
+    private List<HttpClientConfigurer> configurers = new ArrayList<HttpClientConfigurer>();
+    
+    public void addConfigurer(HttpClientConfigurer configurer) {
+        if (configurer != null) {
+            configurers.add(configurer);
+        }
+    }
+    
+    public void removeConfigurer(HttpClientConfigurer configurer) {
+        configurers.remove(configurer);
+    }
+
+    public void configureHttpClient(HttpClient client) {
+        for(HttpClientConfigurer configurer : configurers) {
+            configurer.configureHttpClient(client);
+        }        
+    }
+    
+    public static CompositeHttpConfigurer combineConfigurers(HttpClientConfigurer oldConfigurer, HttpClientConfigurer newConfigurer) {
+        if (oldConfigurer instanceof CompositeHttpConfigurer) {
+            ((CompositeHttpConfigurer)oldConfigurer).addConfigurer(newConfigurer);
+            return (CompositeHttpConfigurer)oldConfigurer;
+        } else {
+            CompositeHttpConfigurer answer = new CompositeHttpConfigurer();
+            answer.addConfigurer(newConfigurer);
+            answer.addConfigurer(oldConfigurer);
+            return answer;
+        }
+    }
+
+}

Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=778689&r1=778688&r2=778689&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 Tue May 26 13:14:50 2009
@@ -73,19 +73,38 @@
             httpBinding = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpBinding.class);
         }
         
+        // lookup http client front configurer in the registry if provided
+        ref = getAndRemoveParameter(parameters, "httpClientConfigurerRef", String.class);
+        if (ref != null) {
+            httpClientConfigurer = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpClientConfigurer.class);
+        }
+        
         // check the user name and password for basic authentication
         String username = getAndRemoveParameter(parameters, "username", String.class);
         String password = getAndRemoveParameter(parameters, "password", String.class);
         if (username != null && password != null) {
-            httpClientConfigurer = new BasicAuthenticationHttpClientConfigurer(username, password);
+            
+            httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
+                httpClientConfigurer, 
+                new BasicAuthenticationHttpClientConfigurer(username, password));
         }
         
-        // lookup http client front configurer in the registry if provided
-        ref = getAndRemoveParameter(parameters, "httpClientConfigurerRef", String.class);
-        if (ref != null) {
-            httpClientConfigurer = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpClientConfigurer.class);
+        // check the proxy details for proxy configuration
+        String host = getAndRemoveParameter(parameters, "proxyHost", String.class);
+        Integer port = getAndRemoveParameter(parameters, "proxyPort", Integer.class);
+        if (host != null && port != null) {
+        	String proxyUsername = getAndRemoveParameter(parameters, "proxyUsername", String.class);
+            String proxyPassword = getAndRemoveParameter(parameters, "proxyPassword", String.class);
+        	if(proxyUsername!=null && proxyPassword!=null){
+        		httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
+        		    httpClientConfigurer,
+        		    new ProxyHttpClientConfigurer(host, port, proxyUsername, proxyPassword));
+        	} else {
+        		httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
+        		    httpClientConfigurer,
+        		    new ProxyHttpClientConfigurer(host, port));
+        	}
         }
-
         matchOnUriPrefix = Boolean.parseBoolean(getAndRemoveParameter(parameters, "matchOnUriPrefix", String.class));
     }
     
@@ -128,8 +147,8 @@
         }
         setProperties(endpoint, parameters);
         return endpoint;
-    }
-
+    }    
+   
     @Override
     protected boolean useIntrospectionOnEndpoint() {
         return false;

Added: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java?rev=778689&view=auto
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java (added)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java Tue May 26 13:14:50 2009
@@ -0,0 +1,56 @@
+/**
+ * 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.http;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+
+/**
+ * Strategy for configuring the HttpClient with a proxy
+ *
+ * @version 
+ */
+public class ProxyHttpClientConfigurer implements HttpClientConfigurer {
+
+    private final String host;
+    private final Integer port;
+    
+    private final String username;
+    private final String password;
+    
+    public ProxyHttpClientConfigurer(String host, Integer port) {
+        this(host, port, null, null);
+    }
+    
+    public ProxyHttpClientConfigurer(String host, Integer port, String username, String password) {
+        this.host = host;
+        this.port = port;
+        this.username = username;
+        this.password = password;
+    }
+	
+	public void configureHttpClient(HttpClient client) {
+		client.getHostConfiguration().setProxy(host, port);
+		
+		if(username!=null && password!=null){
+			Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
+			client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
+		}
+	}
+}

Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date