[jboss-svn-commits] JBL Code SVN: r26878 - in labs/jbossesb/trunk/product: rosetta/src/org/jboss/soa/esb/actions/routing and 5 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jun 9 04:47:36 EDT 2009


Author: beve
Date: 2009-06-09 04:47:36 -0400 (Tue, 09 Jun 2009)
New Revision: 26878

Added:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailRouter.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailWiretap.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/Emailer.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/email/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/email/EmailerUnitTest.java
Modified:
   labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2259 "Create an Emailer action."


Modified: labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
===================================================================
(Binary files differ)

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailRouter.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailRouter.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailRouter.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.routing.email;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.routing.AbstractRouter;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+
+/**
+ * EmailRouter routes the ESB message payload to a configured email account.
+ * <p/>
+ * Like all router processing will terminate after this action has been processed.
+ * 
+ * Example configuration:
+ * <pre>{@code
+ * <action name="send-email" class="org.jboss.soa.esb.actions.routing.email.EmailRouter">
+ *    <property name="host" value="localhost" />
+ *    <property name="port" value="25" />
+ *    <property name="username" value="danbev" />
+ *    <property name="password" value="password" />
+ *    <property name="from" value="jbossesb" />
+ *    <property name="sendTo" value="danbev" />
+ *    <property name="subject" value="quickstart routing" />
+ *    <property name="unwrap" value="true" />
+ * </action>
+ * }</pre>
+ * For details about the properties listed above please see {@link Emailer}s javadoc.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ * @since 4.6
+ *
+ */
+public class EmailRouter extends AbstractRouter
+{
+    private Emailer emailer;
+    private ConfigTree config;
+
+    public EmailRouter(ConfigTree config) throws ConfigurationException
+    {
+        super(config);
+        emailer = new Emailer(config);
+        this.config = config;
+    }
+    
+    @Override
+    public Message process(Message message) throws ActionProcessingException 
+    {
+        Emailer.overrideSmtpProperties(message, config);
+        return super.process(message);
+    }
+
+    @Override
+    public void route(Object object) throws ActionProcessingException
+    {
+        try
+        {
+            emailer.sendEmail(config, object);
+        } 
+        catch (MessageDeliverException e)
+        {
+            throw new ActionProcessingException("Exception while trying to send email.", e);
+        }
+    }
+
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailWiretap.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailWiretap.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/EmailWiretap.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -0,0 +1,75 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.routing.email;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+
+/**
+ * EmailWiretap will publish the ESB message payload to a configured email account.
+ * <p/>
+ * 
+ * Example configuration:
+ * <pre>{@code
+ * <action name="send-email" class="org.jboss.soa.esb.actions.routing.email.EmailWiretap">
+ *    <property name="host" value="localhost" />
+ *    <property name="port" value="25" />
+ *    <property name="username" value="danbev" />
+ *    <property name="password" value="password" />
+ *    <property name="from" value="jbossesb" />
+ *    <property name="sendTo" value="danbev" />
+ *    <property name="subject" value="Subject goes here" />
+ * </action>
+ * }</pre>
+ * 
+ * For details about the properties listed above please see {@link Emailer}s javadoc.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ * @since 4.6
+ *
+ */
+public class EmailWiretap extends AbstractActionPipelineProcessor 
+{
+    private Emailer emailer;
+
+    public EmailWiretap(final ConfigTree config) throws ConfigurationException
+    {
+        emailer = new Emailer(config);
+    }
+    
+    public Message process(final Message message) throws ActionProcessingException 
+    {
+        try
+        {
+            emailer.sendEmail(message);
+        } 
+        catch (final MessageDeliverException e)
+        {
+            throw new ActionProcessingException(e.getMessage(), e);
+        }
+        
+        return message;
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/Emailer.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/Emailer.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/email/Emailer.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -0,0 +1,267 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.routing.email;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.common.Configuration;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.Email;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.MessagePayloadProxy;
+import org.jboss.soa.esb.message.Properties;
+import org.jboss.soa.esb.notification.NotifyEmail;
+
+/**
+ * General Emailer used for both notifiations and router.
+ * </p>
+ * This code was lifted from {@link NotifyEmail}.
+ * 
+ * Configuration properties:
+ * <lu>
+ *    <li>{@code host} The host name of the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.host' in jbossesb-properties.xml. </li>
+ *    <li>{@code port} The port for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.port' in jbossesb-properties.xml.</li>
+ *    <li>{@code username} The username for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.user' in jbossesb-properties.xml.</li>
+ *    <li>{@code password} The password for the above username on the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.password' in jbossesb-properties.xml </li>
+ *    <li>{@code username} The username for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.user' in jbossesb-properties.xml </li>
+ *    <li>{@code auth} If true will attempt to authenticate the user using the AUTH command. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.auth' in jbossesb-properties.xml </li>
+ *    <li>{@code msgAttachmentName} filename of an attachment containing the message payload (optional). If not specified the message payload will be included in the message body.</li>
+ * </lu>
+ * Note that all of the properties except attachments can be specified jboss-esb.xml can be overridden by specifying the same properties on the ESB Message object instance 
+ * passed to this classes sendNotification method.
+ * <p>
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class Emailer
+{
+    private Logger log = Logger.getLogger(Emailer.class);
+    
+    /**
+     * The name of the attachement config attribute.
+     */
+    private static final String MESSAGE_ATTACHMENT_NAME = "msgAttachmentName";
+    
+    /**
+     * {@link MessagePayloadProxy} handles body locations.
+     */
+    private MessagePayloadProxy payloadProxy;
+    
+    /**
+     * The ESB {@link ConfigTree}
+     */
+    private ConfigTree config;
+
+    public Emailer(final ConfigTree config) throws ConfigurationException
+    {
+        this.config = config;
+        checkValidAddress((String) config.getAttribute(Email.FROM), true);
+        checkValidAddress((String) config.getAttribute(Email.SENDTO), false);
+        checkValidAddress((String) config.getAttribute(Email.COPYTO), true);
+        payloadProxy = new MessagePayloadProxy(config);
+    }
+    
+    /**
+     * Send an Email using Email() using p_o.toString() to fill in the message
+     * text
+     * 
+     * @param message
+     *            Object - This object's toString() method will supply contents
+     *            of mail message
+     */
+    public void sendEmail(final Message message) throws MessageDeliverException
+    {
+        ConfigTree configTree = config.cloneObj();
+        overrideSmtpProperties(message, configTree);
+        
+        Object obj = payloadProxy.getPayload(message);
+        sendEmail(configTree, obj);
+    } 
+    
+    public void sendEmail(final Object obj) throws MessageDeliverException
+    {
+        sendEmail(config, obj);
+    }
+    
+     /**
+     * Send an Email using Email() using p_o.toString() to fill in the message
+     * text
+     * 
+     * @param message
+     *            Object - This object's toString() method will supply contents
+     *            of mail message
+     */
+    public void sendEmail(final ConfigTree config, final Object obj) throws MessageDeliverException
+    {
+        try
+        {
+            String content;
+            byte[] payloadBytes;
+
+            if(obj instanceof byte[]) {
+                content = new String((byte[]) obj);
+                payloadBytes = ((byte[]) obj).clone();
+            } else {
+                content = obj.toString();
+                payloadBytes = content.getBytes();
+            }
+
+            ConfigTree configTree = config.cloneObj();
+            String sMsg = configTree.getAttribute(Email.MESSAGE);
+            sMsg = ((null == sMsg) ? content : (sMsg + "\n" + content));
+            configTree.setAttribute(Email.MESSAGE, sMsg);
+            
+            sendEmail(configTree, payloadBytes);
+        } 
+        catch (final AddressException e)
+        {
+            log.error("Send Mail Failed", e);
+            throw new MessageDeliverException("AddressException while trying to send email", e);
+        }
+        catch (final MessagingException e)
+        {
+            throw new MessageDeliverException("MessageingException while trying to send email", e);
+        } 
+        catch (final IOException e) 
+        {
+            log.error("Send Mail Failed", e);
+            throw new MessageDeliverException("IOException while trying to send email", e);
+        }
+    } 
+
+
+    /**
+     * Send an email notification based on the supplied parameters. <p/> This
+     * method allows overriding for test purposes.
+     * 
+     * @param messageParams
+     *            Message parameters.
+     * @param message 
+     * @throws IOException 
+     */
+    protected void sendEmail(ConfigTree messageParams, byte[] msgPayload) throws AddressException, MessagingException, IOException
+    {
+        Email esbMail = createEmailInstance(messageParams);
+        esbMail.setSendTo(messageParams.getAttribute(Email.SENDTO));
+        esbMail.setFrom(messageParams.getAttribute(Email.FROM));
+        esbMail.setCopyTo(messageParams.getAttribute(Email.COPYTO));
+        esbMail.setSubject(messageParams.getAttribute(Email.SUBJECT));
+        esbMail.setAttachments(messageParams.getTextChildren(Email.ATTACH));
+    
+        if (messageParams.getAttribute(MESSAGE_ATTACHMENT_NAME) != null) {
+            esbMail.addAttachment(new ByteArrayInputStream(msgPayload), messageParams.getAttribute(MESSAGE_ATTACHMENT_NAME));
+        } else {
+            esbMail.setMessage(messageParams.getAttribute(Email.MESSAGE));
+        }
+    
+        esbMail.sendMessage();
+    }
+
+    /**
+     * Allows smtp overrides by setting properties on the passed-in message. This could be
+     * populated by a previous action in an action pipline.
+     * 
+     * @param message The ESB Message object that contains the overrides in its properties.
+     * @param configTree The configTree the properties on the ESB Message object will override.
+     */
+    public static void overrideSmtpProperties(final Message message, final ConfigTree configTree)
+    {
+        final Properties properties = message.getProperties();
+        override(Email.HOST, properties, configTree);
+        override(Email.PORT, properties, configTree);
+        override(Email.USERNAME, properties, configTree);
+        override(Email.PASSWORD, properties, configTree);
+        override(Email.AUTH, properties, configTree);
+        override(Email.FROM, properties, configTree);
+        override(Email.SENDTO, properties, configTree);
+        override(Email.COPYTO, properties, configTree);
+        override(Email.SUBJECT, properties, configTree);
+        override(MESSAGE_ATTACHMENT_NAME, properties, configTree);
+    }
+    
+    private void checkValidAddress(final String address, boolean optional) throws ConfigurationException
+    {
+        if (address == null && optional)
+            return;
+        
+        try
+        {
+            InternetAddress.parse(address);
+        } 
+        catch (AddressException e)
+        {
+            throw new ConfigurationException(address + " is not valid", e);
+        }
+    }
+
+    private static void override(final String key, final Properties properties, final ConfigTree configTree)
+    {
+        final String value = (String) properties.getProperty(key);
+        if (value != null)
+        {
+            configTree.setAttribute(key, value);
+        }
+    }
+
+    /**
+     * Creates a new {@link Email} instance using the properties specified in the
+     * passed-in {@link ConfigTree}.
+     * 
+     * @param configTree The {@link ConfigTree} containing the SMTP properties uses to create the Email instance.
+     * @return Email The newly created {@link Email} instance.
+     * @throws AddressException
+     * @throws MessagingException
+     */
+    private Email createEmailInstance(final ConfigTree configTree) throws AddressException, MessagingException
+    {
+        String portStr = configTree.getAttribute(Email.PORT);
+        if (portStr == null)
+        {
+           portStr = Configuration.getSmtpPort(); 
+        }
+        int port;
+        try
+        {
+           port = Integer.parseInt(portStr);
+        }
+        catch(final NumberFormatException e)
+        {
+            throw new MessagingException("Could not parse port '" + portStr + "'");
+        }
+        
+        String host = configTree.getAttribute(Email.HOST, Configuration.getSmtpHost());
+        String username = configTree.getAttribute(Email.USERNAME, Configuration.getSmtpUsername());
+        String password = configTree.getAttribute(Email.PASSWORD,Configuration.getSmtpPassword());
+        boolean auth = configTree.getBooleanAttribute(Email.AUTH, false);
+        
+        return new Email(host, port, username, password, auth);
+    }
+
+}

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java	2009-06-09 01:23:37 UTC (rev 26877)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -27,18 +27,15 @@
 
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 
 import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.routing.email.Emailer;
 import org.jboss.soa.esb.common.Configuration;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.Email;
 import org.jboss.soa.esb.listeners.message.MessageDeliverException;
 import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.message.MessagePayloadProxy;
 import org.jboss.soa.esb.message.Properties;
-import org.jboss.soa.esb.message.body.content.BytesBody;
-import org.jboss.soa.esb.util.Util;
 
 /**
  * This class will send an e-mail using the Email class.
@@ -78,8 +75,10 @@
  */
 public class NotifyEmail extends NotificationTarget
 {
+    private Emailer emailer;
+    
+    @Deprecated
     private static final String MESSAGE_ATTACHMENT_NAME = "msgAttachmentName";
-	private MessagePayloadProxy payloadProxy;
 
     /**
 	 * Instantiate a NotifyEmail object using the information contained in
@@ -94,29 +93,14 @@
 	 */
 	public NotifyEmail (ConfigTree configTree) throws ConfigurationException
 	{
-		super(configTree);
-
-		try
-		{
-			String sAtt = (String) m_oParms.getAttribute(Email.FROM);
-			
-			if (null != sAtt) 
-				InternetAddress.parse(sAtt);
-	
-			InternetAddress.parse(m_oParms.getAttribute(Email.SENDTO));
-	
-			sAtt = (String) m_oParms.getAttribute(Email.COPYTO);
-			if (null != sAtt) 
-				InternetAddress.parse(sAtt);
-		}
-		catch (AddressException ex)
-		{
-			throw new ConfigurationException(ex);
-		}
-        payloadProxy = new MessagePayloadProxy(configTree,
-                                               new String[] {BytesBody.BYTES_LOCATION},
-                                               new String[] {BytesBody.BYTES_LOCATION});
+	    this(configTree, new Emailer(configTree));
 	} // __________________________________
+	
+	public NotifyEmail (final ConfigTree config, final Emailer emailer)
+	{
+	    super(config);
+	    this.emailer = emailer;
+	}
 
 	/**
 	 * Send an Email using Email() using p_o.toString() to fill in the message
@@ -128,47 +112,14 @@
 	 */
 	public void sendNotification (final Message message) throws NotificationException
 	{
-		try
-		{
-            Object obj = payloadProxy.getPayload(message);
-            String content;
-            byte[] payloadBytes;
-
-            if(obj instanceof byte[]) {
-                content = new String((byte[]) obj);
-                payloadBytes = ((byte[]) obj).clone();
-            } else {
-                content = obj.toString();
-                payloadBytes = content.getBytes();
-            }
-
-			ConfigTree configTree = m_oParms.cloneObj();
-			String sMsg = configTree.getAttribute(Email.MESSAGE);
-            sMsg = ((null == sMsg) ? content : (sMsg + "\n" + content));
-			configTree.setAttribute(Email.MESSAGE, sMsg);
-			
-			overrideSmtpProperties(message, configTree);
-			
-			sendEmailNotification(configTree, payloadBytes);
+	    try
+        {
+            emailer.sendEmail(message);
         } 
-		catch (final MessageDeliverException e) {
-            throw new NotificationException(e);
+	    catch (final MessageDeliverException e)
+        {
+	        throw new NotificationException("Exception while trying to send email", e);
         }
-		catch (final AddressException e)
-		{
-			Util.getDefaultLogger(this.getClass()).error("Send Mail Failed", e);
-			
-			throw new NotificationException(e);
-		}
-		catch (final MessagingException ex)
-		{
-			throw new NotificationException(ex);
-		} 
-		catch (final IOException e) {
-			Util.getDefaultLogger(this.getClass()).error("Send Mail Failed", e);
-
-			throw new NotificationException(e);
-		}
 	} // __________________________________
 
 	/**
@@ -176,6 +127,7 @@
 	 * populated by a previsous action in an action pipline.
 	 * @param message The ESB Message object that contains the overrides in its properties.
 	 * @param configTree The configTree the properties on the ESB Message object will override.
+	 * @deprecated Use {@link Emailer#overrideSmtpProperties(Message, ConfigTree)} instead
 	 */
 	protected void overrideSmtpProperties(final Message message, final ConfigTree configTree)
     {
@@ -192,6 +144,9 @@
 	    override(MESSAGE_ATTACHMENT_NAME, properties, configTree);
     }
 	
+	/**
+	 * @deprecated
+	 */
 	private void override(final String key, final Properties properties, final ConfigTree configTree)
 	{
 	    final String value = (String) properties.getProperty(key);
@@ -209,6 +164,7 @@
 	 *            Message parameters.
 	 * @param message 
 	 * @throws IOException 
+	 * @{@link Deprecated} Use {@link Emailer} instead.
 	 */
 	protected void sendEmailNotification (ConfigTree messageParams, byte[] msgPayload) throws AddressException, MessagingException, IOException
 	{
@@ -229,13 +185,7 @@
 	}
 	
 	/**
-	 * Creates a new {@link Email} instance using the properties specified in the
-	 * passed-in {@link ConfigTree}.
-	 * 
-	 * @param configTree The {@link ConfigTree} containing the SMTP properties uses to create the Email instance.
-	 * @return Email The newly created {@link Email} instance.
-	 * @throws AddressException
-	 * @throws MessagingException
+	 * @{@link Deprecated} Use {@link Emailer} instead.
 	 */
 	private Email createEmailInstance(final ConfigTree configTree) throws AddressException, MessagingException
 	{

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/email/EmailerUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/email/EmailerUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/email/EmailerUnitTest.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -0,0 +1,167 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.routing.email;
+
+import static org.junit.Assert.assertEquals;
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.Email;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Test;
+
+/**
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class EmailerUnitTest
+{
+    @Test
+    public void test_NotifyEmail() throws Exception 
+    {
+        ConfigTree config = new ConfigTree("email");
+        
+        config.setAttribute(Email.FROM, "a.b at c.com");
+        config.setAttribute(Email.SENDTO, "d.e at f.com");
+        config.setAttribute(Email.COPYTO, "g.h at i.com");
+        config.setAttribute(Email.MESSAGE, "Hi there!!!");
+        
+        Emailer ne = new MockEmailer(config, "Hi there!!!\nHello");
+        Message message = MessageFactory.getInstance().getMessage();
+        message.getBody().add("Hello".getBytes());
+        ne.sendEmail(message);
+    }
+
+    @Test
+    public void test_NotifyEmailNoMessage() throws Exception {
+        ConfigTree config = new ConfigTree("email");
+                
+        config.setAttribute(Email.FROM, "a.b at c.com");
+        config.setAttribute(Email.SENDTO, "d.e at f.com");
+        config.setAttribute(Email.COPYTO, "g.h at i.com");
+                
+        Emailer ne = new MockEmailer(config, "Hello");
+        Message message = MessageFactory.getInstance().getMessage();
+        message.getBody().add("Hello".getBytes());
+        ne.sendEmail(message);
+    }
+    
+    @Test
+    public void overrideHost() throws Exception {
+        assertOverride(Email.HOST, "b2.host2");
+    }
+    
+    @Test
+    public void overridePort() throws Exception {
+        assertOverride(Email.PORT, "8021");
+    }
+    
+    @Test
+    public void overrideUsername() throws Exception {
+        assertOverride(Email.USERNAME, "daniel");
+    }
+    
+    @Test
+    public void overridePassword() throws Exception {
+        assertOverride(Email.PASSWORD, "letmein");
+    }
+    
+    @Test
+    public void overrideAuth() throws Exception {
+        assertOverride(Email.AUTH, "true");
+    }
+    
+    @Test
+    public void overrideFrom() throws Exception {
+        assertOverride(Email.FROM, "dbevenius at jboss.com");
+    }
+    
+    @Test
+    public void overrideSendTo() throws Exception {
+        assertOverride(Email.SENDTO, "dbevenius at jboss.com");
+    }
+    
+    @Test
+    public void overrideCopyTo() throws Exception {
+        assertOverride(Email.COPYTO, "dbevenius at jboss.com");
+    }
+    
+    @Test
+    public void overrideSubject() throws Exception {
+        assertOverride(Email.SUBJECT, "Overridden Subject");
+    }
+    
+    public void overrideAttachmentName() throws Exception {
+        assertOverride("msgAttachmentName", "newFile.txt");
+    }
+    
+    private void assertOverride(final String overrideName, final String overrideValue) throws Exception
+    {
+        ConfigTree emailMessageEl = new ConfigTree("email");
+        emailMessageEl.setAttribute(Email.SENDTO, "d.e at f.com");
+        emailMessageEl.setAttribute(Email.SUBJECT, "Original Subject");
+        emailMessageEl.setAttribute(Email.MESSAGE, "Hi there!!!");
+        
+        MockEmailer ne = new MockEmailer(emailMessageEl, "Hi there!!!\nHello");
+        Message message = MessageFactory.getInstance().getMessage();
+        
+        message.getProperties().setProperty(overrideName, overrideValue);
+        message.getBody().add("Hello".getBytes());
+        ne.sendEmail(message);
+        
+        assertEquals(overrideValue, ne.getConfigTree().getAttribute(overrideName));
+        
+    }
+    
+    private class MockEmailer extends Emailer 
+    {
+        private final String message ;
+        
+        private ConfigTree configTree;
+        
+        public MockEmailer(ConfigTree config, final String message) throws Exception 
+        {
+            super(config);
+            this.message = message ;
+        }
+        
+        @Override
+        protected void sendEmail(ConfigTree messageParams, byte[] msgPayload)
+        {
+            configTree = messageParams;
+            assertEquals(message, configTree.getAttribute(Email.MESSAGE));
+        }
+
+        public ConfigTree getConfigTree()
+        {
+            return configTree;
+        }       
+    }
+
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(EmailerUnitTest.class);
+    }
+    
+}
+

Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java	2009-06-09 01:23:37 UTC (rev 26877)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java	2009-06-09 08:47:36 UTC (rev 26878)
@@ -23,11 +23,15 @@
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.IOException;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
 
 import junit.framework.JUnit4TestAdapter;
 
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.routing.email.Emailer;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.Email;
 import org.jboss.soa.esb.message.Message;
@@ -133,30 +137,44 @@
         message.getBody().add("Hello".getBytes());
         ne.sendNotification(message);
         
-        assertEquals(overrideValue, ne.getConfigTree().getAttribute(overrideName));
+        assertEquals(overrideValue, MockEmailer.config.getAttribute(overrideName));
 	    
 	}
 	
-	private class TestNotifyEmail extends NotifyEmail {
+	private class TestNotifyEmail extends NotifyEmail 
+	{
         private final String message ;
         
-        private ConfigTree configTree;
-        
-		public TestNotifyEmail(ConfigTree p_oP, final String message) throws Exception {
-			super(p_oP);
+		public TestNotifyEmail(ConfigTree config, final String message) throws Exception 
+		{
+			super(config, new MockEmailer(config, message));
 			this.message = message ;
 		}
 		
 		@Override
-		protected void sendEmailNotification(ConfigTree messageParams, byte[] notusedintest) throws AddressException, MessagingException {
-		    this.configTree = messageParams;
+		protected void sendEmailNotification(ConfigTree messageParams, byte[] notusedintest) throws AddressException, MessagingException 
+		{
 			assertEquals(message, messageParams.getAttribute(Email.MESSAGE));
 		}
-
-        public ConfigTree getConfigTree()
+	}
+	
+	private static class MockEmailer extends Emailer
+	{
+	    private static ConfigTree config;
+	    private String message;
+	    
+        public MockEmailer(ConfigTree config, final String message) throws ConfigurationException
         {
-            return configTree;
-        }		
+            super(config);
+            this.message = message;
+        }
+        
+        @Override
+        protected void sendEmail(ConfigTree messageParams, byte[] msgPayload) throws AddressException, MessagingException, IOException
+        {
+            config = messageParams;
+			assertEquals(message, messageParams.getAttribute(Email.MESSAGE));
+        }
 	}
 	
 	public static junit.framework.Test suite()




More information about the jboss-svn-commits mailing list