You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2024/05/13 07:54:24 UTC
(camel) 03/06: (chores) camel-http-common: code cleanup
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit d5916968c451bffc7128238204799b223f122844
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Sun May 12 09:05:51 2024 +0200
(chores) camel-http-common: code cleanup
- break large and complex methods
---
.../org/apache/camel/http/common/CamelServlet.java | 154 ++++++++++++---------
1 file changed, 90 insertions(+), 64 deletions(-)
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
index a3fb1eaaaf4..0c601e739c4 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
@@ -137,7 +137,8 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
}
}
- private void doAsyncExecution(HttpServletRequest req, HttpServletResponse resp, HttpConsumer consumer, AsyncContext context) {
+ private void doAsyncExecution(
+ HttpServletRequest req, HttpServletResponse resp, HttpConsumer consumer, AsyncContext context) {
try {
final CompletionStage<?> promise = doExecute(req, resp, consumer);
if (promise == null) { // early quit
@@ -169,31 +170,37 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
if (camelContext.isStopping() || camelContext.isStopped()) { // shouldn't occur but as a protection
return null;
}
- return executorServicePerContext.computeIfAbsent(camelContext, ctx -> {
- ExecutorServiceManager manager = camelContext.getExecutorServiceManager();
- ExecutorService es = manager.newThreadPool(this, getClass().getSimpleName() + "Executor", executorRef);
- if (es == null) {
- getServletContext().log(
- "ExecutorServiceRef " + executorRef + " not found in registry (as an ExecutorService instance) " +
- "or as a thread pool profile, will default for " + ctx.getName() + ".");
- es = manager.newDefaultThreadPool(this, getClass().getSimpleName() + "Executor");
- }
- ctx.addLifecycleStrategy(new LifecycleStrategySupport() {
- @Override
- public void onContextStopping(final CamelContext context) {
- final ExecutorService service = executorServicePerContext.remove(context);
- if (service != null && !service.isShutdown() && !service.isTerminated()) {
- service.shutdownNow();
- try { // give it a chance to finish before quitting
- service.awaitTermination(1, MINUTES);
- } catch (final InterruptedException e) {
- Thread.currentThread().interrupt();
- }
+ return executorServicePerContext.computeIfAbsent(camelContext, ctx -> createExecutorService(ctx, camelContext));
+ }
+
+ private ExecutorService createExecutorService(CamelContext ctx, CamelContext camelContext) {
+ ExecutorServiceManager manager = camelContext.getExecutorServiceManager();
+ ExecutorService es = manager.newThreadPool(this, getClass().getSimpleName() + "Executor", executorRef);
+ if (es == null) {
+ getServletContext().log(
+ "ExecutorServiceRef " + executorRef + " not found in registry (as an ExecutorService instance) " +
+ "or as a thread pool profile, will default for " + ctx.getName() + ".");
+ es = manager.newDefaultThreadPool(this, getClass().getSimpleName() + "Executor");
+ }
+ ctx.addLifecycleStrategy(createLifecycleStrategy());
+ return es;
+ }
+
+ private LifecycleStrategySupport createLifecycleStrategy() {
+ return new LifecycleStrategySupport() {
+ @Override
+ public void onContextStopping(final CamelContext context) {
+ final ExecutorService service = executorServicePerContext.remove(context);
+ if (service != null && !service.isShutdown() && !service.isTerminated()) {
+ service.shutdownNow();
+ try { // give it a chance to finish before quitting
+ service.awaitTermination(1, MINUTES);
+ } catch (final InterruptedException e) {
+ Thread.currentThread().interrupt();
}
}
- });
- return es;
- });
+ }
+ };
}
/**
@@ -240,21 +247,7 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
// if its an OPTIONS request then return which method is allowed
if ("OPTIONS".equals(req.getMethod()) && !consumer.isOptionsEnabled()) {
- String allowedMethods = METHODS.stream()
- .filter(m -> getServletResolveConsumerStrategy().isHttpMethodAllowed(req, m, getConsumers()))
- .collect(Collectors.joining(","));
- if (allowedMethods == null && consumer.getEndpoint().getHttpMethodRestrict() != null) {
- allowedMethods = consumer.getEndpoint().getHttpMethodRestrict();
- }
- if (allowedMethods == null) {
- // allow them all
- allowedMethods = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
- }
- if (!allowedMethods.contains("OPTIONS")) {
- allowedMethods = allowedMethods + ",OPTIONS";
- }
- res.addHeader("Allow", allowedMethods);
- res.setStatus(HttpServletResponse.SC_OK);
+ performOptionsRequest(req, res, consumer);
return null;
}
@@ -315,19 +308,7 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
final Processor processor = consumer.getProcessor();
isAsync = isAsync() && !forceAwait && AsyncProcessor.class.isInstance(processor);
if (isAsync) {
- result = AsyncProcessor.class.cast(processor)
- .processAsync(exchange)
- .whenComplete((r, ex) -> {
- if (ex != null) {
- exchange.setException(ex);
- } else {
- try {
- afterProcess(res, consumer, exchange, false);
- } catch (Exception e) {
- exchange.setException(e);
- }
- }
- });
+ result = tryAsyncProcess(res, consumer, processor, exchange);
} else {
processor.process(exchange);
}
@@ -345,6 +326,43 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
return result;
}
+ private CompletionStage<?> tryAsyncProcess(
+ HttpServletResponse res, HttpConsumer consumer, Processor processor, Exchange exchange) {
+ CompletionStage<?> result;
+ result = AsyncProcessor.class.cast(processor)
+ .processAsync(exchange)
+ .whenComplete((r, ex) -> {
+ if (ex != null) {
+ exchange.setException(ex);
+ } else {
+ try {
+ afterProcess(res, consumer, exchange, false);
+ } catch (Exception e) {
+ exchange.setException(e);
+ }
+ }
+ });
+ return result;
+ }
+
+ private void performOptionsRequest(HttpServletRequest req, HttpServletResponse res, HttpConsumer consumer) {
+ String allowedMethods = METHODS.stream()
+ .filter(m -> getServletResolveConsumerStrategy().isHttpMethodAllowed(req, m, getConsumers()))
+ .collect(Collectors.joining(","));
+ if (allowedMethods == null && consumer.getEndpoint().getHttpMethodRestrict() != null) {
+ allowedMethods = consumer.getEndpoint().getHttpMethodRestrict();
+ }
+ if (allowedMethods == null) {
+ // allow them all
+ allowedMethods = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
+ }
+ if (!allowedMethods.contains("OPTIONS")) {
+ allowedMethods = allowedMethods + ",OPTIONS";
+ }
+ res.addHeader("Allow", allowedMethods);
+ res.setStatus(HttpServletResponse.SC_OK);
+ }
+
protected void afterProcess(
HttpServletResponse res, HttpConsumer consumer, Exchange exchange,
boolean rethrow)
@@ -361,25 +379,33 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider {
}
consumer.getBinding().writeResponse(exchange, res);
} catch (IOException e) {
- log.error("Error processing request", e);
- if (rethrow) {
- throw e;
- } else {
- exchange.setException(e);
- }
+ handleIOException(exchange, rethrow, e);
} catch (Exception e) {
- log.error("Error processing request", e);
- if (rethrow) {
- throw new RuntimeCamelException(e);
- } else {
- exchange.setException(e);
- }
+ handleException(exchange, rethrow, e);
} finally {
consumer.doneUoW(exchange);
consumer.releaseExchange(exchange, false);
}
}
+ private void handleException(Exchange exchange, boolean rethrow, Exception e) {
+ log.error("Error processing request", e);
+ if (rethrow) {
+ throw new RuntimeCamelException(e);
+ } else {
+ exchange.setException(e);
+ }
+ }
+
+ private void handleIOException(Exchange exchange, boolean rethrow, IOException e) throws IOException {
+ log.error("Error processing request", e);
+ if (rethrow) {
+ throw e;
+ } else {
+ exchange.setException(e);
+ }
+ }
+
private HttpConsumer doResolve(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Is there a consumer registered for the request.
HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers());