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

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


The following commit(s) were added to refs/heads/main by this push:
     new d44150e4ad22 CAMEL-25867: camel-mail - mimeMultipart unmarshal with 
headersInline keeps the start of a stream-cached body (#28384)
d44150e4ad22 is described below

commit d44150e4ad225c05273de83d38666b6733176bcf
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Oct 24 19:13:11 2026 +0530

    CAMEL-25867: camel-mail - mimeMultipart unmarshal with headersInline keeps 
the start of a stream-cached body (#28384)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../mime/multipart/MimeMultipartDataFormat.java    |   6 +-
 .../MimeMultipartInlineHeadersStreamCacheTest.java | 117 +++++++++++++++++++++
 2 files changed, 122 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 f2eac282a944..8afc46593e6f 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dataformat.mime.multipart;
 
+import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -297,7 +298,10 @@ public class MimeMultipartDataFormat extends 
DefaultDataFormat {
 
     private Object unmarshalWithInlineHeaders(Exchange exchange, InputStream 
stream)
             throws IOException, MessagingException {
-        MimeBodyPart mimeMessage = new MimeBodyPart(stream);
+        // The MIME header parser marks and resets a stream that supports mark 
(after every CR of the headers), and
+        // uses a ByteArrayInputStream as it is. The stream may be the message 
body (an in-memory stream cache), which
+        // goes back to its last mark when it is reset to be read again, so 
give the parser a stream of its own to mark.
+        MimeBodyPart mimeMessage = new MimeBodyPart(new 
BufferedInputStream(stream));
         Message camelMessage = exchange.getMessage();
         MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
         copyNonStandardHeaders(mimeMessage, camelMessage);
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartInlineHeadersStreamCacheTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartInlineHeadersStreamCacheTest.java
new file mode 100644
index 000000000000..cc198d1baa14
--- /dev/null
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartInlineHeadersStreamCacheTest.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dataformat.mime.multipart;
+
+import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+/**
+ * With {@code headersInline=true} the unmarshal parses the MIME headers from 
the body stream. When the body is an
+ * in-memory stream cache, the MIME header parser marks that stream after 
every CR of the headers, so a later reset of
+ * the stream cache must still go back to the start of the body.
+ */
+class MimeMultipartInlineHeadersStreamCacheTest extends CamelTestSupport {
+
+    private static final String MULTIPART = "Content-Type: multipart/mixed; 
boundary=\"XYZ\"\r\n"
+                                            + "MIME-Version: 1.0\r\n"
+                                            + "X-Order: 42\r\n"
+                                            + "\r\n"
+                                            + "--XYZ\r\n"
+                                            + "Content-Type: text/plain\r\n"
+                                            + "\r\n"
+                                            + "Body text\r\n"
+                                            + "--XYZ\r\n"
+                                            + "Content-Type: text/plain\r\n"
+                                            + "Content-Disposition: 
attachment; filename=\"a.txt\"\r\n"
+                                            + "\r\n"
+                                            + "Attachment text\r\n"
+                                            + "--XYZ--\r\n";
+
+    private static final String NOT_MULTIPART = "first line\r\n"
+                                                + "second line\r\n"
+                                                + "\r\n"
+                                                + "third line\r\n";
+
+    private static InputStream stream(String s) {
+        // not a ByteArrayInputStream, so that stream caching copies it into 
an in-memory stream cache, as for the
+        // body of an HTTP request
+        return new FilterInputStream(new 
ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII))) {
+        };
+    }
+
+    @Test
+    void originalMessageOfAFailedExchangeHasTheWholeBody() throws Exception {
+        MockEndpoint dead = getMockEndpoint("mock:dead");
+        dead.expectedMessageCount(1);
+
+        template.sendBody("direct:fail", stream(MULTIPART));
+
+        dead.assertIsSatisfied();
+        assertEquals(MULTIPART, 
dead.getReceivedExchanges().get(0).getMessage().getBody(String.class));
+    }
+
+    @Test
+    void bodyThatIsNotMultipartIsKeptWhole() {
+        Exchange out = template.send("direct:unmarshal", e -> 
e.getMessage().setBody(stream(NOT_MULTIPART)));
+
+        assertEquals(NOT_MULTIPART, out.getMessage().getBody(String.class));
+    }
+
+    @Test
+    void streamCacheKeptInAPropertyCanBeReadAgain() {
+        Exchange out = template.send("direct:keep", e -> 
e.getMessage().setBody(stream(MULTIPART)));
+
+        StreamCache kept = assertInstanceOf(StreamCache.class, 
out.getProperty("original"));
+        kept.reset();
+        assertEquals(MULTIPART, 
out.getContext().getTypeConverter().convertTo(String.class, out, kept));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                context.setStreamCaching(true);
+
+                
errorHandler(deadLetterChannel("mock:dead").useOriginalMessage());
+
+                from("direct:fail")
+                        .unmarshal().mimeMultipart(false, true, false)
+                        .throwException(new IllegalStateException("downstream 
failure"));
+
+                from("direct:unmarshal")
+                        .unmarshal().mimeMultipart(false, true, false);
+
+                from("direct:keep")
+                        .setProperty("original", body())
+                        .unmarshal().mimeMultipart(false, true, false);
+            }
+        };
+    }
+}

Reply via email to