1 5 package com.terracotta.session; 6 7 import com.terracotta.session.util.Assert; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletRequestWrapper ; 11 import javax.servlet.http.HttpServletResponse ; 12 import javax.servlet.http.HttpSession ; 13 14 public class SessionRequest extends HttpServletRequestWrapper implements TerracottaRequest { 15 private final HttpServletRequest req; 16 private final HttpServletResponse 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 req, HttpServletResponse 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 HttpSession 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 getSession() { 48 HttpSession rv = getTerracottaSession(true); 49 Assert.post(rv != null); 50 return rv; 51 } 52 53 public HttpSession 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 encodeRedirectURL(String url) { 64 return mgr.getCookieWriter().encodeRedirectURL(url, this); 65 } 66 67 public String encodeURL(String 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 |