You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/01/29 17:40:25 UTC

camel git commit: CAMEL-9542 - Allow HttpClient configuration (proxy, connection pooling, etc) for camel-geocoder component

Repository: camel
Updated Branches:
  refs/heads/master 4625c5b37 -> 2210dd5ce


CAMEL-9542 - Allow HttpClient configuration (proxy, connection pooling, etc) for camel-geocoder component


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

Branch: refs/heads/master
Commit: 2210dd5ce6b15ab6eed4aaa8514aca04fb1f0ee0
Parents: 4625c5b
Author: lburgazzoli <lb...@gmail.com>
Authored: Thu Jan 28 15:04:51 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Jan 29 17:38:59 2016 +0100

----------------------------------------------------------------------
 .../component/geocoder/GeoCoderEndpoint.java    | 202 +++++++++++++++++++
 .../component/geocoder/GeoCoderProducer.java    |   6 +-
 .../AuthenticationHttpClientConfigurer.java     |  54 +++++
 .../geocoder/http/AuthenticationMethod.java     |  27 +++
 .../geocoder/http/CompositeHttpConfigurer.java  |  50 +++++
 .../geocoder/http/HttpClientConfigurer.java     |  35 ++++
 .../component/geocoder/GeoCoderProxyTest.java   |  43 ++++
 7 files changed, 412 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
index bf65e7b..63d5262 100644
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderEndpoint.java
@@ -16,13 +16,25 @@
  */
 package org.apache.camel.component.geocoder;
 
