You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2020/04/05 19:50:40 UTC

[incubator-streampipes-extensions] 03/03: Configure Slack authentication of Slack sink in element description

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

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git

commit b852632374a2354a26abe10782e01d1e04888d33
Author: Dominik Riemer <ri...@fzi.de>
AuthorDate: Sun Apr 5 21:50:20 2020 +0200

    Configure Slack authentication of Slack sink in element description
---
 .../notifications/jvm/slack/SlackNotification.java | 40 ++++++++++++++++---
 .../jvm/slack/SlackNotificationController.java     | 45 ++--------------------
 .../jvm/slack/SlackNotificationParameters.java     | 23 +++++------
 .../strings.en                                     |  5 ++-
 4 files changed, 52 insertions(+), 61 deletions(-)

diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
index 2ddd7ae..eb9ba37 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotification.java
@@ -19,6 +19,9 @@
 package org.apache.streampipes.sinks.notifications.jvm.slack;
 
 import com.ullink.slack.simpleslackapi.SlackChannel;
+import com.ullink.slack.simpleslackapi.SlackSession;
+import com.ullink.slack.simpleslackapi.SlackUser;
+import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory;
 import org.apache.streampipes.commons.exceptions.SpRuntimeException;
 import org.apache.streampipes.model.runtime.Event;
 import org.apache.streampipes.wrapper.context.EventSinkRuntimeContext;
