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-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new ec612dcb32f CAMEL-21544: Add option to turn on|off whether 
application/x-www-form… (#1363)
ec612dcb32f is described below

commit ec612dcb32f62d60e17cc3d67f4d2c93bec15b49
Author: Federico Mariani <[email protected]>
AuthorDate: Wed Feb 5 11:55:15 2025 +0100

    CAMEL-21544: Add option to turn on|off whether application/x-www-form… 
(#1363)
    
    * CAMEL-21544: Add option to turn on|off whether 
application/x-www-form-urlencoded should populate Map body with form
    
    * Add file content type in case of single upload
---
 .../springboot/SpringBootPlatformHttpBinding.java  | 11 ++++++--
 .../http/springboot/PlatformHttpStreamingTest.java | 32 ++++++++++++++++++++++
 .../SpringBootPlatformHttpEngineTest.java          |  8 +++++-
 3 files changed, 48 insertions(+), 3 deletions(-)

diff --git 
a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpBinding.java
 
b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpBinding.java
index 415d5c5f0a6..71ea2d236f3 100644
--- 
a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpBinding.java
+++ 
b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpBinding.java
@@ -33,7 +33,6 @@ import org.apache.camel.attachment.CamelFileDataSource;
 import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
 import org.apache.camel.component.platform.http.spi.Method;
 import org.apache.camel.converter.stream.CachedOutputStream;
-import org.apache.camel.converter.stream.ReaderCache;
 import org.apache.camel.http.base.HttpHelper;
 import org.apache.camel.http.common.DefaultHttpBinding;
 import org.apache.camel.support.ExchangeHelper;
@@ -128,6 +127,9 @@ public class SpringBootPlatformHttpBinding extends 
DefaultHttpBinding {
                         // populate body in case there is only one attachment
                         if (isSingleAttachment) {
                             message.setHeader(Exchange.FILE_NAME, name);
+                            if (multipartFile.getContentType() != null) {
+                                message.setHeader(Exchange.CONTENT_TYPE, 
multipartFile.getContentType());
+                            }
                             message.setBody(uploadedTmpFile);
                         }
                     } else {
@@ -156,7 +158,12 @@ public class SpringBootPlatformHttpBinding extends 
DefaultHttpBinding {
     public void readRequest(HttpServletRequest request, Message message) {
         super.readRequest(request, message);
 
-        if 
(METHODS_WITH_BODY_ALLOWED.contains(Method.valueOf(request.getMethod())) &&
+        populateMultiFormData(request, message);
+    }
+
+    private static void populateMultiFormData(HttpServletRequest request, 
Message message) {
+        if (((PlatformHttpEndpoint) 
message.getExchange().getFromEndpoint()).isPopulateBodyWithForm() &&
+                
METHODS_WITH_BODY_ALLOWED.contains(Method.valueOf(request.getMethod())) &&
                 (message.getBody() instanceof StreamCache ||
                         (message.getBody() == null && 
!"POST".equals(request.getMethod()))) &&
                 request.getContentType() != null &&
diff --git 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpStreamingTest.java
 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpStreamingTest.java
index 2b130c7f3e8..fe3916d9d1d 100644
--- 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpStreamingTest.java
+++ 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpStreamingTest.java
@@ -40,6 +40,8 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 
 import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.emptyOrNullString;
 import static org.hamcrest.Matchers.is;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -110,6 +112,28 @@ public class PlatformHttpStreamingTest {
                 .then().statusCode(200).body(is("foo=bar"));
     }
 
+    @Test
+    void testHeaderAndBodyWithFormUrlEncodedBody() throws Exception {
+        given().contentType(ContentType.URLENC).formParam("foo", "bar")
+                .post("/headerAndBodyUrlEncoded")
+                .then()
+                .statusCode(200)
+                .header("foo", "bar")
+                .header("BodyClass", containsString("HashMap"))
+                .body(is("{foo=bar}"));
+    }
+
+    @Test
+    void testOnlyHeaderWithFormUrlEncodedBody() throws Exception {
+        given().contentType(ContentType.URLENC).formParam("foo", "bar")
+                .post("/headerUrlEncoded")
+                .then()
+                .statusCode(200)
+                .header("foo", "bar")
+                .header("BodyClass", containsString("ReaderCache"))
+                .body(is("foo=bar"));
+    }
+
     @Test
     void testStreamingWithSpecificEncoding() throws Exception {
         Path input = Files.createTempFile("platform-http-input", "dat");
@@ -148,6 +172,14 @@ public class PlatformHttpStreamingTest {
                     
from("platform-http:/nonStreaming").transform().simple("Hello ${body}");
                     from("platform-http:/nonStreamingFile").log("Done 
processing request");
                     
from("platform-http:/nonStreamingUrlEncoded").setBody().simple("foo=${header.foo}");
+                    from("platform-http:/headerAndBodyUrlEncoded")
+                            .process(exchange ->
+                                    
exchange.getMessage().setHeader("BodyClass", 
exchange.getIn().getBody().getClass().getName()))
+                            .convertBodyTo(String.class);
+                    
from("platform-http:/headerUrlEncoded?populateBodyWithForm=false")
+                            .process(exchange ->
+                                    
exchange.getMessage().setHeader("BodyClass", 
exchange.getIn().getBody().getClass().getName()))
+                            .log("Done processing request");
                 }
             };
         }
diff --git 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngineTest.java
 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngineTest.java
index c9f9d0959b1..3a0fd033d5e 100644
--- 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngineTest.java
+++ 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngineTest.java
@@ -20,6 +20,7 @@ import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
 import jakarta.activation.DataHandler;
 import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
 import org.apache.camel.attachment.AttachmentMessage;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
@@ -93,6 +94,8 @@ public class SpringBootPlatformHttpEngineTest {
                             AttachmentMessage message = 
exchange.getMessage(AttachmentMessage.class);
                             DataHandler attachment = 
message.getAttachment(attachmentId);
                             exchange.getMessage().setHeader("myDataHandler", 
attachment);
+                            
exchange.getMessage().setHeader("singleFileContentType",
+                                    
exchange.getIn().getHeader(Exchange.CONTENT_TYPE));
                             
exchange.getMessage().setBody(attachment.getContent());
                         });
 
@@ -147,14 +150,17 @@ public class SpringBootPlatformHttpEngineTest {
 
         Files.write(tempFile.toPath(), 
fileContent.getBytes(StandardCharsets.UTF_8));
 
+        String dummyMimeType = "custom/mime-type";
+
         given()
-                .multiPart(attachmentId, tempFile)
+                .multiPart(attachmentId, tempFile, dummyMimeType)
                 .when()
                 .post("/uploadSingle")
                 .then()
                 .statusCode(200)
                 // Assert that the attachment is a DataHandler
                 .header("myDataHandler", 
containsString("jakarta.activation.DataHandler"))
+                .header("singleFileContentType", is(dummyMimeType))
                 .body(is(fileContent));
     }
 

Reply via email to