Search in sources :

Example 1 with CurrentVertxRequest

use of io.quarkus.vertx.http.runtime.CurrentVertxRequest in project quarkus by quarkusio.

the class UndertowDeploymentRecorder method setupRequestScope.

public void setupRequestScope(DeploymentInfo deploymentInfo, BeanContainer beanContainer) {
    CurrentVertxRequest currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get();
    Instance<CurrentIdentityAssociation> identityAssociations = CDI.current().select(CurrentIdentityAssociation.class);
    CurrentIdentityAssociation association;
    if (identityAssociations.isResolvable()) {
        association = identityAssociations.get();
    } else {
        association = null;
    }
    deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {

        @Override
        public <T, C> ThreadSetupHandler.Action<T, C> create(Action<T, C> action) {
            return new Action<T, C>() {

                @Override
                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.terminate();
                        }
                    } 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) {
                                QuarkusHttpUser existing = (QuarkusHttpUser) rc.user();
                                if (existing != null) {
                                    SecurityIdentity identity = existing.getSecurityIdentity();
                                    association.setIdentity(identity);
                                } else {
                                    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 {
                                        }
                                    });
                                }
                            } else {
                                requestContext.terminate();
                            }
                        }
                    }
                }
            };
        }
    });
}
Also used : InjectableContext(io.quarkus.arc.InjectableContext) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) IOException(java.io.IOException) CurrentVertxRequest(io.quarkus.vertx.http.runtime.CurrentVertxRequest) AsyncEvent(javax.servlet.AsyncEvent) AuthenticationFailedException(io.quarkus.security.AuthenticationFailedException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ForbiddenException(io.quarkus.security.ForbiddenException) UnauthorizedException(io.quarkus.security.UnauthorizedException) HttpServerExchange(io.undertow.server.HttpServerExchange) SecurityIdentity(io.quarkus.security.identity.SecurityIdentity) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) VertxHttpExchange(io.undertow.vertx.VertxHttpExchange) RoutingContext(io.vertx.ext.web.RoutingContext) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) CurrentIdentityAssociation(io.quarkus.security.identity.CurrentIdentityAssociation) AsyncListener(javax.servlet.AsyncListener) ManagedContext(io.quarkus.arc.ManagedContext) QuarkusHttpUser(io.quarkus.vertx.http.runtime.security.QuarkusHttpUser)

Example 2 with CurrentVertxRequest

use of io.quarkus.vertx.http.runtime.CurrentVertxRequest in project quarkus by quarkusio.

the class ResteasyServlet method service.

@Override
public void service(String httpMethod, HttpServletRequest request, HttpServletResponse response) throws IOException {
    CurrentVertxRequest currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get();
    ContextUtil.pushContext(currentVertxRequest.getCurrent());
    super.service(httpMethod, request, response);
}
Also used : CurrentVertxRequest(io.quarkus.vertx.http.runtime.CurrentVertxRequest)

Example 3 with CurrentVertxRequest

use of io.quarkus.vertx.http.runtime.CurrentVertxRequest in project quarkus by quarkusio.

the class ResteasyFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    if (request.getMethod().equals("GET") || request.getMethod().equals("HEAD") || isCORSPreflightRequest(request)) {
        // we only serve get requests from the default servlet and CORS preflight requests
        filterChain.doFilter(servletRequest, new ResteasyResponseWrapper(response, request));
    } else {
        CurrentVertxRequest currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get();
        ContextUtil.pushContext(currentVertxRequest.getCurrent());
        servletContainerDispatcher.service(request.getMethod(), request, response, true);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) CurrentVertxRequest(io.quarkus.vertx.http.runtime.CurrentVertxRequest)

Example 4 with CurrentVertxRequest

use of io.quarkus.vertx.http.runtime.CurrentVertxRequest in project quarkus by quarkusio.

the class AwsHttpContextProducers method getContextObjects.

private Map<Class<?>, Object> getContextObjects() {
    if (current == null) {
        return Collections.EMPTY_MAP;
    }
    CurrentVertxRequest currentVertxRequest = current.get();
    if (currentVertxRequest == null) {
        return Collections.EMPTY_MAP;
    }
    RoutingContext routingContext = currentVertxRequest.getCurrent();
    if (routingContext == null) {
        return Collections.EMPTY_MAP;
    }
    MultiMap qheaders = routingContext.request().headers();
    if (qheaders == null) {
        return Collections.EMPTY_MAP;
    }
    if (qheaders instanceof QuarkusHttpHeaders) {
        return ((QuarkusHttpHeaders) qheaders).getContextObjects();
    } else {
        return Collections.EMPTY_MAP;
    }
}
Also used : MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) CurrentVertxRequest(io.quarkus.vertx.http.runtime.CurrentVertxRequest) QuarkusHttpHeaders(io.quarkus.vertx.http.runtime.QuarkusHttpHeaders)

Aggregations

CurrentVertxRequest (io.quarkus.vertx.http.runtime.CurrentVertxRequest)4 RoutingContext (io.vertx.ext.web.RoutingContext)2 InjectableContext (io.quarkus.arc.InjectableContext)1 ManagedContext (io.quarkus.arc.ManagedContext)1 AuthenticationFailedException (io.quarkus.security.AuthenticationFailedException)1 ForbiddenException (io.quarkus.security.ForbiddenException)1 UnauthorizedException (io.quarkus.security.UnauthorizedException)1 CurrentIdentityAssociation (io.quarkus.security.identity.CurrentIdentityAssociation)1 SecurityIdentity (io.quarkus.security.identity.SecurityIdentity)1 QuarkusHttpHeaders (io.quarkus.vertx.http.runtime.QuarkusHttpHeaders)1 QuarkusHttpUser (io.quarkus.vertx.http.runtime.security.QuarkusHttpUser)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)1 HttpServletRequestImpl (io.undertow.servlet.spec.HttpServletRequestImpl)1 VertxHttpExchange (io.undertow.vertx.VertxHttpExchange)1 MultiMap (io.vertx.core.MultiMap)1 IOException (java.io.IOException)1 AsyncEvent (javax.servlet.AsyncEvent)1 AsyncListener (javax.servlet.AsyncListener)1