commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1624843 - in /commons/proper/email/trunk: pom.xml src/changes/changes.xml src/main/java/org/apache/commons/mail/util/MimeMessageParser.java src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
Date Sun, 14 Sep 2014 12:09:26 GMT
Author: tn
Date: Sun Sep 14 12:09:25 2014
New Revision: 1624843

URL: http://svn.apache.org/r1624843
Log:
[EMAIL-141] Added support for content-id mapping in MimeMessageParser. Thanks to Stephen Kruger.

Modified:
    commons/proper/email/trunk/pom.xml
    commons/proper/email/trunk/src/changes/changes.xml
    commons/proper/email/trunk/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java
    commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java

Modified: commons/proper/email/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/pom.xml?rev=1624843&r1=1624842&r2=1624843&view=diff
==============================================================================
--- commons/proper/email/trunk/pom.xml (original)
+++ commons/proper/email/trunk/pom.xml Sun Sep 14 12:09:25 2014
@@ -176,6 +176,9 @@
             <name>Regis Koenig</name>
         </contributor>
         <contributor>
+            <name>Stephen Kruger</name>
+        </contributor>
+        <contributor>
             <name>Sean Legassick</name>
             <email>sean@informage.net</email>
         </contributor>

Modified: commons/proper/email/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/changes/changes.xml?rev=1624843&r1=1624842&r2=1624843&view=diff
==============================================================================
--- commons/proper/email/trunk/src/changes/changes.xml (original)
+++ commons/proper/email/trunk/src/changes/changes.xml Sun Sep 14 12:09:25 2014
@@ -23,6 +23,9 @@
 
   <body>
     <release version="1.3.4" date="TBD">
+      <action dev="tn" type="add" issue="EMAIL-141" date="2014-09-14" due-to="Stephen
Kruger">
+        Added support for mapping of content-ids to DataSource in "MimeMessageParser".
+      </action>
     </release>
     <release version="1.3.3" date="2014-07-12">
       <action dev="tn" type="fix" issue="EMAIL-140" date="2014-07-01" due-to="Gary Gregory">

Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java?rev=1624843&r1=1624842&r2=1624843&view=diff
==============================================================================
--- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java
(original)
+++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/util/MimeMessageParser.java
Sun Sep 14 12:09:25 2014
@@ -24,7 +24,11 @@ import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
@@ -62,6 +66,9 @@ public class MimeMessageParser
     /** List of attachments of MimeMessage */
     private final List<DataSource> attachmentList;
 
+    /** Attachments stored by their content-id */
+    private final Map<String, DataSource> cidMap;
+
     /** Is this a Multipart email */
     private boolean isMultiPart;
 
@@ -73,6 +80,7 @@ public class MimeMessageParser
     public MimeMessageParser(final MimeMessage message)
     {
         attachmentList = new ArrayList<DataSource>();
+        cidMap = new HashMap<String, DataSource>();
         this.mimeMessage = message;
         this.isMultiPart = false;
     }
@@ -202,13 +210,33 @@ public class MimeMessageParser
                 }
                 else
                 {
-                    this.attachmentList.add(createDataSource(parent, part));
+                    final String cid = stripContentId(part.getContentID());
+                    final DataSource ds = createDataSource(parent, part);
+                    if (cid != null)
+                    {
+                        this.cidMap.put(cid, ds);
+                    }
+                    this.attachmentList.add(ds);
                 }
             }
         }
     }
 
     /**
+     * Strips the content id of any whitespace and angle brackets.
+     * @param contentId the string to strip
+     * @return a stripped version of the content id
+     */
+    private String stripContentId(final String contentId)
+    {
+        if (contentId == null)
+        {
+            return null;
+        }
+        return contentId.trim().replaceAll("[\\<\\>]", "");
+    }
+
+    /**
      * Checks whether the MimePart contains an object of the given mime type.
      *
      * @param part     the current MimePart
@@ -281,6 +309,20 @@ public class MimeMessageParser
         return attachmentList;
     }
 
+    /**
+     * Returns a collection of all content-ids in the parsed message.
+     * <p>
+     * The content-ids are stripped of any angle brackets, i.e. "part1" instead
+     * of "&lt;part1&gt;".
+     *
+     * @return the collection of content ids.
+     * @since 1.3.4
+     */
+    public Collection<String> getContentIds()
+    {
+        return Collections.unmodifiableSet(cidMap.keySet());
+    }
+
     /** @return Returns the htmlContent if any */
     public String getHtmlContent()
     {
@@ -328,6 +370,22 @@ public class MimeMessageParser
     }
 
     /**
+     * Find an attachment using its content-id.
+     * <p>
+     * The content-id must be stripped of any angle brackets,
+     * i.e. "part1" instead of "&lt;part1&gt;".
+     *
+     * @param cid the content-id of the attachment
+     * @return the corresponding datasource or null if nothing was found
+     * @since 1.3.4
+     */
+    public DataSource findAttachmentByCid(final String cid)
+    {
+        final DataSource dataSource = cidMap.get(cid);
+        return dataSource;
+    }
+
+    /**
      * Determines the name of the data source if it is not already set.
      *
      * @param part the mail part

Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java?rev=1624843&r1=1624842&r2=1624843&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
(original)
+++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/util/MimeMessageParserTest.java
Sun Sep 14 12:09:25 2014
@@ -468,4 +468,34 @@ public class MimeMessageParserTest
         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
         assertFalse(mimeMessageParser.hasAttachments());
     }
+
+    @Test
+    public void testParseInlineCID() throws Exception
+    {
+        final Session session = Session.getDefaultInstance(new Properties());
+        final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment.eml"));
+        final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
+
+        mimeMessageParser.parse();
+
+        assertEquals("Test", mimeMessageParser.getSubject());
+        assertNotNull(mimeMessageParser.getMimeMessage());
+        assertTrue(mimeMessageParser.isMultipart());
+        assertTrue(mimeMessageParser.hasHtmlContent());
+        assertNotNull(mimeMessageParser.getHtmlContent());
+        assertTrue(mimeMessageParser.getTo().size() == 1);
+        assertTrue(mimeMessageParser.getCc().size() == 0);
+        assertTrue(mimeMessageParser.getBcc().size() == 0);
+        assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
+        assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
+        assertTrue(mimeMessageParser.hasAttachments());
+
+        assertTrue(mimeMessageParser.getContentIds().contains("part1.01080006.06060206@it20one.at"));
+        assertFalse(mimeMessageParser.getContentIds().contains("part2"));
+
+        final DataSource ds = mimeMessageParser.findAttachmentByCid("part1.01080006.06060206@it20one.at");
+        assertNotNull(ds);
+        assertEquals(ds, mimeMessageParser.getAttachmentList().get(0));
+    }
+
 }



Mime
View raw message