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

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


The following commit(s) were added to refs/heads/camel-2.25.x by this push:
     new 8116831  [CAMEL-15188] Add proxy type query parameter in 
camel-telegram, add support for SOCKS proxy
8116831 is described below

commit 81168313087f150c6716c8c5af7c1a8489c39fba
Author: iliya <[email protected]>
AuthorDate: Mon Jun 15 01:34:19 2020 +0300

    [CAMEL-15188] Add proxy type query parameter in camel-telegram, add support 
for SOCKS proxy
---
 .../src/main/docs/telegram-component.adoc          | 17 ++++---
 .../component/telegram/TelegramConfiguration.java  | 12 ++++-
 .../camel/component/telegram/TelegramEndpoint.java | 20 +++++++-
 .../component/telegram/TelegramProxyType.java      |  8 +++
 .../camel/component/telegram/TelegramService.java  |  5 +-
 .../service/TelegramServiceRestBotAPIAdapter.java  |  8 +--
 .../telegram/TelegramComponentParametersTest.java  |  9 ++++
 .../telegram/TelegramConfigurationTest.java        |  3 +-
 .../integration/TelegramServiceProxyTest.java      | 58 ++++++++++++++++++++++
 9 files changed, 123 insertions(+), 17 deletions(-)

diff --git a/components/camel-telegram/src/main/docs/telegram-component.adoc 
b/components/camel-telegram/src/main/docs/telegram-component.adoc
index ba79ead..039100b 100644
--- a/components/camel-telegram/src/main/docs/telegram-component.adoc
+++ b/components/camel-telegram/src/main/docs/telegram-component.adoc
@@ -79,7 +79,7 @@ with the following path and query parameters:
 |===
 
 
-=== Query Parameters (24 parameters):
+=== Query Parameters (25 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -87,6 +87,7 @@ with the following path and query parameters:
 | 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
+| *proxyType* (common) | The proxyType which could be used when sending out 
the message. | HTTP | TelegramProxyType
 | *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
@@ -354,16 +355,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))
@@ -371,7 +372,7 @@ 
from("telegram:bots/123456789:insertYourAuthorizationTokenHere")
                     .close()
                 .oneTimeKeyboard(true)
                 .build();
-        
+
         msg.setReplyKeyboardMarkup(replyMarkup);
 
         exchange.getIn().setBody(msg);
