001: package org.enhydra.shark.toolagent;
002:
003: import java.io.InputStream;
004: import java.io.Serializable;
005: import java.net.URL;
006: import java.util.ArrayList;
007: import java.util.Date;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Properties;
012:
013: import javax.activation.DataHandler;
014: import javax.activation.DataSource;
015: import javax.activation.FileDataSource;
016: import javax.activation.URLDataSource;
017: import javax.mail.Address;
018: import javax.mail.BodyPart;
019: import javax.mail.Flags;
020: import javax.mail.Folder;
021: import javax.mail.Message;
022: import javax.mail.MessagingException;
023: import javax.mail.Multipart;
024: import javax.mail.Session;
025: import javax.mail.Store;
026: import javax.mail.Transport;
027: import javax.mail.internet.InternetAddress;
028: import javax.mail.internet.MimeBodyPart;
029: import javax.mail.internet.MimeMessage;
030: import javax.mail.internet.MimeMultipart;
031: import javax.mail.util.ByteArrayDataSource;
032:
033: import org.enhydra.shark.Shark;
034: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
035: import org.enhydra.shark.api.client.wfservice.SharkConnection;
036: import org.enhydra.shark.api.internal.toolagent.AppParameter;
037: import org.enhydra.shark.api.internal.working.CallbackUtilities;
038:
039: /**
040: * This DefaultMailMessageHandler also includes attachment sending capability.<br>
041: * <br>
042: * Receiving mail with attachments is not currently supported.<br>
043: * <br>
044: * This handler knows how to interpret many attributes for sending e-mail. The attachments
045: * can be passed in several ways: file, URL or shark variable attachment.
046: *
047: * @author Sasa Bojanic
048: * @author Veljko Zivkovic
049: */
050: public class DefaultMailMessageHandler implements MailMessageHandler,
051: Serializable {
052:
053: public static final String FROM_ADDRESSES_NAME = "from_addresses";
054:
055: public static final String FROM_NAMES_NAME = "from_names";
056:
057: public static final String TO_ADDRESSES_NAME = "to_addresses";
058:
059: public static final String TO_NAMES_NAME = "to_names";
060:
061: public static final String CC_ADDRESSES_NAME = "cc_addresses";
062:
063: public static final String CC_NAMES_NAME = "cc_names";
064:
065: public static final String BCC_ADDRESSES_NAME = "bcc_addresses";
066:
067: public static final String BCC_NAMES_NAME = "bcc_names";
068:
069: public static final String SUBJECT_NAME = "subject";
070:
071: public static final String CONTENT_NAME = "content";
072:
073: public static final String CHARSET_NAME = "charset";
074:
075: public static final String MIME_TYPE_NAME = "mime_type";
076:
077: public static final String FILE_ATTACHMENTS_NAME = "file_attachments";
078:
079: public static final String FILE_ATTACHMENTS_NAMES_NAME = "file_attachments_names";
080:
081: public static final String URL_ATTACHMENTS_NAME = "url_attachments";
082:
083: public static final String URL_ATTACHMENTS_NAMES_NAME = "url_attachments_names";
084:
085: public static final String VAR_ATTACHMENTS_NAME = "var_attachments";
086:
087: public static final String VAR_ATTACHMENTS_NAMES_NAME = "var_attachments_names";
088:
089: public static final String VAR_ATTACHMENTS_MIME_TYPES_NAME = "var_attachments_mime_types";
090:
091: protected CallbackUtilities cus;
092:
093: protected AppParameter[] sharkParams;
094:
095: protected Map varAttachmentsVariablesMap = new HashMap();
096:
097: protected Map varAttachmentsMimeTypesMap = new HashMap();
098:
099: protected String procId;
100:
101: protected String assId;
102:
103: protected static String useAuthentication = "false";
104:
105: protected static String SMTPMailServer;
106:
107: protected static String incomingMailServer;
108:
109: protected static int SMTPport;
110:
111: protected static int IMAPport;
112:
113: protected static int POP3port;
114:
115: protected static String sourceAddress;
116:
117: protected static String login;
118:
119: protected static String password;
120:
121: protected static String incomingMailProtocol;
122:
123: protected static String storeFolderName;
124:
125: public void configure(CallbackUtilities cus,
126: WMSessionHandle shandle, String procId, String assId,
127: AppParameter[] aps) throws Exception {
128:
129: this .cus = cus;
130: this .sharkParams = aps;
131: this .procId = procId;
132: this .assId = assId;
133:
134: if (DefaultMailMessageHandler.SMTPMailServer == null) {
135: DefaultMailMessageHandler.SMTPMailServer = cus
136: .getProperty("DefaultMailMessageHandler.SMTPMailServer");
137: DefaultMailMessageHandler.incomingMailServer = cus
138: .getProperty("DefaultMailMessageHandler.IncomingMailServer");
139: try {
140: DefaultMailMessageHandler.SMTPport = Integer
141: .parseInt(cus
142: .getProperty("DefaultMailMessageHandler.SMTPPortNo"));
143: DefaultMailMessageHandler.IMAPport = Integer
144: .parseInt(cus
145: .getProperty("DefaultMailMessageHandler.IMAPPortNo"));
146: DefaultMailMessageHandler.POP3port = Integer
147: .parseInt(cus
148: .getProperty("DefaultMailMessageHandler.POP3PortNo"));
149: } catch (Exception ex) {
150: }
151: DefaultMailMessageHandler.sourceAddress = cus
152: .getProperty("DefaultMailMessageHandler.SourceAddress");
153: DefaultMailMessageHandler.login = cus
154: .getProperty("DefaultMailMessageHandler.Login");
155: DefaultMailMessageHandler.password = cus.getProperty(
156: "DefaultMailMessageHandler.Password", "");
157: DefaultMailMessageHandler.incomingMailProtocol = cus
158: .getProperty("DefaultMailMessageHandler.IncomingMailProtocol");
159: DefaultMailMessageHandler.storeFolderName = cus
160: .getProperty("DefaultMailMessageHandler.StoreFolderName");
161:
162: DefaultMailMessageHandler.useAuthentication = this .cus
163: .getProperty(
164: "DefaultMailMessageHandler.useAuthentication",
165: "false");
166: }
167:
168: String[] varAttachments = AbstractToolAgent
169: .getParameterStringArray(this .sharkParams,
170: VAR_ATTACHMENTS_NAME,
171: AbstractToolAgent.COMMA_SEPARATOR_STR);
172: String[] varAttachmentsMimeTypes = AbstractToolAgent
173: .getParameterStringArray(this .sharkParams,
174: VAR_ATTACHMENTS_MIME_TYPES_NAME,
175: AbstractToolAgent.COMMA_SEPARATOR_STR);
176: if (varAttachments != null && varAttachments.length > 0) {
177: SharkConnection sc = Shark.getInstance()
178: .getSharkConnection();
179: sc.attachToHandle(shandle);
180: Map cntxt = sc.getActivity(
181: procId,
182: Shark.getInstance().getAdminMisc()
183: .getAssignmentActivityId(shandle, procId,
184: assId)).process_context();
185: for (int i = 0; i < varAttachments.length; i++) {
186: Object var = cntxt.get(varAttachments[i]);
187: varAttachmentsVariablesMap.put(varAttachments[i], var);
188: if (varAttachmentsMimeTypes != null
189: && varAttachmentsMimeTypes.length > i) {
190: varAttachmentsMimeTypesMap.put(varAttachments[i],
191: varAttachmentsMimeTypes[i]);
192: } else {
193: varAttachmentsMimeTypesMap.put(varAttachments[i],
194: "text/plain");
195: }
196: }
197: }
198: }
199:
200: protected Session createSession() {
201: // Get system properties
202: Properties props = new Properties();
203:
204: // Setup mail server
205: props.put("mail.smtp.host",
206: DefaultMailMessageHandler.SMTPMailServer);
207: props.put("mail.smtp.port", ""
208: + DefaultMailMessageHandler.SMTPport);
209:
210: // User name
211: props.put("mail.smtp.user", DefaultMailMessageHandler.login);
212:
213: // props.put("mail.debug","true");
214:
215: // there is a problem when using different
216: // Mail API implementations, so if strange
217: // AuthenticationFailedException occures,
218: // set authentication to false as a workaround
219: props.put("mail.smtp.auth",
220: DefaultMailMessageHandler.useAuthentication);
221:
222: // Get session
223: javax.mail.Session session = Session.getInstance(props,
224: new SmtpAuthenticator(DefaultMailMessageHandler.login,
225: DefaultMailMessageHandler.password));
226:
227: return session;
228:
229: }
230:
231: protected MimeMessage createMail() throws Exception {
232:
233: javax.mail.Session session = this .createSession();
234:
235: // Define message
236: MimeMessage message = new MimeMessage(session);
237:
238: String[] fromAddresses = getFromAddresses();
239: String[] fromNames = AbstractToolAgent.getParameterStringArray(
240: this .sharkParams, FROM_NAMES_NAME,
241: AbstractToolAgent.COMMA_SEPARATOR_STR);
242: String charset = AbstractToolAgent.getParameterString(
243: this .sharkParams, CHARSET_NAME);
244: if (charset != null && charset.equals("")) {
245: charset = null;
246: }
247: if (fromAddresses.length > 1) {
248: Address[] addresses = new Address[fromAddresses.length];
249: for (int i = 0; i < fromAddresses.length; i++) {
250: String fromName = null;
251: if (fromNames != null && fromNames.length > i) {
252: fromName = fromNames[i];
253: }
254: if (fromName != null) {
255: if (charset == null) {
256: addresses[i] = new InternetAddress(
257: fromAddresses[i], fromName);
258: } else {
259: addresses[i] = new InternetAddress(
260: fromAddresses[i], fromName, charset);
261: }
262: } else {
263: addresses[i] = new InternetAddress(fromAddresses[i]);
264: }
265: }
266: message.addFrom(addresses);
267: } else {
268: String fromName = null;
269: if (fromNames != null && fromNames.length > 0) {
270: fromName = fromNames[0];
271: }
272: if (charset == null) {
273: message.setFrom(new InternetAddress(fromAddresses[0],
274: fromName));
275: } else {
276: message.setFrom(new InternetAddress(fromAddresses[0],
277: fromName, charset));
278: }
279: }
280:
281: String[] tos = AbstractToolAgent.getParameterStringArray(
282: this .sharkParams, TO_ADDRESSES_NAME,
283: AbstractToolAgent.COMMA_SEPARATOR_STR);
284: String[] toNames = AbstractToolAgent.getParameterStringArray(
285: this .sharkParams, TO_NAMES_NAME,
286: AbstractToolAgent.COMMA_SEPARATOR_STR);
287: String[] ccs = AbstractToolAgent.getParameterStringArray(
288: this .sharkParams, CC_ADDRESSES_NAME,
289: AbstractToolAgent.COMMA_SEPARATOR_STR);
290: String[] ccNames = AbstractToolAgent.getParameterStringArray(
291: this .sharkParams, CC_NAMES_NAME,
292: AbstractToolAgent.COMMA_SEPARATOR_STR);
293: String[] bccs = AbstractToolAgent.getParameterStringArray(
294: this .sharkParams, BCC_ADDRESSES_NAME,
295: AbstractToolAgent.COMMA_SEPARATOR_STR);
296: String[] bccNames = AbstractToolAgent.getParameterStringArray(
297: this .sharkParams, BCC_NAMES_NAME,
298: AbstractToolAgent.COMMA_SEPARATOR_STR);
299: if ((tos == null || tos.length == 0)
300: && (ccs == null || ccs.length == 0)
301: && (bccs == null || bccs.length == 0)) {
302: throw new Exception(
303: "DefaultMailMessageHandler: at least one To, CC or BCC address must be specified!");
304: }
305: if (tos != null) {
306: for (int i = 0; i < tos.length; i++) {
307: String toName = null;
308: if (toNames != null && toNames.length > i) {
309: toName = toNames[i];
310: }
311: if (toName != null) {
312: if (charset == null) {
313: message.addRecipient(Message.RecipientType.TO,
314: new InternetAddress(tos[i], toName));
315: } else {
316: message.addRecipient(Message.RecipientType.TO,
317: new InternetAddress(tos[i], toName,
318: charset));
319: }
320: } else {
321: message.addRecipient(Message.RecipientType.TO,
322: new InternetAddress(tos[i]));
323: }
324: }
325: }
326: if (ccs != null) {
327: for (int i = 0; i < ccs.length; i++) {
328: String ccName = null;
329: if (ccNames != null && ccNames.length > i) {
330: ccName = ccNames[i];
331: }
332: if (ccName != null) {
333: if (charset == null) {
334: message.addRecipient(Message.RecipientType.CC,
335: new InternetAddress(ccs[i], ccName));
336: } else {
337: message.addRecipient(Message.RecipientType.CC,
338: new InternetAddress(ccs[i], ccName,
339: charset));
340: }
341: } else {
342: message.addRecipient(Message.RecipientType.CC,
343: new InternetAddress(ccs[i]));
344: }
345: }
346: }
347: if (bccs != null) {
348: for (int i = 0; i < bccs.length; i++) {
349: String bccName = null;
350: if (bccNames != null && bccNames.length > i) {
351: bccName = bccNames[i];
352: }
353: if (bccName != null) {
354: if (charset == null) {
355: message.addRecipient(Message.RecipientType.BCC,
356: new InternetAddress(bccs[i], bccName));
357: } else {
358: message.addRecipient(Message.RecipientType.BCC,
359: new InternetAddress(bccs[i], bccName,
360: charset));
361: }
362: } else {
363: message.addRecipient(Message.RecipientType.BCC,
364: new InternetAddress(bccs[i]));
365: }
366: }
367: }
368:
369: String subject = AbstractToolAgent.getParameterString(
370: this .sharkParams, SUBJECT_NAME);
371: if (subject != null) {
372: if (charset == null) {
373: message.setSubject(subject);
374: } else {
375: message.setSubject(subject, charset);
376: }
377: }
378:
379: String content = getContent();
380: String mimeType = AbstractToolAgent.getParameterString(
381: this .sharkParams, MIME_TYPE_NAME);
382: if (mimeType != null && mimeType.equals("")) {
383: mimeType = null;
384: }
385: String[] fileAttachments = AbstractToolAgent
386: .getParameterStringArray(this .sharkParams,
387: FILE_ATTACHMENTS_NAME,
388: AbstractToolAgent.COMMA_SEPARATOR_STR);
389: String[] urlAttachments = AbstractToolAgent
390: .getParameterStringArray(this .sharkParams,
391: URL_ATTACHMENTS_NAME,
392: AbstractToolAgent.COMMA_SEPARATOR_STR);
393: String[] varAttachments = AbstractToolAgent
394: .getParameterStringArray(this .sharkParams,
395: VAR_ATTACHMENTS_NAME,
396: AbstractToolAgent.COMMA_SEPARATOR_STR);
397: if ((fileAttachments == null || fileAttachments.length == 0)
398: && (urlAttachments == null || urlAttachments.length == 0)
399: && (varAttachments == null || varAttachments.length == 0)) {
400: if (mimeType == null) {
401: if (charset == null) {
402: message.setText(content);
403: } else {
404: message.setText(content, charset);
405: }
406: } else {
407: message.setContent(content, mimeType);
408: }
409: } else {
410: BodyPart messageBodypart = new MimeBodyPart();
411: if (mimeType == null) {
412: messageBodypart.setText(content);
413: } else {
414: messageBodypart.setContent(content, mimeType);
415: }
416:
417: Multipart multipart = new MimeMultipart();
418: multipart.addBodyPart(messageBodypart);
419:
420: BodyPart[] fileBPs = getFileAttachments(fileAttachments,
421: AbstractToolAgent.getParameterStringArray(
422: this .sharkParams,
423: FILE_ATTACHMENTS_NAMES_NAME,
424: AbstractToolAgent.COMMA_SEPARATOR_STR));
425: BodyPart[] urlBPs = getURLAttachments(urlAttachments,
426: AbstractToolAgent.getParameterStringArray(
427: this .sharkParams,
428: URL_ATTACHMENTS_NAMES_NAME,
429: AbstractToolAgent.COMMA_SEPARATOR_STR));
430: BodyPart[] varBPs = getVarAttachments(varAttachments,
431: AbstractToolAgent.getParameterStringArray(
432: this .sharkParams,
433: VAR_ATTACHMENTS_NAMES_NAME,
434: AbstractToolAgent.COMMA_SEPARATOR_STR));
435:
436: if (fileBPs != null && fileBPs.length > 0) {
437: for (int i = 0; i < fileBPs.length; i++) {
438: multipart.addBodyPart(fileBPs[i]);
439: }
440: }
441: if (urlBPs != null && urlBPs.length > 0) {
442: for (int i = 0; i < urlBPs.length; i++) {
443: multipart.addBodyPart(urlBPs[i]);
444: }
445: }
446: if (varBPs != null && varBPs.length > 0) {
447: for (int i = 0; i < varBPs.length; i++) {
448: multipart.addBodyPart(varBPs[i]);
449: }
450: }
451:
452: message.setContent(multipart);
453: }
454: message.setSentDate(new Date());
455:
456: return message;
457: }
458:
459: public void sendMail() throws Exception {
460:
461: MimeMessage message = createMail();
462: // Send message
463: Transport.send(message);
464: }
465:
466: /**
467: * TODO implement receiving the mail with attachments
468: */
469: public String receiveMail() throws Exception {
470: Store store = null;
471: Folder folder = null;
472:
473: try {
474:
475: // Get system properties
476: Properties props = new Properties();
477:
478: // Get session
479: javax.mail.Session session = Session.getInstance(props,
480: null);
481:
482: // Get the store
483: int imPort = DefaultMailMessageHandler.POP3port;
484: if (!DefaultMailMessageHandler.incomingMailProtocol
485: .equals("pop3")) {
486: imPort = DefaultMailMessageHandler.IMAPport;
487: }
488: store = session
489: .getStore(DefaultMailMessageHandler.incomingMailProtocol);
490: store.connect(DefaultMailMessageHandler.incomingMailServer,
491: imPort, DefaultMailMessageHandler.login,
492: DefaultMailMessageHandler.password);
493:
494: // Get folder
495: Message messages[] = null;
496: Message msg = null;
497: folder = store
498: .getFolder(DefaultMailMessageHandler.storeFolderName);
499: String subject = null;
500: if (folder.hasNewMessages()) {
501: folder.open(Folder.READ_WRITE);
502: messages = folder.getMessages();
503:
504: if (messages != null && messages.length > 0) {
505: for (int i = 0; i < messages.length; i++) {
506: Flags flags = messages[i].getFlags();
507: Flags.Flag[] flagarr = flags.getSystemFlags();
508: boolean valid = true;
509: System.out
510: .println("Checking flags for mail message "
511: + messages[i].getSubject());
512: for (int j = 0; j < flagarr.length; j++) {
513: if (flagarr[j].equals(Flags.Flag.SEEN)
514: || flagarr[j]
515: .equals(Flags.Flag.ANSWERED)
516: || flagarr[j]
517: .equals(Flags.Flag.DELETED)) {
518: valid = false;
519: break;
520: }
521: }
522: if (!valid)
523: continue;
524: msg = messages[i];
525: subject = msg.getSubject();
526: // Once we have the subject we mark message as seen
527: msg.setFlag(Flags.Flag.SEEN, true);
528: break;
529: }
530: }
531: // here we use handler to set parameters based on mail content
532: if (msg != null) {
533: this .setParamsBasedOnMailMessage(msg);
534: } else {
535: this .setParamsBasedOnMailMessage(null);
536: }
537: // Close connection
538: folder.close(false);
539: store.close();
540: } else {
541: this .setParamsBasedOnMailMessage(null);
542: }
543:
544: return subject;
545: } finally {
546: // Close connection
547: try {
548: folder.close(false);
549: store.close();
550: } catch (Exception ex) {
551: }
552: }
553:
554: }
555:
556: protected BodyPart[] getFileAttachments(String[] locations,
557: String[] attachmentNames) throws Exception {
558: if (locations == null || locations.length == 0) {
559: return null;
560: }
561:
562: BodyPart attachmentMessageBodyPart = null;
563: BodyPart[] parts = new BodyPart[locations.length];
564: DataSource source = null;
565: for (int i = 0; i < locations.length; i++) {
566: attachmentMessageBodyPart = new MimeBodyPart();
567: String oneAttachment = locations[i];
568: source = new FileDataSource(oneAttachment);
569: try {
570: attachmentMessageBodyPart
571: .setDataHandler(new DataHandler(source));
572: String fname = oneAttachment;
573: if (attachmentNames != null
574: && attachmentNames.length > i) {
575: fname = attachmentNames[i];
576: } else {
577: fname = source.getName();
578: }
579: attachmentMessageBodyPart.setFileName(fname);
580: } catch (MessagingException me) {
581: this .cus.warn(null, "Unable to send file attachment ["
582: + oneAttachment + "].");
583: // if messaging exception occures
584: // skip this attachment
585: }
586: parts[i] = attachmentMessageBodyPart;
587: }
588: return parts;
589: }
590:
591: protected BodyPart[] getURLAttachments(String[] locations,
592: String[] attachmentNames) throws Exception {
593: if (locations == null || locations.length == 0) {
594: return null;
595: }
596:
597: BodyPart attachmentMessageBodyPart = null;
598: BodyPart[] parts = new BodyPart[locations.length];
599: DataSource source = null;
600: for (int i = 0; i < locations.length; i++) {
601: attachmentMessageBodyPart = new MimeBodyPart();
602: String oneAttachment = locations[i];
603: source = new URLDataSource(new URL(oneAttachment));
604: try {
605: attachmentMessageBodyPart
606: .setDataHandler(new DataHandler(source));
607: String fname = oneAttachment;
608: if (attachmentNames != null
609: && attachmentNames.length > i) {
610: fname = attachmentNames[i];
611: } else {
612: int indOfFS = oneAttachment.lastIndexOf("\\");
613: if (indOfFS < 0) {
614: indOfFS = oneAttachment.lastIndexOf("/");
615: }
616: if (indOfFS >= 0) {
617: fname = oneAttachment.substring(indOfFS + 1);
618: } else {
619: fname = source.getName();
620: }
621: }
622: attachmentMessageBodyPart.setFileName(fname);
623: } catch (MessagingException me) {
624: this .cus.warn(null, "Unable to send URL attachment ["
625: + oneAttachment + "].");
626: // if messaging exception occures
627: // skip this attachment
628: }
629: parts[i] = attachmentMessageBodyPart;
630: }
631: return parts;
632: }
633:
634: protected BodyPart[] getVarAttachments(String[] locations,
635: String[] attachmentNames) throws Exception {
636: if (locations == null || locations.length == 0) {
637: return null;
638: }
639:
640: BodyPart attachmentMessageBodyPart = null;
641: DataSource source = null;
642: List parts = new ArrayList();
643: for (int i = 0; i < locations.length; i++) {
644: attachmentMessageBodyPart = new MimeBodyPart();
645: String oneAttachment = locations[i];
646: Object var = varAttachmentsVariablesMap.get(oneAttachment);
647: String mimeType = (String) varAttachmentsMimeTypesMap
648: .get(oneAttachment);
649: if (var instanceof String) {
650: source = new ByteArrayDataSource((String) var, mimeType);
651: } else if (var instanceof byte[]) {
652: source = new ByteArrayDataSource((byte[]) var, mimeType);
653: } else if (var instanceof InputStream) {
654: source = new ByteArrayDataSource((InputStream) var,
655: mimeType);
656: } else {
657: this .cus.warn(null, "Unable to send var attachment ["
658: + oneAttachment + "].");
659: continue;
660: }
661: attachmentMessageBodyPart.setDataHandler(new DataHandler(
662: source));
663: String fname = locations[i];
664: if (attachmentNames != null && attachmentNames.length > i) {
665: fname = attachmentNames[i];
666: }
667: attachmentMessageBodyPart.setFileName(fname);
668: parts.add(attachmentMessageBodyPart);
669: }
670: BodyPart[] bparts = new BodyPart[parts.size()];
671: parts.toArray(bparts);
672: return bparts;
673: }
674:
675: public String[] getFromAddresses() throws Exception {
676: String[] ret = AbstractToolAgent.getParameterStringArray(
677: this .sharkParams, FROM_ADDRESSES_NAME,
678: AbstractToolAgent.COMMA_SEPARATOR_STR);
679: if (ret == null || ret.length == 0) {
680: ret = new String[] { DefaultMailMessageHandler.sourceAddress };
681: }
682: return ret;
683: }
684:
685: public String getContent() throws Exception {
686: AppParameter param = AbstractToolAgent.getParameterByName(
687: this .sharkParams, CONTENT_NAME);
688: if (param != null && param.the_value != null) {
689: if (param.the_value instanceof String) {
690: return (String) param.the_value;
691: } else if (param.the_value instanceof byte[]) {
692: return new String((byte[]) param.the_value, "UTF-8");
693: }
694: }
695: return "";
696: }
697:
698: public void setParamsBasedOnMailMessage(Message mmessage)
699: throws Exception {
700: if (mmessage != null) {
701: AppParameter param = AbstractToolAgent.getParameterByName(
702: this .sharkParams, FROM_ADDRESSES_NAME);
703: if (param != null) {
704: Address[] addresses = mmessage.getFrom();
705: if (addresses != null) {
706: String v = "";
707: for (int i = 0; i < addresses.length; i++) {
708: if (!v.equals("")) {
709: v += AbstractToolAgent.COMMA_SEPARATOR_STR;
710: }
711: v += addresses[i].toString();
712: }
713: param.the_value = v;
714: }
715: }
716: Address[] addresses = mmessage.getAllRecipients();
717: if (addresses != null) {
718: String tos = "";
719: String ccs = "";
720: String bccs = "";
721: for (int i = 0; i < addresses.length; i++) {
722: if (addresses[i].getType().equals(
723: Message.RecipientType.CC)) {
724: if (!ccs.equals("")) {
725: ccs += AbstractToolAgent.COMMA_SEPARATOR_STR;
726: }
727: ccs += addresses[i].toString();
728: } else if (addresses[i].getType().equals(
729: Message.RecipientType.BCC)) {
730: if (!bccs.equals("")) {
731: bccs += AbstractToolAgent.COMMA_SEPARATOR_STR;
732: }
733: bccs += addresses[i].toString();
734: } else {
735: if (!tos.equals("")) {
736: tos += AbstractToolAgent.COMMA_SEPARATOR_STR;
737: }
738: tos += addresses[i].toString();
739: }
740: }
741: if (!tos.equals("")) {
742: param = AbstractToolAgent.getParameterByName(
743: this .sharkParams, TO_ADDRESSES_NAME);
744: if (param != null) {
745: param.the_value = tos;
746: }
747: }
748: if (!ccs.equals("")) {
749: param = AbstractToolAgent.getParameterByName(
750: this .sharkParams, CC_ADDRESSES_NAME);
751: if (param != null) {
752: param.the_value = ccs;
753: }
754: }
755: if (!bccs.equals("")) {
756: param = AbstractToolAgent.getParameterByName(
757: this .sharkParams, BCC_ADDRESSES_NAME);
758: if (param != null) {
759: param.the_value = bccs;
760: }
761: }
762: }
763: param = AbstractToolAgent.getParameterByName(
764: this .sharkParams, SUBJECT_NAME);
765: if (param != null) {
766: if (param != null) {
767: param.the_value = mmessage.getSubject();
768: }
769: }
770: param = AbstractToolAgent.getParameterByName(
771: this .sharkParams, CONTENT_NAME);
772: if (param != null) {
773: Object content = mmessage.getContent();
774: if (content instanceof String) {
775: param.the_value = content;
776: } else if (content instanceof Multipart) {
777: Multipart mp = (Multipart) content;
778: for (int i = 0; i < mp.getCount(); i++) {
779: BodyPart bp = mp.getBodyPart(i);
780: if (bp.getContent() instanceof String) {
781: param.the_value = bp.getContent();
782: break;
783: }
784: }
785: }
786: }
787: param = AbstractToolAgent.getParameterByName(
788: this.sharkParams, MIME_TYPE_NAME);
789: if (param != null) {
790: param.the_value = mmessage.getContentType();
791: }
792: }
793: }
794:
795: }
|