[jboss-svn-commits] JBL Code SVN: r5505 - in labs/jbossesb/trunk/product/core/common: src/org/jboss/soa/esb/helpers src/org/jboss/soa/esb/notification tests/src/org/jboss/soa/esb/helpers
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sun Aug 6 07:55:16 EDT 2006
Author: arvinder
Date: 2006-08-06 07:55:10 -0400 (Sun, 06 Aug 2006)
New Revision: 5505
Added:
labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/Email.java
labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java
Removed:
labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/EsbEmail.java
labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java
Modified:
labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/notification/NotifyEmail.java
Log:
renamed EsbEmail to Email
Copied: labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/Email.java (from rev 5504, labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/helpers/Email.java)
Deleted: labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/EsbEmail.java
===================================================================
--- labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/EsbEmail.java 2006-08-06 08:19:09 UTC (rev 5504)
+++ labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/helpers/EsbEmail.java 2006-08-06 11:55:10 UTC (rev 5505)
@@ -1,283 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, JBoss Inc., and individual contributors as indicated
- * 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.helpers;
-
-import java.util.Properties;
-
-import javax.activation.DataHandler;
-import javax.activation.FileDataSource;
-import javax.mail.Address;
-import javax.mail.Authenticator;
-import javax.mail.BodyPart;
-import javax.mail.MessagingException;
-import javax.mail.Multipart;
-import javax.mail.PasswordAuthentication;
-import javax.mail.Session;
-import javax.mail.Transport;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-
-import org.jboss.soa.esb.common.SystemProperties;
-
-/**
- * Simplifies sending of e-mails with attachments from java.
- * <p/>
- * The message is sent via a subsequent call to the {@link #sendMessage()}
- * method.
- *
- * <h3 id="p_oMessageParms">Message Parmeters</h3>
- * The class constructor is supplied the following parameters via a
- * {@link org.jboss.soa.esb.helpers.DomElement} instance:
- * <ol>
- * <li><b>FROM</b>: valid e-mail address</li>
- * <li><b>SENDTO</b>: valid comma separated list of e-mail addresses</li>
- * <li><b>SUBJECT</b>: free text that will appear as subject of the e-mail</li>
- * <li><b>MESSAGE</b>: free text that will appear as contents of the e-mail</li>
- * <li><b>ATTACH</b>: list (as Text children) of valid accessible filenames to
- * attach to the e-mail</li>
- * </ol>
- * All parameters except 'ATTACH' are attributes of the DomElement argument. ATTACH
- * parameters (attachments) are added as child DomElements of the supplied
- * DomElement.
- *
- * @author: Heuristica - Buenos Aires - Argentina
- */
-public class EsbEmail {
- /**
- * DomElement attribute : will be the contents of the From: field in the
- * outgoing message
- */
- public static final String FROM = "from";
-
- /**
- * DomElement attribute : comma separated list of recipients
- */
- public static final String SENDTO = "sendTo";
-
- /**
- * DomElement attribute : comma separated list of Copy recipients
- */
- public static final String COPYTO = "ccTo";
-
- /**
- * DomElement attribute : will be the contents of the Subject: field in the
- * outgoing message
- */
- public static final String SUBJECT = "subject";
-
- /**
- * DomElement attribute : Value of this attribute will be the content of the
- * e-mail's text
- */
- public static final String ATTACH = "attachment";
-
- /**
- * DomElement child Text elements : Each child represents the name of a file
- * to be attached in the outgoing message
- */
- public static final String MESSAGE = "message";
-
- /**
- * The message to b sent via a subsequent call to {@link #sendMessage()}.
- */
- private MimeMessage mailMessage;
-
- /**
- * Public constructor.
- * <p/>
- * Initialises the mail server session and creates the mail message using
- * the information provided in the p_oMessageParms
- * <a href="p_oMessageParms">listed above</a>.
- * <p/>
- * The message is sent via a subsequent call to the {@link #sendMessage()}
- * method.
- *
- * @param p_oMessageParms
- * A DomElement object containing the elements mentioned above
- * @throws MessagingException
- * @throws AddressException
- * @see DomElement
- *
- */
- public EsbEmail(DomElement p_oMessageParms) throws AddressException, MessagingException {
- // REVIEW: How about typing the parameters a little better??? Looks as though that might have a huge ripple-on effect accross the codebase!!
- // REVIEW: Does the session need to ne initialised every time???
- Session oMailSess = initMailServerSession();
-
- // Initialise the oMailMess property...
- mailMessage = createMailMessage(p_oMessageParms, oMailSess);
-
- // Message can be "sent" via a subsequent call to the sendMessage method!
- } // __________________________________
-
- /**
- * Send the mail message associated with this instance.
- * @throws MessagingException Unable to transport the message associated with this
- * EsbEmail instance.
- */
- public void sendMessage() throws MessagingException {
- Transport.send(mailMessage);
- }
-
- /**
- * Get the {@link MimeMessage mail message} associated with this EsbMail
- * instance.
- * @return The mailMessage property value.
- */
- public MimeMessage getMailMessage() {
- return mailMessage;
- }
-
- /**
- * Create the mail message instance to be {@link #sendMessage() sent}.
- * @param p_oMessageParms <a href="p_oMessageParms">Message parameters.</a>.
- * @param oMailSess Mail session on which the message is to be transported.
- * @return The message.
- * @throws AddressException
- * @throws MessagingException
- */
- private MimeMessage createMailMessage(DomElement p_oMessageParms, Session oMailSess) throws AddressException, MessagingException {
- MimeMessage oMessage = new MimeMessage(oMailSess);
-
- // Populate the message with the data supplied in the p_oMessageParms DomElement.
- addMessageAddressing(p_oMessageParms, oMessage);
- addMessageSubject(p_oMessageParms, oMessage);
- addMessageContent(p_oMessageParms, oMessage);
-
- return oMessage;
- }
-
- /**
- * Add the message addressing information to the message.
- * @param p_oMessageParms <a href="p_oMessageParms">Message parameters.</a>.
- * @param oMessage The message.
- * @throws AddressException
- * @throws MessagingException
- */
- private void addMessageAddressing(DomElement p_oMessageParms, MimeMessage oMessage) throws AddressException, MessagingException {
- String sFrom = p_oMessageParms.getAttr(FROM);
- InternetAddress oFrom = new InternetAddress(sFrom);
-
- oMessage.setFrom(oFrom);
- oMessage.setReplyTo(new Address[] { oFrom });
- InternetAddress[] oaTo =
- InternetAddress.parse(p_oMessageParms.getAttr(SENDTO));
-
- for (int i1 = 0; i1 < oaTo.length; i1++) {
- oMessage.addRecipient(MimeMessage.RecipientType.TO, oaTo[i1]);
- }
-
- String sCopyToAddress = (String) p_oMessageParms.getAttr(COPYTO);
- if (null != sCopyToAddress) {
- oaTo = InternetAddress.parse(sCopyToAddress);
- for (int i1 = 0; i1 < oaTo.length; i1++) {
- oMessage.addRecipient(MimeMessage.RecipientType.CC, oaTo[i1]);
- }
- }
- }
-
- /**
- * Add the message Subject to the message.
- * @param p_oMessageParms <a href="p_oMessageParms">Message parameters.</a>.
- * @param oMessage The message.
- * @throws MessagingException
- */
- private void addMessageSubject(DomElement p_oMessageParms, MimeMessage oMessage) throws MessagingException {
- String sSubject = (String) p_oMessageParms.getAttr(SUBJECT);
-
- if (null != sSubject) {
- oMessage.setSubject(sSubject);
- }
- }
-
- /**
- * Add the message content (body, attachments etc) to from the
- * message parameters to the message.
- * @param p_oMessageParms <a href="p_oMessageParms">Message parameters.</a>.
- * @param oMessage The message.
- * @throws MessagingException
- */
- private void addMessageContent(DomElement p_oMessageParms, MimeMessage oMessage) throws MessagingException {
- BodyPart oBodyP = new MimeBodyPart();
- Multipart oMultiP = new MimeMultipart();
-
- oMultiP.addBodyPart(oBodyP);
- oMessage.setContent(oMultiP);
- String sBodyPartText = p_oMessageParms.getAttr(MESSAGE);
- if (null == sBodyPartText) {
- sBodyPartText = "";
- }
- oBodyP.setText(sBodyPartText + "\n");
-
- String[] saFn = p_oMessageParms.getTextChildren(ATTACH);
- for (int i1 = 0; i1 < saFn.length; i1++) {
- oMultiP.addBodyPart(oBodyP = new MimeBodyPart());
- String sFile = saFn[i1];
- oBodyP.setDataHandler(new DataHandler(new FileDataSource(sFile)));
- oBodyP.setFileName(sFile.substring(1 + sFile.lastIndexOf("\\")));
- }
- }
-
- /**
- * Initialise an authenticated {@link javax.mail.Session} with the mail server.
- * @return The {@link javax.mail.Session}.
- */
- private Session initMailServerSession() {
- Authenticator oAuth = null;
- String sSmtpUser = SystemProperties.getSmtpUsername();
-
- if (null != sSmtpUser) {
- oAuth = new MyAuth(sSmtpUser, SystemProperties.getSmtpPassword());
- }
-
- Properties oMailP = new Properties();
- oMailP.setProperty("mail.smtp.host", SystemProperties.getSmtpHost());
- try {
- String sPort = SystemProperties.getSmtpPort();
- Integer.parseInt(sPort);
- oMailP.setProperty("mail.smtp.port", sPort);
- } catch (Exception e) { /* OK just leave standard port */
- }
- javax.mail.Session oMailSess =
- javax.mail.Session.getDefaultInstance(oMailP, oAuth);
- return oMailSess;
- }
-
- private class MyAuth extends Authenticator {
- private String m_sUser, m_sPwd;
-
- private MyAuth(String p_sU, String p_sP) {
- m_sUser = p_sU;
- m_sPwd = p_sP;
- }
-
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(m_sUser, m_sPwd);
- } // ________________________________
- } // ______________________________________________________
-
-} // ____________________________________________________________________________
-
Modified: labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/notification/NotifyEmail.java
===================================================================
--- labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/notification/NotifyEmail.java 2006-08-06 08:19:09 UTC (rev 5504)
+++ labs/jbossesb/trunk/product/core/common/src/org/jboss/soa/esb/notification/NotifyEmail.java 2006-08-06 11:55:10 UTC (rev 5505)
@@ -51,19 +51,19 @@
* EsbEmail(DomElement) constructor - The MESSAGE attribute will
* be filled in at sendNotification(Serializable) time
* @throws Exception
- * @see EsbEmail#EsbEmail(DomElement)
+ * @see Email#Email(DomElement)
* @see NotifyEmail#sendNotification(Serializable)
*/
public NotifyEmail(DomElement p_oP) throws Exception {
super(p_oP);
- String sAtt = (String) m_oParms.getAttr(EsbEmail.FROM);
+ String sAtt = (String) m_oParms.getAttr(Email.FROM);
if (null != sAtt)
InternetAddress.parse(sAtt);
- InternetAddress.parse(m_oParms.getAttr(EsbEmail.SENDTO));
+ InternetAddress.parse(m_oParms.getAttr(Email.SENDTO));
- sAtt = (String) m_oParms.getAttr(EsbEmail.COPYTO);
+ sAtt = (String) m_oParms.getAttr(Email.COPYTO);
if (null != sAtt)
InternetAddress.parse(sAtt);
} // __________________________________
@@ -79,9 +79,9 @@
public void sendNotification(Serializable p_o) {
try {
DomElement oP = m_oParms.cloneObj();
- String sMsg = oP.getAttr(EsbEmail.MESSAGE);
+ String sMsg = oP.getAttr(Email.MESSAGE);
sMsg = ((null == sMsg) ? p_o.toString() : sMsg + "\n") + p_o.toString();
- oP.setAttr(EsbEmail.MESSAGE, sMsg);
+ oP.setAttr(Email.MESSAGE, sMsg);
sendEmailNotification(oP);
} catch (Exception e) {
EsbUtil.getDefaultLogger(this.getClass()).error("Send Mail Failed",
@@ -97,7 +97,7 @@
* @param messageParams Message parameters.
*/
protected void sendEmailNotification(DomElement messageParams) throws AddressException, MessagingException {
- EsbEmail esbMail = new EsbEmail(messageParams);
+ Email esbMail = new Email(messageParams);
esbMail.sendMessage();
}
Copied: labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java (from rev 5500, labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java)
===================================================================
--- labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java 2006-08-05 21:52:32 UTC (rev 5500)
+++ labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java 2006-08-06 11:55:10 UTC (rev 5505)
@@ -0,0 +1,87 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * 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.helpers;
+
+import java.io.IOException;
+
+import javax.mail.Address;
+import javax.mail.MessagingException;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for the EsbEmail class.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class EmailUnitTest extends TestCase {
+
+ private DomElement mailParams;
+
+ @Override
+ protected void setUp() throws Exception {
+ mailParams = DomElement.fromInputStream(getClass().getResourceAsStream("testfile4.xml"));
+ }
+
+ public void test_Email() throws AddressException, MessagingException, IOException {
+ // Look at the source message in testfile4.xml before reading this test.
+
+ Email esbMail = new Email(mailParams);
+ MimeMessage message = esbMail.getMailMessage();
+ Address[] addresses;
+
+ // Check the "from" address details...
+ addresses = message.getFrom();
+ assertEquals(1, addresses.length);
+ assertEquals("x.x at x.com", addresses[0].toString());
+
+ // Check the "to" address details...
+ addresses = message.getRecipients(MimeMessage.RecipientType.TO);
+ assertEquals(2, addresses.length);
+ assertEquals("b.b at b.com", addresses[0].toString());
+ assertEquals("c.c at c.com", addresses[1].toString());
+
+ // Check the "cc" address details...
+ addresses = message.getRecipients(MimeMessage.RecipientType.CC);
+ assertEquals(2, addresses.length);
+ assertEquals("d.d at c.com", addresses[0].toString());
+ assertEquals("e.e at e.com", addresses[1].toString());
+
+ // Check the "subject"...
+ assertEquals("Unit Test - Subject", message.getSubject());
+
+ // Check the message body, including attachments...
+ MimeMultipart content = (MimeMultipart) message.getContent(); // expect a cast exception if it's not the expected type!
+ assertEquals(3, content.getCount()); // the message and the 2 attachments - see testfile4.xml
+ MimeBodyPart part = (MimeBodyPart) content.getBodyPart(0); // the message
+ assertEquals("Unit Test - Message", ((String)part.getContent()).trim());
+ part = (MimeBodyPart) content.getBodyPart(1); // attachement 1
+ assertEquals("attachment1.txt", ((String)part.getFileName()).trim());
+ part = (MimeBodyPart) content.getBodyPart(2); // attachement 2
+ assertEquals("attachment2.txt", ((String)part.getFileName()).trim());
+ }
+
+ // TODO: Add some negative tests!!!
+}
Deleted: labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java 2006-08-06 08:19:09 UTC (rev 5504)
+++ labs/jbossesb/trunk/product/core/common/tests/src/org/jboss/soa/esb/helpers/EsbEmailUnitTest.java 2006-08-06 11:55:10 UTC (rev 5505)
@@ -1,87 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, JBoss Inc., and individual contributors as indicated
- * 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.helpers;
-
-import java.io.IOException;
-
-import javax.mail.Address;
-import javax.mail.MessagingException;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-
-import junit.framework.TestCase;
-
-/**
- * Tests for the EsbEmail class.
- * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
- */
-public class EsbEmailUnitTest extends TestCase {
-
- private DomElement mailParams;
-
- @Override
- protected void setUp() throws Exception {
- mailParams = DomElement.fromInputStream(getClass().getResourceAsStream("testfile4.xml"));
- }
-
- public void test_EsbEmail() throws AddressException, MessagingException, IOException {
- // Look at the source message in testfile4.xml before reading this test.
-
- EsbEmail esbMail = new EsbEmail(mailParams);
- MimeMessage message = esbMail.getMailMessage();
- Address[] addresses;
-
- // Check the "from" address details...
- addresses = message.getFrom();
- assertEquals(1, addresses.length);
- assertEquals("x.x at x.com", addresses[0].toString());
-
- // Check the "to" address details...
- addresses = message.getRecipients(MimeMessage.RecipientType.TO);
- assertEquals(2, addresses.length);
- assertEquals("b.b at b.com", addresses[0].toString());
- assertEquals("c.c at c.com", addresses[1].toString());
-
- // Check the "cc" address details...
- addresses = message.getRecipients(MimeMessage.RecipientType.CC);
- assertEquals(2, addresses.length);
- assertEquals("d.d at c.com", addresses[0].toString());
- assertEquals("e.e at e.com", addresses[1].toString());
-
- // Check the "subject"...
- assertEquals("Unit Test - Subject", message.getSubject());
-
- // Check the message body, including attachments...
- MimeMultipart content = (MimeMultipart) message.getContent(); // expect a cast exception if it's not the expected type!
- assertEquals(3, content.getCount()); // the message and the 2 attachments - see testfile4.xml
- MimeBodyPart part = (MimeBodyPart) content.getBodyPart(0); // the message
- assertEquals("Unit Test - Message", ((String)part.getContent()).trim());
- part = (MimeBodyPart) content.getBodyPart(1); // attachement 1
- assertEquals("attachment1.txt", ((String)part.getFileName()).trim());
- part = (MimeBodyPart) content.getBodyPart(2); // attachement 2
- assertEquals("attachment2.txt", ((String)part.getFileName()).trim());
- }
-
- // TODO: Add some negative tests!!!
-}
More information about the jboss-svn-commits
mailing list