Source Code Cross Referenced for SessionHandler.java in  » Sevlet-Container » jetty-modules » org » mortbay » jetty » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » jetty modules » org.mortbay.jetty.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //========================================================================
002:        //$Id: SessionHandler.java,v 1.5 2005/11/11 22:55:39 gregwilkins Exp $
003:        //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004:        //------------------------------------------------------------------------
005:        //Licensed under the Apache License, Version 2.0 (the "License");
006:        //you may not use this file except in compliance with the License.
007:        //You may obtain a copy of the License at 
008:        //http://www.apache.org/licenses/LICENSE-2.0
009:        //Unless required by applicable law or agreed to in writing, software
010:        //distributed under the License is distributed on an "AS IS" BASIS,
011:        //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:        //See the License for the specific language governing permissions and
013:        //limitations under the License.
014:        //========================================================================
015:
016:        package org.mortbay.jetty.servlet;
017:
018:        import java.io.IOException;
019:        import java.util.EventListener;
020:
021:        import javax.servlet.ServletException;
022:        import javax.servlet.http.Cookie;
023:        import javax.servlet.http.HttpServletRequest;
024:        import javax.servlet.http.HttpServletResponse;
025:        import javax.servlet.http.HttpSession;
026:
027:        import org.mortbay.jetty.HttpConnection;
028:        import org.mortbay.jetty.Request;
029:        import org.mortbay.jetty.RetryRequest;
030:        import org.mortbay.jetty.Server;
031:        import org.mortbay.jetty.SessionManager;
032:        import org.mortbay.jetty.handler.HandlerWrapper;
033:        import org.mortbay.log.Log;
034:
035:        /* ------------------------------------------------------------ */
036:        /** SessionHandler.
037:         * 
038:         * @author gregw
039:         *
040:         */
041:        public class SessionHandler extends HandlerWrapper {
042:            /* -------------------------------------------------------------- */
043:            private SessionManager _sessionManager;
044:
045:            /* ------------------------------------------------------------ */
046:            /** Constructor.
047:             * Construct a SessionHandler witha a HashSessionManager with a standard
048:             * java.util.Random generator is created.
049:             */
050:            public SessionHandler() {
051:                this (new HashSessionManager());
052:            }
053:
054:            /* ------------------------------------------------------------ */
055:            /**
056:             * @param manager The session manager
057:             */
058:            public SessionHandler(SessionManager manager) {
059:                setSessionManager(manager);
060:            }
061:
062:            /* ------------------------------------------------------------ */
063:            /**
064:             * @return Returns the sessionManager.
065:             */
066:            public SessionManager getSessionManager() {
067:                return _sessionManager;
068:            }
069:
070:            /* ------------------------------------------------------------ */
071:            /**
072:             * @param sessionManager The sessionManager to set.
073:             */
074:            public void setSessionManager(SessionManager sessionManager) {
075:                if (isStarted())
076:                    throw new IllegalStateException();
077:                SessionManager old_session_manager = _sessionManager;
078:
079:                if (getServer() != null)
080:                    getServer().getContainer().update(this ,
081:                            old_session_manager, sessionManager,
082:                            "sessionManager", true);
083:
084:                if (sessionManager != null)
085:                    sessionManager.setSessionHandler(this );
086:
087:                _sessionManager = sessionManager;
088:
089:                if (old_session_manager != null)
090:                    old_session_manager.setSessionHandler(null);
091:            }
092:
093:            /* ------------------------------------------------------------ */
094:            public void setServer(Server server) {
095:                Server old_server = getServer();
096:                if (old_server != null && old_server != server)
097:                    old_server.getContainer().update(this , _sessionManager,
098:                            null, "sessionManager", true);
099:                super .setServer(server);
100:                if (server != null && server != old_server)
101:                    server.getContainer().update(this , null, _sessionManager,
102:                            "sessionManager", true);
103:            }
104:
105:            /* ------------------------------------------------------------ */
106:            /* 
107:             * @see org.mortbay.thread.AbstractLifeCycle#doStart()
108:             */
109:            protected void doStart() throws Exception {
110:                _sessionManager.start();
111:                super .doStart();
112:            }
113:
114:            /* ------------------------------------------------------------ */
115:            /* 
116:             * @see org.mortbay.thread.AbstractLifeCycle#doStop()
117:             */
118:            protected void doStop() throws Exception {
119:                super .doStop();
120:                _sessionManager.stop();
121:            }
122:
123:            /* ------------------------------------------------------------ */
124:            /* 
125:             * @see org.mortbay.jetty.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
126:             */
127:            public void handle(String target, HttpServletRequest request,
128:                    HttpServletResponse response, int dispatch)
129:                    throws IOException, ServletException {
130:                setRequestedId(request, dispatch);
131:
132:                Request base_request = (request instanceof  Request) ? (Request) request
133:                        : HttpConnection.getCurrentConnection().getRequest();
134:                SessionManager old_session_manager = null;
135:                HttpSession old_session = null;
136:
137:                try {
138:                    old_session_manager = base_request.getSessionManager();
139:                    old_session = base_request.getSession(false);
140:
141:                    if (old_session_manager != _sessionManager) {
142:                        // new session context
143:                        base_request.setSessionManager(_sessionManager);
144:                        base_request.setSession(null);
145:                    }
146:
147:                    // access any existing session
148:                    HttpSession session = null;
149:                    if (_sessionManager != null) {
150:                        session = request.getSession(false);
151:                        if (session != null) {
152:                            if (session != old_session) {
153:                                Cookie cookie = _sessionManager.access(session,
154:                                        request.isSecure());
155:                                if (cookie != null) // Handle changed ID or max-age refresh
156:                                    response.addCookie(cookie);
157:                            }
158:                        } else {
159:                            session = base_request
160:                                    .recoverNewSession(_sessionManager);
161:                            if (session != null)
162:                                base_request.setSession(session);
163:                        }
164:                    }
165:
166:                    if (Log.isDebugEnabled()) {
167:                        Log.debug("sessionManager=" + _sessionManager);
168:                        Log.debug("session=" + session);
169:                    }
170:
171:                    getHandler().handle(target, request, response, dispatch);
172:                } catch (RetryRequest r) {
173:                    HttpSession session = base_request.getSession(false);
174:                    if (session != null && session.isNew())
175:                        base_request.saveNewSession(_sessionManager, session);
176:                    throw r;
177:                } finally {
178:                    HttpSession session = request.getSession(false);
179:                    if (session != null && session != old_session)
180:                        getSessionManager().complete(session);
181:                    base_request.setSessionManager(old_session_manager);
182:                    base_request.setSession(old_session);
183:                }
184:            }
185:
186:            /* ------------------------------------------------------------ */
187:            /** Look for a requested session ID in cookies and URI parameters
188:             * @param request
189:             * @param dispatch
190:             */
191:            protected void setRequestedId(HttpServletRequest request,
192:                    int dispatch) {
193:                Request base_request = (request instanceof  Request) ? (Request) request
194:                        : HttpConnection.getCurrentConnection().getRequest();
195:                String requested_session_id = request.getRequestedSessionId();
196:                if (dispatch != REQUEST || requested_session_id != null) {
197:                    return;
198:                }
199:
200:                SessionManager sessionManager = getSessionManager();
201:                boolean requested_session_id_from_cookie = false;
202:
203:                // Look for session id cookie     
204:                Cookie[] cookies = request.getCookies();
205:                if (cookies != null && cookies.length > 0) {
206:                    for (int i = 0; i < cookies.length; i++) {
207:                        if (sessionManager.getSessionCookie().equalsIgnoreCase(
208:                                cookies[i].getName())) {
209:                            if (requested_session_id != null) {
210:                                // Multiple jsessionid cookies. Probably due to
211:                                // multiple paths and/or domains. Pick the first
212:                                // known session or the last defined cookie.
213:                                if (sessionManager
214:                                        .getHttpSession(requested_session_id) != null)
215:                                    break;
216:                            }
217:
218:                            requested_session_id = cookies[i].getValue();
219:                            requested_session_id_from_cookie = true;
220:                            if (Log.isDebugEnabled())
221:                                Log
222:                                        .debug("Got Session ID "
223:                                                + requested_session_id
224:                                                + " from cookie");
225:                        }
226:                    }
227:                }
228:
229:                if (requested_session_id == null) {
230:                    String uri = request.getRequestURI();
231:
232:                    int semi = uri.lastIndexOf(';');
233:                    if (semi >= 0) {
234:                        String path_params = uri.substring(semi + 1);
235:
236:                        // check if there is a url encoded session param.
237:                        if (path_params != null
238:                                && path_params.startsWith(sessionManager
239:                                        .getSessionURL())) {
240:                            requested_session_id = path_params
241:                                    .substring(sessionManager.getSessionURL()
242:                                            .length() + 1);
243:                            if (Log.isDebugEnabled())
244:                                Log.debug("Got Session ID "
245:                                        + requested_session_id + " from URL");
246:                        }
247:                    }
248:                }
249:
250:                base_request.setRequestedSessionId(requested_session_id);
251:                base_request
252:                        .setRequestedSessionIdFromCookie(requested_session_id != null
253:                                && requested_session_id_from_cookie);
254:            }
255:
256:            /* ------------------------------------------------------------ */
257:            /**
258:             * @param listener
259:             */
260:            public void addEventListener(EventListener listener) {
261:                if (_sessionManager != null)
262:                    _sessionManager.addEventListener(listener);
263:            }
264:
265:            /* ------------------------------------------------------------ */
266:            public void clearEventListeners() {
267:                if (_sessionManager != null)
268:                    _sessionManager.clearEventListeners();
269:            }
270:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.