KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > SessionRequest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session;
6
7 import com.terracotta.session.util.Assert;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import javax.servlet.http.HttpSession JavaDoc;
13
14 public class SessionRequest extends HttpServletRequestWrapper JavaDoc implements TerracottaRequest {
15   private final HttpServletRequest JavaDoc req;
16   private final HttpServletResponse JavaDoc res;
17   private final SessionId requestedSessionId;
18   private final long requestStartMillis;
19   private final boolean isForwarded;
20   private final boolean isSessionOwner;
21
22   private Session session;
23   private TerracottaSessionManager mgr;
24
25   public SessionRequest(SessionId requestedSessionId, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
26     super(req);
27     Assert.pre(req != null);
28     Assert.pre(res != null);
29
30     this.req = req;
31     this.res = res;
32     this.requestedSessionId = requestedSessionId;
33     this.requestStartMillis = System.currentTimeMillis();
34     // in some cases, we could be multi-wrapping native requests.
35
// in this case, we need to check if TC session has already been created
36
HttpSession JavaDoc nativeSess = req.getSession(false);
37     if (nativeSess instanceof Session) {
38       this.isForwarded = true;
39       this.isSessionOwner = false;
40       this.session = (Session) nativeSess;
41     } else {
42       this.isSessionOwner = true;
43       this.isForwarded = req.getAttribute("javax.servlet.forward.request_uri") != null;
44     }
45   }
46
47   public HttpSession JavaDoc getSession() {
48     HttpSession JavaDoc rv = getTerracottaSession(true);
49     Assert.post(rv != null);
50     return rv;
51   }
52
53   public HttpSession JavaDoc getSession(boolean createNew) {
54     return getTerracottaSession(createNew);
55   }
56
57   public boolean isRequestedSessionIdValid() {
58     if (requestedSessionId == null) return false;
59     Session sess = getTerracottaSession(false);
60     return (sess != null && requestedSessionId.getKey().equals(sess.getSessionId().getKey()));
61   }
62
63   public String JavaDoc encodeRedirectURL(String JavaDoc url) {
64     return mgr.getCookieWriter().encodeRedirectURL(url, this);
65   }
66
67   public String JavaDoc encodeURL(String JavaDoc url) {
68     return mgr.getCookieWriter().encodeURL(url, this);
69   }
70
71   public Session getTerracottaSession(boolean createNew) {
72     if (session != null) return session;
73     session = (createNew) ? mgr.getSession(requestedSessionId, req, res) : mgr.getSessionIfExists(requestedSessionId,
74                                                                                                   req, res);
75     Assert.post(!createNew || session != null);
76     return session;
77   }
78
79   public boolean isSessionOwner() {
80     return isSessionOwner && session != null;
81   }
82
83   public boolean isForwarded() {
84     return isForwarded;
85   }
86
87   public long getRequestStartMillis() {
88     return requestStartMillis;
89   }
90
91   public void setSessionManager(TerracottaSessionManager sessionManager) {
92     this.mgr = sessionManager;
93   }
94 }
95
Popular Tags