diff --git a/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoService.java b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoService.java new file mode 100644 index 00000000000000..4db7b3e9ad27ec --- /dev/null +++ b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoService.java @@ -0,0 +1,11 @@ +package io.quarkus.undertow.websockets.test; + +import javax.enterprise.context.RequestScoped; + +@RequestScoped +public class EchoService { + + public String echo(String msg) { + return msg; + } +} diff --git a/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoWebSocket.java b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoWebSocket.java index 374dc45f7807ac..8af19f76134932 100644 --- a/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoWebSocket.java +++ b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/EchoWebSocket.java @@ -1,14 +1,18 @@ package io.quarkus.undertow.websockets.test; +import javax.inject.Inject; import javax.websocket.OnMessage; import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/echo") public class EchoWebSocket { + @Inject + EchoService echoService; + @OnMessage String echo(String msg) { - return msg; + return echoService.echo(msg); } } diff --git a/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/WebsocketDevModeTestCase.java b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/WebsocketDevModeTestCase.java index af2e5f531cfc7d..82451f3262efa8 100644 --- a/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/WebsocketDevModeTestCase.java +++ b/extensions/undertow-websockets/deployment/src/test/java/io/quarkus/undertow/websockets/test/WebsocketDevModeTestCase.java @@ -35,7 +35,7 @@ public class WebsocketDevModeTestCase { @Override public JavaArchive get() { return ShrinkWrap.create(JavaArchive.class) - .addClass(EchoWebSocket.class); + .addClasses(EchoWebSocket.class, EchoService.class); } }); @@ -62,7 +62,7 @@ public void onMessage(String s) { session.close(); } - test.modifySourceFile(EchoWebSocket.class, (s) -> s.replace("return msg;", "return \"changed:\" + msg;")); + test.modifySourceFile(EchoService.class, (s) -> s.replace("return msg;", "return \"changed:\" + msg;")); session = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() { @Override diff --git a/extensions/undertow/deployment/src/main/java/io/quarkus/undertow/deployment/UndertowArcIntegrationBuildStep.java b/extensions/undertow/deployment/src/main/java/io/quarkus/undertow/deployment/UndertowArcIntegrationBuildStep.java index 2fea9581ba3f73..d33c5486851ffc 100644 --- a/extensions/undertow/deployment/src/main/java/io/quarkus/undertow/deployment/UndertowArcIntegrationBuildStep.java +++ b/extensions/undertow/deployment/src/main/java/io/quarkus/undertow/deployment/UndertowArcIntegrationBuildStep.java @@ -1,22 +1,11 @@ package io.quarkus.undertow.deployment; -import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; -import io.quarkus.deployment.annotations.ExecutionTime; -import io.quarkus.deployment.annotations.Record; -import io.quarkus.undertow.runtime.UndertowDeploymentRecorder; public class UndertowArcIntegrationBuildStep { - @BuildStep - @Record(ExecutionTime.STATIC_INIT) - ServletExtensionBuildItem integrateRequestContext(BeanContainerBuildItem beanContainerBuildItem, - UndertowDeploymentRecorder recorder) { - return new ServletExtensionBuildItem(recorder.setupRequestScope(beanContainerBuildItem.getValue())); - } - @BuildStep void beanDefiningAnnotations(BuildProducer annotations) { annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_FILTER)); diff --git a/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java index 6d44272453ac3f..245ba1b0973feb 100644 --- a/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java +++ b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java @@ -429,6 +429,7 @@ public DeploymentManager bootServletContainer(RuntimeValue info, .addInitParam(QuarkusErrorServlet.SHOW_STACK, Boolean.toString(launchMode.isDevOrTest()))); } } + setupRequestScope(info.getValue(), beanContainer); try { ClassIntrospecter defaultVal = info.getValue().getClassIntrospecter(); @@ -519,91 +520,90 @@ public void addServletExtension(RuntimeValue deployment, Servlet deployment.getValue().addServletExtension(extension); } - public ServletExtension setupRequestScope(BeanContainer beanContainer) { - return new ServletExtension() { + public void setupRequestScope(DeploymentInfo deploymentInfo, BeanContainer beanContainer) { + CurrentVertxRequest currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get(); + Instance identityAssociations = CDI.current() + .select(CurrentIdentityAssociation.class); + CurrentIdentityAssociation association; + if (identityAssociations.isResolvable()) { + association = identityAssociations.get(); + } else { + association = null; + } + deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() { @Override - public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) { - CurrentVertxRequest currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get(); - Instance identityAssociations = CDI.current() - .select(CurrentIdentityAssociation.class); - CurrentIdentityAssociation association; - if (identityAssociations.isResolvable()) { - association = identityAssociations.get(); - } else { - association = null; - } - deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() { + public ThreadSetupHandler.Action create(Action action) { + return new Action() { @Override - public ThreadSetupHandler.Action create(Action action) { - return new Action() { - @Override - public T call(HttpServerExchange exchange, C context) throws Exception { - // Not sure what to do here - if (exchange == null) { - return action.call(exchange, context); + public T call(HttpServerExchange exchange, C context) throws Exception { + // Not sure what to do here + ManagedContext requestContext = beanContainer.requestContext(); + if (requestContext.isActive()) { + return action.call(exchange, context); + } else if (exchange == null) { + requestContext.activate(); + try { + return action.call(exchange, context); + } finally { + requestContext.destroy(); + } + } else { + InjectableContext.ContextState existingRequestContext = exchange + .getAttachment(REQUEST_CONTEXT); + try { + requestContext.activate(existingRequestContext); + + VertxHttpExchange delegate = (VertxHttpExchange) exchange.getDelegate(); + RoutingContext rc = (RoutingContext) delegate.getContext(); + currentVertxRequest.setCurrent(rc); + + if (association != null) { + association + .setIdentity(QuarkusHttpUser.getSecurityIdentity(rc, null)); } - ManagedContext requestContext = beanContainer.requestContext(); - if (requestContext.isActive()) { - return action.call(exchange, context); - } else { - InjectableContext.ContextState existingRequestContext = exchange - .getAttachment(REQUEST_CONTEXT); - try { - requestContext.activate(existingRequestContext); - - VertxHttpExchange delegate = (VertxHttpExchange) exchange.getDelegate(); - RoutingContext rc = (RoutingContext) delegate.getContext(); - currentVertxRequest.setCurrent(rc); - - if (association != null) { - association - .setIdentity(QuarkusHttpUser.getSecurityIdentity(rc, null)); - } - - return action.call(exchange, context); - } finally { - ServletRequestContext src = exchange - .getAttachment(ServletRequestContext.ATTACHMENT_KEY); - HttpServletRequestImpl req = src.getOriginalRequest(); - if (req.isAsyncStarted()) { - exchange.putAttachment(REQUEST_CONTEXT, requestContext.getState()); - requestContext.deactivate(); - if (existingRequestContext == null) { - req.getAsyncContextInternal().addListener(new AsyncListener() { - @Override - public void onComplete(AsyncEvent event) throws IOException { - requestContext.activate(exchange - .getAttachment(REQUEST_CONTEXT)); - requestContext.terminate(); - } - - @Override - public void onTimeout(AsyncEvent event) throws IOException { - onComplete(event); - } - - @Override - public void onError(AsyncEvent event) throws IOException { - onComplete(event); - } - - @Override - public void onStartAsync(AsyncEvent event) throws IOException { - - } - }); + + return action.call(exchange, context); + } finally { + ServletRequestContext src = exchange + .getAttachment(ServletRequestContext.ATTACHMENT_KEY); + HttpServletRequestImpl req = src.getOriginalRequest(); + if (req.isAsyncStarted()) { + exchange.putAttachment(REQUEST_CONTEXT, requestContext.getState()); + requestContext.deactivate(); + if (existingRequestContext == null) { + req.getAsyncContextInternal().addListener(new AsyncListener() { + @Override + public void onComplete(AsyncEvent event) throws IOException { + requestContext.activate(exchange + .getAttachment(REQUEST_CONTEXT)); + requestContext.terminate(); + } + + @Override + public void onTimeout(AsyncEvent event) throws IOException { + onComplete(event); } - } else { - requestContext.terminate(); - } + + @Override + public void onError(AsyncEvent event) throws IOException { + onComplete(event); + } + + @Override + public void onStartAsync(AsyncEvent event) throws IOException { + + } + }); } + } else { + requestContext.terminate(); } } - }; + } } - }); + }; } - }; + }); } public void addServletContainerInitializer(RuntimeValue deployment,