001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session;
006:
007: import com.terracotta.session.util.Assert;
008:
009: import javax.servlet.RequestDispatcher;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletRequestWrapper;
012: import javax.servlet.http.HttpServletResponse;
013: import javax.servlet.http.HttpSession;
014:
015: public class SessionRequest extends HttpServletRequestWrapper implements
016: TerracottaRequest {
017:
018: // Attribute name for storing the Session in the request
019: private static final String SESSION_ATTRIBUTE_NAME = SessionRequest.class
020: .getName()
021: + ".session";
022:
023: // Attribute name for storing a Boolean.TRUE indicating the request has been forwarded
024: public static final String SESSION_FORWARD_ATTRIBUTE_NAME = SessionRequest.class
025: .getName()
026: + ".forward";
027:
028: private final HttpServletRequest req;
029: private final HttpServletResponse res;
030: private SessionId requestedSessionId;
031: private final long requestStartMillis;
032: private final boolean isForwarded;
033: private final boolean isSessionOwner;
034: private final SessionManager mgr;
035:
036: public SessionRequest(SessionId requestedSessionId,
037: HttpServletRequest req, HttpServletResponse res,
038: SessionManager sessionManager) {
039: super (req);
040:
041: Assert.pre(req != null);
042: Assert.pre(res != null);
043: Assert.pre(sessionManager != null);
044:
045: this .req = req;
046: this .res = res;
047: this .mgr = sessionManager;
048: this .requestedSessionId = requestedSessionId;
049: this .requestStartMillis = System.currentTimeMillis();
050: // in some cases, we could be multi-wrapping native requests.
051: // in this case, we need to check if TC session has already been created
052: HttpSession nativeSess = req.getSession(false);
053: if (nativeSess instanceof Session) {
054: this .isForwarded = true;
055: this .isSessionOwner = false;
056: setAttribute(SESSION_ATTRIBUTE_NAME, nativeSess);
057: } else {
058: this .isSessionOwner = true;
059:
060: // Added by TerracottaDispatcher when requests get forwarded through
061: Object fwdFlag = req
062: .getAttribute(SESSION_FORWARD_ATTRIBUTE_NAME);
063: this .isForwarded = (fwdFlag != null && fwdFlag
064: .equals(Boolean.TRUE));
065: }
066: }
067:
068: public HttpSession getSession() {
069: HttpSession rv = getTerracottaSession(true);
070: Assert.post(rv != null);
071: return rv;
072: }
073:
074: public HttpSession getSession(boolean createNew) {
075: return getTerracottaSession(createNew);
076: }
077:
078: public boolean isRequestedSessionIdValid() {
079: if (requestedSessionId == null)
080: return false;
081: Session sess = getTerracottaSession(false);
082: return (sess != null && requestedSessionId.getKey().equals(
083: sess.getSessionId().getKey()));
084: }
085:
086: public String encodeRedirectURL(String url) {
087: return mgr.getCookieWriter().encodeRedirectURL(url, this );
088: }
089:
090: public String encodeURL(String url) {
091: return mgr.getCookieWriter().encodeURL(url, this );
092: }
093:
094: public final Session getTerracottaSession(boolean createNew) {
095: Session session = (Session) getAttribute(SESSION_ATTRIBUTE_NAME);
096: if (session != null)
097: return session;
098: session = (createNew) ? mgr.getSession(requestedSessionId, req,
099: res) : mgr.getSessionIfExists(requestedSessionId, req,
100: res);
101: setAttribute(SESSION_ATTRIBUTE_NAME, session);
102:
103: if (session != null) {
104: session.associateRequest(this );
105: }
106: Assert.post(!createNew || session != null);
107: return session;
108: }
109:
110: public boolean isSessionOwner() {
111: return isSessionOwner
112: && getAttribute(SESSION_ATTRIBUTE_NAME) != null;
113: }
114:
115: public boolean isForwarded() {
116: return isForwarded;
117: }
118:
119: public long getRequestStartMillis() {
120: return requestStartMillis;
121: }
122:
123: public RequestDispatcher getRequestDispatcher(String path) {
124: return new TerracottaDispatcher(super
125: .getRequestDispatcher(path));
126: }
127:
128: void clearSession() {
129: Assert.pre(getAttribute(SESSION_ATTRIBUTE_NAME) != null);
130: removeAttribute(SESSION_ATTRIBUTE_NAME);
131:
132: requestedSessionId = null;
133: }
134:
135: }
|