You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by mg...@apache.org on 2014/12/29 21:13:45 UTC

[3/6] isis git commit: ISIS-987 Provide some sort of mechanism to allow users to self-register for an Isis application.

http://git-wip-us.apache.org/repos/asf/isis/blob/3bd8294d/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailNotificationServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailNotificationServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailNotificationServiceDefault.java
new file mode 100644
index 0000000..d4321a7
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailNotificationServiceDefault.java
@@ -0,0 +1,106 @@
+package org.apache.isis.core.runtime.services.email;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Properties;
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import org.apache.commons.mail.DefaultAuthenticator;
+import org.apache.commons.mail.Email;
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.HtmlEmail;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.applib.annotation.DomainService;
+import org.apache.isis.applib.services.email.EmailNotificationService;
+import org.apache.isis.applib.services.email.events.EmailRegistrationEvent;
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+
+/**
+ * TODO ISIS-987 Javadoc
+ */
+@DomainService
+public class EmailNotificationServiceDefault implements EmailNotificationService {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(EmailNotificationServiceDefault.class);
+
+    @Override
+    public boolean send(EmailRegistrationEvent emailRegistrationEvent) {
+
+        boolean mailSent = true;
+        try {
+            String senderEmailAddress = getSenderEmailAddress();
+            String senderEmailPasswd  = getSenderEmailPassword();
+
+            Email email = new HtmlEmail();
+            email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPasswd));
+            email.setHostName(getSenderEmailHostName());
+            email.setSmtpPort(getSenderEmailPort());
+            email.setStartTLSEnabled(getSenderEmailTlsEnabled());
+            Properties properties = email.getMailSession().getProperties();
+            // TODO mgrigorov: check whether all these are required and extract as configuration settings
+            properties.put("mail.smtps.auth", "true");
+            properties.put("mail.debug", "true");
+            properties.put("mail.smtps.port", "587");
+            properties.put("mail.smtps.socketFactory.port", "587");
+            properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+            properties.put("mail.smtps.socketFactory.fallback", "false");
+            properties.put("mail.smtp.starttls.enable", "true");
+
+            email.setFrom(senderEmailAddress);
+
+            email.setSubject("SignUp verification");
+            email.setMsg(loadMessage(emailRegistrationEvent));
+
+            // TODO ISIS-987 use the provided email once development/testing is done
+            email.addTo("martin.grigorov@gmail.com");
+            email.send();
+
+        } catch (EmailException ex) {
+            LOGGER.error("An error occurred while trying to send an email about user email verification", ex);
+            mailSent = false;
+        }
+
+        return mailSent;
+    }
+
+    private String loadMessage(EmailRegistrationEvent emailRegistrationEvent) {
+        String message;
+        try {
+            URL emailVerificationTemplateUrl = Resources.getResource(EmailNotificationServiceDefault.class, "EmailVerificationTemplate.html");
+            String emailVerificationTemplate = Resources.toString(emailVerificationTemplateUrl, Charsets.UTF_8);
+            message = emailVerificationTemplate.replace("${email}", emailRegistrationEvent.getEmail());
+            message = message.replace("${confirmationUrl}", emailRegistrationEvent.getConfirmationUrl());
+        } catch (IOException e) {
+            e.printStackTrace();
+            message = "Problem: " + e.getMessage();
+        }
+
+        return message;
+    }
+
+    protected String getSenderEmailAddress() {
+        return getConfiguration().getString("isis.notification.email.sender.address");
+    }
+
+    protected String getSenderEmailPassword() {
+        return getConfiguration().getString("isis.notification.email.sender.password");
+    }
+
+    protected String getSenderEmailHostName() {
+        return getConfiguration().getString("isis.notification.email.sender.hostname", "smtp.gmail.com");
+    }
+
+    protected Integer getSenderEmailPort() {
+        return getConfiguration().getInteger("isis.notification.email.port", 587);
+    }
+
+    protected Boolean getSenderEmailTlsEnabled() {
+        return getConfiguration().getBoolean("isis.notification.email.tls.enabled", true);
+    }
+
+    private IsisConfiguration getConfiguration() {
+        return IsisContext.getConfiguration();
+    }
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/3bd8294d/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailSendingServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailSendingServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailSendingServiceDefault.java
deleted file mode 100644
index 0bb4bc5..0000000
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailSendingServiceDefault.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package org.apache.isis.core.runtime.services.email;
-
-import java.util.Properties;
-import org.apache.commons.mail.DefaultAuthenticator;
-import org.apache.commons.mail.Email;
-import org.apache.commons.mail.EmailException;
-import org.apache.commons.mail.HtmlEmail;
-import org.apache.isis.applib.annotation.DomainService;
-import org.apache.isis.applib.services.email.EmailSendingService;
-import org.apache.isis.applib.services.email.events.UserCreationEvent;
-
-/**
- *
- */
-@DomainService
-public class EmailSendingServiceDefault implements EmailSendingService {
-
-    private static final String EMAIL = "apache.isis.test@gmail.com";
-    private static final String PASSWD = "ApacheIsis987^";
-
-    @Override
-    public void send(UserCreationEvent userCreationEvent) {
-
-        try {
-            Email email = new HtmlEmail();
-            email.setHostName("smtp.gmail.com");
-            email.setSmtpPort(587);
-            email.setAuthenticator(new DefaultAuthenticator(EMAIL, PASSWD));
-            email.setStartTLSEnabled(true);
-            Properties properties = email.getMailSession().getProperties();
-            properties.put("mail.smtps.auth", "true");
-            properties.put("mail.debug", "true");
-            properties.put("mail.smtps.port", "587");
-            properties.put("mail.smtps.socketFactory.port", "587");
-            properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
-            properties.put("mail.smtps.socketFactory.fallback", "false");
-            properties.put("mail.smtp.starttls.enable", "true");
-
-            email.setFrom("apache.isis.test@gmail.com");
-
-            email.setSubject("SignUp verification");
-            email.setMsg("This is a test mail ... :-)\n\nEmail: " +
-                         userCreationEvent.getEmail() + "\n\nPasswd: " + userCreationEvent.getPassword());
-            email.addTo("martin.grigorov@gmail.com");
-            email.send();
-
-        } catch (EmailException e) {
-            e.printStackTrace();
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/isis/blob/3bd8294d/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailVerificationTemplate.html
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailVerificationTemplate.html b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailVerificationTemplate.html
new file mode 100644
index 0000000..0ab564f
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailVerificationTemplate.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+    <head lang="en">
+        <meta charset="UTF-8">
+        <title>Email verification</title>
+    </head>
+    <body>
+        Welcome, <strong>${email}</strong>!
+
+        <p>
+            Please click on the link to confirm that you own this email and create an account: <a href="${confirmationUrl}">confirm</a>.
+        </p>
+
+    </body>
+</html>

http://git-wip-us.apache.org/repos/asf/isis/blob/3bd8294d/example/application/todoapp/webapp/src/main/webapp/WEB-INF/viewer_wicket.properties
----------------------------------------------------------------------
diff --git a/example/application/todoapp/webapp/src/main/webapp/WEB-INF/viewer_wicket.properties b/example/application/todoapp/webapp/src/main/webapp/WEB-INF/viewer_wicket.properties
index ea62094..4e4c088 100644
--- a/example/application/todoapp/webapp/src/main/webapp/WEB-INF/viewer_wicket.properties
+++ b/example/application/todoapp/webapp/src/main/webapp/WEB-INF/viewer_wicket.properties
@@ -84,3 +84,9 @@ isis.viewer.wicket.themes.showChooser=true
 #
 #isis.viewer.wicket.themes.enabled=bootstrap-theme,Cosmo,Flatly,Darkly,Sandstone,United
 isis.viewer.wicket.themes.enabled=bootstrap-theme,Cosmo,Flatly,Darkly,Sandstone,United
+
+#
+# TODO ISIS-987 Add docu and all other properties
+#
+#isis.notification.email.sender.address=some.valid@email.address
+#isis.notification.email.sender.password=the.password.for-isis.notification.email.sender.address