[jbpm-commits] JBoss JBPM SVN: r7031 - jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/mail.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Oct 10 21:41:01 EDT 2011


Author: marco.rietveld
Date: 2011-10-10 21:41:01 -0400 (Mon, 10 Oct 2011)
New Revision: 7031

Modified:
   jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/mail/Mail.java
Log:
JBPM-3386: jBPM Mail Node does not authenticate, results in "Relay access denied" when recipient is not on local server

Modified: jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/mail/Mail.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/mail/Mail.java	2011-10-11 01:39:55 UTC (rev 7030)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/mail/Mail.java	2011-10-11 01:41:01 UTC (rev 7031)
@@ -1,5 +1,6 @@
 package org.jbpm.mail;
 
+
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -12,6 +13,7 @@
 
 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;
@@ -30,9 +32,12 @@
 import org.jbpm.jpdl.el.impl.JbpmExpressionEvaluator;
 import org.jbpm.util.ClassLoaderUtil;
 import org.jbpm.util.XmlUtil;
+import org.pdfbox.filter.CCITTFaxDecodeFilter;
 
 public class Mail implements ActionHandler {
 
+  private static final Log log = LogFactory.getLog(Mail.class);
+  
   private String template;
   private String to;
   private String actors;
@@ -73,7 +78,6 @@
     this.executionContext = executionContext;
     send();
   }
-
   
   public List getRecipients() {
     Collection recipientsCollection = collectRecipients(actors, to);
@@ -183,7 +187,7 @@
   }
 
   private Object evaluate(String expression, Class expectedType) {
-    VariableResolver variableResolver = new MailVariableResolver(getTemplateVariables(), JbpmExpressionEvaluator.getVariableResolver());
+    VariableResolver variableResolver = new MailVariableResolver(MailTemplates.getTemplateVariables(), JbpmExpressionEvaluator.getVariableResolver());
     return JbpmExpressionEvaluator.evaluate(expression, executionContext, expectedType, variableResolver, JbpmExpressionEvaluator.getFunctionMapper());
   }
 
