[
https://issues.apache.org/jira/browse/CAMEL-11474?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16309436#comment-16309436
]
ASF GitHub Bot commented on CAMEL-11474:
----------------------------------------
oscerd closed pull request #2157: CAMEL-11474: adding HttpClient custom
configuration support
URL: https://github.com/apache/camel/pull/2157
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/components/camel-hipchat/src/main/docs/hipchat-component.adoc
b/components/camel-hipchat/src/main/docs/hipchat-component.adoc
index 2b15e62801f..7c9814d7867 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 6f54aed7559..a76836912da 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.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 @@
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 String hipChatUrl() {
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 908ebb0e14f..39a6917ecc2 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 @@
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 @@ private void processApiResponse(Exchange exchange,
CloseableHttpResponse respons
}
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 7df991b348a..739ec56e767 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 @@
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 @@ private StatusLine sendUserMessage(String user, Exchange
exchange) throws IOExce
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 00000000000..4e96d884f92
--- /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);
+ }
+ }
+
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Camel-Hipchat: Allow configurable http client
> ---------------------------------------------
>
> Key: CAMEL-11474
> URL: https://issues.apache.org/jira/browse/CAMEL-11474
> Project: Camel
> Issue Type: Improvement
> Components: camel-hipcat
> Affects Versions: 2.19.1
> Reporter: Paul Watson
> Priority: Minor
>
> As it stands the Hipchat component uses a sealed private static final to
> define the apache http client for communication. It would be great if we
> could allow a more configurable client.
> One such prominent use-case here is on-premise versions of Hipchat using
> private certificates that need configuring on the SSLContext.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)