You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2017/09/18 09:23:00 UTC

isis git commit: ISIS-1706: adds support to use a different username than the sender address when authenticating with the Email SMTP service

Repository: isis
Updated Branches:
  refs/heads/master bf056704a -> 7d06ccc50


ISIS-1706: adds support to use a different username than the sender address when authenticating with the Email SMTP service


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/7d06ccc5
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/7d06ccc5
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/7d06ccc5

Branch: refs/heads/master
Commit: 7d06ccc50a5857ecfe95f1dedf98b8310e335783
Parents: bf05670
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Mon Sep 18 10:14:31 2017 +0100
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Mon Sep 18 10:17:39 2017 +0100

----------------------------------------------------------------------
 .../_rgsvc_integration-api_EmailService.adoc    |  6 ++-
 .../services/email/EmailServiceDefault.java     | 48 ++++++++++++--------
 2 files changed, 34 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/7d06ccc5/adocs/documentation/src/main/asciidoc/guides/rgsvc/_rgsvc_integration-api_EmailService.adoc
----------------------------------------------------------------------
diff --git a/adocs/documentation/src/main/asciidoc/guides/rgsvc/_rgsvc_integration-api_EmailService.adoc b/adocs/documentation/src/main/asciidoc/guides/rgsvc/_rgsvc_integration-api_EmailService.adoc
index fd91997..8a4188d 100644
--- a/adocs/documentation/src/main/asciidoc/guides/rgsvc/_rgsvc_integration-api_EmailService.adoc
+++ b/adocs/documentation/src/main/asciidoc/guides/rgsvc/_rgsvc_integration-api_EmailService.adoc
@@ -53,7 +53,7 @@ and these properties may optionally be configured (each has a default to use gma
 * `isis.service.email.port`
 * `isis.service.email.tls.enabled`
 
-These configuration properties can be specified either in `isis.properties` or in an xref:../ugbtb/ugbtb.adoc#_ugbtb_deployment_externalized-configuration[external configuration file].
+These configuration properties can be specified either in `isis.properties` or in an xref:../ugbtb/ugbtb.adoc#_ugbtb_deployment_externalized-configuration[external configuration file], or programmatically using the xref:../rgcms/rgcms.adoc#_rgcms_classes_AppManifest-bootstrapping[`AppManifest`].
 
 If prototyping (that is, running the app using `org.apache.isis.WebServer`), the configuration properties can also be specified as system properties.
 For example, if you create a test email account on gmail, you can configure the service using:
@@ -68,6 +68,10 @@ where "xxx" is the gmail user account and "yyy" is its password
 
 In addition the following properties can be set:
 
+* `isis.service.email.sender.username` +
++
+(As of `1.15.1-SNAPSHOT`), rather than authenticate using the sender address, instead use the specified username.
+
 * `isis.service.email.throwExceptionOnFail` +
 +
 Whether to throw an exception if there the email cannot be sent (probably because of some misconfiguration).

http://git-wip-us.apache.org/repos/asf/isis/blob/7d06ccc5/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
index 317c293..3223515 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
@@ -59,7 +59,8 @@ public class EmailServiceDefault implements EmailService {
         }
     }
 
-    //region > constants
+    // region > constants
+    private static final String ISIS_SERVICE_EMAIL_SENDER_USERNAME = "isis.service.email.sender.username";
     private static final String ISIS_SERVICE_EMAIL_SENDER_ADDRESS = "isis.service.email.sender.address";
     private static final String ISIS_SERVICE_EMAIL_SENDER_PASSWORD = "isis.service.email.sender.password";
 
