001: /**
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */package com.bostechcorp.cbesb.runtime.email;
024:
025: import java.io.Serializable;
026: import java.text.SimpleDateFormat;
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.List;
030: import java.util.Locale;
031:
032: import javax.mail.Address;
033: import javax.mail.MessagingException;
034: import javax.mail.internet.AddressException;
035: import javax.mail.internet.InternetAddress;
036: import javax.mail.internet.NewsAddress;
037: import javax.mail.internet.ParseException;
038:
039: /**
040: * @author LPS
041: */
042: public class EmailBean implements Serializable {
043: /**
044: *
045: */
046: private static final long serialVersionUID = -316437254651083181L;
047:
048: private static final String SIMPLE_DATE_FORMAT = "EEE MM/dd/yyy HH:mm";
049:
050: private Address[] from;
051:
052: private Address[] to;
053:
054: private Address[] cc;
055:
056: private Address[] bcc;
057:
058: private String subject;
059:
060: private List<String> bodyAttachements;
061:
062: private String bodyContent = "";
063:
064: private Address[] replyTo;
065:
066: private Date sent = new Date();
067:
068: private Date received = new Date();
069:
070: private boolean htmlBody = false;
071: private String enchoding = "";
072:
073: /**
074: * @return
075: * @throws MessagingException
076: * @throws AddressException
077: * @throws ParseException
078: */
079: public InternetAddress[] getToAsArray() throws MessagingException,
080: AddressException, ParseException {
081: return InternetAddress.parse(getTo());
082: }
083:
084: /**
085: * @return
086: * @throws MessagingException
087: * @throws AddressException
088: * @throws ParseException
089: */
090: public InternetAddress[] getCcAsArray() throws MessagingException,
091: AddressException, ParseException {
092: return InternetAddress.parse(getCc());
093: }
094:
095: /**
096: * @return
097: * @throws MessagingException
098: * @throws AddressException
099: * @throws ParseException
100: */
101: public InternetAddress[] getBccAsArray() throws MessagingException,
102: AddressException, ParseException {
103: return InternetAddress.parse(getBcc());
104: }
105:
106: /**
107: * @return
108: * @throws MessagingException
109: * @throws AddressException
110: * @throws ParseException
111: */
112: public InternetAddress[] getReplyToAsArray()
113: throws MessagingException, AddressException, ParseException {
114: return InternetAddress.parse(getReplyTo());
115: }
116:
117: public String getDataEnvelope() {
118: String envelope = "";
119: // envelope+="<DataEnvelope>\n";
120: envelope += "\t<from>" + this .getFrom() + "</from>\n";
121: envelope += "\t<to>" + this .getTo() + "</to>\n";
122: envelope += "\t<cc>" + this .getCc() + "</cc>\n";
123: envelope += "\t<bcc>" + this .getBcc() + "</bcc>\n";
124: envelope += "\t<sent>" + this .getSent() + "</sent>\n";
125: envelope += "\t<received>" + this .getReceived()
126: + "</received>\n";
127: envelope += "\t<subject>" + this .getSubject() + "</subject>\n";
128: envelope += "\t<body> <![CDATA[ " + this .getBodyContent()
129: + "]]></body>\n";
130: envelope += "\t<enchoding>" + this .getEnchoding()
131: + "</enchoding>\n";
132: envelope += "\t<attachement>"
133: + this .getBodyAttachementsString() + "</attachement>\n";
134: // envelope+="</DataEnvelope>\n";
135: return envelope;
136: }
137:
138: /**
139: * @return the bcc
140: */
141: public String getBcc() {
142: return arrayToString(bcc);
143: }
144:
145: /**
146: * @param bcc
147: * the bcc to set
148: * @throws MessagingException,AddressException, ParseException
149: */
150: public void setBcc(String bcc) throws MessagingException,
151: AddressException, ParseException {
152: this .bcc = InternetAddress.parse(bcc);
153: }
154:
155: /**
156: * @return the bodyAttachements
157: */
158: public List<String> getBodyAttachements() {
159: return bodyAttachements;
160: }
161:
162: /**
163: * @return the String value of bodyAttachements
164: */
165: public String getBodyAttachementsString() {
166: String attachements = "";
167: if (bodyAttachements != null) {
168: for (int i = 0; i < bodyAttachements.size(); i++) {
169: if (!"".equals(attachements)) {
170: attachements = attachements + ",";
171: }
172: attachements = attachements + bodyAttachements.get(i);
173: }
174: }
175: return attachements;//.length()>2 ? attachements.substring(0,attachements.length()-2):attachements;
176:
177: }
178:
179: /**
180: * @param bodyAttachements
181: * the bodyAttachements to set
182: */
183: public void setBodyAttachements(List<String> bodyAttachements) {
184: this .bodyAttachements = bodyAttachements;
185: }
186:
187: /**
188: * @return the bodyContent
189: */
190: public String getBodyContent() {
191: return bodyContent;
192: }
193:
194: /**
195: * @param bodyContent
196: * the bodyContent to set
197: */
198: public void setBodyContent(String bodyContent, boolean isHtml,
199: String enchoding) {
200: this .bodyContent = bodyContent;
201: this .htmlBody = isHtml;
202: this .enchoding = enchoding;
203: }
204:
205: /**
206: * @return the cc
207: */
208: public String getCc() {
209: return arrayToString(cc);
210: }
211:
212: /**
213: * @param cc
214: * the cc to set
215: * @throws AddressException
216: */
217: public void setCc(String cc) throws MessagingException,
218: AddressException, ParseException {
219: this .cc = InternetAddress.parse(cc);
220: }
221:
222: /**
223: * @return the from
224: */
225: public String getFrom() {
226: return arrayToString(from);
227: }
228:
229: /**
230: * @param from
231: * the from to set
232: * @throws MessagingException,AddressException, ParseException
233: */
234: public void setFrom(String from) throws MessagingException,
235: AddressException, ParseException {
236: this .from = InternetAddress.parse(from);
237: }
238:
239: /**
240: * @return the received
241: */
242: public Date getReceived() {
243: return received;
244: }
245:
246: /**
247: * @return the string value of received
248: */
249: public String getReceivedString() {
250: if (received == null) {
251: return "";
252: }
253: SimpleDateFormat sdf = new SimpleDateFormat(SIMPLE_DATE_FORMAT,
254: Locale.getDefault());
255: return sdf.format(received);
256: }
257:
258: /**
259: * @param received
260: * the received to set
261: */
262: public void setReceived(Date received) {
263: this .received = received;
264: }
265:
266: /**
267: * @return the sent
268: */
269: public Date getSent() {
270: return sent;
271: }
272:
273: /**
274: * @return the string value of sent
275: */
276: public String getSentString() {
277: if (sent == null) {
278: return "";
279: }
280: SimpleDateFormat sdf = new SimpleDateFormat(SIMPLE_DATE_FORMAT,
281: Locale.getDefault());
282: return sdf.format(sent);
283: }
284:
285: /**
286: * @param sent
287: * the sent to set
288: */
289: public void setSent(Date sent) {
290: this .sent = sent;
291: }
292:
293: /**
294: * @return the subject
295: */
296: public String getSubject() {
297: return subject;
298: }
299:
300: /**
301: * @param subject
302: * the subject to set
303: */
304: public void setSubject(String subject) {
305: this .subject = subject;
306: }
307:
308: /**
309: * @return the to
310: */
311: public String getTo() {
312: return arrayToString(to);
313: }
314:
315: /**
316: * @param to
317: * the to to set
318: * @throws MessagingException,AddressException, ParseException
319: */
320: public void setTo(String to) throws MessagingException,
321: AddressException, ParseException {
322: this .to = InternetAddress.parse(to);
323: }
324:
325: /**
326: * @return the replyTo
327: */
328: public String getReplyTo() {
329: return arrayToString(replyTo);
330: }
331:
332: /**
333: * @param replyTo the replyTo to set
334: * @throws MessagingException,AddressException, ParseException
335: */
336: public void setReplyTo(String replyTo) throws MessagingException,
337: AddressException, ParseException {
338: this .replyTo = InternetAddress.parse(replyTo);
339: }
340:
341: /**
342: * @return the enchoding
343: */
344: public String getEnchoding() {
345: return enchoding;
346: }
347:
348: /**
349: * @return the htmlBody
350: */
351: public boolean isHtmlBody() {
352: return htmlBody;
353: }
354:
355: public void setFrom(Address[] from2) {
356: this .from = from2;
357: }
358:
359: public void setTo(Address[] recipients) {
360: this .to = recipients;
361: }
362:
363: public void setCc(Address[] recipients) {
364: this .cc = recipients;
365: }
366:
367: public void setBcc(Address[] recipients) {
368: this .bcc = recipients;
369: }
370:
371: public void setReplyTo(Address[] replyTo2) {
372: this .replyTo = replyTo2;
373: }
374:
375: public void addBodyAttachements(String fileName) {
376: if (bodyAttachements == null) {
377: bodyAttachements = new ArrayList<String>();
378: }
379:
380: bodyAttachements.add(fileName);
381: }
382:
383: private String arrayToString(Address[] array) {
384: String string = "";
385: if (array == null) {
386: return string;
387: }
388: for (int i = 0; i < array.length; i++) {
389: if (array[i] instanceof InternetAddress) {
390: string += ((InternetAddress) array[i]).getAddress()
391: + ((i == array.length - 1) ? ", " : " ");
392: } else if (array[i] instanceof NewsAddress) {
393: string += ((NewsAddress) array[i]).getHost()
394: + ((i == array.length - 1) ? ", " : " ");
395: }
396: }
397: return string.length() > 2 ? string.substring(0, string
398: .length() - 2) : string;
399: }
400:
401: }
|