You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2015/10/27 17:03:57 UTC

ambari git commit: AMBARI-13540 - Emails Dispatched From Ambari Are Not RFC 2822 Compliant And May Be Rejected (jonathanhurley)

Repository: ambari
Updated Branches:
  refs/heads/trunk c55233a83 -> 4a1ff5cb1


AMBARI-13540 - Emails Dispatched From Ambari Are Not RFC 2822 Compliant And May Be Rejected (jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4a1ff5cb
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4a1ff5cb
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4a1ff5cb

Branch: refs/heads/trunk
Commit: 4a1ff5cb12ab9cad24cb69390b9ca5762d16ff61
Parents: c55233a
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Fri Oct 23 16:39:01 2015 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Tue Oct 27 12:03:10 2015 -0400

----------------------------------------------------------------------
 .../server/notifications/Notification.java      | 14 +++++++++++
 .../dispatchers/EmailDispatcher.java            | 25 ++++++++++++++++++--
 2 files changed, 37 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4a1ff5cb/ambari-server/src/main/java/org/apache/ambari/server/notifications/Notification.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/Notification.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/Notification.java
index 5a58aff..7f5104f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/Notification.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/Notification.java
@@ -20,6 +20,8 @@ package org.apache.ambari.server.notifications;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * The {@link Notification} class is a generic way to relay content through an
  * {@link NotificationDispatcher}.
@@ -89,4 +91,16 @@ public class Notification {
   public Type getType() {
     return Type.GENERIC;
   }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuilder buffer = new StringBuilder("Notification{ ");
+    buffer.append("type=").append(getType());
+    buffer.append(", subject=").append(StringUtils.strip(Subject));
+    buffer.append("}");
+    return buffer.toString();
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/4a1ff5cb/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/EmailDispatcher.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/EmailDispatcher.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/EmailDispatcher.java
index f0fc032..9c2b42b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/EmailDispatcher.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/EmailDispatcher.java
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.notifications.dispatchers;
 
+import java.util.Date;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Properties;
@@ -65,6 +66,11 @@ public class EmailDispatcher implements NotificationDispatcher {
   private static final Logger LOG = LoggerFactory.getLogger(EmailDispatcher.class);
 
   /**
+   * The JavaMail property for the {@code From:} header.
+   */
+  private static final String JAVAMAIL_FROM_PROPERTY = "mail.smtp.from";
+
+  /**
    * {@inheritDoc}
    */
   @Override
@@ -97,10 +103,19 @@ public class EmailDispatcher implements NotificationDispatcher {
       return;
     }
 
-    // convert properties to JavaMail properties
+    // convert properties to JavaMail properties, extracting certain properties for use later
+    String fromAddress = null;
     Properties properties = new Properties();
     for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) {
-      properties.put(entry.getKey(), entry.getValue());
+      String key = entry.getKey();
+      String value = entry.getValue();
+
+      properties.put(key, value);
+
+      // this is needed later on for RFC 2822 compliance when setting headers
+      if (key.equals(JAVAMAIL_FROM_PROPERTY)) {
+        fromAddress = value;
+      }
     }
 
     // notifications must have recipients
@@ -133,9 +148,15 @@ public class EmailDispatcher implements NotificationDispatcher {
         message.addRecipient(RecipientType.TO, address);
       }
 
+      message.setSentDate(new Date());
       message.setSubject(notification.Subject);
       message.setText(notification.Body, "UTF-8", "html");
 
+      // RFC 2822 compliance
+      if (null != fromAddress) {
+        message.setFrom(fromAddress);
+      }
+
       Transport.send(message);
 
       if (LOG.isDebugEnabled()) {