1 10 11 package org.mule.providers.email; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedReader ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.util.Date ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.mail.Header ; 23 import javax.mail.Message ; 24 import javax.mail.Multipart ; 25 import javax.mail.Part ; 26 27 import org.apache.commons.io.IOUtils; 28 import org.apache.commons.io.output.ByteArrayOutputStream; 29 import org.apache.commons.lang.SystemUtils; 30 import org.mule.config.i18n.Messages; 31 import org.mule.providers.AbstractMessageAdapter; 32 import org.mule.umo.MessagingException; 33 import org.mule.umo.provider.MessageTypeNotSupportedException; 34 import org.mule.util.StringUtils; 35 36 39 public class MailMessageAdapter extends AbstractMessageAdapter 40 { 41 44 private static final long serialVersionUID = -6013198455030918360L; 45 46 public static final String ATTACHMENT_HEADERS_PROPERTY_POSTFIX = "Headers"; 47 48 private Part messagePart = null; 49 private byte[] contentBuffer; 50 51 public MailMessageAdapter(Object message) throws MessagingException 52 { 53 setMessage(message); 54 } 55 56 61 public Object getPayload() 62 { 63 return messagePart; 64 } 65 66 71 public byte[] getPayloadAsBytes() throws Exception 72 { 73 if (contentBuffer == null) 74 { 75 String contentType = messagePart.getContentType(); 76 77 if (contentType.startsWith("text/")) 78 { 79 getPayloadAsString(); 80 } 81 else 82 { 83 InputStream is = messagePart.getInputStream(); 84 if (!(is instanceof BufferedInputStream )) 87 { 88 is = new BufferedInputStream (is); 89 } 90 91 ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); 92 IOUtils.copy(is, baos); 93 contentBuffer = baos.toByteArray(); 94 } 95 } 96 return contentBuffer; 97 } 98 99 107 public String getPayloadAsString(String encoding) throws Exception 108 { 109 if (contentBuffer == null) 110 { 111 String contentType = messagePart.getContentType(); 112 113 if (contentType.startsWith("text/")) 114 { 115 InputStream is = messagePart.getInputStream(); 116 if (!(is instanceof BufferedInputStream )) 119 { 120 is = new BufferedInputStream (is); 121 } 122 123 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 124 StringBuffer buffer = new StringBuffer (32768); 125 126 String line = reader.readLine(); 127 buffer.append(line).append(SystemUtils.LINE_SEPARATOR); 128 while (line != null) 129 { 130 line = reader.readLine(); 131 buffer.append(line).append(SystemUtils.LINE_SEPARATOR); 132 } 133 134 contentBuffer = buffer.toString().getBytes(); 135 } 136 else 137 { 138 contentBuffer = getPayloadAsBytes(); 139 } 140 } 141 return new String (contentBuffer, encoding); 142 } 143 144 149 protected void setMessage(Object message) throws MessagingException 150 { 151 Message msg; 152 if (message instanceof Message ) 153 { 154 msg = (Message )message; 155 } 156 else 157 { 158 throw new MessageTypeNotSupportedException(message, MailMessageAdapter.class); 159 } 160 161 try 162 { 163 Object content = msg.getContent(); 164 165 if (content instanceof Multipart ) 166 { 167 this.messagePart = ((Multipart )content).getBodyPart(0); 168 logger.debug("Received Multipart message"); 169 170 for (int i = 1; i < ((Multipart )content).getCount(); i++) 171 { 172 Part part = ((Multipart )content).getBodyPart(i); 173 String name = part.getFileName(); 174 if (name == null) 175 { 176 name = String.valueOf(i - 1); 177 } 178 addAttachment(name, part.getDataHandler()); 179 addAttachmentHeaders(name, part); 180 } 181 } 182 else 183 { 184 messagePart = msg; 185 } 186 187 setProperty(MailProperties.TO_ADDRESSES_PROPERTY, 189 MailUtils.mailAddressesToString(msg.getRecipients(Message.RecipientType.TO))); 190 setProperty(MailProperties.CC_ADDRESSES_PROPERTY, 191 MailUtils.mailAddressesToString(msg.getRecipients(Message.RecipientType.CC))); 192 setProperty(MailProperties.BCC_ADDRESSES_PROPERTY, 193 MailUtils.mailAddressesToString(msg.getRecipients(Message.RecipientType.BCC))); 194 setProperty(MailProperties.REPLY_TO_ADDRESSES_PROPERTY, 195 MailUtils.mailAddressesToString(msg.getReplyTo())); 196 setProperty(MailProperties.FROM_ADDRESS_PROPERTY, MailUtils.mailAddressesToString(msg.getFrom())); 197 setProperty(MailProperties.SUBJECT_PROPERTY, StringUtils.defaultIfEmpty(msg.getSubject(), 198 "(no subject)")); 199 setProperty(MailProperties.CONTENT_TYPE_PROPERTY, StringUtils.defaultIfEmpty( 200 msg.getContentType(), "text/plain")); 201 202 Date sentDate = msg.getSentDate(); 203 if (sentDate == null) 204 { 205 sentDate = new Date (); 206 } 207 setProperty(MailProperties.SENT_DATE_PROPERTY, sentDate); 208 209 for (Enumeration e = msg.getAllHeaders(); e.hasMoreElements();) 210 { 211 Header h = (Header )e.nextElement(); 212 setProperty(h.getName(), h.getValue()); 213 } 214 215 } 216 catch (Exception e) 217 { 218 throw new MessagingException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X, 219 "Message Adapter"), e); 220 } 221 } 222 223 protected void addAttachmentHeaders(String name, Part part) throws javax.mail.MessagingException 224 { 225 Map headers = new HashMap (4); 226 for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();) 227 { 228 Header h = (Header )e.nextElement(); 229 headers.put(h.getName(), h.getValue()); 230 } 231 if (headers.size() > 0) 232 { 233 setProperty(name + ATTACHMENT_HEADERS_PROPERTY_POSTFIX, headers); 234 } 235 } 236 } 237 | Popular Tags |