+import java.security.InvalidKeyException;
+
+import com.google.code.geocoder.AdvancedGeoCoder;
+import com.google.code.geocoder.Geocoder;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.component.geocoder.http.AuthenticationHttpClientConfigurer;
+import org.apache.camel.component.geocoder.http.AuthenticationMethod;
+import org.apache.camel.component.geocoder.http.CompositeHttpConfigurer;
+import org.apache.camel.component.geocoder.http.HttpClientConfigurer;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpConnectionManager;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 
 /**
  * The geocoder component is used for looking up geocodes (latitude and longitude) for a given address, or reverse lookup.
@@ -42,6 +54,24 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
     private String clientKey;
     @UriParam
     private boolean headersOnly;
+    @UriParam
+    private String proxyHost;
+    @UriParam
+    private Integer proxyPort;
+    @UriParam
+    private String proxyAuthMethod;
+    @UriParam
+    private String proxyAuthUsername;
+    @UriParam
+    private String proxyAuthPassword;
+    @UriParam
+    private String proxyAuthDomain;
+    @UriParam
+    private String proxyAuthHost;
+    @UriParam
+    private HttpClientConfigurer httpClientConfigurer;
+    @UriParam
+    private HttpConnectionManager httpConnectionManager;
 
     public GeoCoderEndpoint() {
     }
@@ -127,4 +157,176 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
     public void setClientKey(String clientKey) {
         this.clientKey = clientKey;
     }
+
+    /**
+     * The proxy host name
+     */
+    public void setProxyHost(String proxyHost) {
+        this.proxyHost = proxyHost;
+    }
+
+    public int getProxyPort() {
+        return proxyPort;
+    }
+
+    /**
+     * The proxy port number
+     */
+    public void setProxyPort(int proxyPort) {
+        this.proxyPort = proxyPort;
+    }
+
+
+    public String getProxyAuthMethod() {
+        return proxyAuthMethod;
+    }
+
+    /**
+     * Authentication method for proxy, either as Basic, Digest or NTLM.
+     */
+    public void setProxyAuthMethod(String proxyAuthMethod) {
+        this.proxyAuthMethod = proxyAuthMethod;
+    }
+
+    public String getProxyAuthUsername() {
+        return proxyAuthUsername;
+    }
+
+    /**
+     * Username for proxy authentication
+     */
+    public void setProxyAuthUsername(String proxyAuthUsername) {
+        this.proxyAuthUsername = proxyAuthUsername;
+    }
+
+    public String getProxyAuthPassword() {
+        return proxyAuthPassword;
+    }
+
+    /**
+     * Password for proxy authentication
+     */
+    public void setProxyAuthPassword(String proxyAuthPassword) {
+        this.proxyAuthPassword = proxyAuthPassword;
+    }
+
+    public String getProxyAuthDomain() {
+        return proxyAuthDomain;
+    }
+
+    /**
+     * Domain for proxy NTML authentication
+     */
+    public void setProxyAuthDomain(String proxyAuthDomain) {
+        this.proxyAuthDomain = proxyAuthDomain;
+    }
+
+    public String getProxyAuthHost() {
+        return proxyAuthHost;
+    }
+
+    /**
+     * Optional host for proxy NTML authentication
+     */
+    public void setProxyAuthHost(String proxyAuthHost) {
+        this.proxyAuthHost = proxyAuthHost;
+    }
+
+    public HttpClientConfigurer getHttpClientConfigurer() {
+        return httpClientConfigurer;
+    }
+
+    /**
+     * Register a custom configuration strategy for new {@link HttpClient} instances
+     * created by producers or consumers such as to configure authentication mechanisms etc
+     *
+     * @param httpClientConfigurer the strategy for configuring new {@link HttpClient} instances
+     */
+    public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
+        this.httpClientConfigurer = httpClientConfigurer;
+    }
+
+    public HttpConnectionManager getHttpConnectionManager() {
+        return httpConnectionManager;
+    }
+
+    /**
+     * To use a custom HttpConnectionManager to manage connections
+     */
+    public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
+        this.httpConnectionManager = httpConnectionManager;
+    }
+
+    Geocoder createGeocoder() throws InvalidKeyException {
+        HttpConnectionManager connectionManager = this.httpConnectionManager;
+        if (connectionManager == null) {
+            connectionManager = new MultiThreadedHttpConnectionManager();
+        }
+
+        HttpClient httpClient = new HttpClient(connectionManager);
+        if (proxyHost != null && proxyPort != null) {
+            httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
+        }
+
+        // validate that if proxy auth username is given then the proxy auth method is also provided
+        if (proxyAuthUsername != null && proxyAuthMethod == null) {
+            throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
+        }
+
+        CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
+        if (proxyAuthMethod != null) {
+            configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost);
+        }
+        if (httpClientConfigurer != null) {
+            configurer.addConfigurer(httpClientConfigurer);
+        }
+
+        configurer.configureHttpClient(httpClient);
+
+        Geocoder geocoder;
+        if (clientId != null) {
+            geocoder = new AdvancedGeoCoder(httpClient, clientId, clientKey);
+        } else {
+            geocoder = new AdvancedGeoCoder(httpClient);
+        }
+
+        return geocoder;
+    }
+
+    /**
+     * Configures the proxy authentication method to be used
+     *
+     * @return configurer to used
+     */
+    protected CompositeHttpConfigurer configureProxyAuth(CompositeHttpConfigurer configurer,
+            String authMethod, String username, String password, String domain, String host) {
+
+        // no proxy auth is in use
+        if (username == null && authMethod == null) {
+            return configurer;
+        }
+
+        // validate mandatory options given
+        if (username != null && authMethod == null) {
+            throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
+        }
+
+        ObjectHelper.notNull(authMethod, "proxyAuthMethod");
+        ObjectHelper.notNull(username, "proxyAuthUsername");
+        ObjectHelper.notNull(password, "proxyAuthPassword");
+
+        AuthenticationMethod auth = getCamelContext().getTypeConverter().convertTo(AuthenticationMethod.class, authMethod);
+
+        if (auth == AuthenticationMethod.Basic || auth == AuthenticationMethod.Digest) {
+            configurer.addConfigurer(AuthenticationHttpClientConfigurer.basicAutenticationConfigurer(true, username, password));
+            return configurer;
+        } else if (auth == AuthenticationMethod.NTLM) {
+            // domain is mandatory for NTML
+            ObjectHelper.notNull(domain, "proxyAuthDomain");
+            configurer.addConfigurer(AuthenticationHttpClientConfigurer.ntlmAutenticationConfigurer(true, username, password, domain, host));
+            return configurer;
+        }
+
+        throw new IllegalArgumentException("Unknown proxyAuthMethod " + authMethod);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
index 4023586..403d01a 100644
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/GeoCoderProducer.java
@@ -197,11 +197,7 @@ public class GeoCoderProducer extends DefaultProducer {
 
     @Override
     protected void doStart() throws Exception {
-        if (endpoint.getClientId() != null) {
-            geocoder = new Geocoder(endpoint.getClientId(), endpoint.getClientKey());
-        } else {
-            geocoder = new Geocoder();
-        }
+        geocoder = endpoint.createGeocoder();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationHttpClientConfigurer.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationHttpClientConfigurer.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationHttpClientConfigurer.java
new file mode 100644
index 0000000..6ee7890
--- /dev/null
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationHttpClientConfigurer.java
@@ -0,0 +1,54 @@
+/**
+ * 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.geocoder.http;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.NTCredentials;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+
+
+public final class AuthenticationHttpClientConfigurer implements HttpClientConfigurer {
+
+    private final boolean proxy;
+    private final Credentials credentials;
+
+    private AuthenticationHttpClientConfigurer(boolean proxy, Credentials credentials) {
+        this.proxy = proxy;
+        this.credentials = credentials;
+    }
+
+    @Override
+    public HttpClient configureHttpClient(HttpClient client) {
+        if (proxy) {
+            client.getState().setProxyCredentials(AuthScope.ANY, this.credentials);
+        } else {
+            client.getState().setCredentials(AuthScope.ANY, this.credentials);
+        }
+
+        return client;
+    }
+
+    public static HttpClientConfigurer basicAutenticationConfigurer(boolean proxy, String user, String pwd) {
+        return new AuthenticationHttpClientConfigurer(proxy, new UsernamePasswordCredentials(user, pwd));
+    }
+
+    public static HttpClientConfigurer ntlmAutenticationConfigurer(boolean proxy, String user, String pwd, String domain, String host) {
+        return new AuthenticationHttpClientConfigurer(proxy, new NTCredentials(user, pwd, host, domain));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationMethod.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationMethod.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationMethod.java
new file mode 100644
index 0000000..0e07b4a
--- /dev/null
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationMethod.java
@@ -0,0 +1,27 @@
+/**
+ * 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.geocoder.http;
+
+/**
+ * Authentication policy
+ *
+ * @version 
+ */
+public enum AuthenticationMethod {
+
+    Basic, Digest, NTLM
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/CompositeHttpConfigurer.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/CompositeHttpConfigurer.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/CompositeHttpConfigurer.java
new file mode 100644
index 0000000..182c192
--- /dev/null
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/CompositeHttpConfigurer.java
@@ -0,0 +1,50 @@
+/**
+ * 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.geocoder.http;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.httpclient.HttpClient;
+
+public class CompositeHttpConfigurer implements HttpClientConfigurer {
+    
+    private final List<HttpClientConfigurer> configurers = new ArrayList<>();
+    
+    public void addConfigurer(HttpClientConfigurer configurer) {
+        if (configurer != null) {
+            configurers.add(configurer);
+        }
+    }
+    
+    public void removeConfigurer(HttpClientConfigurer configurer) {
+        configurers.remove(configurer);
+    }
+
+    public HttpClient configureHttpClient(HttpClient client) {
+        for (HttpClientConfigurer configurer : configurers) {
+            configurer.configureHttpClient(client);
+        }
+
+        return client;
+    }
+
+    public List<HttpClientConfigurer> getConfigurers() {
+        return Collections.unmodifiableList(configurers);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/HttpClientConfigurer.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/HttpClientConfigurer.java b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/HttpClientConfigurer.java
new file mode 100644
index 0000000..0d00269
--- /dev/null
+++ b/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/HttpClientConfigurer.java
@@ -0,0 +1,35 @@
+/**
+ * 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.geocoder.http;
+
+import org.apache.commons.httpclient.HttpClient;
+
+/**
+ * A pluggable strategy for configuring the HttpClient used by this component
+ *
+ * @version 
+ */
+public interface HttpClientConfigurer {
+
+    /**
+     * Configure the HttpClient such as setting the authentication or proxying details
+     *
+     * @param client the client
+     * @return the client
+     */
+    HttpClient configureHttpClient(HttpClient client);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2210dd5c/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderProxyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderProxyTest.java b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderProxyTest.java
new file mode 100644
index 0000000..0513327
--- /dev/null
+++ b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderProxyTest.java
@@ -0,0 +1,43 @@
+/**
+ * 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.geocoder;
+
+import com.google.code.geocoder.Geocoder;
+import com.google.code.geocoder.model.GeocodeResponse;
+import com.google.code.geocoder.model.GeocoderRequest;
+import com.google.code.geocoder.model.LatLng;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Test;
+
+@Ignore("This test should be executed manually")
+public class GeoCoderProxyTest extends CamelTestSupport {
+
+    @Test
+    public void testGeoCoder() throws Exception {
+        GeoCoderEndpoint endpoint = context.getEndpoint(
+            "geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=3128&proxyAuthMethod=Basic&proxyAuthUsername=proxy&proxyAuthPassword=proxy",
+            GeoCoderEndpoint.class);
+
+        Geocoder geocoder = endpoint.createGeocoder();
+        GeocoderRequest req = new GeocoderRequest();
+        req.setLocation(new LatLng("45.4643", "9.1895"));
+        GeocodeResponse res = geocoder.geocode(req);
+
+        log.info("Response {} ", res);
+    }
+}