You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/04/24 10:48:29 UTC

[sling-org-apache-sling-committer-cli] 19/42: SLING-8311 - Investigate creating a Sling CLI tool for development task automation

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

rombert pushed a commit to branch feature/SLING-8337
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git

commit d9ae57105690c4ddaf25301bbd07d816f6a0defa
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Fri Mar 29 15:23:50 2019 +0100

    SLING-8311 - Investigate creating a Sling CLI tool for development task automation
    
    * finished Mailer implementation
---
 Dockerfile                                         |  2 +-
 src/main/features/app.json                         |  5 +++-
 .../org/apache/sling/cli/impl/mail/Mailer.java     | 30 ++++++++++++----------
 3 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index ca438e9..48d4d4e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,7 +11,7 @@
 # ----------------------------------------------------------------------------------------
 FROM azul/zulu-openjdk-alpine:11 as builder
 MAINTAINER dev@sling.apache.org
-RUN jlink --add-modules java.logging,java.naming,java.xml,java.security.jgss,java.sql,jdk.crypto.ec \
+RUN jlink --add-modules java.logging,java.naming,java.xml,java.security.jgss,java.sql,jdk.crypto.ec,java.desktop \
           --output /opt/jre --strip-debug --compress=2 --no-header-files --no-man-pages
 
 FROM alpine
diff --git a/src/main/features/app.json b/src/main/features/app.json
index 5daa106..d8a2bb2 100644
--- a/src/main/features/app.json
+++ b/src/main/features/app.json
@@ -66,7 +66,10 @@
             "start-level": "3"
         },
         {
-            "id":"javax.mail:mail:1.5.0-b01"
+            "id": "javax.mail:mail:1.5.0-b01"
+        },
+        {
+            "id": "org.apache.sling:org.apache.sling.javax.activation:0.1.0"
         }
     ]
 }
diff --git a/src/main/java/org/apache/sling/cli/impl/mail/Mailer.java b/src/main/java/org/apache/sling/cli/impl/mail/Mailer.java
index e4bd604..0c0a4e4 100644
--- a/src/main/java/org/apache/sling/cli/impl/mail/Mailer.java
+++ b/src/main/java/org/apache/sling/cli/impl/mail/Mailer.java
@@ -18,32 +18,38 @@
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 package org.apache.sling.cli.impl.mail;
 
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 import java.util.Properties;
 
-import javax.mail.Authenticator;
+import javax.mail.Address;
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 
+import org.apache.sling.cli.impl.Credentials;
 import org.apache.sling.cli.impl.CredentialsService;
+import org.apache.sling.cli.impl.people.Member;
 import org.apache.sling.cli.impl.people.MembersFinder;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Component(
         service = Mailer.class
 )
 public class Mailer {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Mailer.class);
+
     private static final Properties SMTP_PROPERTIES = new Properties() {{
         put("mail.smtp.host", "mail-relay.apache.org");
+        put("mail.smtp.port", "465");
         put("mail.smtp.auth", "true");
-        put("mail.smtp.socketFactory.port", 465);
         put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         put("mail.smtp.socketFactory.fallback", "false");
     }};
@@ -56,22 +62,18 @@ public class Mailer {
 
     public void send(String to, String subject, String body) {
         Properties properties = new Properties(SMTP_PROPERTIES);
-        Session session = Session.getDefaultInstance(properties, new Authenticator() {
-            @Override
-            protected PasswordAuthentication getPasswordAuthentication() {
-                return new PasswordAuthentication(credentialsService.getCredentials().getUsername(),
-                        credentialsService.getCredentials().getPassword());
-            }
-        });
+        Session session = Session.getInstance(properties);
         try {
             MimeMessage message = new MimeMessage(session);
-            message.setFrom(membersFinder.getCurrentMember().getEmail());
+            Member sender = membersFinder.getCurrentMember();
+            Credentials credentials = credentialsService.getCredentials();
+            message.setFrom(new InternetAddress(sender.getEmail(), sender.getEmail(), StandardCharsets.UTF_8.name()));
             message.setSubject(subject);
             message.setText(body, StandardCharsets.UTF_8.name());
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
-            Transport.send(message);
-        } catch (MessagingException e) {
-
+            Transport.send(message, new Address[] {new InternetAddress(to)}, credentials.getUsername(), credentials.getPassword());
+        } catch (MessagingException | UnsupportedEncodingException e) {
+            LOGGER.error(String.format("Unable to send email with Subject '%s' to '%s'.", subject, to), e);
         }
 
     }