View Javadoc

1   // ========================================================================
2   // Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.terracotta.servlet;
16  
17  import java.io.IOException;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.Cookie;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.mortbay.jetty.HttpConnection;
26  import org.mortbay.jetty.Request;
27  import org.mortbay.jetty.RetryRequest;
28  import org.mortbay.jetty.SessionManager;
29  import org.mortbay.jetty.servlet.SessionHandler;
30  import org.mortbay.log.Log;
31  
32  /**
33   * @version $Revision: 1256 $ $Date: 2008-10-18 01:50:04 +1100 (Sat, 18 Oct 2008) $
34   */
35  public class TerracottaSessionHandler extends SessionHandler
36  {
37      public TerracottaSessionHandler()
38      {
39      }
40  
41      public TerracottaSessionHandler(SessionManager manager)
42      {
43          super(manager);
44      }
45  
46      @Override
47      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
48      {
49          setRequestedId(request, dispatch);
50  
51          Request currentRequest = (request instanceof Request) ? (Request)request: HttpConnection.getCurrentConnection().getRequest();
52  
53          SessionManager requestSessionManager = currentRequest.getSessionManager();
54          HttpSession requestSession = currentRequest.getSession(false);
55  
56          TerracottaSessionManager sessionManager = (TerracottaSessionManager)getSessionManager();
57          Log.debug("SessionManager = {}", sessionManager);
58  
59          // Is it a cross context dispatch or a direct hit to this context ?
60          if (sessionManager != requestSessionManager)
61          {
62              // Setup the request for this context
63              currentRequest.setSessionManager(sessionManager);
64              currentRequest.setSession(null);
65          }
66  
67          // Tell the session manager that the request is entering
68          if (sessionManager != null) sessionManager.enter(currentRequest);
69          try
70          {
71              HttpSession currentSession = null;
72              if (sessionManager != null)
73              {
74                  currentSession = currentRequest.getSession(false);
75                  if (currentSession != null)
76                  {
77                      if (currentSession != requestSession)
78                      {
79                          // Access the session only if we did not already
80                          Cookie cookie = sessionManager.access(currentSession, request.isSecure());
81                          if (cookie != null)
82                          {
83                              // Handle changed session id or cookie max-age refresh
84                              response.addCookie(cookie);
85                          }
86                      }
87                      else
88                      {
89                          // Handle resume of the request
90                          currentSession = currentRequest.recoverNewSession(sessionManager);
91                          if (currentSession != null) currentRequest.setSession(currentSession);
92                      }
93                  }
94              }
95              Log.debug("Session = {}", currentSession);
96  
97              getHandler().handle(target, request, response, dispatch);
98          }
99          catch (RetryRequest x)
100         {
101             // User may have invalidated the session, must get it again
102             HttpSession currentSession = currentRequest.getSession(false);
103             if (currentSession != null && currentSession.isNew())
104                 currentRequest.saveNewSession(sessionManager, currentSession);
105 
106             throw x;
107         }
108         finally
109         {
110             if (sessionManager != null)
111             {
112                 // User may have invalidated the session, must get it again
113                 HttpSession currentSession = currentRequest.getSession(false);
114                 if (currentSession != null) sessionManager.complete(currentSession);
115 
116                 sessionManager.exit(currentRequest);
117             }
118 
119             // Restore cross context dispatch
120             if (sessionManager != requestSessionManager)
121             {
122                 currentRequest.setSessionManager(requestSessionManager);
123                 currentRequest.setSession(requestSession);
124             }
125         }
126     }
127 }