diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/FilterWithPathParamsTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/FilterWithPathParamsTest.java new file mode 100644 index 0000000000000..8ca89f3494238 --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/FilterWithPathParamsTest.java @@ -0,0 +1,111 @@ +package io.quarkus.resteasy.reactive.server.test.customproviders; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; +import static org.hamcrest.CoreMatchers.is; + +import java.util.function.Supplier; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.reactive.server.ServerRequestFilter; +import org.jboss.resteasy.reactive.server.ServerResponseFilter; +import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext; +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; + +import io.quarkus.test.QuarkusUnitTest; +import io.smallrye.mutiny.Uni; + +public class FilterWithPathParamsTest { + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(HelloResource.class, Filters.class); + } + }); + + @Test + public void testNonExistingPath() { + when().get("/dummy") + .then() + .statusCode(404) + .header("path-params", is("0")); + } + + @Test + public void testNoPathParamsPathNoAbort() { + when().get("/hello") + .then() + .statusCode(200) + .header("path-params", is("0")); + } + + @Test + public void testNoPathParamsPathWithAbort() { + given().header("abort", "true") + .when().get("/hello") + .then() + .statusCode(401) + .header("path-params", is("0")); + } + + @Test + public void testPathParamsPathNoAbort() { + when().get("/hello/resteasy") + .then() + .statusCode(200) + .header("path-params", is("1")); + } + + @Test + public void testPathParamsPathWithAbort() { + given().header("abort", "true") + .when().get("/hello/resteasy") + .then() + .statusCode(401) + .header("path-params", is("1")); + } + + @Path("hello") + public static class HelloResource { + + @GET + public String hello() { + return "hello"; + } + + @Path("{name}") + @GET + public String helloName(String name) { + return name; + } + } + + public static class Filters { + + @ServerRequestFilter + public Uni filter(ResteasyReactiveContainerRequestContext requestContext) { + if ("true".equals(requestContext.getHeaders().getFirst("abort"))) { + requestContext.getUriInfo().getPathParameters(); + return Uni.createFrom().item(Response.status(401).build()); + } + return null; + } + + @ServerResponseFilter + public void responseFilter(ResteasyReactiveContainerRequestContext requestContext, + ContainerResponseContext responseContext) { + responseContext.getHeaders().add("path-params", requestContext.getUriInfo().getPathParameters().size()); + } + } +} diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/ContainerRequestContextImpl.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/ContainerRequestContextImpl.java index 5a84c9154eb5d..cd565a16f80d8 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/ContainerRequestContextImpl.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/ContainerRequestContextImpl.java @@ -181,7 +181,7 @@ public ContainerRequestContextImpl setPreMatch(boolean preMatch) { public void abortWith(Response response) { assertNotResponse(); quarkusRestContext.setResult(response); - quarkusRestContext.restart(quarkusRestContext.getAbortHandlerChain()); + quarkusRestContext.restart(quarkusRestContext.getAbortHandlerChain(), true); aborted = true; // this is a valid action after suspend, in which case we must resume if (quarkusRestContext.isSuspended()) diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/UriInfoImpl.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/UriInfoImpl.java index 17cb32fb312a3..5daaa95dca575 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/UriInfoImpl.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/jaxrs/UriInfoImpl.java @@ -18,6 +18,7 @@ import org.jboss.resteasy.reactive.server.core.Deployment; import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; import org.jboss.resteasy.reactive.server.core.UriMatch; +import org.jboss.resteasy.reactive.server.mapping.RuntimeResource; import org.jboss.resteasy.reactive.server.spi.ServerHttpRequest; /** @@ -138,8 +139,11 @@ public MultivaluedMap getPathParameters(boolean decode) { throw encodedNotSupported(); if (pathParams == null) { pathParams = new QuarkusMultivaluedHashMap<>(); - for (Entry pathParam : currentRequest.getTarget().getPathParameterIndexes().entrySet()) { - pathParams.add(pathParam.getKey(), currentRequest.getPathParam(pathParam.getValue())); + RuntimeResource target = currentRequest.getTarget(); + if (target != null) { // a target can be null if this happens in a filter that runs before the target is set + for (Entry pathParam : target.getPathParameterIndexes().entrySet()) { + pathParams.add(pathParam.getKey(), currentRequest.getPathParam(pathParam.getValue())); + } } } return new UnmodifiableMultivaluedMap<>(pathParams);