001: package com.technoetic.xplanner.mail;
002:
003: import java.io.File;
004: import java.io.PrintWriter;
005: import java.io.StringWriter;
006: import java.util.ArrayList;
007: import java.util.Date;
008: import java.util.Iterator;
009: import java.util.Properties;
010: import javax.activation.DataHandler;
011: import javax.activation.FileDataSource;
012: import javax.mail.Message;
013: import javax.mail.MessagingException;
014: import javax.mail.Session;
015: import javax.mail.Transport;
016: import javax.mail.internet.AddressException;
017: import javax.mail.internet.InternetAddress;
018: import javax.mail.internet.MimeBodyPart;
019: import javax.mail.internet.MimeMessage;
020: import javax.mail.internet.MimeMultipart;
021:
022: import net.sf.hibernate.HibernateException;
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.log4j.Logger;
025: import org.springframework.orm.hibernate.HibernateOperations;
026:
027: import com.technoetic.xplanner.XPlannerProperties;
028: import com.technoetic.xplanner.db.hibernate.HibernateOperationsWrapper;
029: import com.technoetic.xplanner.db.hibernate.ThreadSession;
030: import com.technoetic.xplanner.domain.Person;
031: import com.technoetic.xplanner.domain.repository.ObjectRepository;
032: import com.technoetic.xplanner.domain.repository.RepositoryException;
033:
034: // DEBT: Should not have ANY sql or hql in any domain class. Instead it should be injected with one of the repository
035: public class EmailMessageImpl implements EmailMessage {
036: protected Logger log = Logger.getLogger(getClass());
037: private Message msg;
038: private StringWriter bodyWriter;
039: private ArrayList attachments = new ArrayList();
040: HibernateOperationsWrapper hibernateOperationsWrapper;
041: private ObjectRepository objectRepository;
042:
043: /*package scope*/
044: EmailMessageImpl() throws MessagingException {
045: hibernateOperationsWrapper = new HibernateOperationsWrapper(
046: ThreadSession.get());
047: Properties transportProperties = new Properties();
048: XPlannerProperties xplannerProperties = new XPlannerProperties();
049: Object hostName = xplannerProperties
050: .getProperty("xplanner.mail.smtp.host");
051: if (hostName != null)
052: transportProperties.put("mail.smtp.host", hostName);
053: Object portNbr = xplannerProperties
054: .getProperty("xplanner.mail.smtp.port");
055: if (portNbr != null)
056: transportProperties.put("mail.smtp.port", portNbr);
057: Session session = Session.getDefaultInstance(
058: transportProperties, null);
059: session.setDebug(log.isDebugEnabled());
060: msg = new MimeMessage(session);
061: msg.setSentDate(new Date());
062: }
063:
064: public void setHibernateOperations(
065: HibernateOperations hibernateOperations) {
066: this .hibernateOperationsWrapper = new HibernateOperationsWrapper(
067: hibernateOperations);
068: }
069:
070: public void setFrom(String from) throws AddressException,
071: MessagingException {
072: msg.setFrom(new InternetAddress(from));
073: }
074:
075: public void setRecipient(int personId) throws MessagingException {
076: try {
077: Person person = getPerson(personId);
078: if (StringUtils.isEmpty(person.getEmail())) {
079: throw new MessagingException(
080: "no email address for user: uid="
081: + person.getUserId() + ",id="
082: + person.getId());
083: }
084: setRecipients(person.getEmail());
085: } catch (MessagingException e) {
086: throw e;
087: } catch (Exception e) {
088: throw new MessagingException("error setting recipient", e);
089: }
090: }
091:
092: public void setObjectRepository(ObjectRepository objectRepository) {
093: this .objectRepository = objectRepository;
094: }
095:
096: private Person getPerson(int personId) throws RepositoryException {
097: return (Person) objectRepository.load(personId);
098: }
099:
100: public void setRecipients(String recipients)
101: throws AddressException, MessagingException {
102: if (StringUtils.isNotEmpty(recipients)) {
103: setRecipients(Message.RecipientType.TO, recipients
104: .split(","));
105: }
106: }
107:
108: public void setRecipients(String[] recipients)
109: throws AddressException, MessagingException {
110: InternetAddress[] addresses = new InternetAddress[recipients.length];
111: for (int i = 0; i < recipients.length; i++) {
112: addresses[i] = new InternetAddress(recipients[i]);
113: }
114: msg.setRecipients(Message.RecipientType.TO, addresses);
115: }
116:
117: public void setCcRecipients(String recipients)
118: throws MessagingException, AddressException {
119: if (StringUtils.isNotEmpty(recipients)) {
120: setRecipients(Message.RecipientType.CC, recipients
121: .split(","));
122: }
123: }
124:
125: private void setRecipients(Message.RecipientType recipientType,
126: String[] recipients) throws AddressException,
127: MessagingException {
128: if (recipients.length > 0) {
129: InternetAddress[] addresses = new InternetAddress[recipients.length];
130: for (int i = 0; i < recipients.length; i++) {
131: addresses[i] = new InternetAddress(recipients[i]);
132: }
133: msg.setRecipients(recipientType, addresses);
134: }
135: }
136:
137: public void setBody(String body) throws MessagingException {
138: bodyWriter = new StringWriter();
139: bodyWriter.write(body);
140: }
141:
142: public PrintWriter getBodyWriter() {
143: bodyWriter = new StringWriter();
144: return new PrintWriter(bodyWriter);
145: }
146:
147: public void setSubject(String subject) throws MessagingException {
148: msg.setSubject(subject);
149: }
150:
151: public void setSentDate(Date sentDate) throws MessagingException {
152: msg.setSentDate(sentDate);
153: }
154:
155: public void addAttachment(String filename)
156: throws MessagingException {
157: File file = new File(filename);
158: addAttachment(file.getName(), file);
159: }
160:
161: public void addAttachment(String filename, File file)
162: throws MessagingException {
163: MimeBodyPart part = new MimeBodyPart();
164: FileDataSource fds = new FileDataSource(file);
165: part.setDataHandler(new DataHandler(fds));
166: part.setFileName(filename);
167: attachments.add(part);
168: }
169:
170: public void send() throws MessagingException {
171: MimeMultipart parts = new MimeMultipart();
172: if (bodyWriter == null) {
173: setBody("");
174: }
175: MimeBodyPart bodyPart = new MimeBodyPart();
176: bodyPart.setContent(bodyWriter.toString(), "text/html");
177: parts.addBodyPart(bodyPart);
178: Iterator iter = attachments.iterator();
179: while (iter.hasNext()) {
180: parts.addBodyPart((MimeBodyPart) iter.next());
181: }
182: msg.setContent(parts);
183: Transport.send(msg);
184: }
185: }
|