You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by on...@apache.org on 2018/07/10 07:02:27 UTC

[camel] 02/04: implemented proxy support for GeoCoderEndpoint, removed commons-httpclient

This is an automated email from the ASF dual-hosted git repository.

onders pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit bf61db52581ca187e8615eafee1d745a0db0d239
Author: jborza <jb...@gmail.com>
AuthorDate: Sun Jul 8 08:58:15 2018 +0200

    implemented proxy support for GeoCoderEndpoint, removed commons-httpclient
---
 components/camel-geocoder/pom.xml                  |   4 -
 .../src/main/docs/geocoder-component.adoc          |   6 +-
 .../camel/component/geocoder/GeoCoderEndpoint.java | 141 +++++----------------
 .../http/AuthenticationHttpClientConfigurer.java   |  54 --------
 .../geocoder/http/AuthenticationMethod.java        |   2 +-
 .../geocoder/http/CompositeHttpConfigurer.java     |  50 --------
 .../geocoder/http/HttpClientConfigurer.java        |  35 -----
 .../component/geocoder/GeoCoderApiKeyTestBase.java |   2 +-
 .../component/geocoder/GeoCoderProxyTest.java      |  20 ++-
 9 files changed, 50 insertions(+), 264 deletions(-)

diff --git a/components/camel-geocoder/pom.xml b/components/camel-geocoder/pom.xml
index 0302ae3..ed56b89 100644
--- a/components/camel-geocoder/pom.xml
+++ b/components/camel-geocoder/pom.xml
@@ -74,10 +74,6 @@
       <artifactId>log4j-slf4j-impl</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>commons-httpclient</groupId>
-      <artifactId>commons-httpclient</artifactId>
-    </dependency>
   </dependencies>
 
   <build>
diff --git a/components/camel-geocoder/src/main/docs/geocoder-component.adoc b/components/camel-geocoder/src/main/docs/geocoder-component.adoc
index 03e3fce..e0d2b00 100644
--- a/components/camel-geocoder/src/main/docs/geocoder-component.adoc
+++ b/components/camel-geocoder/src/main/docs/geocoder-component.adoc
@@ -70,12 +70,8 @@ with the following path and query parameters:
 | *clientKey* (producer) | Private key for Google Maps API Premium Plan |  | String
 | *headersOnly* (producer) | Whether to only enrich the Exchange with headers, and leave the body as-is. | false | boolean
 | *language* (producer) | The language in which to return results. | en | String
-| *httpClientConfigurer* (advanced) | Register a custom configuration strategy for new HttpClient instances created by producers or consumers such as to configure authentication mechanisms etc |  | HttpClientConfigurer
-| *httpConnectionManager* (advanced) | To use a custom HttpConnectionManager to manage connections |  | HttpConnectionManager
 | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
