Java Code Examples for io.undertow.servlet.handlers.ServletRequestContext#getSession()

The following examples show how to use io.undertow.servlet.handlers.ServletRequestContext#getSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpSessionImpl.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
public static HttpSessionImpl forSession(final Session session, final ServletContext servletContext, final boolean newSession) {
    // forSession is called by privileged actions only so no need to do it again
    ServletRequestContext current = ServletRequestContext.current();
    if (current == null) {
        return new HttpSessionImpl(session, servletContext, newSession, null);
    } else {
        HttpSessionImpl httpSession = current.getSession();
        if (httpSession == null) {
            httpSession = new HttpSessionImpl(session, servletContext, newSession, current);
            current.setSession(httpSession);
        } else {
            if(httpSession.session != session) {
                //in some rare cases it may be that there are two different service contexts involved in the one request
                //in this case we just return a new session rather than using the thread local version
                httpSession = new HttpSessionImpl(session, servletContext, newSession, current);
            }
        }
        return httpSession;
    }
}
 
Example 2
Source File: HttpSessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static HttpSessionImpl forSession(final Session session, final ServletContext servletContext, final boolean newSession) {
    // forSession is called by privileged actions only so no need to do it again
    ServletRequestContext current = ServletRequestContext.current();
    if (current == null) {
        return new HttpSessionImpl(session, servletContext, newSession, null);
    } else {
        HttpSessionImpl httpSession = current.getSession();
        if (httpSession == null) {
            httpSession = new HttpSessionImpl(session, servletContext, newSession, current);
            current.setSession(httpSession);
        } else {
            if(httpSession.session != session) {
                //in some rare cases it may be that there are two different service contexts involved in the one request
                //in this case we just return a new session rather than using the thread local version
                httpSession = new HttpSessionImpl(session, servletContext, newSession, current);
            }
        }
        return httpSession;
    }
}
 
Example 3
Source File: RequestDispatcherImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
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);
            // update time in old context and run the requestDone for the session
            servletRequestContext.getCurrentServletContext().updateSessionAccessTime(servletRequestContext.getExchange());
        }
    } else {
        forwardImpl(request, response, servletRequestContext);
    }

}
 
Example 4
Source File: RequestDispatcherImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private void setupIncludeImpl(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;
    }
    final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    ServletContextImpl oldServletContext = null;
    HttpSessionImpl oldSession = null;
    if (servletRequestContext.getCurrentServletContext() != this.servletContext) {
        //cross context request, we need to run the thread setup actions
        oldServletContext = servletRequestContext.getCurrentServletContext();
        oldSession = servletRequestContext.getSession();
        servletRequestContext.setSession(null);
        servletRequestContext.setCurrentServletContext(this.servletContext);
        try {
            servletRequestContext.getCurrentServletContext().invokeAction(servletRequestContext.getExchange(), new ThreadSetupHandler.Action<Void, Object>() {
                @Override
                public Void call(HttpServerExchange exchange, Object context) throws Exception {
                    includeImpl(request, response, servletRequestContext, requestImpl, responseImpl);
                    return null;
                }
            });
        } finally {
            // update time in new context and run the requestDone for the session
            servletRequestContext.getCurrentServletContext().updateSessionAccessTime(servletRequestContext.getExchange());
            servletRequestContext.setSession(oldSession);
            servletRequestContext.setCurrentServletContext(oldServletContext);
        }
    } else {
        includeImpl(request, response, servletRequestContext, requestImpl, responseImpl);
    }
}
 
Example 5
Source File: SessionListenerBridge.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: RequestDispatcherImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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);
    }

}
 
Example 7
Source File: RequestDispatcherImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void setupIncludeImpl(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;
    }
    final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    ServletContextImpl oldServletContext = null;
    HttpSessionImpl oldSession = null;
    if (servletRequestContext.getCurrentServletContext() != this.servletContext) {
        //cross context request, we need to run the thread setup actions
        oldServletContext = servletRequestContext.getCurrentServletContext();
        oldSession = servletRequestContext.getSession();
        servletRequestContext.setSession(null);
        servletRequestContext.setCurrentServletContext(this.servletContext);
        try {
            servletRequestContext.getCurrentServletContext().invokeAction(servletRequestContext.getExchange(), new ThreadSetupHandler.Action<Void, Object>() {
                @Override
                public Void call(HttpServerExchange exchange, Object context) throws Exception {
                    includeImpl(request, response, servletRequestContext, requestImpl, responseImpl);
                    return null;
                }
            });
        } finally {
            servletRequestContext.setSession(oldSession);
            servletRequestContext.setCurrentServletContext(oldServletContext);
        }
    } else {
        includeImpl(request, response, servletRequestContext, requestImpl, responseImpl);
    }
}
 
Example 8
Source File: SessionListenerBridge.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}