camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [camel] 01/02: camel-11474: adding HttpClient custom configuration support
Date Wed, 03 Jan 2018 10:26:50 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 1f5af18f3629ecf18dc654f41b487a0373034b59
Author: Ricardo Zanini <ricardozanini@gmail.com>
AuthorDate: Tue Jan 2 11:29:01 2018 -0200

    camel-11474: adding HttpClient custom configuration support
---
 .../src/main/docs/hipchat-component.adoc           | 19 +++++
 .../component/hipchat/HipchatConfiguration.java    | 18 ++++-
 .../camel/component/hipchat/HipchatConsumer.java   | 13 ++--
 .../camel/component/hipchat/HipchatProducer.java   | 12 ++--
 .../HipchatComponentCustomHttpClientTest.java      | 84 ++++++++++++++++++++++
 5 files changed, 128 insertions(+), 18 deletions(-)

diff --git a/components/camel-hipchat/src/main/docs/hipchat-component.adoc b/components/camel-hipchat/src/main/docs/hipchat-component.adoc
index 2b15e62..7c9814d 100644
--- a/components/camel-hipchat/src/main/docs/hipchat-component.adoc
+++ b/components/camel-hipchat/src/main/docs/hipchat-component.adoc
@@ -79,6 +79,7 @@ with the following path and query parameters:
 | *startScheduler* (scheduler) | Whether the scheduler should be auto started. | true | boolean
 | *timeUnit* (scheduler) | Time unit for initialDelay and delay options. | MILLISECONDS |
TimeUnit
 | *useFixedDelay* (scheduler) | Controls if fixed delay or fixed rate is used. See ScheduledExecutorService
in JDK for details. | true | boolean
+| *httpClient* | The custom `CloseableHttpClient` reference from registry to be used during
API HTTP requests. Could be configured using Http Client class `HttpClientBuilder`. | Default
`CloseableHttpClinent` from HttpClient library | org.apache.http.impl.client.CloseableHttpClient
 |===
 // endpoint options: END
 
@@ -180,6 +181,24 @@ The status of the API response received when message sent to the user.
 |HipchatFromUserResponseStatus |HipchatConstants.TO_ROOM_RESPONSE_STATUS |_http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/StatusLine.html[StatusLine]_
|The status of the API response received when message sent to the room.
 |=======================================================================
 
+#### Configuring Http Client
+
+The HipChat component allow your own `HttpClient` configuration. This can be done by defining
a reference for `CloseableHttpClient` in the http://camel.apache.org/registry.html[registry]
(e.g. Spring Context) and then, set the parameter during the Endpoint definition, for example:
`hipchat:http://api.hipchat.com?httpClient=#myHttpClient`.
+
+[source,java]
+------------------------------------------
+CloseableHttpClient httpclient = HttpClients.custom()
+    .setConnectionManager(connManager)
+    .setDefaultCookieStore(cookieStore)
+    .setDefaultCredentialsProvider(credentialsProvider)
+    .setProxy(new HttpHost("myproxy", 8080))
+    .setDefaultRequestConfig(defaultRequestConfig)
+    .build();
+------------------------------------------
+
+To see more information about Http Client configuration, please check the https://hc.apache.org/httpcomponents-client-ga/examples.html[official
documentation].
+ 
+
 #### Dependencies
 
 Maven users will need to add the following dependency to their pom.xml.
