Search in sources :

Example 16 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class HttpServletRequestImpl method startAsync.

@Override
public AsyncContext startAsync() throws IllegalStateException {
    if (!isAsyncSupported()) {
        throw UndertowServletMessages.MESSAGES.startAsyncNotAllowed();
    } else if (asyncStarted) {
        throw UndertowServletMessages.MESSAGES.asyncAlreadyStarted();
    }
    asyncStarted = true;
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    return asyncContext = new AsyncContextImpl(exchange, servletRequestContext.getServletRequest(), servletRequestContext.getServletResponse(), servletRequestContext, false, asyncContext);
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 17 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class MetricsChainHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletInfo servletInfo = context.getCurrentServlet().getManagedServlet().getServletInfo();
    MetricsHandler handler = servletHandlers.get(servletInfo.getName());
    if (handler != null) {
        handler.handleRequest(exchange);
    } else {
        next.handleRequest(exchange);
    }
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) MetricsHandler(io.undertow.server.handlers.MetricsHandler) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 18 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletRequestContextThreadSetupAction method create.

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

        @Override
        public T call(HttpServerExchange exchange, C context) throws Exception {
            if (exchange == null) {
                return action.call(null, context);
            } else {
                ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                final ServletRequestContext old = ServletRequestContext.current();
                SecurityActions.setCurrentRequestContext(servletRequestContext);
                try {
                    return action.call(exchange, context);
                } finally {
                    ServletRequestContext.setCurrentRequestContext(old);
                }
            }
        }
    };
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 19 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletFormAuthenticationMechanism method handleRedirectBack.

@Override
protected void handleRedirectBack(final HttpServerExchange exchange) {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    HttpServletResponse resp = (HttpServletResponse) servletRequestContext.getServletResponse();
    HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchange, false);
    if (httpSession != null) {
        Session session;
        if (System.getSecurityManager() == null) {
            session = httpSession.getSession();
        } else {
            session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
        }
        String path = (String) session.getAttribute(SESSION_KEY);
        if (path != null) {
            try {
                resp.sendRedirect(path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Session(io.undertow.server.session.Session)

Example 20 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletFormAuthenticationMechanism method servePage.

@Override
protected Integer servePage(final HttpServerExchange exchange, final String location) {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest req = servletRequestContext.getServletRequest();
    ServletResponse resp = servletRequestContext.getServletResponse();
    RequestDispatcher disp = req.getRequestDispatcher(location);
    //make sure the login page is never cached
    exchange.getResponseHeaders().add(Headers.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
    exchange.getResponseHeaders().add(Headers.PRAGMA, "no-cache");
    exchange.getResponseHeaders().add(Headers.EXPIRES, "0");
    final FormResponseWrapper respWrapper = exchange.getStatusCode() != OK && resp instanceof HttpServletResponse ? new FormResponseWrapper((HttpServletResponse) resp) : null;
    try {
        disp.forward(req, respWrapper != null ? respWrapper : resp);
    } catch (ServletException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return respWrapper != null ? respWrapper.getStatus() : null;
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)40 IOException (java.io.IOException)8 Session (io.undertow.server.session.Session)5 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)5 ServletException (javax.servlet.ServletException)5 SecurityContext (io.undertow.security.api.SecurityContext)4 Account (io.undertow.security.idm.Account)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 HttpString (io.undertow.util.HttpString)4 ServletRequest (javax.servlet.ServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Subject (javax.security.auth.Subject)3 GenericMessageInfo (org.jboss.security.auth.message.GenericMessageInfo)3 AuthenticatedSessionManager (io.undertow.security.api.AuthenticatedSessionManager)2 Resource (io.undertow.server.handlers.resource.Resource)2 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)2 AuthorizationManager (io.undertow.servlet.api.AuthorizationManager)2 SingleConstraintMatch (io.undertow.servlet.api.SingleConstraintMatch)2