diff --git a/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextEndpoint.java b/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextEndpoint.java index 64a4f8a1e8c19..f10a430828518 100644 --- a/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextEndpoint.java +++ b/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextEndpoint.java @@ -7,6 +7,7 @@ import java.util.concurrent.TimeUnit; import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; import javax.transaction.Status; import javax.transaction.SystemException; import javax.transaction.Transaction; @@ -43,6 +44,8 @@ public class ContextEndpoint { ManagedExecutor all; @Inject ThreadContext allTc; + @Inject + HttpServletRequest servletRequest; @GET @Path("/resteasy") @@ -54,6 +57,16 @@ public CompletionStage resteasyTest(@Context UriInfo uriInfo) { }); } + @GET + @Path("/servlet") + public CompletionStage servletTest(@Context UriInfo uriInfo) { + CompletableFuture ret = all.completedFuture("OK"); + return ret.thenApplyAsync(text -> { + servletRequest.getContentType(); + return text; + }); + } + @GET @Path("/thread-context") public CompletionStage threadContextTest(@Context UriInfo uriInfo) { diff --git a/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextUnitTest.java b/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextUnitTest.java index 5fb7ea73affcc..8430cf9be192b 100644 --- a/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextUnitTest.java +++ b/extensions/smallrye-context-propagation/deployment/src/test/java/io/quarkus/context/test/ContextUnitTest.java @@ -30,6 +30,12 @@ public void testRESTEasyContextPropagation() { .statusCode(Response.Status.OK.getStatusCode()); } + @Test() + public void testServletContextPropagation() { + RestAssured.when().get("/context/servlet").then() + .statusCode(Response.Status.OK.getStatusCode()); + } + @Test() public void testThreadContextPropagation() { RestAssured.when().get("/context/thread-context").then() diff --git a/extensions/undertow/runtime/pom.xml b/extensions/undertow/runtime/pom.xml index 040be2c74b619..6f0dab0352818 100644 --- a/extensions/undertow/runtime/pom.xml +++ b/extensions/undertow/runtime/pom.xml @@ -37,6 +37,10 @@ io.quarkus quarkus-core + + org.eclipse.microprofile.context-propagation + microprofile-context-propagation-api + diff --git a/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/ServletThreadContextProvider.java b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/ServletThreadContextProvider.java new file mode 100644 index 0000000000000..3e8e25fdf8936 --- /dev/null +++ b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/ServletThreadContextProvider.java @@ -0,0 +1,42 @@ +package io.quarkus.undertow.runtime; + +import java.util.Map; + +import org.eclipse.microprofile.context.spi.ThreadContextProvider; +import org.eclipse.microprofile.context.spi.ThreadContextSnapshot; + +import io.undertow.servlet.handlers.ServletRequestContext; + +public class ServletThreadContextProvider implements ThreadContextProvider { + + @Override + public ThreadContextSnapshot currentContext(Map props) { + ServletRequestContext captured = ServletRequestContext.current(); + return () -> { + ServletRequestContext current = restore(captured); + return () -> restore(current); + }; + } + + private ServletRequestContext restore(ServletRequestContext context) { + ServletRequestContext currentContext = ServletRequestContext.current(); + if (context == null) + ServletRequestContext.clearCurrentServletAttachments(); + else + ServletRequestContext.setCurrentRequestContext(context); + return currentContext; + } + + @Override + public ThreadContextSnapshot clearedContext(Map props) { + return () -> { + ServletRequestContext current = restore(null); + return () -> restore(current); + }; + } + + @Override + public String getThreadContextType() { + return "Servlet"; + } +} diff --git a/extensions/undertow/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider b/extensions/undertow/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider new file mode 100644 index 0000000000000..3a087898e5479 --- /dev/null +++ b/extensions/undertow/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider @@ -0,0 +1 @@ +io.quarkus.undertow.runtime.ServletThreadContextProvider