[jboss-svn-commits] JBL Code SVN: r19636 - in labs/jbossesb/trunk: product/docs and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Apr 18 17:53:36 EDT 2008


Author: mark.little at jboss.com
Date: 2008-04-18 17:53:36 -0400 (Fri, 18 Apr 2008)
New Revision: 19636

Modified:
   labs/jbossesb/trunk/Contributors.txt
   labs/jbossesb/trunk/product/docs/MessageActionGuide.odt
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java
   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:
http://jira.jboss.com/jira/browse/JBESB-1412

Modified: labs/jbossesb/trunk/Contributors.txt
===================================================================
--- labs/jbossesb/trunk/Contributors.txt	2008-04-18 21:51:51 UTC (rev 19635)
+++ labs/jbossesb/trunk/Contributors.txt	2008-04-18 21:53:36 UTC (rev 19636)
@@ -30,4 +30,5 @@
 Thomas Cunningham
 Len DiMaggio
 Dominik Kunz
-Faisal Azizullah
\ No newline at end of file
+Faisal Azizullah
+Kenneth Larsen
\ No newline at end of file

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

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java	2008-04-18 21:51:51 UTC (rev 19635)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java	2008-04-18 21:53:36 UTC (rev 19636)
@@ -22,10 +22,18 @@
 
 package org.jboss.soa.esb.helpers;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 import java.util.StringTokenizer;
 
 import javax.activation.DataHandler;
+import javax.activation.DataSource;
 import javax.activation.FileDataSource;
 import javax.mail.Address;
 import javax.mail.Authenticator;
@@ -70,7 +78,7 @@
  * @author Heuristica - Buenos Aires - Argentina
  */
 public class Email {
-	
+
 	/**
 	 * Class logger.
 	 */
@@ -135,6 +143,11 @@
     private String[] attachments = null;
     
     /**
+     * List of MimeBodyParts containing the attachments that should be added to the mail. The attachments are added using the {@link #addAttachment(InputStream, String)} method.
+     */
+    private List<MimeBodyPart> attachmentParts = new ArrayList<MimeBodyPart>();
+    
+    /**
      * Value of this attribute will be the content of the e-mail's text
      */
     private String message = null;    
@@ -208,6 +221,52 @@
     }
 
     /**
+     * @param is the InputStream containing the attachment to set
+     * @param attachmentName the filename of the attachment in the mail 
+     * @throws MessagingException
+     * @throws IOException
+     */
+    public void addAttachment(final InputStream is, final String attachmentName) throws MessagingException, IOException {
+		MimeBodyPart part = new MimeBodyPart();
+		
+		// This part should be replaced by javax.mail.util.ByteArrayDataSource when upgrading to use java mail API 1.4 or later
+		part.setDataHandler(new DataHandler(new DataSource() {
+
+			private ByteArrayOutputStream bos;
+
+			{
+				bos = new ByteArrayOutputStream();
+	            
+	            int read;
+	            byte[] buff = new byte[1024*8];
+	            while((read = is.read(buff)) != -1) {
+	                bos.write(buff, 0, read);
+	            }
+	            bos.close();
+			}
+			
+			public String getContentType() {
+				return "application/octet-stream";
+			}
+
+			public InputStream getInputStream() throws IOException {
+				return new ByteArrayInputStream(bos.toByteArray());
+			}
+
+			public String getName() {
+				return attachmentName;
+			}
+
+			public OutputStream getOutputStream() throws IOException {
+				throw new IOException("Cannot write to this read-only resource");
+			}
+			
+		}));
+		part.setFileName(attachmentName);
+		attachmentParts.add(part);
+    }
+    
+    /**
      * @return Returns the copyTo.
      */
     public String getCopyTo() {
@@ -362,6 +421,10 @@
 	            oBodyP.setFileName(sFile.substring(1 + sFile.lastIndexOf("\\")));
 	        }
         }
+        
+        for (MimeBodyPart part : attachmentParts) {
+            oMultiP.addBodyPart(part);
+        }
 	}
 
 	/**
@@ -399,6 +462,7 @@
 		
 		return oMailSess;
 	}
+	
     
     /**  
      * Method parsing filename from a string containing a comma separated list of filenames
@@ -432,6 +496,5 @@
 			return new PasswordAuthentication(m_sUser, m_sPwd);
 		} // ________________________________
 	} // ______________________________________________________
-
 } // ____________________________________________________________________________
 

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	2008-04-18 21:51:51 UTC (rev 19635)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java	2008-04-18 21:53:36 UTC (rev 19636)
@@ -22,14 +22,17 @@
 
 package org.jboss.soa.esb.notification;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
 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.listeners.message.MessageDeliverException;
 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.body.content.BytesBody;
@@ -48,7 +51,8 @@
  */
 public class NotifyEmail extends NotificationTarget
 {
-    private MessagePayloadProxy payloadProxy;
+    private static final String MESSAGE_ATTACHMENT_NAME = "msgAttachmentName";
+	private MessagePayloadProxy payloadProxy;
 
     /**
 	 * Instantiate a NotifyEmail object using the information contained in
@@ -101,18 +105,21 @@
 		{
             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 oP = m_oParms.cloneObj();
 			String sMsg = oP.getAttribute(Email.MESSAGE);
                         sMsg = ((null == sMsg) ? content : (sMsg + "\n" + content));
 			oP.setAttribute(Email.MESSAGE, sMsg);
-			sendEmailNotification(oP);
+			sendEmailNotification(oP, payloadBytes);
         } catch (MessageDeliverException e) {
             throw new NotificationException(e);
         }
@@ -125,6 +132,10 @@
 		catch (MessagingException ex)
 		{
 			throw new NotificationException(ex);
+		} catch (IOException e) {
+			Util.getDefaultLogger(this.getClass()).error("Send Mail Failed", e);
+
+			throw new NotificationException(e);
 		}
 	} // __________________________________
 
@@ -134,9 +145,11 @@
 	 * 
 	 * @param messageParams
 	 *            Message parameters.
+	 * @param message 
+	 * @throws IOException 
 	 */
-	protected void sendEmailNotification (ConfigTree messageParams)
-			throws AddressException, MessagingException
+	protected void sendEmailNotification (ConfigTree messageParams, byte[] msgPayload)
+			throws AddressException, MessagingException, IOException
 	{
 
 		Email esbMail = new Email();
@@ -144,10 +157,15 @@
 		esbMail.setFrom(messageParams.getAttribute(Email.FROM));
 		esbMail.setCopyTo(messageParams.getAttribute(Email.COPYTO));
 		esbMail.setSubject(messageParams.getAttribute(Email.SUBJECT));
-
 		esbMail.setAttachments(messageParams.getTextChildren(Email.ATTACH));
-		esbMail.setMessage(messageParams.getAttribute(Email.MESSAGE));
 
+		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();
 	}
 

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	2008-04-18 21:51:51 UTC (rev 19635)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java	2008-04-18 21:53:36 UTC (rev 19636)
@@ -72,7 +72,9 @@
 			super(p_oP);
 			this.message = message ;
 		}
-		protected void sendEmailNotification(ConfigTree messageParams) throws AddressException, MessagingException {
+		
+		@Override
+		protected void sendEmailNotification(ConfigTree messageParams, byte[] notusedintest) throws AddressException, MessagingException {
 			assertEquals(message, messageParams.getAttribute(Email.MESSAGE));
 		}		
 	}




More information about the jboss-svn-commits mailing list