You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by lh...@apache.org on 2009/04/03 01:13:06 UTC

svn commit: r761469 - in /servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail: marshaler/DefaultMailMarshaler.java utils/MailUtils.java

Author: lhein
Date: Thu Apr  2 23:13:06 2009
New Revision: 761469

URL: http://svn.apache.org/viewvc?rev=761469&view=rev
Log:
improved the parsing of attachments (also inline attachments) to be recursive
(SMXCOMP-499)

Modified:
    servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/DefaultMailMarshaler.java
    servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailUtils.java

Modified: servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/DefaultMailMarshaler.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/DefaultMailMarshaler.java?rev=761469&r1=761468&r2=761469&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/DefaultMailMarshaler.java (original)
+++ servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/DefaultMailMarshaler.java Thu Apr  2 23:13:06 2009
@@ -22,7 +22,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Date;
@@ -44,13 +43,13 @@
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
-import javax.mail.internet.MimeUtility;
 import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.mail.utils.MailUtils;
 
 /**
  * this is the default marshaler for conversion between the normalized message
@@ -108,6 +107,7 @@
      */
     protected void fillMailBodyAndAttachments(MimeMessage mimeMessage, MessageExchange exchange,
                                               NormalizedMessage nmsg) throws Exception {
+        
         // if there are attachments, then a multipart mime mail with
         // attachments will be sent
         if (nmsg.getAttachmentNames().size() > 0) {
@@ -456,9 +456,8 @@
     protected void copyBodyAndAttachments(MessageExchange exchange, NormalizedMessage nmsg,
                                           MimeMessage mailMsg) throws javax.mail.MessagingException {
         // now convert the mail body and attachments and put it to the msg
-        Multipart mp;
         Object content;
-        Multipart subMP;
+        Multipart mp;
         String text = null;
         String html = null;
 
@@ -471,42 +470,15 @@
             } else if (content instanceof Multipart) {
                 // mail with attachment
                 mp = (Multipart)content;
-                int nbMP = mp.getCount();
-                log.debug("MultiPart count: " + nbMP);
-                for (int i = 0; i < nbMP; i++) {
+                // first grab all attachments
+                MailUtils.extractFromMultipart(mp, nmsg);
+                log.debug("Attachments found: " + nmsg.getAttachmentNames().size());
+                
+                for (int i = 0; i < mp.getCount(); i++) {
                     Part part = mp.getBodyPart(i);
                     String disposition = part.getDisposition();
 
-                    log.debug("MultiPart " + i + ": " + part);
-                    log.debug("Disposition: " + disposition);
-
-                    if (disposition != null
-                        && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition
-                            .equalsIgnoreCase(Part.INLINE))) {
-                        // only add named attachments
-                        if (part.getFileName() != null) {
-                            // Parts marked with a disposition of Part.ATTACHMENT
-                            // from part.getDisposition() are clearly attachments
-                            DataHandler att = part.getDataHandler();
-                            // this is clearly a attachment
-                            // Try to find the correct name to work around some bugs in the geronimo javamail
-                            String name = att.getName();
-                            if (name == null || name.length() == 0) {
-                                name = part.getFileName();
-                            }
-                            if (name != null) {
-                                try {
-                                    name = MimeUtility.decodeText(name);
-                                } catch (UnsupportedEncodingException e) {
-                                    // ignore it
-                                }
-                            }
-                            nmsg.addAttachment(name, att);
-                        } else {
-                            // inline part without name?
-                            text = part.getContent() != null ? part.getContent().toString() : "null";
-                        }
-                    } else if (disposition == null) {
+                    if (disposition == null) {
                         // Check if plain
                         MimeBodyPart mbp = (MimeBodyPart)part;
                         if (mbp.isMimeType("text/plain")) {
@@ -518,9 +490,8 @@
                         } else {
                             // Special non-attachment cases (image/gif, ...)
                             if (mbp.getContent() instanceof MimeMultipart) {
-                                subMP = (MimeMultipart)mbp.getContent();
-                                int nbsubMP = subMP.getCount();
-                                for (int j = 0; j < nbsubMP; j++) {
+                                MimeMultipart subMP = (MimeMultipart)mbp.getContent();
+                                for (int j = 0; j < subMP.getCount(); j++) {
                                     MimeBodyPart subMBP = (MimeBodyPart)subMP.getBodyPart(j);
 
                                     if (subMBP.getContent() instanceof InputStream) {
@@ -604,12 +575,13 @@
         log.debug("Parsing: " + subContent.getClass().getName());
 
         if (subContent instanceof InputStream) {
-            String altName = mbp.getContentID() + "."
-                             + mbp.getContentType().substring(mbp.getContentType().lastIndexOf('/') + 1);
-            altName = altName.replaceAll("<", "").replaceAll(">", "").toLowerCase();
+            String cid = mbp.getContentID();
+            if (cid != null) {
+                cid = cid.replaceAll("<", "").replaceAll(">", "").toLowerCase();
+            }
 
             log.debug("Adding special attachment: "
-                      + (mbp.getFileName() != null ? mbp.getFileName() : altName));
+                      + (mbp.getFileName() != null ? mbp.getFileName() : cid));
 
             // read the stream into a byte array
             byte[] data = new byte[mbp.getSize()];
@@ -618,12 +590,16 @@
 
             // create a byte array data source for use in data handler
             ByteArrayDataSource bads = new ByteArrayDataSource(data, mbp.getContentType());
-
+            
             // remember the name of the attachment
-            bads.setName(mbp.getFileName() != null ? mbp.getFileName() : altName);
+            bads.setName(mbp.getFileName() != null ? mbp.getFileName() : cid);
 
             // add the attachment to the message
             nmsg.addAttachment(bads.getName(), new DataHandler(bads));
+            // add the cid2attachment mapping to properties
+            if (cid != null) {
+                nmsg.setProperty(cid, mbp.getFileName());
+            }
         }
     }
 

Modified: servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailUtils.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailUtils.java?rev=761469&r1=761468&r2=761469&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailUtils.java (original)
+++ servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailUtils.java Thu Apr  2 23:13:06 2009
@@ -16,9 +16,16 @@
  */
 package org.apache.servicemix.mail.utils;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.util.Properties;
 
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.mail.Multipart;
+import javax.mail.Part;
+import javax.mail.internet.MimeUtility;
 import javax.mail.internet.ParseException;
 
 import org.apache.servicemix.mail.security.CustomSSLSocketFactory;
@@ -207,4 +214,49 @@
 
         return mailConnectionProperties;
     }
+    
+    /**
+     * extracts attachments from a multipart mail part
+     * 
+     * @param mp        the multipart
+     * @param map       the map to add the attachments to
+     * @throws javax.mail.MessagingException    on mail errors
+     * @throws MessagingException       on jbi messaging errors
+     * @throws IOException      on io errors
+     */
+    public static void extractFromMultipart(Multipart mp, NormalizedMessage nmsg)
+        throws javax.mail.MessagingException, MessagingException, IOException {
+
+        for (int i = 0; i < mp.getCount(); i++) {
+            Part part = mp.getBodyPart(i);
+            if (part.isMimeType("multipart/*")) {
+                extractFromMultipart((Multipart)part.getContent(), nmsg);
+            } else {
+                String disposition = part.getDisposition();
+                if (disposition != null) {
+                    if (disposition.equalsIgnoreCase(Part.ATTACHMENT)
+                        || disposition.equalsIgnoreCase(Part.INLINE)) {
+                        String name = part.getFileName();
+                        // only add named attachments
+                        if (name != null) {
+                            // Parts marked with a disposition of
+                            // Part.ATTACHMENT
+                            // are clearly attachments
+                            if (name != null) {
+                                try {
+                                    name = MimeUtility.decodeText(name);
+                                } catch (UnsupportedEncodingException e) {
+                                    // ignore it
+                                }
+                            }
+                            nmsg.addAttachment(name, part.getDataHandler());
+                        } else if (part.getDataHandler() != null) {
+                            // also add unnamed if there is a data handler
+                            nmsg.addAttachment(disposition, part.getDataHandler());                            
+                        }
+                    }
+                }
+            }
+        }
+    }
 }