You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by si...@apache.org on 2016/05/24 07:12:58 UTC

camel git commit: CAMEL-9986: MIME-Multipart Data Format is inconsistent if trying to unmarshal non-MIME data

Repository: camel
Updated Branches:
  refs/heads/master ff99845fb -> f84963b2b


CAMEL-9986: MIME-Multipart Data Format is inconsistent if trying to unmarshal non-MIME data

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

Branch: refs/heads/master
Commit: f84963b2ba5e7146b0e4e59df3dd4195640443ff
Parents: ff99845
Author: Stephan Siano <st...@sap.com>
Authored: Tue May 24 09:09:33 2016 +0200
Committer: Stephan Siano <st...@sap.com>
Committed: Tue May 24 09:12:27 2016 +0200

----------------------------------------------------------------------
 .../mime/multipart/MimeMultipartDataFormat.java | 21 ++++++------
 .../multipart/MimeMultipartDataFormatTest.java  | 36 +++++++++++++++++++-
 2 files changed, 45 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f84963b2/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 6504bc3..86e5f35 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
@@ -245,18 +245,17 @@ public class MimeMultipartDataFormat implements DataFormat {
             BodyPart bp = (BodyPart) content;
             camelMessage.setBody(bp.getInputStream());
             contentType = bp.getContentType();
-        } else {
-            // Last fallback: I don't see how this can happen, but we do this
-            // just to be safe
-            camelMessage.setBody(content);
-        }
-        if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) {
-            camelMessage.setHeader(CONTENT_TYPE, contentType);
-            ContentType ct = new ContentType(contentType);
-            String charset = ct.getParameter("charset");
-            if (charset != null) {
-                camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset));
+            if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) {
+                camelMessage.setHeader(CONTENT_TYPE, contentType);
+                ContentType ct = new ContentType(contentType);
+                String charset = ct.getParameter("charset");
+                if (charset != null) {
+                    camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset));
+                }
             }
+        } else {
+            // If we find no body part, try to leave the message alone
+            LOG.info("no MIME part found");
         }
         return camelMessage;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/f84963b2/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 db62669..7759c49 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dataformat.mime.multipart;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
@@ -299,6 +300,38 @@ public class MimeMultipartDataFormatTest extends CamelTestSupport {
         unmarshalAndCheckAttachmentName("@camel.apache.org");
     }
 
+    @Test
+    public void unmarshalNonMimeBody() {
+        in.setBody("This is not a MIME-Multipart");
+        Exchange out = template.send("direct:unmarshalonly", exchange);
+        assertNotNull(out.getOut());
+        String bodyStr = out.getOut().getBody(String.class);
+        assertEquals("This is not a MIME-Multipart", bodyStr);
+    }
+
+    @Test
+    public void unmarshalInlineHeadersNonMimeBody() {
+        in.setBody("This is not a MIME-Multipart");
+        Exchange out = template.send("direct:unmarshalonlyinlineheaders", exchange);
+        assertNotNull(out.getOut());
+        String bodyStr = out.getOut().getBody(String.class);
+        assertEquals("This is not a MIME-Multipart", bodyStr);
+    }
+
+    /*
+     * This test will only work of stream caching is enabled on the route. In order to find out whether the body
+     * is a multipart or not the stream has to be read, and if the underlying data is a stream (but not a stream cache)
+     * there is no way back
+     */
+    @Test
+    public void unmarshalInlineHeadersNonMimeBodyStream() throws UnsupportedEncodingException {
+        in.setBody(new ByteArrayInputStream("This is not a MIME-Multipart".getBytes("UTF-8")));
+        Exchange out = template.send("direct:unmarshalonlyinlineheaders", exchange);
+        assertNotNull(out.getOut());
+        String bodyStr = out.getOut().getBody(String.class);
+        assertEquals("This is not a MIME-Multipart", bodyStr);
+    }
+
     private void unmarshalAndCheckAttachmentName(String matcher) throws IOException, UnsupportedEncodingException {
         Exchange intermediate = template.send("direct:unmarshalonlyinlineheaders", exchange);
         assertNotNull(intermediate.getOut());
@@ -332,7 +365,8 @@ public class MimeMultipartDataFormatTest extends CamelTestSupport {
                 from("direct:marshalonlyrelated").marshal().mimeMultipart("related");
                 from("direct:marshalonlymixed").marshal().mimeMultipart();
                 from("direct:marshalonlyinlineheaders").marshal().mimeMultipart("mixed", false, true, "(included|x-.*)", false);
-                from("direct:unmarshalonlyinlineheaders").unmarshal().mimeMultipart(false, true, false);
+                from("direct:unmarshalonly").unmarshal().mimeMultipart(false, false, false);
+                from("direct:unmarshalonlyinlineheaders").streamCaching().unmarshal().mimeMultipart(false, true, false);
             }
         };
     }