Skip to content

Commit

Permalink
Merge pull request quarkusio#40408 from manovotn/issue40307
Browse files Browse the repository at this point in the history
Quarkus REST - reuse CDI request context if it exists
  • Loading branch information
manovotn authored May 3, 2024
2 parents 4eaa586 + 864be71 commit 8832fa8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public void deactivate() {
public ThreadState currentState() {
return toThreadState(managedContext.getState());
}

@Override
public boolean isRequestContextActive() {
return managedContext.isActive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,17 @@ public void requireCDIRequestScope() {
if (requestScopeActivated) {
return;
}
requestScopeActivated = true;
if (isRequestScopeManagementRequired()) {
if (currentRequestScope == null) {
currentRequestScope = requestContext.activateInitial();
if (requestContext.isRequestContextActive()) {
// req. context is already active, just reuse existing one
currentRequestScope = requestContext.currentState();
} else {
currentRequestScope.activate();
requestScopeActivated = true;
if (currentRequestScope == null) {
currentRequestScope = requestContext.activateInitial();
} else {
currentRequestScope.activate();
}
}
} else {
currentRequestScope = requestContext.currentState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface ThreadSetupAction {

ThreadState currentState();

boolean isRequestContextActive();

interface ThreadState {
void close();

Expand Down Expand Up @@ -39,5 +41,10 @@ public void deactivate() {
public ThreadState currentState() {
return activateInitial();
}

@Override
public boolean isRequestContextActive() {
return false;
}
};
}
5 changes: 5 additions & 0 deletions integration-tests/openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-security</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.it.openapi.security;

import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.SecurityContext;

import io.quarkus.vertx.web.RouteFilter;
import io.vertx.ext.web.RoutingContext;

@Path("/security")
public class TestSecurityResource {

@RolesAllowed("admin")
@GET
@Path("reactive-routes")
public String reactiveRoutes(@Context SecurityContext securityContext) {
return securityContext.getUserPrincipal().getName();
}

@RouteFilter(401)
public void doNothing(RoutingContext routingContext) {
// here so that the Reactive Routes extension activates CDI request context
routingContext.response().putHeader("reactive-routes-filter", "true");
routingContext.next();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.it.openapi.security;

import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;
import io.restassured.RestAssured;

@QuarkusTest
@TestHTTPEndpoint(TestSecurityResource.class)
public class TestSecurityReactiveRoutesTest {

@TestSecurity(user = "Martin", roles = "admin")
@Test
public void testSecurityWithReactiveRoutesAndQuarkusRest() {
RestAssured.get("reactive-routes")
.then()
.statusCode(200)
.header("reactive-routes-filter", is("true"))
.body(is("Martin"));
}

}

0 comments on commit 8832fa8

Please sign in to comment.