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 2015/09/06 09:22:19 UTC

camel git commit: CAMEL-8569: Fixed split attachment not working correctly on Java 8.

Repository: camel
Updated Branches:
  refs/heads/master ae30d8815 -> 68eb60839


CAMEL-8569: Fixed split attachment not working correctly on Java 8.


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

Branch: refs/heads/master
Commit: 68eb60839eec7c1f2031d9765cb772f52c90b08d
Parents: ae30d88
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 6 09:22:25 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 6 09:23:07 2015 +0200

----------------------------------------------------------------------
 .../mail/SplitAttachmentsExpression.java        | 22 ++++++++++----------
 .../mail/MailSplitAttachmentsTest.java          | 16 ++++++--------
 2 files changed, 17 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/68eb6083/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java
index df24c06..708428e 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java
@@ -28,6 +28,7 @@ import org.apache.camel.Message;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultMessage;
 import org.apache.camel.support.ExpressionAdapter;
+import org.apache.camel.util.IOHelper;
 
 /**
  * A {@link org.apache.camel.Expression} which can be used to split a {@link MailMessage}
@@ -43,10 +44,10 @@ import org.apache.camel.support.ExpressionAdapter;
  *     </td>
  *   </tr>
  *   <tr>
- *     <td>As a byte[]</td>
+ *     <td>As a byte[] or String</td>
  *     <td>
  *       The attachments are split into new messages as the body. This allows the split messages to be easily used by
- *       other processors / routes, as many other camel components can work on the byte[], e.g. it can be written to disk
+ *       other processors / routes, as many other camel components can work on the byte[] or String, e.g. it can be written to disk
  *       using camel-file.
  *     </td>
  *   </tr>
@@ -112,21 +113,20 @@ public class SplitAttachmentsExpression extends ExpressionAdapter {
         if (attachment instanceof InputStream) {
             outMessage.setBody(readMimePart((InputStream) attachment));
             return outMessage;
+        } else if (attachment instanceof String || attachment instanceof byte[]) {
+            outMessage.setBody(attachment);
+            return outMessage;
+        } else {
+            return null;
         }
-        return null;
     }
 
 
     private byte[] readMimePart(InputStream mimePartStream) throws Exception {
-        //  mimePartStream could be base64 encoded, or not, but we don't need to worry about it as
-        // camel is smart enough to wrap it in a decoder stream (eg Base64DecoderStream) when required
+        // mimePartStream could be base64 encoded, or not, but we don't need to worry about it as
+        // Camel is smart enough to wrap it in a decoder stream (eg Base64DecoderStream) when required
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        int len;
-        byte[] buf = new byte[1024];
-        while ((len = mimePartStream.read(buf, 0, 1024)) != -1) {
-            bos.write(buf, 0, len);
-        }
-        mimePartStream.close();
+        IOHelper.copyAndCloseInput(mimePartStream, bos);
         return bos.toByteArray();
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/68eb6083/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java
index c66eff9..2ba65be 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java
@@ -59,6 +59,8 @@ public class MailSplitAttachmentsTest extends CamelTestSupport {
 
     @Test
     public void testSplitAttachments() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedMessageCount(2);
 
         Producer producer = endpoint.createProducer();
         producer.start();
@@ -66,9 +68,6 @@ public class MailSplitAttachmentsTest extends CamelTestSupport {
 
         Thread.sleep(2000);
 
-        MockEndpoint mock = getMockEndpoint("mock:split");
-        mock.expectedMessageCount(2);
-
         mock.assertIsSatisfied();
 
         Message first = mock.getReceivedExchanges().get(0).getIn();
@@ -87,23 +86,20 @@ public class MailSplitAttachmentsTest extends CamelTestSupport {
         assertTrue("Should have license.txt file attachment", license);
     }
 
-
     @Test
     public void testExtractAttachments() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedMessageCount(2);
 
         // set the expression to extract the attachments as byte[]s
         splitAttachmentsExpression.setExtractAttachments(true);
 
-
         Producer producer = endpoint.createProducer();
         producer.start();
         producer.process(exchange);
 
         Thread.sleep(2000);
 
-        MockEndpoint mock = getMockEndpoint("mock:split");
-        mock.expectedMessageCount(2);
-
         mock.assertIsSatisfied();
 
         Message first = mock.getReceivedExchanges().get(0).getIn();
@@ -119,8 +115,8 @@ public class MailSplitAttachmentsTest extends CamelTestSupport {
         byte[] expected1 = IOUtils.toByteArray(new FileDataSource("src/test/data/logo.jpeg").getInputStream());
         byte[] expected2 = IOUtils.toByteArray(new FileDataSource("src/main/resources/META-INF/LICENSE.txt").getInputStream());
 
-        assertArrayEquals(expected1, (byte[]) first.getBody());
-        assertArrayEquals(expected2, (byte[]) second.getBody());
+        assertArrayEquals(expected1, first.getBody(byte[].class));
+        assertArrayEquals(expected2, second.getBody(byte[].class));
     }
 
     @Override