Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server.session;
 
 
 
 /* ------------------------------------------------------------ */
SessionHandler.
 
 public class SessionHandler extends ScopedHandler
 {
     final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
 
 
     /* -------------------------------------------------------------- */
     private SessionManager _sessionManager;
 
     /* ------------------------------------------------------------ */
    
Constructor. Construct a SessionHandler witha a HashSessionManager with a standard java.util.Random generator is created.
 
     public SessionHandler()
     {
         this(new HashSessionManager());
     }
 
     /* ------------------------------------------------------------ */
    

Parameters:
manager The session manager
 
     public SessionHandler(SessionManager manager)
     {
         setSessionManager(manager);
     }
 
     /* ------------------------------------------------------------ */
    

Returns:
Returns the sessionManager.
 
     public SessionManager getSessionManager()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
    

Parameters:
sessionManager The sessionManager to set.
 
     public void setSessionManager(SessionManager sessionManager)
     {
         if (isStarted())
             throw new IllegalStateException();
         SessionManager old_session_manager = ;
 
         if (getServer() != null)
             getServer().getContainer().update(this,old_session_manager,sessionManager,"sessionManager",true);
 
         if (sessionManager != null)
             sessionManager.setSessionHandler(this);
 
          = sessionManager;
 
        if (old_session_manager != null)
            old_session_manager.setSessionHandler(null);
    }
    /* ------------------------------------------------------------ */
    @Override
    public void setServer(Server server)
    {
        Server old_server = getServer();
        if (old_server != null && old_server != server)
            old_server.getContainer().update(this,,null,"sessionManager",true);
        super.setServer(server);
        if (server != null && server != old_server)
            server.getContainer().update(this,null,,"sessionManager",true);
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.thread.AbstractLifeCycle#doStart()
     */
    @Override
    protected void doStart() throws Exception
    {
        .start();
        super.doStart();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.thread.AbstractLifeCycle#doStop()
     */
    @Override
    protected void doStop() throws Exception
    {
        // Destroy sessions before destroying servlets/filters see JETTY-1266
        .stop();
        super.doStop();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
     */
    @Override
    public void doScope(String targetRequest baseRequestHttpServletRequest requestHttpServletResponse responsethrows IOExceptionServletException
    {
        SessionManager old_session_manager = null;
        HttpSession old_session = null;
        HttpSession access = null;
        try
        {
            old_session_manager = baseRequest.getSessionManager();
            old_session = baseRequest.getSession(false);
            if (old_session_manager != )
            {
                // new session context
                baseRequest.setSessionManager();
                baseRequest.setSession(null);
                checkRequestedSessionId(baseRequest,request);
            }
            // access any existing session
            HttpSession session = null;
            if ( != null)
            {
                session = baseRequest.getSession(false);
                if (session != null)
                {
                    if (session != old_session)
                    {
                        access = session;
                        HttpCookie cookie = .access(session,request.isSecure());
                        if (cookie != null// Handle changed ID or max-age refresh
                            baseRequest.getResponse().addCookie(cookie);
                    }
                }
                else
                {
                    session = baseRequest.recoverNewSession();
                    if (session != null)
                        baseRequest.setSession(session);
                }
            }
            if (.isDebugEnabled())
            {
                .debug("sessionManager=" + );
                .debug("session=" + session);
            }
            // start manual inline of nextScope(target,baseRequest,request,response);
            if ( != null)
                .doScope(target,baseRequest,request,response);
            else if ( != null)
                .doHandle(target,baseRequest,request,response);
            else
                doHandle(target,baseRequest,request,response);
            // end manual inline (pathentic attempt to reduce stack depth)
        }
        finally
        {
            if (access != null)
                .complete(access);
            HttpSession session = baseRequest.getSession(false);
            if (session != null && old_session == null && session != access)
                .complete(session);
            if (old_session_manager != null && old_session_manager != )
            {
                baseRequest.setSessionManager(old_session_manager);
                baseRequest.setSession(old_session);
            }
        }
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
     */
    @Override
    public void doHandle(String targetRequest baseRequestHttpServletRequest requestHttpServletResponse responsethrows IOExceptionServletException
    {
        // start manual inline of nextHandle(target,baseRequest,request,response);
        if (never())
            nextHandle(target,baseRequest,request,response);
        else if ( != null &&  == )
            .doHandle(target,baseRequest,request,response);
        else if ( != null)
            .handle(target,baseRequest,request,response);
        // end manual inline
    }
    /* ------------------------------------------------------------ */
    
Look for a requested session ID in cookies and URI parameters

Parameters:
baseRequest
request
    protected void checkRequestedSessionId(Request baseRequestHttpServletRequest request)
    {
        String requested_session_id = request.getRequestedSessionId();
        SessionManager sessionManager = getSessionManager();
        if (requested_session_id != null && sessionManager != null)
        {
            HttpSession session = sessionManager.getHttpSession(requested_session_id);
            if (session != null && sessionManager.isValid(session))
                baseRequest.setSession(session);
            return;
        }
        else if (!..equals(baseRequest.getDispatcherType()))
            return;
        boolean requested_session_id_from_cookie = false;
        HttpSession session = null;
        // Look for session id cookie
        if (.isUsingCookies())
        {
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0)
            {
                final String sessionCookie=sessionManager.getSessionCookieConfig().getName();
                for (int i = 0; i < cookies.lengthi++)
                {
                    if (sessionCookie.equalsIgnoreCase(cookies[i].getName()))
                    {
                        requested_session_id = cookies[i].getValue();
                        requested_session_id_from_cookie = true;
                        .debug("Got Session ID {} from cookie",requested_session_id);
                        if (requested_session_id != null)
                        {
                            session = sessionManager.getHttpSession(requested_session_id);
                            if (session != null && sessionManager.isValid(session))
                            {
                                break;
                            }
                        }
                        else
                        {
                            .warn("null session id from cookie");
                        }
                    }
                }
            }
        }
        if (requested_session_id == null || session == null)
        {
            String uri = request.getRequestURI();
            String prefix = sessionManager.getSessionIdPathParameterNamePrefix();
            if (prefix != null)
            {
                int s = uri.indexOf(prefix);
                if (s >= 0)
                {
                    s += prefix.length();
                    int i = s;
                    while (i < uri.length())
                    {
                        char c = uri.charAt(i);
                        if (c == ';' || c == '#' || c == '?' || c == '/')
                            break;
                        i++;
                    }
                    requested_session_id = uri.substring(s,i);
                    requested_session_id_from_cookie = false;
                    session = sessionManager.getHttpSession(requested_session_id);
                    if (.isDebugEnabled())
                        .debug("Got Session ID {} from URL",requested_session_id);
                }
            }
        }
        baseRequest.setRequestedSessionId(requested_session_id);
        baseRequest.setRequestedSessionIdFromCookie(requested_session_id != null && requested_session_id_from_cookie);
        if (session != null && sessionManager.isValid(session))
            baseRequest.setSession(session);
    }
    /* ------------------------------------------------------------ */
    

Parameters:
listener
    public void addEventListener(EventListener listener)
    {
        if ( != null)
            .addEventListener(listener);
    }
    /* ------------------------------------------------------------ */
    public void clearEventListeners()
    {
        if ( != null)
            .clearEventListeners();
    }
New to GrepCode? Check out our FAQ X