Repository: camel
Updated Branches:
  refs/heads/camel-2.17.x df4004898 -> a863c8868


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/a863c886
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a863c886
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a863c886

Branch: refs/heads/camel-2.17.x
Commit: a863c88688471ff42d1272fb9fa95ad98b4217a7
Parents: df40048
Author: Stephan Siano <stephan.si...@sap.com>
Authored: Tue May 24 09:09:33 2016 +0200
Committer: Stephan Siano <stephan.si...@sap.com>
Committed: Tue May 24 09:15:56 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/a863c886/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/a863c886/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);
             }
         };
     }

Reply via email to