001: /**
002: *
003: */package clime.messadmin.model;
004:
005: import java.util.Hashtable;
006: import java.util.Map;
007:
008: import javax.servlet.ServletContext;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpSession;
011: import javax.servlet.http.HttpSessionActivationListener;
012: import javax.servlet.http.HttpSessionEvent;
013:
014: import clime.messadmin.core.Constants;
015: import clime.messadmin.filter.MessAdminRequestWrapper;
016: import clime.messadmin.filter.MessAdminResponseWrapper;
017: import clime.messadmin.utils.SessionUtils;
018: import clime.messadmin.utils.StringUtils;
019:
020: /**
021: * @author Cédrik LIME
022: */
023: public class Session implements HttpSessionActivationListener,
024: IRequestListener {
025: protected final SessionInfo sessionInfo;
026: protected final Request cumulativeRequests = new Request(null);
027: /**
028: * Map of user-defined data to store in the Session scope.
029: * This is mainly used for plugins (key == plugin FQ name, for example)
030: */
031: protected Map userData;
032:
033: /**
034: * @param session
035: */
036: public Session(HttpSession session) {
037: super ();
038: sessionInfo = new SessionInfo(session);
039: }
040:
041: /**
042: */
043: public Session(HttpSession session, ClassLoader cl) {
044: this (session);
045: sessionInfo.setClassLoader(cl);
046: }
047:
048: /**
049: * @return Returns the sessionInfo.
050: */
051: public ISessionInfo getSessionInfo() {
052: return sessionInfo;
053: }
054:
055: /**
056: * @return Returns the userData.
057: */
058: // implementation note: this should be synchronized, but
059: // we don't really care if multiple copies are created.
060: public Map getUserData() {
061: if (userData == null) {
062: userData = new Hashtable();
063: }
064: return userData;
065: }
066:
067: /**
068: * {@inheritDoc}
069: */
070: public void sessionDidActivate(final HttpSessionEvent se) {
071: sessionInfo.setHttpSession(se.getSession());
072: //FIXME can the id change? Need to read the Servlet spec.
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public void sessionWillPassivate(final HttpSessionEvent se) {
079: sessionInfo.setHttpSession(null);
080: }
081:
082: /**
083: * If message if blank or null, remove HttpSession attribute, otherwise inject message into HttpSessions
084: * @param message
085: */
086: public boolean injectMessage(String message) {
087: boolean inject = StringUtils.isNotBlank(message);
088: boolean actionDone = false;
089: if (inject) {
090: if (!message.equals(getSessionInfo().getAttribute(
091: Constants.SESSION_MESSAGE_KEY))) {
092: // Setting message
093: getSessionInfo().setAttribute(
094: Constants.SESSION_MESSAGE_KEY, message);
095: actionDone = true;
096: }
097: } else {
098: if (getSessionInfo().getAttribute(
099: Constants.SESSION_MESSAGE_KEY) != null) {
100: // Removing message
101: getSessionInfo().removeAttribute(
102: Constants.SESSION_MESSAGE_KEY);
103: actionDone = true;
104: }
105: }
106: return actionDone;
107: }
108:
109: /*****************************************/
110: /** Request/Response Listener methods **/
111: /*****************************************/
112:
113: /** {@inheritDoc} */
114: public void requestInitialized(final HttpServletRequest request,
115: final ServletContext servletContext) {
116: if (request == null || servletContext == null) {
117: return;
118: }
119: final HttpSession httpSession = request.getSession(false);
120: if (httpSession == null) {
121: // should never happen!
122: return;
123: } // else
124: try {
125: sessionInfo.authType = request.getAuthType();
126: sessionInfo.remoteHost = request.getRemoteHost();//FIXME getRemoteAddr() if config says not to resolve IPs
127: sessionInfo.lastRequestURL = SessionUtils
128: .getRequestURLWithMethodAndQueryString(request);
129: sessionInfo.userPrincipal = request.getUserPrincipal();
130: sessionInfo.remoteUser = request.getRemoteUser();
131: sessionInfo.isSecure = request.isSecure();
132: sessionInfo.userAgent = request.getHeader("user-agent");//$NON-NLS-1$
133: sessionInfo.referer = request.getHeader("referer");//$NON-NLS-1$
134: sessionInfo.sslCipherSuite = (String) request
135: .getAttribute("javax.servlet.request.cipher_suite");//$NON-NLS-1$
136: sessionInfo.sslAlgorithmSize = (Integer) request
137: .getAttribute("javax.servlet.request.key_size");//$NON-NLS-1$
138: // sessionInfo.sslCertificates = (X509Certificate[])request.getAttribute("javax.servlet.request.X509Certificate");//$NON-NLS-1$
139: cumulativeRequests.requestInitialized(
140: sessionInfo.cumulativeRequestStats, request,
141: servletContext);
142: } catch (IllegalStateException ise) {
143: // session is invalidated
144: }
145: }
146:
147: /** {@inheritDoc} */
148: public void requestDestroyed(final MessAdminRequestWrapper request,
149: final MessAdminResponseWrapper response,
150: final ServletContext servletContext) {
151: if (request == null || response == null
152: || servletContext == null) {
153: return;
154: }
155: final HttpSession httpSession = request.getSession(false);
156: if (httpSession == null) {
157: // should never happen!
158: return;
159: } // else
160: try {
161: // request not already counted
162: cumulativeRequests.requestDestroyed(
163: sessionInfo.cumulativeRequestStats, request,
164: response, servletContext);
165: } catch (IllegalStateException ise) {
166: // session is invalidated
167: }
168: }
169:
170: /** {@inheritDoc} */
171: public void requestException(Exception e,
172: MessAdminRequestWrapper request,
173: MessAdminResponseWrapper response,
174: ServletContext servletContext) {
175: if (request == null || response == null
176: || servletContext == null) {
177: return;
178: }
179: cumulativeRequests.requestException(
180: sessionInfo.cumulativeRequestStats, e, request,
181: response, servletContext);
182: }
183: }
|