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

[camel] branch master updated (c0a9808 -> d0d5dfc)

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

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


    from c0a9808  CAMEL-12108 - PR on behalf of Bas Claessen
     new 1f5af18  camel-11474: adding HttpClient custom configuration support
     new d0d5dfc  CAMEL-11474 - Fixed CS

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/docs/hipchat-component.adoc           |  21 ++++-
 .../component/hipchat/HipchatConfiguration.java    |  18 +++-
 .../camel/component/hipchat/HipchatConsumer.java   |   7 +-
 .../camel/component/hipchat/HipchatProducer.java   |   8 +-
 .../HipchatComponentCustomHttpClientTest.java      | 100 +++++++++++++++++++++
 5 files changed, 140 insertions(+), 14 deletions(-)
 create mode 100644 components/camel-hipchat/src/test/java/org/apache/camel/component/hipchat/HipchatComponentCustomHttpClientTest.java

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

[camel] 02/02: CAMEL-11474 - Fixed CS

Posted by ac...@apache.org.
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 d0d5dfc22b983ab20cef4da3bfa2282c1894e78e
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Jan 3 11:24:57 2018 +0100

    CAMEL-11474 - Fixed CS
---
 .../camel-hipchat/src/main/docs/hipchat-component.adoc   |  4 ++--
 .../apache/camel/component/hipchat/HipchatConsumer.java  |  8 ++++----
 .../apache/camel/component/hipchat/HipchatProducer.java  |  6 +++---
 .../hipchat/HipchatComponentCustomHttpClientTest.java    | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/components/camel-hipchat/src/main/docs/hipchat-component.adoc b/components/camel-hipchat/src/main/docs/hipchat-component.adoc
index 7c9814d..51fa1e8 100644
--- a/components/camel-hipchat/src/main/docs/hipchat-component.adoc
+++ b/components/camel-hipchat/src/main/docs/hipchat-component.adoc
@@ -53,13 +53,14 @@ with the following path and query parameters:
 | *port* | The port for the hipchat server. Is by default 80. | 80 | Integer
 |===
 
-==== Query Parameters (21 parameters):
+==== Query Parameters (22 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
 | *authToken* (common) | OAuth 2 auth token |  | String
 | *consumeUsers* (common) | Username(s) when consuming messages from the hiptchat server. Multiple user names can be separated by comma. |  | String
+| *httpClient* (common) | The CloseableHttpClient reference from registry to be used during API HTTP requests. | CloseableHttpClient default from HttpClient library | CloseableHttpClient
 | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN or ERROR level and ignored. | false | boolean
 | *sendEmptyMessageWhenIdle* (consumer) | If the polling consumer did not poll any files you can enable this option to send an empty message (no body) instead. | false | boolean
 | *exceptionHandler* (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
@@ -79,7 +80,6 @@ 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
 
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 39a6917..8947729 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
@@ -22,6 +22,10 @@ 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;
@@ -31,10 +35,6 @@ import org.apache.http.client.methods.HttpGet;
 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.
  */
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 739ec56..0b4225e 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,12 +16,12 @@
  */
 package org.apache.camel.component.hipchat;
 
-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;
@@ -35,7 +35,7 @@ import org.apache.http.entity.StringEntity;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
+import static org.apache.camel.util.UnsafeUriCharactersEncoder.encodeHttpURI;
 
 /**
  * The Hipchat producer to send message to a user and/or a room.
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
index 4e96d88..56d0439 100644
--- 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
@@ -1,3 +1,19 @@
+/**
+ * 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.hipchat;
 
 import java.io.IOException;

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

[camel] 01/02: camel-11474: adding HttpClient custom configuration support

Posted by ac...@apache.org.
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 <ri...@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" <co...@camel.apache.org>.