1 10 11 package org.mule.providers.email.transformers; 12 13 import java.io.IOException ; 14 import java.io.Serializable ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import javax.activation.DataHandler ; 19 import javax.mail.BodyPart ; 20 import javax.mail.Message ; 21 import javax.mail.MessagingException ; 22 import javax.mail.internet.MimeBodyPart ; 23 import javax.mail.internet.MimeMultipart ; 24 25 import org.mule.providers.email.MailMessageAdapter; 26 import org.mule.transformers.simple.SerializableToByteArray; 27 import org.mule.umo.UMOEventContext; 28 import org.mule.umo.transformer.TransformerException; 29 import org.mule.util.StringUtils; 30 31 34 public class ObjectToMimeMessage extends StringToEmailMessage 35 { 36 39 private static final long serialVersionUID = 7225142214620572674L; 40 41 protected void setContent(Object payload, Message msg, String contentType, UMOEventContext context) 42 throws Exception 43 { 44 45 if (context.getMessage().getAttachmentNames().size() > 0) 46 { 47 MimeMultipart multipart = new MimeMultipart ("mixed"); 49 multipart.addBodyPart(getPayloadBodyPart(payload, contentType)); 50 for (Iterator it = context.getMessage().getAttachmentNames().iterator(); it.hasNext();) 51 { 52 String name = (String )it.next(); 53 BodyPart part = getBodyPartForAttachment(context.getMessage().getAttachment(name), name); 54 addBodyPartHeaders(part, name, context); 56 multipart.addBodyPart(part); 57 } 58 payload = multipart; 60 contentType = multipart.getContentType(); 62 } 64 super.setContent(payload, msg, contentType, context); 67 } 68 69 protected void addBodyPartHeaders(BodyPart part, String name, UMOEventContext context) 70 { 71 72 Map headers = (Map )context.getMessage().getProperty( 73 name + MailMessageAdapter.ATTACHMENT_HEADERS_PROPERTY_POSTFIX); 74 if (null != headers) 75 { 76 for (Iterator it = headers.keySet().iterator(); it.hasNext();) 77 { 78 try 79 { 80 String key = (String )it.next(); 81 part.setHeader(key, (String )headers.get(key)); 82 } 83 catch (MessagingException me) 84 { 85 logger.error("Failed to set bodypart header", me); 86 } 87 } 88 } 89 } 90 91 protected BodyPart getBodyPartForAttachment(DataHandler handler, String name) throws MessagingException 92 { 93 BodyPart part = new MimeBodyPart (); 94 part.setDataHandler(handler); 95 part.setDescription(name); 96 part.setFileName(StringUtils.defaultString(handler.getName(), name)); 97 return part; 98 } 99 100 protected BodyPart getPayloadBodyPart(Object payload, String contentType) 101 throws MessagingException , TransformerException, IOException 102 { 103 104 DataHandler handler; 105 if (payload instanceof String ) 106 { 107 handler = new DataHandler (new PlainTextDataSource(contentType, payload.toString())); 108 } 109 else if (payload instanceof byte[]) 110 { 111 handler = new DataHandler (new ByteArrayDataSource((byte[])payload, contentType)); 112 } 113 else if (payload instanceof Serializable ) 114 { 115 handler = new DataHandler (new ByteArrayDataSource( 116 (byte[])new SerializableToByteArray().transform(payload), contentType)); 117 } 118 else 119 { 120 throw new IllegalArgumentException (); 121 } 122 BodyPart part = new MimeBodyPart (); 123 part.setDataHandler(handler); 124 part.setDescription("Payload"); 125 return part; 126 } 127 128 } 129 | Popular Tags |