001: package clime.messadmin.utils;
002:
003: import java.net.MalformedURLException;
004:
005: import javax.servlet.ServletContext;
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpSession;
008:
009: import clime.messadmin.model.ISessionInfo;
010: import clime.messadmin.model.Server;
011:
012: /**
013: * Utility methods on HttpSessions...
014: * @author Cédrik LIME
015: */
016: public class SessionUtils {
017:
018: /**
019: *
020: */
021: private SessionUtils() {
022: super ();
023: }
024:
025: public static int getUsedTimeForSession(HttpSession in_session) {
026: try {
027: ISessionInfo extraSessionInfo = Server.getInstance()
028: .getSession(in_session).getSessionInfo();
029: if (null != extraSessionInfo) {
030: return extraSessionInfo.getTotalUsedTime();
031: } else {
032: return -1;
033: }
034: //long diffMilliSeconds = in_session.getLastAccessedTime() - in_session.getCreationTime();
035: //return diffMilliSeconds;
036: } catch (IllegalStateException ise) {
037: //ignore: invalidated session
038: return -1;
039: }
040: }
041:
042: public static int getTTLForSession(HttpSession in_session) {
043: try {
044: long diffMilliSeconds = (1000 * in_session
045: .getMaxInactiveInterval())
046: - (System.currentTimeMillis() - in_session
047: .getLastAccessedTime());
048: return (int) diffMilliSeconds;
049: } catch (IllegalStateException ise) {
050: //ignore: invalidated session
051: return -1;
052: }
053: }
054:
055: public static int getIdleTimeForSession(HttpSession in_session) {
056: try {
057: long diffMilliSeconds = System.currentTimeMillis()
058: - in_session.getLastAccessedTime();
059: return (int) diffMilliSeconds;
060: } catch (IllegalStateException ise) {
061: //ignore: invalidated session
062: return -1;
063: }
064: }
065:
066: //TODO: move this method in a utility package
067:
068: /**
069: * Returns the context path of the web application. The context path is
070: * the portion of the request URI that is used to select the context of
071: * the request. The context path always comes first in a request URI. The
072: * path starts with a "/" character but does not end with a "/" character.
073: * For servlets in the default (root) context, this method returns "".
074: *
075: * It is possible that a servlet container may match a context by more than
076: * one context path. In such cases getContextPath() will return the actual
077: * context path used by the request and it may differ from the path returned
078: * by this method. The context path returned by this method should be considered
079: * as the prime or preferred context path of the application.
080: *
081: * @return The context path of the web application.
082: */
083: /**
084: * FIXME debug this method so that it really works! Should return the same as HttpServletRequest.getContextPath(). Now, where the hell is ServletContext.getContextPath()? :-( UPDATE: it's in 2.5 now!
085: * Returns the portion of the request URI that indicates the context
086: * of the request. The context path always comes first in a request
087: * URI. The path starts with a "/" character but does not end with a "/"
088: * character. For servlets in the default (root) context, this method
089: * returns "". The container does not decode this string.
090: *
091: * It is possible that a servlet container may match a context by more
092: * than one context path. In such cases this method will return the actual
093: * context path used by the request and it may differ from the path returned
094: * by the ServletContext.getContextPath() method. The context path returned
095: * by ServletContext.getContextPath() should be considered as the prime or
096: * preferred context path of the application.
097: *
098: * @param session
099: * @return a <code>String</code> specifying the portion of
100: * the request URI that indicates the context of the request
101: */
102: public static String getContext(HttpSession session) {
103: return getContext(session.getServletContext());
104: }
105:
106: public static String getContext(ServletContext context) {
107: //FIXME try new 2.5 ServletContext.getContextPath() first; use new plugin architecture
108: try {
109: return context.getResource("/").getPath();//$NON-NLS-1$
110: } catch (MalformedURLException mue) {
111: throw new RuntimeException(mue.getMessage());
112: }
113: }
114:
115: /**
116: * Reconstructs the URL the client used to make the request,
117: * using information in the <code>HttpServletRequest</code> object.
118: * The returned URL contains a protocol, server name, port
119: * number, and server path, and include query
120: * string parameters.
121: *
122: * <p>This method is useful for creating redirect messages
123: * and for reporting errors.
124: *
125: * @param req a <code>HttpServletRequest</code> object
126: * containing the client's request
127: *
128: * @return a <code>String</code> object containing
129: * the reconstructed URL
130: */
131: public static String getRequestURLWithMethodAndQueryString(
132: HttpServletRequest req) {
133: /*
134: StringBuffer url = new StringBuffer(32);
135: String scheme = req.getScheme();
136: int port = req.getServerPort();
137: if (port < 0) {
138: port = 80; // Work around java.net.URL bug
139: }
140: //String servletPath = req.getServletPath();
141: //String pathInfo = req.getPathInfo();
142: String queryString = req.getQueryString();
143:
144: url.append(scheme); // http, https
145: url.append("://"); //$NON-NLS-1$
146: url.append(req.getServerName());
147: if ((scheme.equals ("http") && port != 80) //$NON-NLS-1$
148: || (scheme.equals ("https") && port != 443)) { //$NON-NLS-1$
149: url.append (':'); //$NON-NLS-1$
150: url.append (port);
151: }
152: //if (servletPath != null)
153: // url.append (servletPath);
154: //if (pathInfo != null)
155: // url.append (pathInfo);
156: url.append(req.getRequestURI());
157: if (queryString != null) {
158: url.append('?').append(queryString); //$NON-NLS-1$
159: }
160: return url.toString();
161: */
162: String method = req.getMethod();
163: StringBuffer requestURL = req.getRequestURL();
164: String queryString = req.getQueryString();
165: int totalLength = method.length() + 1 + requestURL.length()
166: + (queryString == null ? 0 : 1 + queryString.length());
167: StringBuffer buffer = new StringBuffer(totalLength);
168: buffer.append(method).append(' ').append(requestURL);
169: if (queryString != null && !"".equals(queryString)) {//$NON-NLS-1$
170: buffer.append('?').append(queryString);
171: }
172: return buffer.toString();
173: }
174:
175: }
|