@@ -85,9 +86,9 @@ public class EmailServiceDefault implements EmailService {
     private static final String ISIS_SERVICE_EMAIL_OVERRIDE_CC = "isis.service.email.override.cc";
     private static final String ISIS_SERVICE_EMAIL_OVERRIDE_BCC = "isis.service.email.override.bcc";
 
-    //endregion
+    // endregion
 
-    //region > init
+    // region > init
     private boolean initialized;
 
     /**
@@ -97,19 +98,23 @@ public class EmailServiceDefault implements EmailService {
     @Programmatic
     public void init() {
 
-        if(initialized) {
+        if (initialized) {
             return;
         }
 
         initialized = true;
 
-        if(!isConfigured()) {
+        if (!isConfigured()) {
             LOG.warn("NOT configured");
         } else {
             LOG.debug("configured");
         }
     }
 
+    protected String getSenderEmailUsername() {
+        return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_USERNAME);
+    }
+
     protected String getSenderEmailAddress() {
         return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_ADDRESS);
     }
@@ -154,9 +159,9 @@ public class EmailServiceDefault implements EmailService {
         return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_BCC);
     }
 
-    //endregion
+    // endregion
 
-    //region > isConfigured
+    // region > isConfigured
 
     @Override
     public boolean isConfigured() {
@@ -164,9 +169,9 @@ public class EmailServiceDefault implements EmailService {
         final String senderEmailPassword = getSenderEmailPassword();
         return !Strings.isNullOrEmpty(senderEmailAddress) && !Strings.isNullOrEmpty(senderEmailPassword);
     }
-    //endregion
+    // endregion
 
-    //region > send
+    // region > send
 
     @Override
     public boolean send(final List<String> toList, final List<String> ccList, final List<String> bccList, final String subject, final String body,
@@ -175,6 +180,7 @@ public class EmailServiceDefault implements EmailService {
         try {
             final ImageHtmlEmail email = new ImageHtmlEmail();
 
+            final String senderEmailUsername = getSenderEmailUsername();
             final String senderEmailAddress = getSenderEmailAddress();
             final String senderEmailPassword = getSenderEmailPassword();
             final String senderEmailHostName = getSenderEmailHostName();
@@ -183,7 +189,11 @@ public class EmailServiceDefault implements EmailService {
             final int socketTimeout = getSocketTimeout();
             final int socketConnectionTimeout = getSocketConnectionTimeout();
 
-            email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
+            if (senderEmailUsername != null) {
+                email.setAuthenticator(new DefaultAuthenticator(senderEmailUsername, senderEmailPassword));
+            } else {
+                email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
+            }
             email.setHostName(senderEmailHostName);
             email.setSmtpPort(senderEmailPort);
             email.setStartTLSEnabled(senderEmailTlsEnabled);
@@ -219,15 +229,15 @@ public class EmailServiceDefault implements EmailService {
             final String overrideBcc = getEmailOverrideBcc();
 
             final String[] toListElseOverride = actually(toList, overrideTo);
-            if(notEmpty(toListElseOverride)) {
+            if (notEmpty(toListElseOverride)) {
                 email.addTo(toListElseOverride);
             }
             final String[] ccListElseOverride = actually(ccList, overrideCc);
-            if(notEmpty(ccListElseOverride)) {
+            if (notEmpty(ccListElseOverride)) {
                 email.addCc(ccListElseOverride);
             }
             final String[] bccListElseOverride = actually(bccList, overrideBcc);
-            if(notEmpty(bccListElseOverride)) {
+            if (notEmpty(bccListElseOverride)) {
                 email.addBcc(bccListElseOverride);
             }
 
@@ -236,7 +246,7 @@ public class EmailServiceDefault implements EmailService {
         } catch (EmailException ex) {
             LOG.error("An error occurred while trying to send an email", ex);
             final Boolean throwExceptionOnFail = isThrowExceptionOnFail();
-            if(throwExceptionOnFail) {
+            if (throwExceptionOnFail) {
                 throw new EmailServiceException(ex);
             }
             return false;
@@ -244,10 +254,10 @@ public class EmailServiceDefault implements EmailService {
 
         return true;
     }
-    //endregion
+    // endregion
 
 
-    //region > helper methods
+    // region > helper methods
 
     static String[] actually(final List<String> original, final String overrideIfAny) {
         final List<String> addresses = Strings.isNullOrEmpty(overrideIfAny)
@@ -261,12 +271,12 @@ public class EmailServiceDefault implements EmailService {
     static boolean notEmpty(final String[] addresses) {
         return addresses != null && addresses.length > 0;
     }
-    //endregion
+    // endregion
 
 
-    //endregion
+    // endregion
 
     @javax.inject.Inject
     IsisConfiguration configuration;
 
-}
+}
\ No newline at end of file