@@ -28,28 +31,55 @@ import java.io.IOException;
 
 
 public class SlackNotification implements EventSink<SlackNotificationParameters> {
+
     private SlackNotificationParameters params;
+    private SlackSession session;
+    private Boolean sendToUser;
 
     @Override
     public void onInvocation(SlackNotificationParameters parameters, EventSinkRuntimeContext runtimeContext) throws SpRuntimeException {
         this.params = parameters;
+            // Initialize slack session
+            this.session = SlackSessionFactory.createWebSocketSlackSession(params.getAuthToken());
+            try {
+                this.session.connect();
+            } catch (IOException e) {
+                e.printStackTrace();
+                throw new SpRuntimeException("Could not connect to Slack");
+            }
+
+            this.sendToUser = params.getChannelType().equals("User");
+
+            if (this.sendToUser) {
+                SlackUser user = session.findUserByUserName(params.getUserChannel());
+                if (user == null) {
+                    throw new SpRuntimeException("The user: '" + params.getUserChannel() + "' does not exists");
+                }
+            } else {
+                SlackChannel channel = session.findChannelByName(params.getUserChannel());
+                if (channel == null || channel.getId() == null) {
+                    throw new SpRuntimeException("The channel: '" + params.getUserChannel() + "' does not " +
+                            "exists or " +
+                    "the bot has no rights to access it");
+                }
+            }
     }
 
     @Override
     public void onEvent(Event inputEvent) {
 
-        if (params.isSendToUser()) {
-            params.getSession().sendMessageToUser(params.getUserChannel(), params.getMessage(), null);
+        if (this.sendToUser) {
+            this.session.sendMessageToUser(params.getUserChannel(), params.getMessage(), null);
         } else {
-            SlackChannel channel = params.getSession().findChannelByName(params.getUserChannel());
-            params.getSession().sendMessage(channel, params.getMessage());
+            SlackChannel channel = this.session.findChannelByName(params.getUserChannel());
+            this.session.sendMessage(channel, params.getMessage());
         }
     }
 
     @Override
     public void onDetach() throws SpRuntimeException {
         try {
-            params.getSession().disconnect();
+            this.session.disconnect();
         } catch (IOException e) {
             throw new SpRuntimeException("Could not disconnect");
         }
diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
index 92a7ff1..e67b047 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationController.java
@@ -18,10 +18,6 @@
 
 package org.apache.streampipes.sinks.notifications.jvm.slack;
 
-import com.ullink.slack.simpleslackapi.SlackChannel;
-import com.ullink.slack.simpleslackapi.SlackSession;
-import com.ullink.slack.simpleslackapi.SlackUser;
-import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory;
 import org.apache.streampipes.model.DataSinkType;
 import org.apache.streampipes.model.graph.DataSinkDescription;
 import org.apache.streampipes.model.graph.DataSinkInvocation;
@@ -33,18 +29,15 @@ import org.apache.streampipes.sdk.helpers.Labels;
 import org.apache.streampipes.sdk.helpers.Locales;
 import org.apache.streampipes.sdk.helpers.Options;
 import org.apache.streampipes.sdk.utils.Assets;
-import org.apache.streampipes.sinks.notifications.jvm.config.ConfigKeys;
-import org.apache.streampipes.sinks.notifications.jvm.config.SinksNotificationsJvmConfig;
 import org.apache.streampipes.wrapper.standalone.ConfiguredEventSink;
 import org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventSinkDeclarer;
 
-import java.io.IOException;
-
 public class SlackNotificationController extends StandaloneEventSinkDeclarer<SlackNotificationParameters> {
 
   private static final String CHANNEL_TYPE = "channel-type";
   private static final String RECEIVER = "receiver";
   private static final String CONTENT = "content";
+  private static final String AUTH_TOKEN = "auth-token";
 
   @Override
   public DataSinkDescription declareModel() {
@@ -61,52 +54,22 @@ public class SlackNotificationController extends StandaloneEventSinkDeclarer<Sla
             .requiredTextParameter(Labels.withId(CONTENT))
             .requiredSingleValueSelection(Labels.withId(CHANNEL_TYPE),
                     Options.from("User", "Channel"))
+            .requiredSecret(Labels.withId(AUTH_TOKEN))
             .build();
   }
 
 
   @Override
   public ConfiguredEventSink<SlackNotificationParameters> onInvocation(DataSinkInvocation graph, DataSinkParameterExtractor extractor) {
-    String authToken = SinksNotificationsJvmConfig.INSTANCE.getSlackToken();
-
-    if (authToken != ConfigKeys.SLACK_NOT_INITIALIZED) {
-      // Initialize slack session
-      SlackSession session = SlackSessionFactory.createWebSocketSlackSession(authToken);
-      try {
-        session.connect();
-      } catch (IOException e) {
-        e.printStackTrace();
-        //throw new SpRuntimeException("Could not connect to Slack");
-      }
 
       String userChannel = extractor.singleValueParameter(RECEIVER, String.class);
       String channelType = extractor.selectedSingleValue(CHANNEL_TYPE, String.class);
       String message = extractor.singleValueParameter(CONTENT, String.class);
-
-      boolean sendToUser = channelType.equals("User");
-
-      if (sendToUser) {
-        SlackUser user = session.findUserByUserName(userChannel);
-        if (user == null) {
-          //throw new SpRuntimeException("The user: '" + userChannel + "' does not exists");
-        }
-      } else {
-        SlackChannel channel = session.findChannelByName(userChannel);
-        if (channel == null || channel.getId() == null) {
-          //throw new SpRuntimeException("The channel: '" + userChannel + "' does not exists or " +
-                  //"the bot has no rights to access it");
-        }
-      }
+      String authToken = extractor.secretValue(AUTH_TOKEN);
 
       SlackNotificationParameters params = new SlackNotificationParameters(graph, authToken,
-              sendToUser, userChannel, message, session);
+              channelType, userChannel, message);
 
       return new ConfiguredEventSink<>(params, SlackNotification::new);
-
-    } else {
-      // TODO Exception handling of onInvocation
-      //throw new SpRuntimeException("Slack not initialized");
-      return null;
-    }
   }
 }
diff --git a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationParameters.java b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationParameters.java
index 72ecaa2..909aee2 100644
--- a/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationParameters.java
+++ b/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/slack/SlackNotificationParameters.java
@@ -18,35 +18,31 @@
 
 package org.apache.streampipes.sinks.notifications.jvm.slack;
 
-import com.ullink.slack.simpleslackapi.SlackSession;
 import org.apache.streampipes.model.graph.DataSinkInvocation;
 import org.apache.streampipes.wrapper.params.binding.EventSinkBindingParams;
 
 public class SlackNotificationParameters extends EventSinkBindingParams {
     private String authToken;
-    private boolean sendToUser;
     private String userChannel;
+    private String channelType;
     private String message;
-    private SlackSession session;
 
-    public SlackNotificationParameters(DataSinkInvocation graph, String authToken, boolean
-            sendToUser, String userChannel, String message, SlackSession session) {
+    public SlackNotificationParameters(DataSinkInvocation graph,
+                                       String authToken,
+                                       String channelType,
+                                       String userChannel,
+                                       String message) {
         super(graph);
         this.authToken = authToken;
-        this.sendToUser = sendToUser;
         this.userChannel = userChannel;
         this.message = message;
-        this.session = session;
+        this.channelType = channelType;
     }
 
     public String getAuthToken() {
         return authToken;
     }
 
-    public boolean isSendToUser() {
-        return sendToUser;
-    }
-
     public String getUserChannel() {
         return userChannel;
     }
@@ -55,9 +51,8 @@ public class SlackNotificationParameters extends EventSinkBindingParams {
         return message;
     }
 
-    public SlackSession getSession() {
-        return session;
+    public String getChannelType() {
+        return channelType;
     }
-
 }
 
diff --git a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.slack/strings.en b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.slack/strings.en
index 4d1d31f..ae23f47 100644
--- a/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.slack/strings.en
+++ b/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.slack/strings.en
@@ -8,4 +8,7 @@ content.title=Message
 content.description=The message that should be sent
 
 channel-type.title=User or Channel
-channel-type.description=Decide wether you want to sent a notification to a user or to a channel
\ No newline at end of file
+channel-type.description=Decide wether you want to sent a notification to a user or to a channel
+
+auth-token.title=Auth Token
+auth-token.description=The token to authenticate at Slack
\ No newline at end of file