@@ -388,11 +389,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 98d309c..7b6001a 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
@@ -43,6 +43,9 @@ public class TelegramConfiguration {
     @UriParam(description = "The proxyPort which could be used when sending 
out the message.")
     private Integer proxyPort;
 
+    @UriParam(description = "The proxyType which could be used when sending 
out the message.", defaultValue = "HTTP")
+    private TelegramProxyType proxyType = TelegramProxyType.HTTP;
+
     @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")
@@ -120,8 +123,15 @@ public class TelegramConfiguration {
       this.proxyPort = proxyPort;
     }
 
+    public TelegramProxyType getProxyType() {
+        return proxyType;
+    }
+
+    public void setProxyType(TelegramProxyType proxyType) {
+        this.proxyType = proxyType;
+    }
 
-  public String getChatId() {
+    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 1765392..38282b3 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
@@ -26,6 +26,7 @@ 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.apache.cxf.transports.http.configuration.ProxyServerType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,8 +45,9 @@ public class TelegramEndpoint extends ScheduledPollEndpoint {
         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());
+            ProxyServerType proxyServerType = 
getProxyServerType(configuration.getProxyType());
+            LOG.debug("Setup {} proxy host:{} port:{} for TelegramService", 
proxyServerType, configuration.getProxyHost(), configuration.getProxyPort());
+            
TelegramServiceProvider.get().getService().setProxy(configuration.getProxyHost(),
 configuration.getProxyPort(), proxyServerType);
         }
     }
 
@@ -94,4 +96,18 @@ public class TelegramEndpoint extends ScheduledPollEndpoint {
         this.configuration = configuration;
     }
 
+    private ProxyServerType getProxyServerType(TelegramProxyType type) {
+        if (type == null) {
+            return ProxyServerType.HTTP;
+        }
+
+        switch (type) {
+            case HTTP:
+                return ProxyServerType.HTTP;
+            case SOCKS:
+                return ProxyServerType.SOCKS;
+            default:
+                throw new IllegalArgumentException("Unknown proxy type: " + 
type);
+        }
+    }
 }
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProxyType.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProxyType.java
new file mode 100644
index 0000000..4927ccc
--- /dev/null
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProxyType.java
@@ -0,0 +1,8 @@
+package org.apache.camel.component.telegram;
+
+/**
+ * Possible proxy server types
+ */
+public enum TelegramProxyType {
+    HTTP, SOCKS
+}
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 1a62162..0e975b4 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
@@ -18,14 +18,15 @@ package org.apache.camel.component.telegram;
 
 import org.apache.camel.component.telegram.model.OutgoingMessage;
 import org.apache.camel.component.telegram.model.UpdateResult;
+import org.apache.cxf.transports.http.configuration.ProxyServerType;
 
 /**
  * Allows interacting with the Telegram server to exchange messages.
  */
 public interface TelegramService {
 
-    void setHttpProxy(String host, Integer port);
-  
+    void setProxy(String host, Integer port, ProxyServerType type);
+
     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 39337d9..b35682e 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
@@ -41,6 +41,7 @@ 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;
+import org.apache.cxf.transports.http.configuration.ProxyServerType;
 
 /**
  * Adapts the {@code RestBotAPI} to the {@code TelegramService} interface.
@@ -56,10 +57,11 @@ public class TelegramServiceRestBotAPIAdapter implements 
TelegramService {
     }
 
     @Override
-    public void setHttpProxy(String host, Integer port) {
+    public void setProxy(String host, Integer port, ProxyServerType type) {
         HTTPConduit httpConduit = 
WebClient.getConfig(this.api).getHttpConduit();
         httpConduit.getClient().setProxyServer(host);
         httpConduit.getClient().setProxyServerPort(port);
+        httpConduit.getClient().setProxyServerType(type);
     }
 
     @Override
@@ -186,10 +188,10 @@ public class TelegramServiceRestBotAPIAdapter implements 
TelegramService {
     private String escapeMimeName(String name) {
         return name.replace("\"", "");
     }
-    
+
     private JacksonJsonProvider providerByCustomObjectMapper() {
         ObjectMapper mapper = new ObjectMapper();
         mapper.setSerializationInclusion(Include.NON_NULL);
         return new JacksonJsonProvider(mapper);
-    }    
+    }
 }
diff --git 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
index d712f17..0fcf164 100644
--- 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
+++ 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.telegram;
 
+import org.apache.camel.TypeConversionException;
 import org.apache.camel.component.telegram.util.TelegramTestSupport;
 import org.junit.Test;
 
@@ -30,6 +31,7 @@ public class TelegramComponentParametersTest extends 
TelegramTestSupport {
 
         TelegramEndpoint ep1 = (TelegramEndpoint) 
component.createEndpoint("telegram:bots");
         assertEquals("DEFAULT", 
ep1.getConfiguration().getAuthorizationToken());
+        assertEquals(TelegramProxyType.HTTP, 
ep1.getConfiguration().getProxyType());
 
         TelegramEndpoint ep2 = (TelegramEndpoint) 
component.createEndpoint("telegram:bots/CUSTOM");
         assertEquals("CUSTOM", ep2.getConfiguration().getAuthorizationToken());
@@ -59,4 +61,11 @@ public class TelegramComponentParametersTest extends 
TelegramTestSupport {
         component.createEndpoint("telegram:bots/token/s");
     }
 
+    @Test(expected = TypeConversionException.class)
+    public void testWrongURI3() throws Exception {
+        TelegramComponent component = (TelegramComponent) 
context().getComponent("telegram");
+        component.setAuthorizationToken("ANY");
+        component.createEndpoint("telegram:bots?proxyType=ANY");
+    }
+
 }
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 32508bf..0b44099 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
@@ -40,6 +40,7 @@ public class TelegramConfigurationTest extends 
TelegramTestSupport {
         assertEquals(Integer.valueOf(60), config.getLimit());
         assertEquals("127.0.0.1", config.getProxyHost());
         assertEquals(Integer.valueOf(1234), config.getProxyPort());
+        assertEquals(TelegramProxyType.SOCKS, config.getProxyType());
     }
 
 
@@ -50,7 +51,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&proxyHost=127.0.0.1&proxyPort=1234");
+                        
.to("telegram:bots/mock-token?chatId=12345&delay=2000&timeout=10&limit=60&proxyHost=127.0.0.1&proxyPort=1234&proxyType=SOCKS");
             }
         };
     }
diff --git 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceProxyTest.java
 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceProxyTest.java
new file mode 100644
index 0000000..5043807
--- /dev/null
+++ 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/integration/TelegramServiceProxyTest.java
@@ -0,0 +1,58 @@
+package org.apache.camel.component.telegram.integration;
+
+import org.apache.camel.component.telegram.TelegramService;
+import org.apache.camel.component.telegram.TelegramServiceProvider;
+import org.apache.camel.component.telegram.model.OutgoingTextMessage;
+import org.apache.camel.component.telegram.model.UpdateResult;
+import org.apache.cxf.transports.http.configuration.ProxyServerType;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests if the BotAPI are working correctly over proxy.
+ */
+public class TelegramServiceProxyTest {
+
+    private static String authorizationToken;
+
+    private static String chatId;
+
+    private static String proxyHost;
+
+    private static int proxyPort;
+
+    private static ProxyServerType proxyType;
+
+    @BeforeClass
+    public static void init() {
+        authorizationToken = System.getenv("TELEGRAM_AUTHORIZATION_TOKEN");
+        chatId = System.getenv("TELEGRAM_CHAT_ID");
+        proxyHost = System.getenv("TELEGRAM_PROXY_HOST");
+        proxyPort = Integer.parseInt(System.getenv("TELEGRAM_PROXY_PORT"));
+        proxyType = 
ProxyServerType.valueOf(System.getenv("TELEGRAM_PROXY_TYPE"));
+    }
+
+    @Test
+    public void testGetUpdates() {
+        TelegramService service = TelegramServiceProvider.get().getService();
+        service.setProxy(proxyHost, proxyPort, proxyType);
+
+        UpdateResult res = service.getUpdates(authorizationToken, null, null, 
null);
+
+        Assert.assertNotNull(res);
+        Assert.assertTrue(res.isOk());
+    }
+
+    @Test
+    public void testSendMessage() {
+        TelegramService service = TelegramServiceProvider.get().getService();
+        service.setProxy(proxyHost, proxyPort, proxyType);
+
+        OutgoingTextMessage msg = new OutgoingTextMessage();
+        msg.setChatId(chatId);
+        msg.setText("This is an auto-generated message from the Bot");
+
+        service.sendMessage(authorizationToken, msg);
+    }
+}

Reply via email to