Skip to content

Commit

Permalink
Merge pull request #484 from Croway/camel-spring-boot-4.8.0-branch
Browse files Browse the repository at this point in the history
Handle Consumes only for methods with body
  • Loading branch information
Croway authored Nov 21, 2024
2 parents 5e4adf6 + 73ac67c commit 063fb0a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,65 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex

@Override
public void registerHttpEndpoint(HttpEndpointModel model) {
RequestMappingInfo info = asRequestMappingInfo(model);
List<RequestMappingInfo> 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
public void unregisterHttpEndpoint(HttpEndpointModel model) {
// noop
}

private RequestMappingInfo asRequestMappingInfo(HttpEndpointModel model) {
private List<RequestMappingInfo> asRequestMappingInfo(HttpEndpointModel model) {
List<RequestMappingInfo> result = new ArrayList<>();

// allowed methods from model or endpoint
List<RequestMethod> 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<RequestMappingInfo> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down Expand Up @@ -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);
}
Expand All @@ -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()
Expand Down

0 comments on commit 063fb0a

Please sign in to comment.