@@ -202,7 +206,7 @@
 
   public void send() {
     if (template != null) {
-      Properties templateProperties = getTemplateProperties(template);
+      Properties templateProperties = MailTemplates.getTemplateProperties(template);
 
       if (actors == null) actors = templateProperties.getProperty("actors");
       if (to == null) to = templateProperties.getProperty("to");
@@ -214,29 +218,32 @@
       if (text == null) text = templateProperties.getProperty("text");
     }
 
-    String sender = getFromAddress();
-    Collection recipients = getRecipients();
-    Collection ccRecipients = getCcRecipients();
-    Collection bccRecipients = getBccRecipients();
-    if (nullOrEmpty(recipients) && nullOrEmpty(ccRecipients) && nullOrEmpty(bccRecipients))
+    MessageInfo msgInfo = new MessageInfo();
+    msgInfo.sender = getFromAddress();
+    msgInfo.recipients = getRecipients();
+    msgInfo.ccRecipients = getCcRecipients();
+    msgInfo.bccRecipients = getBccRecipients();
+    if (nullOrEmpty(msgInfo.recipients) && nullOrEmpty(msgInfo.ccRecipients) && nullOrEmpty(msgInfo.bccRecipients))
       return;
 
-    String subject = getSubject();
-    String text = getText();
+    msgInfo.subject = getSubject();
+    msgInfo.text = getText();
 
     if (log.isDebugEnabled()) {
       StringBuffer detail = new StringBuffer("sending email");
-      if (!nullOrEmpty(recipients)) detail.append(" to ").append(recipients);
-      if (!nullOrEmpty(ccRecipients)) detail.append(" cc ").append(ccRecipients);
-      if (!nullOrEmpty(bccRecipients)) detail.append(" bcc ").append(bccRecipients);
+      if (!nullOrEmpty(msgInfo.recipients)) detail.append(" to ").append(msgInfo.recipients);
+      if (!nullOrEmpty(msgInfo.ccRecipients)) detail.append(" cc ").append(msgInfo.ccRecipients);
+      if (!nullOrEmpty(msgInfo.bccRecipients)) detail.append(" bcc ").append(msgInfo.bccRecipients);
       if (subject != null) detail.append(" about '").append(subject).append('\'');
       log.debug(detail.toString());
     }
 
-    Session session = Session.getInstance(getServerProperties());
+    Properties sessionProperties = getServerProperties();
+    msgInfo.session = getSession(sessionProperties);
+    
     for (int retries = 4; retries >= 0; retries--) {
       try {
-        sendInternal(session, sender, recipients, ccRecipients, bccRecipients, subject, text);
+        sendInternal(msgInfo);
         break;
       }
       catch (MessagingException me) {
@@ -255,7 +262,8 @@
   }
   
   public static void send(Properties serverProperties, String sender, Collection recipients, String subject, String text) {
-    send(serverProperties, sender, recipients, null, subject, text);
+    MessageInfo msgInfo = new MessageInfo(null, sender, recipients, null, null, subject, text);
+    send(serverProperties, msgInfo);
   }
 
   /**
@@ -267,20 +275,26 @@
   }
   
   public static void send(Properties serverProperties, String sender, Collection recipients, Collection bccRecipients, String subject, String text) {
-    if (nullOrEmpty(recipients) && nullOrEmpty(bccRecipients)) return;
+    MessageInfo msgInfo = new MessageInfo(null, sender, recipients, null, bccRecipients, subject, text);
+    send(serverProperties, msgInfo); 
+  } 
+  
+  private static void send(Properties serverProperties, MessageInfo msgInfo) { 
+    if (nullOrEmpty(msgInfo.recipients) && nullOrEmpty(msgInfo.bccRecipients)) return;
 
     if (log.isDebugEnabled()) {
       StringBuffer detail = new StringBuffer("sending email to ");
-      detail.append(recipients);
-      if (bccRecipients != null) detail.append(" bcc ").append(bccRecipients);
-      if (subject != null) detail.append(" about '").append(subject).append('\'');
+      detail.append(msgInfo.recipients);
+      if (msgInfo.bccRecipients != null) detail.append(" bcc ").append(msgInfo.bccRecipients);
+      if (msgInfo.subject != null) detail.append(" about '").append(msgInfo.subject).append('\'');
       log.debug(detail.toString());
     }
 
-    Session session = Session.getInstance(serverProperties);
+    msgInfo.session = getSession(serverProperties);
+    
     for (int retries = 4; retries >= 0; retries--) {
       try {
-        sendInternal(session, null, recipients, null, bccRecipients, subject, text);
+        sendInternal(msgInfo);
         break;
       }
       catch (MessagingException me) {
@@ -294,49 +308,102 @@
     return col == null || col.isEmpty();
   }
 
-  private static void sendInternal(Session session, String sender, Collection recipients,
-    Collection ccRecipients, Collection bccRecipients, String subject, String text)
-    throws MessagingException {
-    MimeMessage message = new MimeMessage(session);
+  private static void sendInternal(MessageInfo msgInfo) throws MessagingException {
+    
+    Message message = fillMessage(msgInfo);
+    
+    // send the message
+    Transport.send(message);
+  }
+
+  private static Message fillMessage(MessageInfo msgInfo) throws MessagingException { 
+    MimeMessage message = new MimeMessage(msgInfo.session);
     // from
-    if (sender != null) {
-      message.setFrom(new InternetAddress(sender));
+    if (msgInfo.sender != null) {
+      message.setFrom(new InternetAddress(msgInfo.sender));
     }
     else {
       // read sender from session property "mail.from"
       message.setFrom();
     }
     // to
-    if (recipients != null) {
-      for (Iterator iter = recipients.iterator(); iter.hasNext();) {
+    if (msgInfo.recipients != null) {
+      for (Iterator iter = msgInfo.recipients.iterator(); iter.hasNext();) {
         InternetAddress recipient = new InternetAddress((String) iter.next());
         message.addRecipient(Message.RecipientType.TO, recipient);
       }
     }
     // cc
-    if (ccRecipients != null) {
-      for (Iterator iter = ccRecipients.iterator(); iter.hasNext();) {
+    if (msgInfo.ccRecipients != null) {
+      for (Iterator iter = msgInfo.ccRecipients.iterator(); iter.hasNext();) {
         InternetAddress recipient = new InternetAddress((String) iter.next());
         message.addRecipient(Message.RecipientType.CC, recipient);
       }
     }
     // bcc
-    if (bccRecipients != null) {
-      for (Iterator iter = bccRecipients.iterator(); iter.hasNext();) {
+    if (msgInfo.bccRecipients != null) {
+      for (Iterator iter = msgInfo.bccRecipients.iterator(); iter.hasNext();) {
         InternetAddress recipient = new InternetAddress((String) iter.next());
         message.addRecipient(Message.RecipientType.BCC, recipient);
       }
     }
     // subject
-    if (subject != null) message.setSubject(subject);
+    if (msgInfo.subject != null) message.setSubject(msgInfo.subject);
     // text
-    if (text != null) message.setText(text);
-    // send the message
-    Transport.send(message);
+    if (msgInfo.text != null) message.setText(msgInfo.text);
+    
+    return message;
   }
+  
+  private static Session getSession(Properties properties) {
+    String userName = (String) properties.remove(JBPM_MAIL_USER);
+    String password = (String) properties.remove(JBPM_MAIL_PASSWORD);
 
+    Session session;
+    if( userName != null ) { 
+        properties.setProperty("mail.smtp.submitter", userName );
+        Authenticator authenticator = new Authenticator(userName, password);
+        session = Session.getInstance(properties, authenticator);
+        if( password != null ) { 
+          properties.setProperty("mail.smtp.auth", "true");
+        }
+    }
+    else { 
+      properties.remove("mail.smtp.auth");
+      properties.remove("mail.smtp.starttls.enable");
+      session = Session.getInstance(properties);
+    }
+   
+    // set debug prop if available
+    String debugStr = (String) properties.remove(JBPM_MAIL_DEBUG);
+    boolean debug = false;
+    if( debugStr != null ) { 
+      debug = Boolean.getBoolean(debugStr);
+    }
+    session.setDebug(debug);
+
+    return session;
+  }
+
+  private static class Authenticator extends javax.mail.Authenticator implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private PasswordAuthentication authentication;
+
+    public Authenticator(String username, String password) {
+      authentication = new PasswordAuthentication(username, password);
+    }
+
+    protected PasswordAuthentication getPasswordAuthentication() {
+      return authentication;
+    }
+  }
+  
   private static final Map serverPropertiesByResource = new HashMap();
 
+  private static final String JBPM_MAIL_USER = "jbpm.mail.user";
+  private static final String JBPM_MAIL_PASSWORD = "jbpm.mail.password";
+  private static final String JBPM_MAIL_DEBUG = "jbpm.mail.debug";
+  
   private Properties getServerProperties() {
     Properties serverProperties;
 
@@ -353,92 +420,51 @@
       }
     }
     else {
+      //OCRAM: from address
       serverProperties = new Properties();
       // host
-      if (Configs.hasObject("jbpm.mail.smtp.host")) {
-        String smtpHost = Configs.getString("jbpm.mail.smtp.host");
+      String jbpmProperty = "jbpm.mail.smtp.host";
+      if (Configs.hasObject(jbpmProperty)) {
+        String smtpHost = Configs.getString(jbpmProperty);
         serverProperties.setProperty("mail.smtp.host", smtpHost);
       }
       // port
-      if (Configs.hasObject("jbpm.mail.smtp.port")) {
-        int port = Configs.getInt("jbpm.mail.smtp.port");
+      jbpmProperty = "jbpm.mail.smtp.port";
+      if (Configs.hasObject(jbpmProperty)) {
+        int port = Configs.getInt(jbpmProperty);
         serverProperties.setProperty("mail.smtp.port", Integer.toString(port));
       }
-    }
-    return serverProperties;
-  }
-
-  private static final Map templatePropertiesByResource = new HashMap();
-  private static final Map templateVariablesByResource = new HashMap();
-
-  private static Properties getTemplateProperties(String templateName) {
-    String resource = Configs.getString("resource.mail.templates");
-    synchronized (templatePropertiesByResource) {
-      Map templateProperties = (Map) templatePropertiesByResource.get(resource);
-      if (templateProperties == null) {
-        loadTemplates(resource);
-        templateProperties = (Map) templatePropertiesByResource.get(resource);
+      // start TLS
+      jbpmProperty = "jbpm.mail.smtp.starttls";
+      if (Configs.hasObject(jbpmProperty)) {
+        boolean enableTLS = Configs.getBoolean(jbpmProperty);
+        serverProperties.setProperty("mail.smtp.starttls.enable", Boolean.toString(enableTLS) );
       }
-      return (Properties) templateProperties.get(templateName);
-    }
-  }
-
-  private static Map getTemplateVariables() {
-    String resource = Configs.getString("resource.mail.templates");
-    synchronized (templateVariablesByResource) {
-      Map templateVariables = (Map) templateVariablesByResource.get(resource);
-      if (templateVariables == null) {
-        loadTemplates(resource);
-        templateVariables = (Map) templateVariablesByResource.get(resource);
+      // SMTP authentication
+      jbpmProperty = "jbpm.mail.smtp.auth";
+      if (Configs.hasObject(jbpmProperty)) {
+        boolean useAuth = Configs.getBoolean(jbpmProperty);
+        serverProperties.setProperty("mail.smtp.auth", Boolean.toString(useAuth) );
       }
-      return templateVariables;
-    }
-  }
-
-  private static void loadTemplates(String resource) {
-    Element templatesElement = XmlUtil.parseXmlResource(resource, true).getDocumentElement();
-
-    Map templatePropertiesMap = new HashMap();
-    for (Iterator iter = XmlUtil.elementIterator(templatesElement, "mail-template"); iter.hasNext();) {
-      Element templateElement = (Element) iter.next();
-
-      Properties templateProperties = new Properties();
-      addTemplateProperty(templateElement, "to", templateProperties);
-      addTemplateProperty(templateElement, "actors", templateProperties);
-      addTemplateProperty(templateElement, "subject", templateProperties);
-      addTemplateProperty(templateElement, "text", templateProperties);
-      addTemplateProperty(templateElement, "cc", templateProperties);
-      addTemplateProperty(templateElement, "cc-actors", templateProperties);
-      addTemplateProperty(templateElement, "bcc", templateProperties);
-      // preserve backwards compatibility with bccActors element
-      Element bccActorsElement = XmlUtil.element(templateElement, "bccActors");
-      if (bccActorsElement != null) {
-        templateProperties.setProperty("bcc-actors", XmlUtil.getContentText(bccActorsElement));
+      // user, password
+      String [] propNameStrings = { JBPM_MAIL_USER, JBPM_MAIL_PASSWORD };
+      for( int i = 0; i < propNameStrings.length; ++i ) { 
+        if (Configs.hasObject(propNameStrings[i])) {
+          String propVal = Configs.getString(propNameStrings[i]);
+          serverProperties.setProperty(propNameStrings[i], propVal);
+        }
       }
-      else {
-        addTemplateProperty(templateElement, "bcc-actors", templateProperties);
+      // debug
+      jbpmProperty = JBPM_MAIL_DEBUG;
+      if (Configs.hasObject(jbpmProperty)) {
+        boolean debug = Configs.getBoolean(jbpmProperty);
+        serverProperties.setProperty(JBPM_MAIL_DEBUG, Boolean.toString(debug) );
       }
-
-      templatePropertiesMap.put(templateElement.getAttribute("name"), templateProperties);
     }
-    templatePropertiesByResource.put(resource, templatePropertiesMap);
-
-    Map templateVariables = new HashMap();
-    for (Iterator iter = XmlUtil.elementIterator(templatesElement, "variable"); iter.hasNext();) {
-      Element variableElement = (Element) iter.next();
-      templateVariables.put(variableElement.getAttribute("name"), variableElement.getAttribute("value"));
-    }
-    templateVariablesByResource.put(resource, templateVariables);
+    
+    return serverProperties;
   }
 
-  private static void addTemplateProperty(Element templateElement, String property,
-    Properties templateProperties) {
-    Element element = XmlUtil.element(templateElement, property);
-    if (element != null) {
-      templateProperties.setProperty(property, XmlUtil.getContentText(element));
-    }
-  }
-
   static class MailVariableResolver implements VariableResolver, Serializable {
 
     private Map templateVariables;
@@ -459,5 +485,31 @@
     }
   }
 
-  private static final Log log = LogFactory.getLog(Mail.class);
+  private static class MessageInfo implements Serializable { 
+    
+    private static final long serialVersionUID = -4252493407344235335L;
+    
+    public Session session;
+    public String sender; 
+    public Collection recipients;
+    public Collection ccRecipients; 
+    public Collection bccRecipients; 
+    public String subject; 
+    public String text;
+    
+    public MessageInfo() { } 
+    public MessageInfo(Session session, String sender, 
+      Collection recipients, Collection ccRecipients, Collection bccRecipients, 
+      String subject, String text) { 
+      this.session = session;
+      this.sender = sender;
+      this.recipients = recipients;
+      this.ccRecipients = ccRecipients;
+      this.bccRecipients = bccRecipients;
+      this.subject = subject;
+      this.text = text;
+    } 
+
+  }
+  
 }



More information about the jbpm-commits mailing list