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/16 15:42:14 UTC

[sling-org-apache-sling-committer-cli] 18/21: 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 master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git

commit 05ae0d8f253fee2cfead5e2bc0ae837594f10334
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Thu Mar 28 18:51:14 2019 +0100

    SLING-8311 - Investigate creating a Sling CLI tool for development task automation
    
    * added a basic service for sending emails (needs to be tested)
---
 src/main/features/app.json                         |  3 +
 .../org/apache/sling/cli/impl/mail/Mailer.java     | 79 ++++++++++++++++++++++
 .../org/apache/sling/cli/impl/people/Member.java   | 12 +++-
 3 files changed, 91 insertions(+), 3 deletions(-)

diff --git a/src/main/features/app.json b/src/main/features/app.json
index 9b47884..5daa106 100644
--- a/src/main/features/app.json
+++ b/src/main/features/app.json
@@ -64,6 +64,9 @@
         {
             "id"         : "org.apache.servicemix.bundles:org.apache.servicemix.bundles.jsch:0.1.55_1",
             "start-level": "3"
+        },
+        {
+            "id":"javax.mail:mail:1.5.0-b01"
         }
     ]
 }
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
new file mode 100644
index 0000000..e4bd604
--- /dev/null
+++ b/src/main/java/org/apache/sling/cli/impl/mail/Mailer.java
@@ -0,0 +1,79 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.sling.cli.impl.mail;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+import javax.mail.Authenticator;
+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.CredentialsService;
+import org.apache.sling.cli.impl.people.MembersFinder;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component(
+        service = Mailer.class
+)
+public class Mailer {
+
+    private static final Properties SMTP_PROPERTIES = new Properties() {{
+        put("mail.smtp.host", "mail-relay.apache.org");
+        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");
+    }};
+
+    @Reference
+    private CredentialsService credentialsService;
+
+    @Reference
+    private MembersFinder membersFinder;
+
+    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());
+            }
+        });
+        try {
+            MimeMessage message = new MimeMessage(session);
+            message.setFrom(membersFinder.getCurrentMember().getEmail());
+            message.setSubject(subject);
+            message.setText(body, StandardCharsets.UTF_8.name());
+            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
+            Transport.send(message);
+        } catch (MessagingException e) {
+
+        }
+
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/cli/impl/people/Member.java b/src/main/java/org/apache/sling/cli/impl/people/Member.java
index 7b3cb16..5602c66 100644
--- a/src/main/java/org/apache/sling/cli/impl/people/Member.java
+++ b/src/main/java/org/apache/sling/cli/impl/people/Member.java
@@ -20,14 +20,16 @@ package org.apache.sling.cli.impl.people;
 
 public class Member {
 
-    private String id;
-    private String name;
-    private boolean isPMCMember;
+    private final String id;
+    private final String name;
+    private final boolean isPMCMember;
+    private final String email;
 
     Member(String id, String name, boolean isPMCMember) {
         this.id = id;
         this.name = name;
         this.isPMCMember = isPMCMember;
+        email = id + "@apache.org";
     }
 
     public String getId() {
@@ -42,6 +44,10 @@ public class Member {
         return isPMCMember;
     }
 
+    public String getEmail() {
+        return email;
+    }
+
     @Override
     public int hashCode() {
         return id.hashCode();