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

ningjiang pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 7058333  CAMEL-13605 Support setup proxy host and port on Telegram.
7058333 is described below

commit 7058333018644b56f424fb33fbc74d512c283ade
Author: Willem Jiang <willem.ji...@gmail.com>
AuthorDate: Mon Jun 3 20:07:01 2019 +0800

    CAMEL-13605 Support setup proxy host and port on Telegram.
---
 .../src/main/docs/telegram-component.adoc          |  4 +++-
 .../component/telegram/TelegramConfiguration.java  | 25 +++++++++++++++++++++-
 .../camel/component/telegram/TelegramEndpoint.java |  9 ++++++++
 .../camel/component/telegram/TelegramService.java  |  3 ++-
 .../service/TelegramServiceRestBotAPIAdapter.java  | 13 +++++++++--
 .../telegram/TelegramConfigurationTest.java        |  4 +++-
 6 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/components/camel-telegram/src/main/docs/telegram-component.adoc 
b/components/camel-telegram/src/main/docs/telegram-component.adoc
index 83a4461..270d13d 100644
--- a/components/camel-telegram/src/main/docs/telegram-component.adoc
+++ b/components/camel-telegram/src/main/docs/telegram-component.adoc
@@ -78,12 +78,14 @@ with the following path and query parameters:
 |===
 
 
-==== Query Parameters (22 parameters):
+==== Query Parameters (24 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
+| *proxyHost* (common) | The proxyHost which could be used when sending out 
the message. |  | String
+| *proxyPort* (common) | The proxyPort which could be used when sending out 
the message. |  | Integer
 | *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
 | *limit* (consumer) | Limit on the number of updates that can be received in 
a single polling request. | 100 | Integer
 | *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
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java
index 6e52fd4..98d309c 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java
@@ -37,6 +37,12 @@ public class TelegramConfiguration {
     @Metadata(required = "true")
     private String authorizationToken;
 
+    @UriParam(description = "The proxyHost which could be used when sending 
out the message.")
+    private String proxyHost;
+
+    @UriParam(description = "The proxyPort which could be used when sending 
out the message.")
+    private Integer proxyPort;
+
     @UriParam(description = "The identifier of the chat that will receive the 
produced messages. Chat ids can be first obtained from incoming messages "
             + "(eg. when a telegram user starts a conversation with a bot, its 
client sends automatically a '/start' message containing the chat id). "
             + "It is an optional parameter, as the chat id can be set 
dynamically for each outgoing message (using body or headers).", label = 
"producer")
@@ -98,7 +104,24 @@ public class TelegramConfiguration {
         this.authorizationToken = authorizationToken;
     }
 
-    public String getChatId() {
+    public String getProxyHost() {
+      return proxyHost;
+    }
+
+    public void setProxyHost(String proxyHost) {
+      this.proxyHost = proxyHost;
+    }
+
+    public Integer getProxyPort() {
+      return proxyPort;
+    }
+
+    public void setProxyPort(Integer proxyPort) {
+      this.proxyPort = proxyPort;
+    }
+
+
+  public String getChatId() {
         return chatId;
     }
 
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
index d901acf..1765392 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramEndpoint.java
@@ -25,12 +25,16 @@ import org.apache.camel.component.telegram.model.Update;
 import org.apache.camel.impl.ScheduledPollEndpoint;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The telegram component provides access to the <a 
href="https://core.telegram.org/bots/api";>Telegram Bot API</a>.
  */
 @UriEndpoint(firstVersion = "2.18.0", scheme = "telegram", title = "Telegram", 
syntax = "telegram:type/authorizationToken", consumerClass = 
TelegramConsumer.class, label = "chat")
 public class TelegramEndpoint extends ScheduledPollEndpoint {
+    private static final Logger LOG = 
LoggerFactory.getLogger(TelegramEndpoint.class);
 
     @UriParam
     private TelegramConfiguration configuration;
@@ -38,6 +42,11 @@ public class TelegramEndpoint extends ScheduledPollEndpoint {
     public TelegramEndpoint(String endpointUri, Component component, 
TelegramConfiguration configuration) {
         super(endpointUri, component);
         this.configuration = configuration;
+        // setup the proxy setting here
+        if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && 
ObjectHelper.isNotEmpty(configuration.getProxyPort())) {
+            LOG.debug("Setup http proxy host:{} port:{} for TelegramService", 
configuration.getProxyHost(), configuration.getProxyPort());
+            
TelegramServiceProvider.get().getService().setHttpProxy(configuration.getProxyHost(),
 configuration.getProxyPort());
+        }
     }
 
     @Override
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramService.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramService.java
index b38f4ad..1a62162 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramService.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramService.java
@@ -24,7 +24,8 @@ import org.apache.camel.component.telegram.model.UpdateResult;
  */
 public interface TelegramService {
 
-
+    void setHttpProxy(String host, Integer port);
+  
     UpdateResult getUpdates(String authorizationToken, Long offset, Integer 
limit, Integer timeoutSeconds);
 
     void sendMessage(String authorizationToken, OutgoingMessage message);
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java
index 465b253..39337d9 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java
@@ -40,6 +40,7 @@ import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
+import org.apache.cxf.transport.http.HTTPConduit;
 
 /**
  * Adapts the {@code RestBotAPI} to the {@code TelegramService} interface.
@@ -50,7 +51,15 @@ public class TelegramServiceRestBotAPIAdapter implements 
TelegramService {
 
     public TelegramServiceRestBotAPIAdapter() {
         this.api = JAXRSClientFactory.create(RestBotAPI.BOT_API_DEFAULT_URL, 
RestBotAPI.class, Collections.singletonList(providerByCustomObjectMapper()));
-        
WebClient.getConfig(this.api).getHttpConduit().getClient().setAllowChunking(false);
+        HTTPConduit httpConduit = 
WebClient.getConfig(this.api).getHttpConduit();
+        httpConduit.getClient().setAllowChunking(false);
+    }
+
+    @Override
+    public void setHttpProxy(String host, Integer port) {
+        HTTPConduit httpConduit = 
WebClient.getConfig(this.api).getHttpConduit();
+        httpConduit.getClient().setProxyServer(host);
+        httpConduit.getClient().setProxyServerPort(port);
     }
 
     @Override
@@ -183,4 +192,4 @@ public class TelegramServiceRestBotAPIAdapter implements 
TelegramService {
         mapper.setSerializationInclusion(Include.NON_NULL);
         return new JacksonJsonProvider(mapper);
     }    
-}
\ No newline at end of file
+}
diff --git 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java
 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java
index d918661..32508bf 100644
--- 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java
+++ 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConfigurationTest.java
@@ -38,6 +38,8 @@ public class TelegramConfigurationTest extends 
TelegramTestSupport {
         assertEquals(2000L, endpoint.getDelay());
         assertEquals(Integer.valueOf(10), config.getTimeout());
         assertEquals(Integer.valueOf(60), config.getLimit());
+        assertEquals("127.0.0.1", config.getProxyHost());
+        assertEquals(Integer.valueOf(1234), config.getProxyPort());
     }
 
 
@@ -48,7 +50,7 @@ public class TelegramConfigurationTest extends 
TelegramTestSupport {
             public void configure() throws Exception {
 
                 from("direct:telegram")
-                        
.to("telegram:bots/mock-token?chatId=12345&delay=2000&timeout=10&limit=60");
+                        
.to("telegram:bots/mock-token?chatId=12345&delay=2000&timeout=10&limit=60&proxyHost=127.0.0.1&proxyPort=1234");
             }
         };
     }

Reply via email to