001: package com.lutris.appserver.server.sessionContainerAdapter;
002:
003: import java.util.Date;
004: import java.util.Enumeration;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpSession;
008:
009: import com.lutris.appserver.server.StandardApplication;
010: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
011: import com.lutris.appserver.server.session.Session;
012: import com.lutris.appserver.server.session.SessionException;
013: import com.lutris.appserver.server.user.User;
014:
015: /**
016: * <p>
017: * Description:
018: * </p>
019: * Simple session manager to be used with servlet container capable of managing
020: * their sessions. Uses HttpSession to keep the session data. The sessions are
021: * completely managed by the session container. To use this class as a session
022: * manager, enter it as a value of the SessionManager.Class in the config file
023: * of the application - e.g.
024: *
025: * SessionManager.Class =
026: * com.lutris.appserver.server.sessionContainerAdapter.ContainerAdapterSessionManager
027: *
028: * @version 1.0
029: */
030: public class ContainerAdapterSessionManager implements
031: com.lutris.appserver.server.session.SessionManager,
032: java.io.Serializable {
033: /**
034: * Indicates url encoding status. Assumes that
035: * comms.response.writeHTML(HTMLDocument doc) is used to commit the response
036: * to the client. The default is to Auto.<br>
037: * <code>Never</code> indicates urls are never encoded with session keys.
038: * This indicates that session cookies have to be used or no session state
039: * can be maintained.<br>
040: * <code>Always</code> indicates that urls are always encoded with session
041: * keys. Session cookies are never used.<br>
042: * <code>Auto</code> indicates that session cookies will be if available.
043: * If not, urls will automatically be encoded.<br>
044: *
045: * @see #getEncodeUrlState
046: */
047: protected static String defaultEncodeUrlState = "Auto";
048:
049: /**
050: * The name of the config variable for the url encoding state
051: */
052: public static final String CFG_ENCODE_URL_STATE = "SessionEncodeUrlState";
053:
054: /**
055: * Url encoding for first page state (to force it or not)
056: * CFG_ENCODE_URL_STATE must not be set to NEVER for this parameter to take
057: * efect
058: */
059: public static final String CFG_ENCODE_FIRST_URL = "SessionEncodeFirstUrl";
060:
061: /**
062: * The url encoding state. Either <code>Never</code>
063: * <code>Always</code> <code>Auto</code>.
064: */
065: protected String encodeUrlState;
066:
067: /**
068: * Name of the session object in the HttpSession
069: */
070: protected String SESSION = "session";
071:
072: /**
073: * Default maximum session idle time, in seconds. A derived <CODE>SessionManager</CODE>
074: * may override this value to use the default expiration logic or override
075: * <CODE>isSessionExpired</CODE> to define custom expiration logic. A
076: * value less-than or equal to zero disables idle checking. Default value is
077: * 30 minutes.
078: *
079: * @see #getMaxSessionIdleTime
080: */
081: protected static int defaultMaxSessionIdleTime = 30 * 60;
082:
083: /**
084: * Maximum session idle time, in seconds. A value less-than or equal to zero
085: * disables idle checking.
086: *
087: * @see #getMaxSessionIdleTime
088: */
089:
090: protected int maxSessionIdleTime;
091:
092: /**
093: * Url encoding for first page state (force it or not)
094: */
095: protected boolean encodeFirstUrl;
096:
097: // protected boolean isMemoryPersistence=false;
098: /**
099: *
100: * @param application
101: * application that uses this session manager
102: * @param config
103: * the application config object
104: * @param logger
105: * logger, ignored, since some common loggers are not serilizable
106: * @throws SessionException
107: */
108: public ContainerAdapterSessionManager(
109: com.lutris.appserver.server.Application application,
110: com.lutris.util.Config config,
111: com.lutris.logging.LogChannel logger)
112: throws com.lutris.appserver.server.session.SessionException {
113:
114: /**/
115: try {
116: // Url encoding state
117: if (config.containsKey(CFG_ENCODE_URL_STATE)) {
118: encodeUrlState = config.getString(CFG_ENCODE_URL_STATE);
119: } else if (config.containsKey("EncodeUrlState")) {
120: // backwards compatability
121: encodeUrlState = config.getString("EncodeUrlState");
122: } else {
123: encodeUrlState = defaultEncodeUrlState;
124: }
125: if (config.containsKey("MaxIdleTime")) {
126: // backwards compatability
127: maxSessionIdleTime = config.getInt("MaxIdleTime") * 60;
128: } else {
129: maxSessionIdleTime = defaultMaxSessionIdleTime;
130: }
131: /**
132: * Url encoding for first page state (to force it or not)
133: * CFG_ENCODE_URL_STATE must not be set to NEVER for this parameter to
134: * take efect
135: */
136: if (config.containsKey(CFG_ENCODE_FIRST_URL)) {
137: encodeFirstUrl = config.getBoolean(
138: CFG_ENCODE_FIRST_URL, false);
139: } else {
140: encodeFirstUrl = false;
141: }
142:
143: /**
144: * if CFG_ENCODE_URL_STATE is set to ALWAYS or NEVER ignore original and
145: * set adequate CFG_ENCODE_FIRST_URL parameter value
146: */
147: if (encodeUrlState.equalsIgnoreCase(ENCODE_URL_ALWAYS)) {
148: encodeFirstUrl = true;
149: } else if (encodeUrlState
150: .equalsIgnoreCase(ENCODE_URL_NEVER)) {
151: encodeFirstUrl = false;
152: }
153:
154: // if (config.containsKey("MemoryPersistence")) {
155: // backwards compatability
156: // isMemoryPersistence = config.getBoolean("MemoryPersistence");
157: // }
158:
159: } catch (com.lutris.util.ConfigException e) {
160: throw new com.lutris.appserver.server.session.SessionException(
161: e);
162: }
163: // 29.5.2007. Aleksandar Stojsavljevic
164: //((StandardApplication) application).setCookieForNewSession(false);
165: ((StandardApplication) application)
166: .setCookieForNewSession(true);
167: }
168:
169: /**
170: * Creates a new session
171: *
172: * @return Seession
173: * @throws SessionException
174: */
175: public Session createSession()
176: throws com.lutris.appserver.server.session.SessionException {
177: return new ContainerAdapterSession(this , null);
178: }
179:
180: /**
181: * Not implemented - backward compatibility with the API specification
182: *
183: * @param ipport
184: * @return
185: * @throws SessionException
186: */
187: public Session createSession(String ipport)
188: throws com.lutris.appserver.server.session.SessionException {
189:
190: /** No need to implement this method - used only for Director */
191: throw new java.lang.UnsupportedOperationException(
192: "Method createSession() not implemented.");
193: }
194:
195: /**
196: * Extracts the SessionId from the httpSession and creates a newSession
197: * object with that Id
198: *
199: * @param comms
200: * HttpPresentationComms that containes the HttpServletRequest
201: * @return new Session
202: * @throws SessionException
203: */
204: public Session createSession(HttpPresentationComms comms)
205: throws com.lutris.appserver.server.session.SessionException {
206:
207: HttpServletRequest servletRequest = comms.request
208: .getHttpServletRequest();
209: HttpSession httpSession = servletRequest.getSession();
210: httpSession.setMaxInactiveInterval(maxSessionIdleTime);
211: try {
212: comms.session = (Session) httpSession.getAttribute(SESSION);
213: } catch (Exception ex) {
214: comms.session = null;
215: }
216:
217: if (comms.session == null) {
218: comms.session = new ContainerAdapterSession(this ,
219: httpSession);
220:
221: httpSession.setAttribute(SESSION, comms.session);
222: } else {
223: ((ContainerAdapterSession) comms.session)
224: .setSessionManager(this );
225: }
226:
227: return comms.session;
228: }
229:
230: /**
231: * Not implemented, since this session manager doesn't really control the
232: * sessions - they are managed by the servlet container
233: *
234: * @param parm1
235: * @throws SessionException
236: */
237: public void deleteSession(Session parm1)
238: throws com.lutris.appserver.server.session.SessionException {
239:
240: /** Not implemented */
241: throw new java.lang.UnsupportedOperationException(
242: "Method deleteSession() not implemented.");
243: }
244:
245: /**
246: * Not implemented, since this session manager doesn't really control the
247: * sessions - they are managed by the servlet container
248: *
249: * @param parm1
250: * @throws SessionException
251: */
252: public void deleteSession(String parm1)
253: throws com.lutris.appserver.server.session.SessionException {
254:
255: /** Not implementedd */
256: throw new java.lang.UnsupportedOperationException(
257: "Method deleteSession() not implemented.");
258: }
259:
260: /**
261: *
262: * @param sessionId
263: * @return always true
264: * @throws SessionException
265: */
266: public boolean sessionExists(String sessionId)
267: throws com.lutris.appserver.server.session.SessionException {
268: // 29.5.2007. Aleksandar Stojsavljevic
269: //return false;
270: return true;
271: }
272:
273: /**
274: * Not implemented, since the sessions are kept at the container side
275: *
276: * @param sessionId
277: * @return
278: * @throws SessionException
279: */
280: public Session getSession(String sessionId)
281: throws com.lutris.appserver.server.session.SessionException {
282: // System.out.println("Req. sess. " + sessionId);
283: throw new java.lang.UnsupportedOperationException(
284: "Method getSession (String sessionId) not implemented.");
285: }
286:
287: /**
288: * returns the session object contained in the HttpServletRequest
289: *
290: * @param sessionId -
291: * ignored, kept for the backward compatibility
292: * @param servletRequest
293: * HttpServletRequest from which the session is extracted
294: * @return the Session object
295: * @throws SessionException
296: */
297: public Session getSession(String sessionId,
298: HttpServletRequest servletRequest)
299: throws com.lutris.appserver.server.session.SessionException {
300:
301: HttpSession httpSession = servletRequest.getSession();
302:
303: if (httpSession != null) {
304: Session session = null;
305:
306: try {
307: session = (ContainerAdapterSession) httpSession
308: .getAttribute(SESSION);
309: } catch (IllegalStateException ise) {
310: return null;
311: }
312:
313: if (session != null) {
314: session.setHttpSession(httpSession);
315: ((ContainerAdapterSession) session)
316: .setSessionManager(this );
317: if (session.getSessionManager() == null) {
318: ((ContainerAdapterSession) session)
319: .setSessionManager(this );
320: }
321:
322: return session;
323: } else {
324: return null;
325: }
326: } else {
327: return null;
328: }
329: }
330:
331: /**
332: * Returns teh session object corresponding to the HttpPresentationComms
333: *
334: * @param sessionId
335: * ignored, kept for the backward compatibility
336: * @param comms
337: * HttpPresentationComms object that contains HttpServletRequest
338: * from which the session is extracted
339: * @return the Session object
340: * @throws SessionException
341: */
342: public Session getSession(String sessionId,
343: HttpPresentationComms comms)
344: throws com.lutris.appserver.server.session.SessionException {
345: HttpServletRequest servletRequest = comms.request
346: .getHttpServletRequest();
347: HttpSession httpSession = servletRequest.getSession();
348:
349: if (httpSession != null) {
350:
351: Session session = null;
352:
353: try {
354: session = (ContainerAdapterSession) httpSession
355: .getAttribute(SESSION);
356: } catch (IllegalStateException ise) {
357: return null;
358: }
359: if (session != null) {
360: session.setHttpSession(httpSession);
361: ((ContainerAdapterSession) session)
362: .setSessionManager(this );
363: if (session.getSessionManager() == null) {
364: ((ContainerAdapterSession) session)
365: .setSessionManager(this );
366: }
367:
368: return session;
369: } else {
370: return null;
371: }
372: } else {
373: return null;
374: }
375: }
376:
377: /**
378: * Returns teh session object corresponding to the HttpPresentationComms
379: *
380: * @param parm1
381: * ignored
382: * @param sessionId
383: * ignored
384: * @param comms
385: * HttpPresentationComms object that contains HttpServletRequest
386: * from which the session is extracted
387: * @return the Session object
388: * @throws SessionException
389: */
390: public Session getSession(Thread parm1, String sessionId,
391: HttpPresentationComms comms)
392: throws com.lutris.appserver.server.session.SessionException {
393: return getSession(sessionId, comms);
394: }
395:
396: /**
397: * Not implemented, needed for the compatibility with the API specification
398: *
399: * @param parm1
400: * @param sessionId
401: * @return
402: * @throws SessionException
403: */
404: public Session getSession(Thread parm1, String sessionId)
405: throws com.lutris.appserver.server.session.SessionException {
406:
407: /** Not implemented */
408: throw new java.lang.UnsupportedOperationException(
409: "Method getSessionKeys() not implemented.");
410: }
411:
412: /**
413: * Not implemented, this information is kept at the servlet container side
414: *
415: * @param parm1
416: * @return
417: * @throws SessionException
418: */
419: public Enumeration getSessionKeys(User parm1)
420: throws com.lutris.appserver.server.session.SessionException {
421:
422: /** Not implemented */
423: throw new java.lang.UnsupportedOperationException(
424: "Method getSessionKeys() not implemented.");
425: }
426:
427: /**
428: * Not implemented, since this session manager doesn't really control the
429: * sessions - they are managed by the servlet container
430: *
431: * @param parm1
432: * @param parm2
433: * @throws SessionException
434: */
435: public void passivateSession(Thread parm1, String parm2)
436: throws com.lutris.appserver.server.session.SessionException {
437: // throw new java.lang.UnsupportedOperationException("Method
438: // passivateSession() not implemented.");
439: return;
440: }
441:
442: /**
443: * Not implemented, since this session manager doesn't really control the
444: * sessions - they are managed by the servlet container
445: *
446: * @return
447: * @throws SessionException
448: */
449: public int activeSessionCount()
450: throws com.lutris.appserver.server.session.SessionException {
451:
452: /** Not implemented */
453: throw new java.lang.UnsupportedOperationException(
454: "Method activeSessionCount() not implemented.");
455: }
456:
457: /**
458: * Not implemented, this information is kept at the servlet container side
459: *
460: * @return
461: */
462: public int maxSessionCount() {
463:
464: /** Not implemented */
465: throw new java.lang.UnsupportedOperationException(
466: "Method maxSessionCount() not implemented.");
467: }
468:
469: /**
470: * Not implemented
471: *
472: * @return
473: */
474: public Date maxSessionCountDate() {
475:
476: /** Not implemented */
477: throw new java.lang.UnsupportedOperationException(
478: "Method maxSessionCountDate() not implemented.");
479: }
480:
481: /**
482: * Not implemented, since this session manager doesn't really control the
483: * sessions - they are managed by the servlet container
484: *
485: * @throws SessionException
486: */
487: public void resetMaxSessionCount()
488: throws com.lutris.appserver.server.session.SessionException {
489:
490: /** Not implemented */
491: throw new java.lang.UnsupportedOperationException(
492: "Method resetMaxSessionCount() not implemented.");
493: }
494:
495: /**
496: * Not implemented, this information is kept at the servlet container side
497: *
498: * @return
499: * @throws SessionException
500: */
501: public Enumeration getSessionKeys()
502: throws com.lutris.appserver.server.session.SessionException {
503:
504: /** Not implemented */
505: throw new java.lang.UnsupportedOperationException(
506: "Method getSessionKeys() not implemented.");
507: }
508:
509: /**
510: * NOP
511: */
512: public void shutdown() {
513: }
514:
515: /**
516: *
517: * @return the value indicating the url encoding status
518: */
519: public String getEncodeUrlState() {
520: return encodeUrlState;
521: }
522:
523: /**
524: * Not used for container adapter session management scenario! Container
525: * itself should take care about this.
526: *
527: * @return Whether to use empty path for session cookies
528: */
529: public boolean getEmptySessionPath() {
530: return false;
531: }
532:
533: /**
534: * Returns the url encoding rule for the first application page
535: * (true/false).
536: *
537: * CFG_ENCODE_URL_STATE must not be set to NEVER for this parameter to take
538: * efect
539: */
540: public boolean getEncodeFirstUrl() {
541: return encodeFirstUrl;
542: }
543:
544: }
|