From 447dcd3c45715212c3d00a7dcbe2a13bed887f8b Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Sun, 31 Mar 2024 10:32:36 +0200 Subject: [PATCH] CAMEL-20557: Rest DSL to use openapi spec directly --- .../rest/openapi/RestOpenApiProcessor.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java index f76ac9ee666e0..4e379e3a29269 100644 --- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java +++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java @@ -51,8 +51,8 @@ public class RestOpenApiProcessor extends DelegateAsyncProcessor implements Came private static final Logger LOG = LoggerFactory.getLogger(RestOpenApiProcessor.class); - private static final List METHODS - = Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH"); + // just use the most common verbs + private static final List METHODS = Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"); private CamelContext camelContext; private final RestOpenApiEndpoint endpoint; @@ -116,12 +116,14 @@ public boolean process(Exchange exchange, AsyncCallback callback) { // okay we cannot process this requires so return either 404 or 405. // to know if its 405 then we need to check if any other HTTP method would have a consumer for the "same" request final String contextPath = path; - boolean hasAnyMethod - = METHODS.stream().anyMatch(v -> RestConsumerContextPathMatcher.matchBestPath(v, contextPath, paths) != null); - if (hasAnyMethod) { - exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 405); - } else { + List allow = METHODS.stream() + .filter(v -> RestConsumerContextPathMatcher.matchBestPath(v, contextPath, paths) != null).toList(); + if (allow.isEmpty()) { exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 404); + } else { + exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 405); + // include list of allowed VERBs + exchange.getMessage().setHeader("Allow", String.join(", ", allow)); } exchange.setRouteStop(true); callback.done(true);