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 2021/10/05 21:56:09 UTC

[incubator-streampipes] branch STREAMPIPES-426 updated: [STREAMPIPES-441] Add endpoint to send emails over API and client

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

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


The following commit(s) were added to refs/heads/STREAMPIPES-426 by this push:
     new 101da1e  [STREAMPIPES-441] Add endpoint to send emails over API and client
101da1e is described below

commit 101da1e1fe5c6f47f7b9a9b8fb87f299cb92d9e4
Author: Dominik Riemer <ri...@fzi.de>
AuthorDate: Tue Oct 5 23:55:54 2021 +0200

    [STREAMPIPES-441] Add endpoint to send emails over API and client
---
 .../backend/StreamPipesResourceConfig.java         |  1 +
 .../streampipes/client/StreamPipesClient.java      |  7 +++
 .../apache/streampipes/client/paths/ApiPath.java   | 23 +++++++
 .../apache/streampipes/mail/AbstractMailer.java    | 72 ++++++++++++++++++++++
 .../mail/{MailTester.java => MailSender.java}      | 27 +++-----
 .../org/apache/streampipes/mail/MailTester.java    | 16 ++---
 .../org/apache/streampipes/model/mail/SpEmail.java | 68 ++++++++++++++++++++
 .../streampipes/rest/impl/EmailResource.java       | 45 ++++++++++++++
 8 files changed, 230 insertions(+), 29 deletions(-)

diff --git a/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java b/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
index 5200547..fe22e81 100644
--- a/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
+++ b/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
@@ -65,6 +65,7 @@ public class StreamPipesResourceConfig extends ResourceConfig {
         register(DataStream.class);
         register(Deployment.class);
         register(EmailConfigurationResource.class);
+        register(EmailResource.class);
         register(ExtensionsServiceEndpointResource.class);
         register(FileServingResource.class);
         register(InternalPipelineTemplates.class);
diff --git a/streampipes-client/src/main/java/org/apache/streampipes/client/StreamPipesClient.java b/streampipes-client/src/main/java/org/apache/streampipes/client/StreamPipesClient.java
index 11a68a6..269a5e8 100644
--- a/streampipes-client/src/main/java/org/apache/streampipes/client/StreamPipesClient.java
+++ b/streampipes-client/src/main/java/org/apache/streampipes/client/StreamPipesClient.java
@@ -22,10 +22,12 @@ import org.apache.streampipes.client.credentials.CredentialsProvider;
 import org.apache.streampipes.client.model.ClientConnectionUrlResolver;
 import org.apache.streampipes.client.model.StreamPipesClientConfig;
 import org.apache.streampipes.client.model.StreamPipesClientConnectionConfig;
+import org.apache.streampipes.client.paths.ApiPath;
 import org.apache.streampipes.dataformat.SpDataFormatFactory;
 import org.apache.streampipes.dataformat.cbor.CborDataFormatFactory;
 import org.apache.streampipes.dataformat.fst.FstDataFormatFactory;
 import org.apache.streampipes.dataformat.json.JsonDataFormatFactory;
+import org.apache.streampipes.model.mail.SpEmail;
 
 public class StreamPipesClient implements SupportsPipelineApi,
         SupportsPipelineElementTemplateApi,
@@ -175,4 +177,9 @@ public class StreamPipesClient implements SupportsPipelineApi,
   public CustomRequestApi customRequest() {
     return new CustomRequestApi(config);
   }
+
+  public void deliverEmail(SpEmail email) {
+    CustomRequestApi api = customRequest();
+    api.sendPost(ApiPath.EMAIL_RESOURCE, email);
+  }
 }
