Skip to content

Commit

Permalink
Use headers set in PreMatching filter during media type negotiation
Browse files Browse the repository at this point in the history
Fixes: #38130
  • Loading branch information
geoand committed Jan 10, 2024
1 parent 4177598 commit 35fab48
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.jboss.resteasy.reactive.common.util.ServerMediaType;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.mapping.RuntimeResource;
import org.jboss.resteasy.reactive.server.spi.ServerHttpRequest;
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler;

/**
Expand Down Expand Up @@ -100,12 +99,13 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti

public MediaType selectMediaType(ResteasyReactiveRequestContext requestContext, Holder holder) {
MediaType selected = null;
ServerHttpRequest httpServerRequest = requestContext.serverRequest();
if (httpServerRequest.containsRequestHeader(HttpHeaders.ACCEPT)) {
List<String> accepts = requestContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT);
for (String accept : accepts) {
Map.Entry<MediaType, MediaType> entry = holder.serverMediaType
.negotiateProduces(requestContext.serverRequest().getRequestHeader(HttpHeaders.ACCEPT), null);
.negotiateProduces(accept, null);
if (entry.getValue() != null) {
selected = entry.getValue();
break;
}
}
if (selected == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.jboss.resteasy.reactive.server.vertx.test.matching;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class PreMatchAcceptInHeader {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(PathSegmentTest.Resource.class);
}
});

@Test
void browserDefault() {
given().accept("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
.when()
.get("test")
.then()
.statusCode(200)
.body(containsString("<html>"));
}

@Test
void text() {
given().accept("text/plain")
.when()
.get("test")
.then()
.statusCode(200)
.body(equalTo("test"));
}

@Test
void html() {
given().accept("text/html")
.when()
.get("test")
.then()
.statusCode(200)
.body(equalTo("test"));
}

@Test
void json() {
given().accept("application/json")
.when()
.get("test")
.then()
.statusCode(404);
}

@Test
void setAcceptToText() {
given().accept("application/json")
.header("x-set-accept-to-text", "true")
.when()
.get("test")
.then()
.statusCode(200)
.body(equalTo("test"));
}

@Path("/test")
public static class Resource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String text() {
return "text";
}

@GET
@Produces(MediaType.TEXT_HTML)
public String html() {
return """
<html>
<head>
</head>
<body>
Hello World
</body>
</html>
""";
}
}

@PreMatching
@Provider
public static class SetAcceptHeaderFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext requestContext) {
MultivaluedMap<String, String> headers = requestContext.getHeaders();
if ("true".equals(headers.getFirst("x-set-accept-to-text"))) {
headers.putSingle(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN);
}
}
}
}

0 comments on commit 35fab48

Please sign in to comment.