1 17 package org.apache.servicemix.components.email; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.StringTokenizer ; 26 27 import javax.activation.DataHandler ; 28 import javax.activation.DataSource ; 29 import javax.activation.FileDataSource ; 30 import javax.jbi.messaging.MessageExchange; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.mail.Address ; 34 import javax.mail.BodyPart ; 35 import javax.mail.Message ; 36 import javax.mail.Multipart ; 37 import javax.mail.Part ; 38 import javax.mail.internet.AddressException ; 39 import javax.mail.internet.InternetAddress ; 40 import javax.mail.internet.MimeBodyPart ; 41 import javax.mail.internet.MimeMessage ; 42 import javax.mail.internet.MimeMultipart ; 43 import javax.xml.transform.TransformerException ; 44 45 import org.apache.commons.lang.StringUtils; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.apache.servicemix.jbi.jaxp.StringSource; 49 50 56 public class MimeMailMarshaler extends MailMarshalerSupport { 57 private static Log log = LogFactory.getLog(MimeMailPoller.class); 58 59 67 public void prepareExchange(MessageExchange exchange, NormalizedMessage normalizedMessage, MimeMessage mimeMessage) throws javax.mail.MessagingException { 68 String from = InternetAddress.toString(mimeMessage.getFrom()); 69 String to = InternetAddress.toString(mimeMessage.getRecipients(Message.RecipientType.TO)); 70 String cc = InternetAddress.toString(mimeMessage.getRecipients(Message.RecipientType.CC)); 71 String replyTo = InternetAddress.toString(mimeMessage.getReplyTo()); 72 String sentDate = getDateFormat().format(mimeMessage.getSentDate()); 73 String text = null; 74 String html = null; 75 MimeMultipart mp = null; 76 Object content = null; 77 Object subContent = null; 78 MimeMultipart subMP = null; 79 80 try { 81 content = mimeMessage.getContent(); 82 if (content instanceof String ) 83 text = asString(content); 85 else if (content instanceof MimeMultipart ) { 86 mp = (MimeMultipart )content; 88 int nbMP = mp.getCount(); 89 for (int i=0; i < nbMP; i++) { 90 Part part = mp.getBodyPart(i); 91 String disposition = part.getDisposition(); 92 if ((disposition != null) && 93 ((disposition.equals(Part.ATTACHMENT) || 94 (disposition.equals(Part.INLINE))))) { 95 DataHandler att = part.getDataHandler(); 97 normalizedMessage.addAttachment(att.getName(), att); 98 } else { 99 MimeBodyPart mbp = (MimeBodyPart )part; 100 if (mbp.isMimeType("text/plain")) { 101 text = (String )mbp.getContent(); 103 } else if (mbp.isMimeType("text/html")) { 104 html = (String )mbp.getContent(); 106 } else if (mbp.isMimeType("multipart/related")){ 107 subContent = mbp.getContent(); 109 if (subContent instanceof MimeMultipart ) { 110 subMP = (MimeMultipart )subContent; 111 int nbsubMP = subMP.getCount(); 112 for (int j=0; j < nbsubMP; j++) { 113 MimeBodyPart subMBP = (MimeBodyPart )part; 114 normalizedMessage.setProperty("org.apache.servicemix.email.alternativeContent" + j, subMBP.getContent()); 116 } 117 } 118 } else log.warn("Some mail contents can not be traited and is not include into message"); 120 } 121 } 122 } else { log.warn("Some mail contents can not be traited and is not include into message"); 124 } 125 } catch (MessagingException e) { 126 throw new javax.mail.MessagingException ("Error while setting content on normalized message",e); 127 } catch (IOException e) { 128 throw new javax.mail.MessagingException ("Error while fetching content",e); 129 } 130 131 normalizedMessage.setProperty("org.apache.servicemix.email.from", from); 132 normalizedMessage.setProperty("org.apache.servicemix.email.to", to); 133 normalizedMessage.setProperty("org.apache.servicemix.email.cc", cc); 134 normalizedMessage.setProperty("org.apache.servicemix.email.text", text); 135 normalizedMessage.setProperty("org.apache.servicemix.email.replyTo", replyTo); 136 normalizedMessage.setProperty("org.apache.servicemix.email.sentDate", sentDate); 137 normalizedMessage.setProperty("org.apache.servicemix.email.html", html); 138 139 try { 140 normalizedMessage.setContent(new StringSource(text)); 141 } catch (MessagingException e) { 142 throw new javax.mail.MessagingException ("Error while setting content on normalized message",e); 143 } 144 } 145 146 154 public void prepareMessage(MimeMessage mimeMessage, MessageExchange exchange, NormalizedMessage normalizedMessage) throws javax.mail.MessagingException { 155 try { 156 Address to = getTo(exchange, normalizedMessage); 157 if (to != null) { 158 mimeMessage.setRecipient(Message.RecipientType.TO, to); 159 } 160 Address cc = getCc(exchange, normalizedMessage); 161 if (cc != null) { 162 mimeMessage.setRecipient(Message.RecipientType.CC, cc); 163 } 164 Address bcc = getBcc(exchange, normalizedMessage); 165 if (bcc != null) { 166 mimeMessage.setRecipient(Message.RecipientType.BCC, bcc); 167 } 168 Address from = getFrom(exchange, normalizedMessage); 169 if (from != null) { 170 mimeMessage.setFrom(from); 171 } 172 String text = getText(exchange, normalizedMessage); 173 String html = getHtml(exchange, normalizedMessage); 174 if ((text != null) && (html == null)) { 175 mimeMessage.setText(text); 176 } 177 else if ((text != null) && (html != null)) { 178 MimeMultipart content = new MimeMultipart ("alternative"); 179 MimeBodyPart textBodyPart = new MimeBodyPart (); 180 MimeBodyPart htmlBodyPart = new MimeBodyPart (); 181 textBodyPart.setText(text); 182 htmlBodyPart.setContent(html, "text/html"); 183 content.addBodyPart(textBodyPart); 184 content.addBodyPart(htmlBodyPart); 185 186 mimeMessage.setContent(content); 187 } 188 String subject = getSubject(exchange, normalizedMessage); 189 if (subject != null) { 190 mimeMessage.setSubject(subject); 191 } 192 Date sentDate = getSentDate(exchange, normalizedMessage); 193 if (sentDate != null) { 194 mimeMessage.setSentDate(sentDate); 195 } 196 Address [] replyTo = getReplyTo(exchange, normalizedMessage); 197 if (replyTo != null) { 198 mimeMessage.setReplyTo(replyTo); 199 } 200 201 HashMap attachments = this.getAttachments(exchange, normalizedMessage); 203 if (attachments != null) { 204 Set attNames = attachments.keySet(); 205 Iterator itAttNames = attNames.iterator(); 206 if (itAttNames.hasNext()) { BodyPart messageBodyPart = new MimeBodyPart (); 209 messageBodyPart.setText(text); 211 Multipart multipart = new MimeMultipart (); 213 multipart.addBodyPart(messageBodyPart); 215 while (itAttNames.hasNext()) { 216 String oneAttachmentName = (String )itAttNames.next(); 217 messageBodyPart = new MimeBodyPart (); 219 messageBodyPart.setDataHandler(new DataHandler ((DataSource )attachments.get(oneAttachmentName))); 221 messageBodyPart.setFileName(oneAttachmentName); 223 messageBodyPart.setDisposition(Part.ATTACHMENT); 225 multipart.addBodyPart(messageBodyPart); 227 } 228 mimeMessage.setContent(multipart); 230 } 231 } 232 } 233 catch (MessagingException e) { 234 throw new javax.mail.MessagingException (e.getMessage(), e); 235 } 236 catch (TransformerException e) { 237 throw new javax.mail.MessagingException (e.getMessage(), e); 238 } 239 } 240 241 242 protected Address getFrom(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException { 245 return asAddress(getFrom().evaluate(exchange, normalizedMessage)); 246 } 247 248 protected Address getTo(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException { 249 return asAddress(getTo().evaluate(exchange, normalizedMessage)); 250 } 251 252 protected Address getCc(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException { 253 return asAddress(getCc().evaluate(exchange, normalizedMessage)); 254 } 255 256 protected Address getBcc(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException { 257 return asAddress(getBcc().evaluate(exchange, normalizedMessage)); 258 } 259 260 protected Address [] getReplyTo(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException { 261 return asAddressArray(getReplyTo().evaluate(exchange, normalizedMessage)); 262 } 263 264 protected HashMap getAttachments(MessageExchange exchange, NormalizedMessage normalizedMessage) { 265 HashMap attachments = new HashMap (); 266 String filePath = ""; 267 String oneAttachmentName = ""; 268 try { 269 String listAttachment = (String )getAttachments().evaluate(exchange, normalizedMessage); 271 if (StringUtils.isNotBlank(listAttachment)) { 272 StringTokenizer st = new StringTokenizer (listAttachment, ","); 273 if (st != null) { 274 while (st.hasMoreTokens()) { 275 filePath = st.nextToken(); 276 File file = new File (filePath); 277 attachments.put(file.getName(), new FileDataSource (file)); 278 } 279 } 280 } 281 } catch (MessagingException e) { 282 log.warn("file not found for attachment : " + e.getMessage() + " : " + filePath); 283 } 284 Set attNames = normalizedMessage.getAttachmentNames(); 286 Iterator itAttNames = attNames.iterator(); 287 while (itAttNames.hasNext()) { 288 oneAttachmentName = (String )itAttNames.next(); 289 DataSource oneAttchmentInputString = normalizedMessage.getAttachment(oneAttachmentName).getDataSource(); 290 attachments.put(oneAttachmentName, oneAttchmentInputString); 291 } 292 293 return attachments; 294 } 295 296 } 297 | Popular Tags |