diff --git a/components-starter/camel-kamelet-starter/src/test/java/org/apache/camel/component/kamelet/springboot/KameletRouteTest.java b/components-starter/camel-kamelet-starter/src/test/java/org/apache/camel/component/kamelet/springboot/KameletRouteTest.java index 76f35e63a58..c038165c3e1 100644 --- a/components-starter/camel-kamelet-starter/src/test/java/org/apache/camel/component/kamelet/springboot/KameletRouteTest.java +++ b/components-starter/camel-kamelet-starter/src/test/java/org/apache/camel/component/kamelet/springboot/KameletRouteTest.java @@ -16,29 +16,25 @@ */ package org.apache.camel.component.kamelet.springboot; -import java.util.UUID; - -import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.CamelContext; +import org.apache.camel.FluentProducerTemplate; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; -import org.junit.jupiter.api.Test; import org.apache.camel.spring.boot.CamelAutoConfiguration; -import org.apache.camel.FluentProducerTemplate; -import org.apache.camel.CamelContext; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.test.annotation.DirtiesContext; -import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext @CamelSpringBootTest @SpringBootTest(classes = { CamelAutoConfiguration.class, KameletRouteTest.class, }) - public class KameletRouteTest { @Autowired @@ -61,18 +57,6 @@ public void testChain() { assertThat(fluentTemplate.toF("direct:chain").withBody(body).request(String.class)).isEqualTo("b-a-" + body); } - @Test - public void duplicateRouteId() { - RouteBuilder rb = new RouteBuilder(context) { - @Override - public void configure() { - from("direct:start").to("kamelet:echo/test?prefix=test"); - } - }; - - assertThrows(FailedToCreateRouteException.class, () -> rb.addRoutesToCamelContext(context)); - } - // ********************************************** // // test set-up 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 4ff0fee0c58..730982fc7cf 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 @@ -16,13 +16,28 @@ */ package org.apache.camel.component.platform.http.springboot; +import jakarta.activation.DataHandler; +import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import org.apache.camel.Message; +import org.apache.camel.attachment.AttachmentMessage; +import org.apache.camel.attachment.CamelFileDataSource; import org.apache.camel.component.platform.http.PlatformHttpEndpoint; import org.apache.camel.http.base.HttpHelper; import org.apache.camel.http.common.DefaultHttpBinding; +import org.apache.camel.util.FileUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Locale; public class SpringBootPlatformHttpBinding extends DefaultHttpBinding { + private static final Logger LOG = LoggerFactory.getLogger(SpringBootPlatformHttpBinding.class); protected void populateRequestParameters(HttpServletRequest request, Message message) { super.populateRequestParameters(request, message); @@ -47,4 +62,45 @@ private boolean useRestMatching(String path) { return path.indexOf('{') > -1; } + @Override + protected void populateAttachments(HttpServletRequest request, Message message) { + // check if there is multipart files, if so will put it into DataHandler + if (request instanceof MultipartHttpServletRequest multipartHttpServletRequest) { + File tmpFolder = (File) request.getServletContext().getAttribute(ServletContext.TEMPDIR); + multipartHttpServletRequest.getFileMap().forEach((name, multipartFile) -> { + try { + Path uploadedTmpFile = Paths.get(tmpFolder.getPath(), name); + multipartFile.transferTo(uploadedTmpFile); + + if (name != null) { + name = name.replaceAll("[\n\r\t]", "_"); + } + + boolean accepted = true; + + if (getFileNameExtWhitelist() != null) { + String ext = FileUtil.onlyExt(name); + if (ext != null) { + ext = ext.toLowerCase(Locale.US); + if (!getFileNameExtWhitelist().equals("*") && !getFileNameExtWhitelist().contains(ext)) { + accepted = false; + } + } + } + + if (accepted) { + AttachmentMessage am = message.getExchange().getMessage(AttachmentMessage.class); + am.addAttachment(name, new DataHandler(new CamelFileDataSource(uploadedTmpFile.toFile(), name))); + } else { + LOG.debug( + "Cannot add file as attachment: {} because the file is not accepted according to fileNameExtWhitelist: {}", + name, getFileNameExtWhitelist()); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + } + }