Search in sources :

Example 31 with ServletRequestContext

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

the class ServletSingleSignOnAuthenticationMechanism method getSession.

@Override
protected Session getSession(HttpServerExchange exchange) {
    ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    final HttpSessionImpl session = servletRequestContext.getCurrentServletContext().getSession(exchange, true);
    if (System.getSecurityManager() == null) {
        return session.getSession();
    } else {
        return AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
    }
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 32 with ServletRequestContext

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

the class RequestDispatcherImpl method forwardImplSetup.

private void forwardImplSetup(final ServletRequest request, final ServletResponse response) throws ServletException, IOException {
    final ServletRequestContext servletRequestContext = SecurityActions.currentServletRequestContext();
    if (servletRequestContext == null) {
        UndertowLogger.REQUEST_LOGGER.debugf("No servlet request context for %s, dispatching mock request", request);
        mock(request, response);
        return;
    }
    ThreadSetupAction.Handle handle = null;
    ServletContextImpl oldServletContext = null;
    HttpSessionImpl oldSession = null;
    if (servletRequestContext.getCurrentServletContext() != this.servletContext) {
        try {
            //cross context request, we need to run the thread setup actions
            oldServletContext = servletRequestContext.getCurrentServletContext();
            oldSession = servletRequestContext.getSession();
            servletRequestContext.setSession(null);
            servletRequestContext.setCurrentServletContext(this.servletContext);
            this.servletContext.invokeAction(servletRequestContext.getExchange(), new ThreadSetupHandler.Action<Void, Object>() {

                @Override
                public Void call(HttpServerExchange exchange, Object context) throws Exception {
                    forwardImpl(request, response, servletRequestContext);
                    return null;
                }
            });
        } finally {
            servletRequestContext.setSession(oldSession);
            servletRequestContext.setCurrentServletContext(oldServletContext);
        }
    } else {
        forwardImpl(request, response, servletRequestContext);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupAction(io.undertow.servlet.api.ThreadSetupAction) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException)

Example 33 with ServletRequestContext

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

the class HttpServletRequestImpl method startAsync.

@Override
public AsyncContext startAsync(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IllegalStateException {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
        if (servletRequestContext.getOriginalRequest() != servletRequest) {
            if (!(servletRequest instanceof ServletRequestWrapper)) {
                throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(servletRequest);
            }
        }
        if (servletRequestContext.getOriginalResponse() != servletResponse) {
            if (!(servletResponse instanceof ServletResponseWrapper)) {
                throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(servletResponse);
            }
        }
    }
    if (!isAsyncSupported()) {
        throw UndertowServletMessages.MESSAGES.startAsyncNotAllowed();
    } else if (asyncStarted) {
        throw UndertowServletMessages.MESSAGES.asyncAlreadyStarted();
    }
    asyncStarted = true;
    return asyncContext = new AsyncContextImpl(exchange, servletRequest, servletResponse, servletRequestContext, true, asyncContext);
}
Also used : ServletRequestWrapper(javax.servlet.ServletRequestWrapper) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletResponseWrapper(javax.servlet.ServletResponseWrapper)

Example 34 with ServletRequestContext

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

the class HttpServletRequestImpl method getMapping.

@Override
public Mapping getMapping() {
    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletPathMatch match = src.getOriginalServletPathMatch();
    String matchValue;
    switch(match.getMappingMatch()) {
        case EXACT:
            matchValue = getServletPath();
            break;
        case DEFAULT:
            matchValue = "/";
            break;
        case CONTEXT_ROOT:
            matchValue = "";
            break;
        case PATH:
            matchValue = match.getRemaining();
            break;
        case EXTENSION:
            matchValue = match.getMatched().substring(0, match.getMatched().length() - match.getMatchString().length() + 1);
            break;
        default:
            matchValue = match.getRemaining();
    }
    return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch());
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) HttpString(io.undertow.util.HttpString)

Example 35 with ServletRequestContext

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

the class HttpServletResponseImpl method doErrorDispatch.

public void doErrorDispatch(int sc, String error) throws IOException {
    writer = null;
    responseState = ResponseState.NONE;
    resetBuffer();
    treatAsCommitted = false;
    final String location = servletContext.getDeployment().getErrorPages().getErrorLocation(sc);
    if (location != null) {
        RequestDispatcherImpl requestDispatcher = new RequestDispatcherImpl(location, servletContext);
        final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
        try {
            requestDispatcher.error(servletRequestContext, servletRequestContext.getServletRequest(), servletRequestContext.getServletResponse(), exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getCurrentServlet().getManagedServlet().getServletInfo().getName(), error);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    } else if (error != null) {
        setContentType("text/html");
        setCharacterEncoding("UTF-8");
        if (servletContext.getDeployment().getDeploymentInfo().isEscapeErrorMessage()) {
            getWriter().write("<html><head><title>Error</title></head><body>" + escapeHtml(error) + "</body></html>");
        } else {
            getWriter().write("<html><head><title>Error</title></head><body>" + error + "</body></html>");
        }
        getWriter().close();
    }
    responseDone();
}
Also used : ServletException(javax.servlet.ServletException) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpString(io.undertow.util.HttpString)

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