camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nferr...@apache.org
Subject [2/3] camel git commit: camel-telegram: adding default token configuration
Date Mon, 12 Sep 2016 13:21:32 GMT
camel-telegram: adding default token configuration


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ec9f00ec
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ec9f00ec
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ec9f00ec

Branch: refs/heads/master
Commit: ec9f00ec99a70c1538bacd669ba0b9fc91efee87
Parents: c126018
Author: Nicola Ferraro <ni.ferraro@gmail.com>
Authored: Mon Sep 12 12:45:06 2016 +0200
Committer: Nicola Ferraro <ni.ferraro@gmail.com>
Committed: Mon Sep 12 15:18:09 2016 +0200

----------------------------------------------------------------------
 .../TelegramComponentAutoConfiguration.java     | 52 +++++++++++++
 .../TelegramComponentConfiguration.java         | 42 +++++++++++
 .../main/resources/META-INF/spring.factories    | 19 +++++
 .../src/main/docs/telegram-component.adoc       | 41 ++++++++++-
 .../component/telegram/TelegramComponent.java   | 15 +++-
 .../telegram/TelegramConfiguration.java         | 19 +++--
 .../TelegramComponentParametersTest.java        | 77 ++++++++++++++++++++
 7 files changed, 256 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentAutoConfiguration.java
