You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/04/09 07:56:48 UTC

[2/2] camel git commit: CAMEL-9841: NPE in MIME-Multipart Data Format if no file name is defined on attachment. Thanks to Stephan Siano for the patch.

CAMEL-9841: NPE in MIME-Multipart Data Format if no file name is defined on attachment. Thanks to Stephan Siano for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/10d69f22
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/10d69f22
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/10d69f22

Branch: refs/heads/camel-2.17.x
Commit: 10d69f22de4a17522f70661d7316b582c7426037
Parents: a13924a
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Apr 9 07:55:05 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Apr 9 07:56:01 2016 +0200

----------------------------------------------------------------------
 .../mime/multipart/MimeMultipartDataFormat.java | 22 +++++++++++++-
 .../multipart/MimeMultipartDataFormatTest.java  | 30 ++++++++++++++++++++
 .../src/test/resources/multipart-related.txt    | 29 +++++++++++++++++++
 .../src/test/resources/multipart-without-id.txt | 26 +++++++++++++++++
 4 files changed, 106 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/10d69f22/components/camel-mail/src/main/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormat.java b/components/camel-mail/src/main/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormat.java
index 51fdd2c..6504bc3 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormat.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormat.java
@@ -20,10 +20,12 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 import java.util.regex.Pattern;
 
 import javax.activation.DataHandler;
@@ -236,7 +238,7 @@ public class MimeMultipartDataFormat implements DataFormat {
             content = mp.getBodyPart(0);
             for (int i = 1; i < mp.getCount(); i++) {
                 BodyPart bp = mp.getBodyPart(i);
-                camelMessage.addAttachment(MimeUtility.decodeText(bp.getFileName()), bp.getDataHandler());
+                camelMessage.addAttachment(getAttachmentKey(bp), bp.getDataHandler());
             }
         }
         if (content instanceof BodyPart) {
@@ -266,4 +268,22 @@ public class MimeMultipartDataFormat implements DataFormat {
             camelMessage.removeHeader(headerMame);
         }
     }
+
+    private String getAttachmentKey(BodyPart bp) throws MessagingException, UnsupportedEncodingException {
+        // use the filename as key for the map
+        String key = bp.getFileName();
+        // if there is no file name we use the Content-ID header
+        if (key == null && bp instanceof MimeBodyPart) {
+            key = ((MimeBodyPart)bp).getContentID();
+            if (key != null && key.startsWith("<") && key.length() > 2) {
+                // strip <>
+                key = key.substring(1, key.length() - 1);
+            }
+        }
+        // or a generated content id
+        if (key == null) {
+            key = UUID.randomUUID().toString() + "@camel.apache.org";
+        }
+        return MimeUtility.decodeText(key);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/10d69f22/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
index 2d9fa53..db62669 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
@@ -17,8 +17,10 @@
 package org.apache.camel.dataformat.mime.multipart;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
@@ -285,6 +287,34 @@ public class MimeMultipartDataFormatTest extends CamelTestSupport {
         assertEquals("also there", out.getOut().getHeader("x-bar"));
     }
 
+    @Test
+    public void unmarshalRelated() throws IOException {
+        in.setBody(new File("src/test/resources/multipart-related.txt"));
+        unmarshalAndCheckAttachmentName("950120.aaCB@XIson.com");
+    }
+
+    @Test
+    public void unmarshalWithoutId() throws IOException {
+        in.setBody(new File("src/test/resources/multipart-without-id.txt"));
+        unmarshalAndCheckAttachmentName("@camel.apache.org");
+    }
+
+    private void unmarshalAndCheckAttachmentName(String matcher) throws IOException, UnsupportedEncodingException {
+        Exchange intermediate = template.send("direct:unmarshalonlyinlineheaders", exchange);
+        assertNotNull(intermediate.getOut());
+        String bodyStr = intermediate.getOut().getBody(String.class);
+        assertNotNull(bodyStr);
+        assertThat(bodyStr, startsWith("25"));
+        assertEquals(1, intermediate.getOut().getAttachmentNames().size());
+        assertThat(intermediate.getOut().getAttachmentNames().iterator().next(), containsString(matcher));
+        DataHandler dh = intermediate.getOut().getAttachment(intermediate.getOut().getAttachmentNames().iterator().next());
+        assertNotNull(dh);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        dh.writeTo(bos);
+        String attachmentString = new String(bos.toByteArray(), "UTF-8");
+        assertThat(attachmentString, startsWith("Old MacDonald had a farm"));
+    }
+
     private void addAttachment(String attContentType, String attText, String attFileName) throws IOException {
         DataSource ds = new ByteArrayDataSource(attText, attContentType);
         in.addAttachment(attFileName, new DataHandler(ds));

http://git-wip-us.apache.org/repos/asf/camel/blob/10d69f22/components/camel-mail/src/test/resources/multipart-related.txt
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/test/resources/multipart-related.txt b/components/camel-mail/src/test/resources/multipart-related.txt
new file mode 100644
index 0000000..71b9e1c
--- /dev/null
+++ b/components/camel-mail/src/test/resources/multipart-related.txt
@@ -0,0 +1,29 @@
+MIME-Version: 1.0
+Content-Type: Multipart/Related; boundary=example-1; start="<95...@XIson.com>"; type="Application/X-FixedRecord"; start-info="-o ps"
+
+--example-1
+Content-Type: Application/X-FixedRecord
+Content-ID: <95...@XIson.com>
+
+25
+10
+34
+10
+25
+21
+26
+10
+--example-1
+Content-Type: Application/octet-stream
+Content-Description: The fixed length records
+Content-Transfer-Encoding: base64
+Content-ID: <95...@XIson.com>
+
+T2xkIE1hY0RvbmFsZCBoYWQgYSBmYXJtCkUgSS
+BFIEkgTwpBbmQgb24gaGlzIGZhcm0gaGUgaGFk
+IHNvbWUgZHVja3MKRSBJIEUgSSBPCldpdGggYS
+BxdWFjayBxdWFjayBoZXJlLAphIHF1YWNrIHF1
+YWNrIHRoZXJlLApldmVyeSB3aGVyZSBhIHF1YW
+NrIHF1YWNrCkUgSSBFIEkgTwo=
+
+--example-1--
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/10d69f22/components/camel-mail/src/test/resources/multipart-without-id.txt
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/test/resources/multipart-without-id.txt b/components/camel-mail/src/test/resources/multipart-without-id.txt
new file mode 100644
index 0000000..b772878
--- /dev/null
+++ b/components/camel-mail/src/test/resources/multipart-without-id.txt
@@ -0,0 +1,26 @@
+MIME-Version: 1.0
+Content-Type: Multipart/Mixed; boundary=example-1
+
+--example-1
+Content-Type: Application/X-FixedRecord
+
+25
+10
+34
+10
+25
+21
+26
+10
+--example-1
+Content-Type: Application/octet-stream
+Content-Transfer-Encoding: base64
+
+T2xkIE1hY0RvbmFsZCBoYWQgYSBmYXJtCkUgSS
+BFIEkgTwpBbmQgb24gaGlzIGZhcm0gaGUgaGFk
+IHNvbWUgZHVja3MKRSBJIEUgSSBPCldpdGggYS
+BxdWFjayBxdWFjayBoZXJlLAphIHF1YWNrIHF1
+YWNrIHRoZXJlLApldmVyeSB3aGVyZSBhIHF1YW
+NrIHF1YWNrCkUgSSBFIEkgTwo=
+
+--example-1--
\ No newline at end of file