Search in sources :

Example 56 with ServletRequestContext

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

the class SessionListenerBridge method sessionDestroyed.

@Override
public void sessionDestroyed(final Session session, final HttpServerExchange exchange, final SessionDestroyedReason reason) {
    if (reason == SessionDestroyedReason.TIMEOUT) {
        try {
            // we need to perform thread setup actions
            destroyedAction.call(exchange, session);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        doDestroy(session);
    }
    ServletRequestContext current = SecurityActions.currentServletRequestContext();
    Session underlying = null;
    if (current != null && current.getSession() != null) {
        if (System.getSecurityManager() == null) {
            underlying = current.getSession().getSession();
        } else {
            underlying = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(current.getSession()));
        }
    }
    if (current != null && underlying == session) {
        current.setSession(null);
    }
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletException(jakarta.servlet.ServletException) Session(io.undertow.server.session.Session)

Example 57 with ServletRequestContext

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

the class ServletRelativePathAttribute method readAttribute.

@Override
public String readAttribute(final HttpServerExchange exchange) {
    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (src == null) {
        return RequestURLAttribute.INSTANCE.readAttribute(exchange);
    }
    String path = (String) src.getServletRequest().getAttribute(RequestDispatcher.FORWARD_PATH_INFO);
    String sp = (String) src.getServletRequest().getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH);
    if (path == null && sp == null) {
        return RequestURLAttribute.INSTANCE.readAttribute(exchange);
    }
    if (sp == null) {
        return path;
    } else if (path == null) {
        return sp;
    } else {
        return sp + path;
    }
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 58 with ServletRequestContext

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

the class ServletRequestAttribute method writeAttribute.

@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (context != null) {
        context.getServletRequest().setAttribute(attributeName, newValue);
    } else {
        Map<String, String> attrs = exchange.getAttachment(HttpServerExchange.REQUEST_ATTRIBUTES);
        if (attrs == null) {
            exchange.putAttachment(HttpServerExchange.REQUEST_ATTRIBUTES, attrs = new HashMap<>());
        }
        attrs.put(attributeName, newValue);
    }
}
Also used : HashMap(java.util.HashMap) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 59 with ServletRequestContext

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

the class ChangeSessionId method changeSessionId.

public static String changeSessionId(HttpServerExchange exchange, boolean create) {
    final ServletRequestContext sc = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletContextImpl currentServletContext = sc.getCurrentServletContext();
    HttpSessionImpl session = currentServletContext.getSession(exchange, create);
    if (session == null) {
        return null;
    }
    Session underlyingSession;
    if (System.getSecurityManager() == null) {
        underlyingSession = session.getSession();
    } else {
        underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
    }
    return underlyingSession.changeSessionId(exchange, currentServletContext.getSessionConfig());
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) Session(io.undertow.server.session.Session)

Example 60 with ServletRequestContext

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

the class SavedRequest method trySaveRequest.

public static void trySaveRequest(final HttpServerExchange exchange) {
    io.undertow.servlet.util.SavedRequest.trySaveRequest(exchange);
    final ServletRequestContext sc = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    HttpSessionImpl session = sc.getCurrentServletContext().getSession(exchange, true);
    Session underlyingSession;
    if (System.getSecurityManager() == null) {
        underlyingSession = session.getSession();
    } else {
        underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
    }
    io.undertow.servlet.util.SavedRequest request = (io.undertow.servlet.util.SavedRequest) underlyingSession.removeAttribute(io.undertow.servlet.util.SavedRequest.class.getName());
    if (request != null)
        underlyingSession.setAttribute(SESSION_KEY, request);
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpSession(javax.servlet.http.HttpSession) Session(io.undertow.server.session.Session)

Aggregations

ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)71 IOException (java.io.IOException)13 HttpServerExchange (io.undertow.server.HttpServerExchange)12 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)11 Session (io.undertow.server.session.Session)10 HttpServletRequestImpl (io.undertow.servlet.spec.HttpServletRequestImpl)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 ServletException (jakarta.servlet.ServletException)6 ServletRequest (javax.servlet.ServletRequest)6 Account (io.undertow.security.idm.Account)5 HttpServletResponseImpl (io.undertow.servlet.spec.HttpServletResponseImpl)5 HttpString (io.undertow.util.HttpString)5 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)5 SecurityContext (io.undertow.security.api.SecurityContext)4 HttpHandler (io.undertow.server.HttpHandler)3 SessionManager (io.undertow.server.session.SessionManager)3 ServletInfo (io.undertow.servlet.api.ServletInfo)3 ArrayList (java.util.ArrayList)3 HttpSession (javax.servlet.http.HttpSession)3 GenericMessageInfo (org.jboss.security.auth.message.GenericMessageInfo)3