b/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentAutoConfiguration.java
new file mode 100644
index 0000000..585e76e
--- /dev/null
+++ b/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentAutoConfiguration.java
@@ -0,0 +1,52 @@
+/**
+ * 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.telegram.springboot;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.telegram.TelegramComponent;
+import org.apache.camel.util.IntrospectionSupport;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Generated by camel-package-maven-plugin - do not edit this file!
+ */
+@Configuration
+@EnableConfigurationProperties(TelegramComponentConfiguration.class)
+public class TelegramComponentAutoConfiguration {
+
+    @Bean(name = "telegram-component")
+    @ConditionalOnClass(CamelContext.class)
+    @ConditionalOnMissingBean(TelegramComponent.class)
+    public TelegramComponent configureTelegramComponent(
+            CamelContext camelContext,
+            TelegramComponentConfiguration configuration) throws Exception {
+        TelegramComponent component = new TelegramComponent();
+        component.setCamelContext(camelContext);
+        Map<String, Object> parameters = new HashMap<>();
+        IntrospectionSupport.getProperties(configuration, parameters, null,
+                false);
+        IntrospectionSupport.setProperties(camelContext,
+                camelContext.getTypeConverter(), component, parameters);
+        return component;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentConfiguration.java
b/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentConfiguration.java
new file mode 100644
index 0000000..bfc18f8
--- /dev/null
+++ b/components-starter/camel-telegram-starter/src/main/java/org/apache/camel/component/telegram/springboot/TelegramComponentConfiguration.java
@@ -0,0 +1,42 @@
+/**
+ * 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.telegram.springboot;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * The Camel endpoint for a telegram bot.
+ * 
+ * Generated by camel-package-maven-plugin - do not edit this file!
+ */
+@ConfigurationProperties(prefix = "camel.component.telegram")
+public class TelegramComponentConfiguration {
+
+    /**
+     * The default Telegram authorization token to be used when the information
+     * is not provided in the endpoints.
+     */
+    private String authorizationToken;
+
+    public String getAuthorizationToken() {
+        return authorizationToken;
+    }
+
+    public void setAuthorizationToken(String authorizationToken) {
+        this.authorizationToken = authorizationToken;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components-starter/camel-telegram-starter/src/main/resources/META-INF/spring.factories
----------------------------------------------------------------------
diff --git a/components-starter/camel-telegram-starter/src/main/resources/META-INF/spring.factories
b/components-starter/camel-telegram-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000..7e794e6
--- /dev/null
+++ b/components-starter/camel-telegram-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +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.
+#
+
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.apache.camel.component.telegram.springboot.TelegramComponentAutoConfiguration

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components/camel-telegram/src/main/docs/telegram-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/docs/telegram-component.adoc b/components/camel-telegram/src/main/docs/telegram-component.adoc
index 8c5b601..2d79320 100644
--- a/components/camel-telegram/src/main/docs/telegram-component.adoc
+++ b/components/camel-telegram/src/main/docs/telegram-component.adoc
@@ -49,7 +49,17 @@ Options
 ^^^^^^^
 
 // component options: START
-The Telegram component has no options.
+The Telegram component supports 1 options which are listed below.
+
+
+
+{% raw %}
+[width="100%",cols="2,1m,7",options="header"]
+|=======================================================================
+| Name | Java Type | Description
+| authorizationToken | String | The default Telegram authorization token to be used when
the information is not provided in the endpoints.
+|=======================================================================
+{% endraw %}
 // component options: END
 
 
@@ -270,3 +280,32 @@ public class ChatBotLogic {
 Every non-null string returned by the `chatBotProcess` method is automatically routed to
the
 chat that originated the request (as the `CamelTelegramChatId` header is used to route the
message).
 
+[[Telegram-GettingTheChatId]]
+Getting the Chat ID
+^^^^^^^^^^^^^^^^^^^
+
+If you want to push messages to a specific Telegram chat when an event occurs, you need to
+retrieve the corresponding chat ID. The chat ID is not currently shown in the telegram client,
+but you can obtain it using a simple route.
+
+First, add the bot to the chat where you want to push messages, then run a route like the
following one.
+
+[source,java]
+---------------------------------------------------------
+from("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L")
+.to("log:INFO?showHeaders=true");
+---------------------------------------------------------
+
+Any message received by the bot will be dumped to your log together with information about
the chat (`CamelTelegramChatId`
+header).
+
+Once you get the chat ID, you can use the following sample route to push message to it.
+
+[source,java]
+---------------------------------------------------------
+from("timer:tick")
+.setBody().constant("Hello")
+to("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L?chatId=123456")
+---------------------------------------------------------
+
+Note that the corresponding URI parameter is simply `chatId`.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java
index 4f3e0d4..330fa1f 100644
--- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java
+++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramComponent.java
@@ -26,6 +26,8 @@ import org.apache.camel.impl.UriEndpointComponent;
  */
 public class TelegramComponent extends UriEndpointComponent {
 
+    private String authorizationToken;
+
     public TelegramComponent() {
         super(TelegramEndpoint.class);
     }
@@ -34,7 +36,7 @@ public class TelegramComponent extends UriEndpointComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         TelegramConfiguration configuration = new TelegramConfiguration();
         setProperties(configuration, parameters);
-        configuration.updatePathConfig(remaining);
+        configuration.updatePathConfig(remaining, this.getAuthorizationToken());
 
         if (TelegramConfiguration.ENDPOINT_TYPE_BOTS.equals(configuration.getType())) {
             return new TelegramEndpoint(uri, this, configuration);
@@ -43,4 +45,15 @@ public class TelegramComponent extends UriEndpointComponent {
         throw new IllegalArgumentException("Unsupported endpoint type for uri " + uri + ",
remaining " + remaining);
     }
 
+    public String getAuthorizationToken() {
+        return authorizationToken;
+    }
+
+    /**
+     * The default Telegram authorization token to be used when the information is not provided
in the endpoints.
+     */
+    public void setAuthorizationToken(String authorizationToken) {
+        this.authorizationToken = authorizationToken;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramConfiguration.java
----------------------------------------------------------------------
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 cd5f14c..c2181c9 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
@@ -56,21 +56,26 @@ public class TelegramConfiguration {
      * Sets the remaining configuration parameters available in the URI.
      *
      * @param remaining the URI part after the scheme
+     * @param defaultAuthorizationToken the default authorization token to use if not present
in the URI
      */
-    public void updatePathConfig(String remaining) {
+    public void updatePathConfig(String remaining, String defaultAuthorizationToken) {
         String[] parts = remaining.split("/");
-        if (parts.length != 2) {
-            throw new IllegalArgumentException("Unexpected URI format. Expected 'bots/<authorizationToken>',
found '" + remaining + "'");
+        if (parts.length == 0 || parts.length > 2) {
+            throw new IllegalArgumentException("Unexpected URI format. Expected 'bots' or
'bots/<authorizationToken>', found '" + remaining + "'");
         }
 
         String type = parts[0];
         if (!type.equals(ENDPOINT_TYPE_BOTS)) {
-            throw new IllegalArgumentException("Unexpected endpoint type. Expected 'bots',
found '" + parts[0] + "'");
+            throw new IllegalArgumentException("Unexpected endpoint type. Expected 'bots',
found '" + type + "'");
         }
 
-        String authorizationToken = parts[1];
-        if (authorizationToken.length() == 0) {
-            throw new IllegalArgumentException("Authorization token is required");
+        String authorizationToken = defaultAuthorizationToken;
+        if (parts.length > 1) {
+            authorizationToken = parts[1];
+        }
+
+        if (authorizationToken == null || authorizationToken.trim().length() == 0) {
+            throw new IllegalArgumentException("The authorization token must be provided
and cannot be empty");
         }
 
         this.type = type;

http://git-wip-us.apache.org/repos/asf/camel/blob/ec9f00ec/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..6aa0392
--- /dev/null
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramComponentParametersTest.java
@@ -0,0 +1,77 @@
+/**
+ * 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.telegram;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.telegram.model.UpdateResult;
+import org.apache.camel.component.telegram.util.TelegramTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests the usage of defaults in the component configuration
+ */
+public class TelegramComponentParametersTest extends TelegramTestSupport {
+
+    @Test
+    public void testDefaultsAndOverrides() throws Exception {
+
+        TelegramComponent component = (TelegramComponent) context().getComponent("telegram");
+
+        component.setAuthorizationToken("DEFAULT");
+
+        TelegramEndpoint ep1 = (TelegramEndpoint) component.createEndpoint("bots");
+        assertEquals("DEFAULT", ep1.getConfiguration().getAuthorizationToken());
+
+        TelegramEndpoint ep2 = (TelegramEndpoint) component.createEndpoint("bots/CUSTOM");
+        assertEquals("CUSTOM", ep2.getConfiguration().getAuthorizationToken());
+
+        TelegramEndpoint ep3 = (TelegramEndpoint) component.createEndpoint("bots/ANOTHER?chatId=123");
+        assertEquals("ANOTHER", ep3.getConfiguration().getAuthorizationToken());
+
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNonDefaultConfig() throws Exception {
+        TelegramComponent component = (TelegramComponent) context().getComponent("telegram");
+        component.setAuthorizationToken(null);
+        component.createEndpoint("bots");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWrongURI1() throws Exception {
+        TelegramComponent component = (TelegramComponent) context().getComponent("telegram");
+        component.setAuthorizationToken("ANY");
+        component.createEndpoint("bots/ ");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWrongURI2() throws Exception {
+        TelegramComponent component = (TelegramComponent) context().getComponent("telegram");
+        component.setAuthorizationToken("ANY");
+        component.createEndpoint("bots/token/s");
+    }
+
+}


Mime
View raw message