From 73ac67cda93b4d067ac67e211e8f357cf6ebcfdb Mon Sep 17 00:00:00 2001 From: Croway Date: Thu, 21 Nov 2024 14:08:42 +0100 Subject: [PATCH] Handle Consumes only for methods with body --- .../CamelRequestHandlerMapping.java | 32 +++++++++++++++---- ...ringBootPlatformHttpCertificationTest.java | 21 +++++++++++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/CamelRequestHandlerMapping.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/CamelRequestHandlerMapping.java index 84e05a9b6c1..b528eecd3b1 100644 --- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/CamelRequestHandlerMapping.java +++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/CamelRequestHandlerMapping.java @@ -75,10 +75,12 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex @Override public void registerHttpEndpoint(HttpEndpointModel model) { - RequestMappingInfo info = asRequestMappingInfo(model); + List requestMappingInfos = asRequestMappingInfo(model); Method m = ReflectionHelper.findMethod(SpringBootPlatformHttpConsumer.class, "service", HttpServletRequest.class, HttpServletResponse.class); - registerMapping(info, model.getConsumer(), m); + for (RequestMappingInfo info : requestMappingInfos) { + registerMapping(info, model.getConsumer(), m); + } } @Override @@ -86,34 +88,52 @@ public void unregisterHttpEndpoint(HttpEndpointModel model) { // noop } - private RequestMappingInfo asRequestMappingInfo(HttpEndpointModel model) { + private List asRequestMappingInfo(HttpEndpointModel model) { + List result = new ArrayList<>(); + // allowed methods from model or endpoint List methods = new ArrayList<>(); String verbs = model.getVerbs(); if (verbs == null && model.getConsumer() != null) { PlatformHttpEndpoint endpoint = (PlatformHttpEndpoint) model.getConsumer().getEndpoint(); verbs = endpoint.getHttpMethodRestrict(); + + for (RequestMethod rm : RequestMethod.values()) { + createRequestMappingInfo(model, rm, result); + } } if (verbs != null) { for (String v : model.getVerbs().split(",")) { RequestMethod rm = RequestMethod.resolve(v); methods.add(rm); + + createRequestMappingInfo(model, rm, result); } } + return result; + } + + private void createRequestMappingInfo(HttpEndpointModel model, RequestMethod rm, List result) { + RequestMethod[] methods = new RequestMethod[]{}; + if (rm != null) { + methods = new RequestMethod[]{rm}; + } + RequestMappingInfo.Builder info = RequestMappingInfo .paths(model.getUri()) - .methods(methods.toArray(new RequestMethod[0])) + .methods(methods) .options(this.getBuilderConfiguration()); - if (model.getConsumes() != null) { + if (model.getConsumes() != null + && (RequestMethod.POST.name().equals(rm.name()) || RequestMethod.PUT.name().equals(rm.name()) || RequestMethod.PATCH.name().equals(rm.name()))) { info.consumes(model.getConsumes().split(",")); } if (model.getProduces() != null) { info.produces(model.getProduces().split(",")); } - return info.build(); + result.add(info.build()); } } diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java index a889cf1a7a0..4334414716e 100644 --- a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java +++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java @@ -85,6 +85,12 @@ public void configure() { .bindingMode(RestBindingMode.auto) .to("direct:rest"); + rest("rest").post("/test") + .consumes("application/json,application/xml") + .produces("application/json,application/xml") + .bindingMode(RestBindingMode.auto) + .to("direct:rest"); + from("direct:rest") .setBody(simple("Hello")); @@ -166,7 +172,7 @@ public void testLoad() throws Exception { public void nonSupportedContentType() { RestAssured.given() .header("Content-Type", "notSupported") - .get("rest/test") + .post("rest/test") .then() .statusCode(415); } @@ -182,6 +188,19 @@ public void oneContentType() { .body(is("Hello")); } + @Test + public void noContentTypeOkForGet() { + String body = RestAssured.given() + .get("rest/test") + .then() + .statusCode(200) + .extract() + .body() + .asString(); + + Assertions.assertThat(body).contains("Hello"); + } + @Test public void nonSupportedAccept() { RestAssured.given()