Skip to content

Commit

Permalink
Merge pull request #465 from Croway/camel-spring-boot-4.8.0-branch
Browse files Browse the repository at this point in the history
CAMEL-20687: Kamelet have generated id to avoid duplicates
  • Loading branch information
Croway authored Oct 22, 2024
2 parents 897609e + 0aef02b commit acf13be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
});
}
}

}

0 comments on commit acf13be

Please sign in to comment.