This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.7.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.7.x by this push:
     new 6dff856  CAMEL-16582 (#5514)
6dff856 is described below

commit 6dff85675e9b73e8a528bc2683935ec3c1ed26b7
Author: Ricardo M. Augusto <rmaugu...@gmail.com>
AuthorDate: Thu May 6 03:49:16 2021 -0300

    CAMEL-16582 (#5514)
    
    * CAMEL-16551: Fixed property placeholder not working in marshal/unmarshal 
(ref).
    
    * Fix concurrent bug with multiple attachments CAMEL-16582
    
    Co-authored-by: Claus Ibsen <claus.ib...@gmail.com>
    Co-authored-by: Ricardo M Augusto <ricardo.marq...@oruspay.com.br>
---
 .../mime/multipart/MimeMultipartDataFormat.java    |  6 ++-
 .../multipart/MimeMultipartDataFormatTest.java     | 47 ++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)

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 94b29de..cec5749 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
@@ -107,6 +107,7 @@ public class MimeMultipartDataFormat extends 
DefaultDataFormat {
             writeBodyPart(bodyContent, part, contentType);
             mp.addBodyPart(part);
             if (exchange.getIn(AttachmentMessage.class).hasAttachments()) {
+                List<String> idsToRemove = new ArrayList<>();
                 for (Map.Entry<String, Attachment> entry : 
exchange.getIn(AttachmentMessage.class).getAttachmentObjects()
                         .entrySet()) {
                     String attachmentFilename = entry.getKey();
@@ -128,7 +129,10 @@ public class MimeMultipartDataFormat extends 
DefaultDataFormat {
                         }
                     }
                     mp.addBodyPart(part);
-                    
exchange.getMessage(AttachmentMessage.class).removeAttachment(attachmentFilename);
+                    idsToRemove.add(attachmentFilename);
+                }
+                for (String id : idsToRemove) {
+                    
exchange.getMessage(AttachmentMessage.class).removeAttachment(id);
                 }
             }
             mm.setContent(mp);
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 f8846ec..b58c502 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
@@ -194,6 +194,53 @@ public class MimeMultipartDataFormatTest extends 
CamelTestSupport {
     }
 
     @Test
+    public void roundtripWithBinaryMultipleAttachments() throws IOException {
+        String attContentType1 = "application/binary";
+        byte[] attText1 = { 0, 1, 2, 3, 4, 5, 6, 7 };
+        String attFileName1 = "Attachment File Name 1";
+
+        String attContentType2 = "application/binary";
+        byte[] attText2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
+        String attFileName2 = "Attachment File Name 2";
+
+        in.setBody("Body text");
+        DataSource ds1 = new ByteArrayDataSource(attText1, attContentType1);
+        DataSource ds2 = new ByteArrayDataSource(attText2, attContentType2);
+        in.addAttachment(attFileName1, new DataHandler(ds1));
+        in.addAttachment(attFileName2, new DataHandler(ds2));
+        addAttachment(ds1, attFileName1, null);
+        addAttachment(ds2, attFileName2, null);
+
+        Exchange result = template.send("direct:roundtrip", exchange);
+        AttachmentMessage out = result.getMessage(AttachmentMessage.class);
+        assertEquals("Body text", out.getBody(String.class));
+        assertTrue(out.hasAttachments());
+        assertEquals(2, out.getAttachmentNames().size());
+
+        assertTrue(out.getAttachmentNames().contains(attFileName1));
+        assertTrue(out.getAttachmentNames().contains(attFileName2));
+
+        DataHandler dh1 = out.getAttachment(attFileName1);
+        assertNotNull(dh1);
+        assertEquals(attContentType1, dh1.getContentType());
+
+        InputStream is1 = dh1.getInputStream();
+        ByteArrayOutputStream os1 = new ByteArrayOutputStream();
+        IOHelper.copyAndCloseInput(is1, os1);
+        assertArrayEquals(attText1, os1.toByteArray());
+
+        DataHandler dh2 = out.getAttachment(attFileName2);
+        assertNotNull(dh2);
+        assertEquals(attContentType2, dh2.getContentType());
+
+        InputStream is2 = dh2.getInputStream();
+        ByteArrayOutputStream os2 = new ByteArrayOutputStream();
+        IOHelper.copyAndCloseInput(is2, os2);
+        assertArrayEquals(attText2, os2.toByteArray());
+
+    }
+
+    @Test
     public void roundtripWithBinaryAttachmentsAndBinaryContent() throws 
IOException {
         String attContentType = "application/binary";
         byte[] attText = { 0, 1, 2, 3, 4, 5, 6, 7 };

Reply via email to