Skip to content

Commit

Permalink
Use CompletableFuture instead of Interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway committed Jan 29, 2025
1 parent 2df89e2 commit 671f91e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package org.apache.camel.component.platform.http.springboot;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
Expand All @@ -40,8 +41,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements PlatformHttpConsumer, Suspendable, SuspendableService {

Expand All @@ -50,6 +49,7 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
private HttpBinding binding;
private final boolean handleWriteResponseError;
private CookieConfiguration cookieConfiguration;
private Executor executor;

public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
super(endpoint, processor);
Expand All @@ -59,6 +59,12 @@ public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor p
this.binding.setFileNameExtWhitelist(endpoint.getFileNameExtWhitelist());
this.binding.setUseReaderForPayload(!endpoint.isUseStreaming());
this.handleWriteResponseError = endpoint.isHandleWriteResponseError();
this.executor = Executors.newSingleThreadExecutor();
}

public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor, Executor executor) {
this(endpoint, processor);
this.executor = executor;
}

/**
Expand All @@ -82,21 +88,22 @@ public PlatformHttpEndpoint getEndpoint() {
*/
@ResponseBody
public CompletableFuture<Void> service(HttpServletRequest request, HttpServletResponse response) {
LOG.trace("Service: {}", request);
try {
handleService(request, response);
} catch (Exception e) {
// do not leak exception back to caller
LOG.warn("Error handling request due to: {}", e.getMessage(), e);
return CompletableFuture.runAsync(() -> {
LOG.trace("Service: {}", request);
try {
if (!response.isCommitted()) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
handleService(request, response);
} catch (Exception e) {
// do not leak exception back to caller
LOG.warn("Error handling request due to: {}", e.getMessage(), e);
try {
if (!response.isCommitted()) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} catch (Exception e1) {
// ignore
}
} catch (Exception e1) {
// ignore
}
}
return CompletableFuture.completedFuture(null);
}, executor);
}

protected void handleService(HttpServletRequest request, HttpServletResponse response) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
import org.apache.camel.component.platform.http.spi.PlatformHttpConsumer;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.AsyncExecutionInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;

import java.util.concurrent.Executor;

Expand All @@ -43,19 +39,7 @@ public SpringBootPlatformHttpEngine(int port, Executor executor) {

@Override
public PlatformHttpConsumer createConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new SpringBootPlatformHttpConsumer(endpoint, processor));

JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
jdkRegexpMethodPointcut.setPattern("org.apache.camel.component.platform.http.springboot.SpringBootPlatformHttpConsumer.service");

DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
advisor.setAdvice(new AsyncExecutionInterceptor(executor));
advisor.setPointcut(jdkRegexpMethodPointcut);

factory.addAdvisor(advisor);

return (SpringBootPlatformHttpConsumer) factory.getProxy();
return new SpringBootPlatformHttpConsumer(endpoint, processor, executor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand Down Expand Up @@ -400,8 +398,7 @@ public void testAddCookie() throws Exception {
detailedCookie()
.value("bar")
.path(CookieConfiguration.DEFAULT_PATH)
.domain((String) null)
.sameSite(CookieConfiguration.DEFAULT_SAME_SITE.getValue()))
.domain((String) null))
.body(equalTo("add"));
}
}

0 comments on commit 671f91e

Please sign in to comment.