Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure the default content type is available in ContainerResponseFilters and WriterInterceptors when returning Response #44166

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
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.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ContainerResponseFilterContentTypeNoResolutionTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeContainerResponseFilter.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured
.given().accept("text/*")
.when().get("/test").then().statusCode(406);
}

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

@GET
@Produces("text/*")
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeContainerResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
if (responseContext.getMediaType() != null) {
throw new IllegalStateException(
"MediaType shouldn't have been resolved but got: " + responseContext.getMediaType());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
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.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ContainerResponseFilterContentTypeOverrideTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeContainerResponseFilter.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).type(MediaType.TEXT_PLAIN).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeContainerResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
if (!responseContext.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
throw new IllegalStateException(
"MediaType was not overridden by Response, got: " + responseContext.getMediaType()
+ " instead of expected: " + MediaType.TEXT_PLAIN);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
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.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ContainerResponseFilterContentTypeTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeContainerResponseFilter.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeContainerResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
if (!responseContext.getMediaType().isCompatible(MediaType.TEXT_XML_TYPE)) {
throw new IllegalStateException("MediaType was not provided, got: " + responseContext.getMediaType()
+ " instead of expected: " + MediaType.TEXT_XML);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WriterInterceptorContentTypeOverrideTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).type(MediaType.TEXT_PLAIN).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!context.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
throw new IllegalStateException("MediaType was not overridden by Response, got: " + context.getMediaType()
+ " instead of expected: " + MediaType.TEXT_PLAIN);
}
context.proceed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WriterInterceptorContentTypeTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!context.getMediaType().isCompatible(MediaType.TEXT_XML_TYPE)) {
throw new IllegalStateException("MediaType was not provided, got: " + context.getMediaType()
+ " instead of expected: " + MediaType.TEXT_XML);
}
context.proceed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
public static void runWriterInterceptors(ResteasyReactiveRequestContext context, Object entity, MessageBodyWriter writer,
Response response, WriterInterceptor[] writerInterceptor, ServerSerialisers serialisers) throws IOException {
WriterInterceptorContextImpl wc = new WriterInterceptorContextImpl(context, writerInterceptor, writer,
context.getAllAnnotations(), entity.getClass(), context.getGenericReturnType(), entity, response.getMediaType(),
response.getHeaders(), serialisers);
context.getAllAnnotations(), entity.getClass(), context.getGenericReturnType(), entity,
context.getResponseContentType().getMediaType(), response.getHeaders(), serialisers);
wc.proceed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.jboss.resteasy.reactive.server.handlers.BlockingHandler;
import org.jboss.resteasy.reactive.server.handlers.ExceptionHandler;
import org.jboss.resteasy.reactive.server.handlers.FixedProducesHandler;
import org.jboss.resteasy.reactive.server.handlers.FixedProducesSetDefaultContentTypeResponseHandler;
import org.jboss.resteasy.reactive.server.handlers.FormBodyHandler;
import org.jboss.resteasy.reactive.server.handlers.InputHandler;
import org.jboss.resteasy.reactive.server.handlers.InstanceHandler;
Expand All @@ -85,6 +86,7 @@
import org.jboss.resteasy.reactive.server.handlers.ResponseWriterHandler;
import org.jboss.resteasy.reactive.server.handlers.SseResponseWriterHandler;
import org.jboss.resteasy.reactive.server.handlers.VariableProducesHandler;
import org.jboss.resteasy.reactive.server.handlers.VariableProducesSetDefaultContentTypeResponseHandler;
import org.jboss.resteasy.reactive.server.mapping.RuntimeResource;
import org.jboss.resteasy.reactive.server.mapping.URITemplate;
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer;
Expand Down Expand Up @@ -472,6 +474,18 @@ public RuntimeResource buildResourceMethod(ResourceClass clazz,
: ScoreSystem.Diagnostic.WriterNotRequired);
}
} else {
if (method.getProduces() != null && method.getProduces().length > 0) {
if (method.getProduces().length == 1) {
MediaType mediaType = MediaType.valueOf(method.getProduces()[0]);
if (mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
handlers.add(new VariableProducesSetDefaultContentTypeResponseHandler(serverMediaType));
} else {
handlers.add(new FixedProducesSetDefaultContentTypeResponseHandler(mediaType));
}
} else {
handlers.add(new VariableProducesSetDefaultContentTypeResponseHandler(serverMediaType));
}
}
score.add(ScoreSystem.Category.Writer, ScoreSystem.Diagnostic.WriterRunTime);
}

Expand Down
Loading
Loading