Skip to content

Commit

Permalink
Support CP for undertow servlet request quarkusio#2981
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage authored and dmlloyd committed Jul 11, 2019
1 parent b98247e commit d114f51
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class ContextEndpoint {
ManagedExecutor all;
@Inject
ThreadContext allTc;
@Inject
HttpServletRequest servletRequest;

@GET
@Path("/resteasy")
Expand All @@ -54,6 +57,16 @@ public CompletionStage<String> resteasyTest(@Context UriInfo uriInfo) {
});
}

@GET
@Path("/servlet")
public CompletionStage<String> servletTest(@Context UriInfo uriInfo) {
CompletableFuture<String> ret = all.completedFuture("OK");
return ret.thenApplyAsync(text -> {
servletRequest.getContentType();
return text;
});
}

@GET
@Path("/thread-context")
public CompletionStage<String> threadContextTest(@Context UriInfo uriInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions extensions/undertow/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.context-propagation</groupId>
<artifactId>microprofile-context-propagation-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> props) {
return () -> {
ServletRequestContext current = restore(null);
return () -> restore(current);
};
}

@Override
public String getThreadContextType() {
return "Servlet";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.undertow.runtime.ServletThreadContextProvider

0 comments on commit d114f51

Please sign in to comment.