diff --git a/streampipes-client/src/main/java/org/apache/streampipes/client/paths/ApiPath.java b/streampipes-client/src/main/java/org/apache/streampipes/client/paths/ApiPath.java
new file mode 100644
index 0000000..fa1f82a
--- /dev/null
+++ b/streampipes-client/src/main/java/org/apache/streampipes/client/paths/ApiPath.java
@@ -0,0 +1,23 @@
+/*
+ * 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.streampipes.client.paths;
+
+public class ApiPath {
+
+  public static String EMAIL_RESOURCE = "api/v2/mail";
+}
diff --git a/streampipes-mail/src/main/java/org/apache/streampipes/mail/AbstractMailer.java b/streampipes-mail/src/main/java/org/apache/streampipes/mail/AbstractMailer.java
new file mode 100644
index 0000000..2bd908b
--- /dev/null
+++ b/streampipes-mail/src/main/java/org/apache/streampipes/mail/AbstractMailer.java
@@ -0,0 +1,72 @@
+/*
+ * 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.streampipes.mail;
+
+import org.apache.streampipes.config.backend.BackendConfig;
+import org.apache.streampipes.config.backend.model.EmailConfig;
+import org.apache.streampipes.mail.config.MailConfigurationBuilder;
+import org.simplejavamail.api.email.Email;
+import org.simplejavamail.api.email.EmailPopulatingBuilder;
+import org.simplejavamail.api.email.Recipient;
+import org.simplejavamail.api.mailer.Mailer;
+import org.simplejavamail.email.EmailBuilder;
+
+import javax.mail.Message;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class AbstractMailer {
+
+  protected Mailer getMailer() {
+    return getMailer(getEmailConfig());
+  }
+
+  protected Mailer getMailer(EmailConfig config) {
+    return new MailConfigurationBuilder().buildMailerFromConfig(config);
+  }
+
+  protected EmailConfig getEmailConfig() {
+    return BackendConfig.INSTANCE.getEmailConfig();
+  }
+
+  protected void deliverMail(Email email) {
+    deliverMail(getEmailConfig(), email);
+  }
+
+  protected void deliverMail(EmailConfig config, Email email) {
+    Mailer mailer = getMailer(config);
+    mailer.sendMail(email);
+  }
+
+  protected EmailPopulatingBuilder baseEmail() {
+    return baseEmail(getEmailConfig());
+  }
+
+  protected EmailPopulatingBuilder baseEmail(EmailConfig config) {
+    return EmailBuilder
+            .startingBlank()
+            .from(config.getSenderAddress());
+  }
+
+  protected List<Recipient> toSimpleRecipientList(List<String> recipients) {
+    return recipients
+            .stream()
+            .map(r -> new Recipient("", r, Message.RecipientType.TO))
+            .collect(Collectors.toList());
+  }
+}
diff --git a/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java b/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailSender.java
similarity index 56%
copy from streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java
copy to streampipes-mail/src/main/java/org/apache/streampipes/mail/MailSender.java
index f38f469..588de06 100644
--- a/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java
+++ b/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailSender.java
@@ -17,27 +17,18 @@
  */
 package org.apache.streampipes.mail;
 
-import org.apache.streampipes.config.backend.model.EmailConfig;
-import org.apache.streampipes.mail.config.MailConfigurationBuilder;
+import org.apache.streampipes.model.mail.SpEmail;
 import org.simplejavamail.api.email.Email;
-import org.simplejavamail.api.mailer.Mailer;
-import org.simplejavamail.email.EmailBuilder;
 
