This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 2331a41 CAMEL-13605 Support setup proxy host and port on Telegram. 2331a41 is described below commit 2331a41fc7628b123762c57b92940913782033da 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 | 18 +++++++++------- .../component/telegram/TelegramConfiguration.java | 25 +++++++++++++++++++++- .../camel/component/telegram/TelegramEndpoint.java | 10 ++++++++- .../camel/component/telegram/TelegramService.java | 2 ++ .../service/TelegramServiceRestBotAPIAdapter.java | 13 +++++++++-- .../telegram/TelegramConfigurationTest.java | 4 +++- 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/components/camel-telegram/src/main/docs/telegram-component.adoc b/components/camel-telegram/src/main/docs/telegram-component.adoc index 13939b0..4439707 100644 --- a/components/camel-telegram/src/main/docs/telegram-component.adoc +++ b/components/camel-telegram/src/main/docs/telegram-component.adoc @@ -79,12 +79,14 @@ with the following path and query parameters: |=== -==== Query Parameters (23 parameters): +==== Query Parameters (25 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 @@ -358,16 +360,16 @@ from("telegram:bots/123456789:insertYourAuthorizationTokenHere") OutgoingTextMessage msg = new OutgoingTextMessage(); msg.setText("Choose one option!"); - + InlineKeyboardButton buttonOptionOneI = InlineKeyboardButton.builder() .text("Option One - I").build(); - + InlineKeyboardButton buttonOptionOneII = InlineKeyboardButton.builder() .text("Option One - II").build(); - + InlineKeyboardButton buttonOptionTwoI = InlineKeyboardButton.builder() .text("Option Two - I").build(); - + ReplyKeyboardMarkup replyMarkup = ReplyKeyboardMarkup.builder() .keyboard() .addRow(Arrays.asList(buttonOptionOneI, buttonOptionOneII)) @@ -375,7 +377,7 @@ from("telegram:bots/123456789:insertYourAuthorizationTokenHere") .close() .oneTimeKeyboard(true) .build(); - + msg.setReplyKeyboardMarkup(replyMarkup); exchange.getIn().setBody(msg); @@ -392,11 +394,11 @@ from("telegram:bots/123456789:insertYourAuthorizationTokenHere") OutgoingTextMessage msg = new OutgoingTextMessage(); msg.setText("Your answer was accepted!"); - + ReplyKeyboardMarkup replyMarkup = ReplyKeyboardMarkup.builder() .removeKeyboard(true) .build(); - + msg.setReplyKeyboardMarkup(replyMarkup); exchange.getIn().setBody(msg); 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 aafc73f..52f94dd 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 1fae6f5..e3e662c 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 @@ -31,7 +31,9 @@ import org.apache.camel.component.webhook.WebhookConfiguration; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.support.ScheduledPollEndpoint; - +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static org.apache.camel.component.telegram.util.TelegramMessageHelper.populateExchange; /** @@ -39,6 +41,7 @@ import static org.apache.camel.component.telegram.util.TelegramMessageHelper.pop */ @UriEndpoint(firstVersion = "2.18.0", scheme = "telegram", title = "Telegram", syntax = "telegram:type/authorizationToken", label = "chat") public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCapableEndpoint { + private static final Logger LOG = LoggerFactory.getLogger(TelegramEndpoint.class); @UriParam private TelegramConfiguration configuration; @@ -48,6 +51,11 @@ public class TelegramEndpoint extends ScheduledPollEndpoint implements WebhookCa 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 35a9e0a..d90a811 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,6 +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); Object 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 6aac1da..87d2bf8 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 @@ -45,6 +45,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. @@ -55,7 +56,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); } public TelegramServiceRestBotAPIAdapter(RestBotAPI api) { @@ -214,4 +223,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 fd3c061..31925e4 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"); } }; }