Skip to content

Commit

Permalink
Merge pull request #18995 from geoand/#18990
Browse files Browse the repository at this point in the history
Fix target handling in RESTEasy Reactive when used in filters
  • Loading branch information
geoand authored Jul 27, 2021
2 parents 35bf51b + d08be78 commit 768ac03
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<Response> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -138,8 +139,11 @@ public MultivaluedMap<String, String> getPathParameters(boolean decode) {
throw encodedNotSupported();
if (pathParams == null) {
pathParams = new QuarkusMultivaluedHashMap<>();
for (Entry<String, Integer> 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<String, Integer> pathParam : target.getPathParameterIndexes().entrySet()) {
pathParams.add(pathParam.getKey(), currentRequest.getPathParam(pathParam.getValue()));
}
}
}
return new UnmodifiableMultivaluedMap<>(pathParams);
Expand Down

0 comments on commit 768ac03

Please sign in to comment.