-public class MailTester {
+public class MailSender extends AbstractMailer {
 
-  public void sendTestMail(EmailConfig emailConfig) {
-    Mailer mailer = new MailConfigurationBuilder().buildMailerFromConfig(emailConfig);
-
-    mailer.sendMail(makeTestMail(emailConfig));
-  }
-
-  private Email makeTestMail(EmailConfig emailConfig) {
-    return EmailBuilder
-            .startingBlank()
-            .appendText("Hello from StreamPipes")
-            .from(emailConfig.getSenderAddress())
-            .to(emailConfig.getTestRecipientAddress())
+  public void sendEmail(SpEmail mail) {
+    Email email = baseEmail()
+            .withRecipients(toSimpleRecipientList(mail.getRecipients()))
+            .withSubject(mail.getSubject())
+            .appendText(mail.getMessage())
             .buildEmail();
-  }
 
+    deliverMail(email);
+  }
 }
diff --git a/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java b/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java
index f38f469..26ca20d 100644
--- a/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java
+++ b/streampipes-mail/src/main/java/org/apache/streampipes/mail/MailTester.java
@@ -18,24 +18,18 @@
 package org.apache.streampipes.mail;
 
 import org.apache.streampipes.config.backend.model.EmailConfig;
-import org.apache.streampipes.mail.config.MailConfigurationBuilder;
 import org.simplejavamail.api.email.Email;
-import org.simplejavamail.api.mailer.Mailer;
-import org.simplejavamail.email.EmailBuilder;
 
-public class MailTester {
+public class MailTester extends AbstractMailer{
 
   public void sendTestMail(EmailConfig emailConfig) {
-    Mailer mailer = new MailConfigurationBuilder().buildMailerFromConfig(emailConfig);
-
-    mailer.sendMail(makeTestMail(emailConfig));
+    deliverMail(emailConfig, makeTestMail(emailConfig));
   }
 
   private Email makeTestMail(EmailConfig emailConfig) {
-    return EmailBuilder
-            .startingBlank()
-            .appendText("Hello from StreamPipes")
-            .from(emailConfig.getSenderAddress())
+    return baseEmail(emailConfig)
+            .withSubject("Hello from Apache StreamPipes")
+            .appendText("Your email configuration is working!")
             .to(emailConfig.getTestRecipientAddress())
             .buildEmail();
   }
diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/mail/SpEmail.java b/streampipes-model/src/main/java/org/apache/streampipes/model/mail/SpEmail.java
new file mode 100644
index 0000000..3cfcaf6
--- /dev/null
+++ b/streampipes-model/src/main/java/org/apache/streampipes/model/mail/SpEmail.java
@@ -0,0 +1,68 @@
+/*
+ * 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.streampipes.model.mail;
+
+import java.util.List;
+
+public class SpEmail {
+
+  private List<String> recipients;
+  private String subject;
+  private String message;
+
+  public static SpEmail from(List<String> recipients,
+                             String subject,
+                             String message) {
+    return new SpEmail(recipients, subject, message);
+  }
+
+  public SpEmail() {
+  }
+
+  public SpEmail(List<String> recipients,
+                  String subject,
+                  String message) {
+    this.recipients = recipients;
+    this.subject = subject;
+    this.message = message;
+  }
+
+  public List<String> getRecipients() {
+    return recipients;
+  }
+
+  public void setRecipients(List<String> recipients) {
+    this.recipients = recipients;
+  }
+
+  public String getSubject() {
+    return subject;
+  }
+
+  public void setSubject(String subject) {
+    this.subject = subject;
+  }
+
+  public String getMessage() {
+    return message;
+  }
+
+  public void setMessage(String message) {
+    this.message = message;
+  }
+}
diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/EmailResource.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/EmailResource.java
new file mode 100644
index 0000000..6ecd9d6
--- /dev/null
+++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/EmailResource.java
@@ -0,0 +1,45 @@
+/*
+ * 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.streampipes.rest.impl;
+
+import org.apache.streampipes.config.backend.BackendConfig;
+import org.apache.streampipes.mail.MailSender;
+import org.apache.streampipes.model.mail.SpEmail;
+import org.apache.streampipes.rest.core.base.impl.AbstractAuthGuardedRestResource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+
+@Path("v2/mail")
+public class EmailResource extends AbstractAuthGuardedRestResource {
+
+  @POST
+  @Consumes(MediaType.APPLICATION_JSON)
+  public Response sendEmail(SpEmail email) {
+    if (BackendConfig.INSTANCE.getEmailConfig().isEmailConfigured()) {
+      new MailSender().sendEmail(email);
+      return ok();
+    } else {
+      return serverError("Could not send email - no valid mail configuration provided in StreamPipes (go to settings -> mail)");
+    }
+  }
+}