Skip to content

Commit

Permalink
Add the ability to inject ContainerRequestContext via CDI
Browse files Browse the repository at this point in the history
Closes: #28889
(cherry picked from commit 6fffa79)
  • Loading branch information
geoand authored and gsmet committed Nov 1, 2022
1 parent edb6e6d commit a373b8e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.resteasy.reactive.server.test.resource.basic;

import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;

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.restassured.RestAssured;

public class ContainerRequestContextTest {

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

@Test
public void helloWorldTest() {
RestAssured.get("/hello")
.then()
.body(equalTo("hello foo"));
}

@Path("/hello")
public static class HelloResource {

private final ContainerRequestContext containerRequestContext;

public HelloResource(ContainerRequestContext containerRequestContext) {
this.containerRequestContext = containerRequestContext;
}

@GET
public String hello() {
return "hello " + containerRequestContext.getProperty("name");
}

}

@Provider
public static class TestFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext context) throws IOException {
context.setProperty("name", "foo");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Application;
Expand Down Expand Up @@ -51,6 +52,13 @@ HttpHeaders headers() {
return getContext().getHttpHeaders();
}

// this is added for compatibility reasons
@RequestScoped
@Produces
ContainerRequestContext containerRequestContext() {
return getContext().getContainerRequestContext();
}

@ApplicationScoped
@Produces
Sse sse() {
Expand Down

0 comments on commit a373b8e

Please sign in to comment.