-| *proxyAuthDomain* (proxy) | Domain for proxy NTML authentication |  | String
-| *proxyAuthHost* (proxy) | Optional host for proxy NTML authentication |  | String
-| *proxyAuthMethod* (proxy) | Authentication method for proxy, either as Basic, Digest or NTLM. |  | String
+| *proxyAuthMethod* (proxy) | Authentication method for proxy, either as Basic or Digest. |  | String
 | *proxyAuthPassword* (proxy) | Password for proxy authentication |  | String
 | *proxyAuthUsername* (proxy) | Username for proxy authentication |  | String
 | *proxyHost* (proxy) | The proxy host name |  | String
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 a9e0a2a..c1e4595 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
@@ -6,7 +6,7 @@
  * (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
+ * 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,
@@ -16,24 +16,18 @@
  */
 package org.apache.camel.component.geocoder;
 
-import java.security.InvalidKeyException;
-
 import com.google.maps.GeoApiContext;
 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;
+
+import java.net.InetSocketAddress;
+import java.net.Proxy;
 
 /**
  * The geocoder component is used for looking up geocodes (latitude and longitude) for a given address, or reverse lookup.
@@ -69,10 +63,6 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
     private String proxyAuthDomain;
     @UriParam(label = "proxy")
     private String proxyAuthHost;
-    @UriParam(label = "advanced")
-    private HttpClientConfigurer httpClientConfigurer;
-    @UriParam(label = "advanced")
-    private HttpConnectionManager httpConnectionManager;
 
     public GeoCoderEndpoint() {
     }
@@ -242,113 +232,42 @@ public class GeoCoderEndpoint extends DefaultEndpoint {
         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;
-    }
-
     GeoApiContext createGeoApiContext() {
+        GeoApiContext.Builder builder = new GeoApiContext.Builder();
         if (clientId != null) {
-            return new GeoApiContext.Builder()
-                    .enterpriseCredentials(clientId, clientKey)
-                    .build();
+            builder = builder.enterpriseCredentials(clientId, clientKey);
         } else {
-            return new GeoApiContext.Builder()
-                    .apiKey(getApiKey())
-                    .build();
+            builder = builder.apiKey(getApiKey());
         }
+        if (isProxyDefined()) {
+            builder = builder.proxy(createProxy());
+            if (isProxyAuthDefined()) {
+                builder = configureProxyAuth(builder);
+            }
+        }
+        return builder.build();
     }
 
-//    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;
+    private GeoApiContext.Builder configureProxyAuth(GeoApiContext.Builder builder) {
+        AuthenticationMethod auth = getCamelContext().getTypeConverter().convertTo(AuthenticationMethod.class, proxyAuthMethod);
+        if (auth == AuthenticationMethod.Basic || auth == AuthenticationMethod.Digest) {
+            builder = builder.proxyAuthentication(proxyAuthUsername, proxyAuthPassword);
         }
-
-        // validate mandatory options given
-        if (username != null && authMethod == null) {
-            throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
+        else{
+            throw new IllegalArgumentException("Unknown proxyAuthMethod " + proxyAuthMethod);
         }
+        return builder;
+    }
 
-        ObjectHelper.notNull(authMethod, "proxyAuthMethod");
-        ObjectHelper.notNull(username, "proxyAuthUsername");
-        ObjectHelper.notNull(password, "proxyAuthPassword");
-
-        AuthenticationMethod auth = getCamelContext().getTypeConverter().convertTo(AuthenticationMethod.class, authMethod);
+    private Proxy createProxy() {
+        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
+    }
 
-        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;
-        }
+    private boolean isProxyDefined() {
+        return proxyHost != null && proxyPort != null;
+    }
 
-        throw new IllegalArgumentException("Unknown proxyAuthMethod " + authMethod);
+    private boolean isProxyAuthDefined() {
+        return proxyAuthMethod != null;
     }
 }
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
deleted file mode 100644
index 6ee7890..0000000
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/AuthenticationHttpClientConfigurer.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/**
- * 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));
-    }
-}
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
index 0e07b4a..a3f294e 100644
--- 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
@@ -23,5 +23,5 @@ package org.apache.camel.component.geocoder.http;
  */
 public enum AuthenticationMethod {
 
-    Basic, Digest, NTLM
+    Basic, Digest
 }
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
deleted file mode 100644
index 182c192..0000000
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/CompositeHttpConfigurer.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/**
- * 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);
-    }
-}
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
deleted file mode 100644
index 0d00269..0000000
--- a/components/camel-geocoder/src/main/java/org/apache/camel/component/geocoder/http/HttpClientConfigurer.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * 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);
-}
diff --git a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderApiKeyTestBase.java b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderApiKeyTestBase.java
index 409ae7b..ff7e9f8 100644
--- a/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderApiKeyTestBase.java
+++ b/components/camel-geocoder/src/test/java/org/apache/camel/component/geocoder/GeoCoderApiKeyTestBase.java
@@ -31,6 +31,6 @@ public class GeoCoderApiKeyTestBase extends CamelTestSupport {
     }
 
     protected String getApiKey() {
-        return System.getenv("CAMEL_GEOCODER_APIKEYx");
+        return System.getenv("CAMEL_GEOCODER_APIKEY");
     }
 }
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
index 97b4d2d..58721f7 100644
--- 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
@@ -27,12 +27,12 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 @Ignore("This test should be executed manually with a proxy set up")
-public class GeoCoderProxyTest extends CamelTestSupport {
+public class GeoCoderProxyTest extends GeoCoderApiKeyTestBase {
 
     @Test
-    public void testGeoCoder() throws Exception {
+    public void testGeoCoderProxyNoAuth() throws Exception {
         GeoCoderEndpoint endpoint = context.getEndpoint(
-            "geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=3128&proxyAuthMethod=Basic&proxyAuthUsername=proxy&proxyAuthPassword=proxy",
+            "geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=8888&apiKey="+getApiKey(),
             GeoCoderEndpoint.class);
 
         GeoApiContext context = endpoint.createGeoApiContext();
@@ -41,4 +41,18 @@ public class GeoCoderProxyTest extends CamelTestSupport {
 
         log.info("Response {}", results);
     }
+
+    @Test
+    public void testGeoCoderWithAuth() throws Exception {
+        GeoCoderEndpoint endpoint = context.getEndpoint(
+                "geocoder:address:current?headersOnly=true&proxyHost=localhost&proxyPort=8888&proxyAuthMethod=Basic&proxyAuthUsername=proxy&proxyAuthPassword=proxy&apiKey="+getApiKey(),
+                GeoCoderEndpoint.class);
+
+        GeoApiContext context = endpoint.createGeoApiContext();
+        GeocodingApiRequest geocodingApiRequest = GeocodingApi.reverseGeocode(context, new LatLng(45.4643, 9.1895));
+        GeocodingResult[] results = geocodingApiRequest.await();
+
+        log.info("Response {}", results);
+    }
+
 }