Skip to content

Commit

Permalink
Take MediaType set in pre-match filter into account during serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Apr 11, 2024
1 parent 47d782f commit 4f86368
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
//TODO?
return;
}
MediaType res = mediaTypeList.negotiateProduces(requestContext.serverRequest().getRequestHeader(HttpHeaders.ACCEPT))
.getKey();
MediaType res = null;
List<String> accepts = requestContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT);
for (String accept : accepts) {
res = mediaTypeList.negotiateProduces(accept).getKey();
if (res != null) {
break;
}
}
if (res == null) { // fallback for some tests
res = mediaTypeList.negotiateProduces(requestContext.serverRequest().getRequestHeader(HttpHeaders.ACCEPT))
.getKey();
}
if (res == null) {
throw new WebApplicationException(Response
.notAcceptable(Variant.mediaTypes(mediaTypeList.getSortedMediaTypes()).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
Expand All @@ -17,6 +22,9 @@
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
Expand Down Expand Up @@ -84,6 +92,37 @@ void setAcceptToTextInFilter() {
.body(equalTo("text"));
}

@Test
void entityJsonWithoutAcceptToTextInFilter() {
given().accept("application/json")
.when()
.get("test/entity")
.then()
.statusCode(200)
.body(containsString("\"text\""));
}

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

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

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

Expand All @@ -106,6 +145,16 @@ public String html() {
</html>
""";
}

@GET
@Path("entity")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public Entity entity() {
return new Entity("text");
}
}

public record Entity(String value) {
}

@PreMatching
Expand All @@ -120,4 +169,62 @@ public void filter(ContainerRequestContext requestContext) {
}
}
}

@Provider
@Produces(MediaType.TEXT_PLAIN)
public static class DummyTextMessageBodyWriter implements ServerMessageBodyWriter<Object> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target,
MediaType mediaType) {
return Entity.class.equals(type);
}

@Override
public void writeResponse(Object o, Type genericType, ServerRequestContext context)
throws WebApplicationException, IOException {
context.serverResponse().end(((Entity) o).value());
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Entity.class.equals(type);
}

@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
throw new IllegalStateException("should not be called");
}
}

@Provider
@Produces(MediaType.APPLICATION_JSON)
public static class DummyJsonMessageBodyWriter implements ServerMessageBodyWriter<Object> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target,
MediaType mediaType) {
return Entity.class.equals(type);
}

@Override
public void writeResponse(Object o, Type genericType, ServerRequestContext context)
throws WebApplicationException, IOException {
context.serverResponse().end("{\"value\":\"" + ((Entity) o).value() + "\"}");
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Entity.class.equals(type);
}

@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
throw new IllegalStateException("should not be called");
}
}
}

0 comments on commit 4f86368

Please sign in to comment.