diff --git a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConfiguration.java
b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConfiguration.java
index 6f54aed..a768369 100644
--- a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConfiguration.java
+++ b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConfiguration.java
@@ -20,12 +20,16 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
 
 @UriParams
 public class HipchatConfiguration {
-    @UriPath @Metadata(required = "true")
+    @UriPath
+    @Metadata(required = "true")
     private String protocol;
-    @UriPath @Metadata(required = "true")
+    @UriPath
+    @Metadata(required = "true")
     private String host = HipchatConstants.DEFAULT_HOST;
     @UriPath(defaultValue = "" + HipchatConstants.DEFAULT_PORT)
     private Integer port = HipchatConstants.DEFAULT_PORT;
@@ -33,6 +37,8 @@ public class HipchatConfiguration {
     private String authToken;
     @UriParam
     private String consumeUsers;
+    @UriParam(description = "The CloseableHttpClient reference from registry to be used during
API HTTP requests.", defaultValue = "CloseableHttpClient default from HttpClient library")
+    private CloseableHttpClient httpClient = HttpClients.createDefault();
 
     public String getHost() {
         return host;
@@ -102,4 +108,12 @@ public class HipchatConfiguration {
     public String withAuthToken(String urlPath) {
         return urlPath + HipchatApiConstants.AUTH_TOKEN_PREFIX + getAuthToken();
     }
+
+    public CloseableHttpClient getHttpClient() {
+        return httpClient;
+    }
+
+    public void setHttpClient(CloseableHttpClient httpClient) {
+        this.httpClient = httpClient;
+    }
 }
diff --git a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConsumer.java
b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConsumer.java
index 908ebb0..39a6917 100644
--- a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConsumer.java
+++ b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatConsumer.java
@@ -17,26 +17,24 @@
 package org.apache.camel.component.hipchat;
 
 import java.io.IOException;
-
 import java.util.Calendar;
 import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.type.MapType;
-import com.fasterxml.jackson.databind.type.TypeFactory;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.ScheduledPollConsumer;
 import org.apache.camel.util.URISupport;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.MapType;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
 /**
  * The Hipchat consumer consumes messages from a list of users.
  */
@@ -45,7 +43,6 @@ public class HipchatConsumer extends ScheduledPollConsumer {
     private static final Logger LOG = LoggerFactory.getLogger(HipchatConsumer.class);
     private static final MapType MAP_TYPE = TypeFactory.defaultInstance().constructMapType(Map.class,
String.class, Object.class);
     private static final ObjectMapper MAPPER = new ObjectMapper();
-    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();
     
     private transient String hipchatConsumerToString;
 
@@ -100,7 +97,7 @@ public class HipchatConsumer extends ScheduledPollConsumer {
     }
 
     protected CloseableHttpResponse executeGet(HttpGet httpGet) throws IOException {
-        return HTTP_CLIENT.execute(httpGet);
+        return getConfig().getHttpClient().execute(httpGet);
     }
 
     private String getMostRecentMessageUrl() {
diff --git a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatProducer.java
b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatProducer.java
index 7df991b..739ec56 100644
--- a/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatProducer.java
+++ b/components/camel-hipchat/src/main/java/org/apache/camel/component/hipchat/HipchatProducer.java
@@ -16,29 +16,26 @@
  */
 package org.apache.camel.component.hipchat;
 
-import java.io.IOException;
+import static org.apache.camel.util.UnsafeUriCharactersEncoder.encodeHttpURI;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.URISupport;
-import org.apache.camel.util.UnsafeUriCharactersEncoder;
 import org.apache.http.StatusLine;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.camel.util.UnsafeUriCharactersEncoder.encodeHttpURI;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 /**
  * The Hipchat producer to send message to a user and/or a room.
@@ -46,7 +43,6 @@ import static org.apache.camel.util.UnsafeUriCharactersEncoder.encodeHttpURI;
 public class HipchatProducer extends DefaultProducer {
     private static final Logger LOG = LoggerFactory.getLogger(HipchatProducer.class);
     private static final ObjectMapper MAPPER = new ObjectMapper();
-    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();
     
     private transient String hipchatProducerToString;
 
@@ -103,7 +99,7 @@ public class HipchatProducer extends DefaultProducer {
     protected StatusLine post(String urlPath, Map<String, String> postParam) throws
IOException {
         HttpPost httpPost = new HttpPost(getConfig().hipChatUrl() + urlPath);
         httpPost.setEntity(new StringEntity(MAPPER.writeValueAsString(postParam), ContentType.APPLICATION_JSON));
-        CloseableHttpResponse closeableHttpResponse = HTTP_CLIENT.execute(httpPost);
+        CloseableHttpResponse closeableHttpResponse = getConfig().getHttpClient().execute(httpPost);
         try {
             return closeableHttpResponse.getStatusLine();
         } finally {
diff --git a/components/camel-hipchat/src/test/java/org/apache/camel/component/hipchat/HipchatComponentCustomHttpClientTest.java
b/components/camel-hipchat/src/test/java/org/apache/camel/component/hipchat/HipchatComponentCustomHttpClientTest.java
new file mode 100644
index 0000000..4e96d88
--- /dev/null
+++ b/components/camel-hipchat/src/test/java/org/apache/camel/component/hipchat/HipchatComponentCustomHttpClientTest.java
@@ -0,0 +1,84 @@
+package org.apache.camel.component.hipchat;
+
+import java.io.IOException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import org.junit.Test;
+
+public class HipchatComponentCustomHttpClientTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "hipchat:http://api.hipchat.com?httpClient=#myHttpClient&authToken=anything&consumeUsers=@AUser")
+    private HipchatEndpoint hipchatEndpoint;
+    
+    @Test
+    public void ensureCustomHttpClientIsDefined() {
+        HttpClient httpClient = hipchatEndpoint.getConfiguration().getHttpClient();
+        assertNotNull(httpClient);
+        assertIsInstanceOf(MyCustomHttpClient.class, httpClient);
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry reg = super.createRegistry();
+        reg.bind("myHttpClient", new MyCustomHttpClient());
+        return reg;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                    .to("hipchat:http://api.hipchat.com?httpClient=#myHttpClient&authToken=anything&consumeUsers=@AUser")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    public static class MyCustomHttpClient extends CloseableHttpClient {
+
+        private final CloseableHttpClient innerHttpClient;
+
+        public MyCustomHttpClient() {
+            this.innerHttpClient = HttpClientBuilder.create().build();
+        }
+
+        @Override
+        public HttpParams getParams() {
+            return innerHttpClient.getParams();
+        }
+
+        @Override
+        public ClientConnectionManager getConnectionManager() {
+            return innerHttpClient.getConnectionManager();
+        }
+
+        @Override
+        public void close() throws IOException {
+            innerHttpClient.close();
+        }
+
+        @Override
+        protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext
context) throws IOException, ClientProtocolException {
+            return innerHttpClient.execute(target, request, context);
+        }
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <commits@camel.apache.org>.

Mime
View raw message