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 958036619c28 CAMEL-26139: camel-servlet - attachmentMultipartBinding 
only reads parts of multipart/form-data requests (#28912)
958036619c28 is described below

commit 958036619c2828853254818fda4488d1565b5d4f
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 2 20:42:44 2026 +0100

    CAMEL-26139: camel-servlet - attachmentMultipartBinding only reads parts of 
multipart/form-data requests (#28912)
    
    …rts of multipart/form-data requests
    
    AttachmentHttpBinding called HttpServletRequest.getParts() for every 
request.
    The servlet container throws for a request that is not multipart/form-data,
    so with attachmentMultipartBinding=true every GET or plain POST failed with
    500 "Cannot populate attachments". The parts are now only read when the
    Content-Type is multipart/form-data.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../component/servlet/AttachmentHttpBinding.java    | 11 +++++++++++
 .../component/servlet/MultipartUploadTest.java      | 21 +++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git 
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
 
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
index e60c8f94219f..0f992f9ff73e 100644
--- 
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
+++ 
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
@@ -44,12 +44,18 @@ import org.slf4j.LoggerFactory;
 public final class AttachmentHttpBinding extends DefaultHttpBinding {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(AttachmentHttpBinding.class);
+    private static final String MULTIPART_FORM_DATA = "multipart/form-data";
 
     AttachmentHttpBinding() {
     }
 
     @Override
     protected void populateAttachments(HttpServletRequest request, Message 
message) {
+        // the servlet container throws from getParts() when the request is 
not multipart/form-data, so a plain
+        // request (GET, or a POST of text, JSON and so on) has no parts to 
read and must not fail
+        if (!isMultipartFormData(request)) {
+            return;
+        }
         try {
             Collection<Part> parts = request.getParts();
             for (Part part : parts) {
@@ -80,6 +86,11 @@ public final class AttachmentHttpBinding extends 
DefaultHttpBinding {
         }
     }
 
+    private static boolean isMultipartFormData(HttpServletRequest request) {
+        String contentType = request.getContentType();
+        return contentType != null && contentType.regionMatches(true, 0, 
MULTIPART_FORM_DATA, 0, MULTIPART_FORM_DATA.length());
+    }
+
     public final class PartDataSource implements DataSource {
         private final Part part;
 
diff --git 
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultipartUploadTest.java
 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultipartUploadTest.java
index fcc9f05ecd07..8122a19c86e4 100644
--- 
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultipartUploadTest.java
+++ 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultipartUploadTest.java
@@ -54,6 +54,24 @@ public class MultipartUploadTest extends 
ServletCamelRouterTestSupport {
         assertEquals(content, response.getText());
     }
 
+    @Test
+    void testPlainPostWithAttachmentMultipartBinding() throws IOException {
+        // a request that is not multipart/form-data has no parts and must not 
fail with "Cannot populate attachments"
+        InputStream inputStream = 
context.getTypeConverter().convertTo(InputStream.class, "Hello World");
+        PostMethodWebRequest request
+                = new PostMethodWebRequest(contextUrl + 
"/services/plainRequest", inputStream, "text/plain");
+        WebResponse response = query(request, false);
+        assertEquals(200, response.getResponseCode());
+        assertEquals("Bye Hello World", response.getText());
+    }
+
+    @Test
+    void testGetWithAttachmentMultipartBinding() throws IOException {
+        WebResponse response = query(new GetMethodWebRequest(contextUrl + 
"/services/plainRequest"), false);
+        assertEquals(200, response.getResponseCode());
+        assertEquals("Bye ", response.getText());
+    }
+
     @Override
     protected RoutesBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -72,6 +90,9 @@ public class MultipartUploadTest extends 
ServletCamelRouterTestSupport {
                                 }
                             }
                         });
+
+                from("servlet:plainRequest?attachmentMultipartBinding=true")
+                        .transform(simple("Bye ${body}"));
             }
         };
     }

Reply via email to