001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.manager.util;
019:
020: import java.lang.reflect.Method;
021: import java.security.Principal;
022: import java.util.ArrayList;
023: import java.util.Enumeration;
024: import java.util.List;
025: import java.util.Locale;
026:
027: import javax.security.auth.Subject;
028: import javax.servlet.http.HttpSession;
029:
030: import org.apache.catalina.Session;
031:
032: /**
033: * Utility methods on HttpSessions...
034: * @author Cédrik LIME
035: */
036: public class SessionUtils {
037:
038: /**
039: *
040: */
041: private SessionUtils() {
042: super ();
043: }
044:
045: /**
046: * The session attributes key under which the user's selected
047: * <code>java.util.Locale</code> is stored, if any.
048: */
049: // org.apache.struts.Globals.LOCALE_KEY
050: private static final String STRUTS_LOCALE_KEY = "org.apache.struts.action.LOCALE";//$NON-NLS-1$
051: // javax.servlet.jsp.jstl.core.Config.FMT_LOCALE
052: private static final String JSTL_LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale";//$NON-NLS-1$
053: // org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME
054: private static final String SPRING_LOCALE_KEY = "org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE";//$NON-NLS-1$
055: /**
056: * Lower and upper-case strings will be dynamically generated. Put mid-capitalised strings here!
057: */
058: private static final String[] LOCALE_TEST_ATTRIBUTES = new String[] {
059: STRUTS_LOCALE_KEY, SPRING_LOCALE_KEY, JSTL_LOCALE_KEY,
060: "Locale", "java.util.Locale" };
061: /**
062: * Lower and upper-case strings will be dynamically generated. Put mid-capitalised strings here!
063: */
064: private static final String[] USER_TEST_ATTRIBUTES = new String[] {
065: "Login", "User", "userName", "UserName", "Utilisateur" };
066:
067: /**
068: * Try to get user locale from the session, if possible.
069: * IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3, Struts 1.x and Spring
070: * JSF check the browser meta tag "accept languages" to choose what langage to display.
071: * @param in_session
072: * @return String
073: */
074: public static Locale guessLocaleFromSession(final Session in_session) {
075: return guessLocaleFromSession(in_session.getSession());
076: }
077:
078: public static Locale guessLocaleFromSession(
079: final HttpSession in_session) {
080: if (null == in_session) {
081: return null;
082: }
083: try {
084: Locale locale = null;
085:
086: // First search "known locations"
087: for (int i = 0; i < LOCALE_TEST_ATTRIBUTES.length; ++i) {
088: Object obj = in_session
089: .getAttribute(LOCALE_TEST_ATTRIBUTES[i]);
090: if (null != obj && obj instanceof Locale) {
091: locale = (Locale) obj;
092: break;
093: }
094: obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i]
095: .toLowerCase());
096: if (null != obj && obj instanceof Locale) {
097: locale = (Locale) obj;
098: break;
099: }
100: obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i]
101: .toUpperCase());
102: if (null != obj && obj instanceof Locale) {
103: locale = (Locale) obj;
104: break;
105: }
106: }
107:
108: if (null != locale) {
109: return locale;
110: }
111:
112: // Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
113: // TODO: Tapestry 4+
114: {
115: final List tapestryArray = new ArrayList();
116: for (Enumeration enumeration = in_session
117: .getAttributeNames(); enumeration
118: .hasMoreElements();) {
119: String name = (String) enumeration.nextElement();
120: if (name.indexOf("tapestry") > -1 && name.indexOf("engine") > -1 && null != in_session.getAttribute(name)) {//$NON-NLS-1$ //$NON-NLS-2$
121: tapestryArray
122: .add(in_session.getAttribute(name));
123: }
124: }
125: if (tapestryArray.size() == 1) {
126: // found a potential Engine! Let's call getLocale() on it.
127: Object probableEngine = tapestryArray.get(0);
128: if (null != probableEngine) {
129: try {
130: Method readMethod = probableEngine
131: .getClass().getMethod(
132: "getLocale", null);//$NON-NLS-1$
133: if (null != readMethod) {
134: // Call the property getter and return the value
135: Object possibleLocale = readMethod
136: .invoke(probableEngine, null);
137: if (null != possibleLocale
138: && possibleLocale instanceof Locale) {
139: locale = (Locale) possibleLocale;
140: }
141: }
142: } catch (Exception e) {
143: // stay silent
144: }
145: }
146: }
147: }
148:
149: if (null != locale) {
150: return locale;
151: }
152:
153: // Last guess: iterate over all attributes, to find a Locale
154: // If there is only one, consider it to be /the/ locale
155: {
156: final List localeArray = new ArrayList();
157: for (Enumeration enumeration = in_session
158: .getAttributeNames(); enumeration
159: .hasMoreElements();) {
160: String name = (String) enumeration.nextElement();
161: Object obj = in_session.getAttribute(name);
162: if (null != obj && obj instanceof Locale) {
163: localeArray.add(obj);
164: }
165: }
166: if (localeArray.size() == 1) {
167: locale = (Locale) localeArray.get(0);
168: }
169: }
170:
171: return locale;
172: } catch (IllegalStateException ise) {
173: //ignore: invalidated session
174: return null;
175: }
176: }
177:
178: /**
179: * Try to get user from the session, if possible.
180: * @param in_session
181: * @return Object
182: */
183: public static Object guessUserFromSession(final Session in_session) {
184: if (null == in_session) {
185: return null;
186: }
187: if (in_session.getPrincipal() != null) {
188: return in_session.getPrincipal().getName();
189: }
190: HttpSession httpSession = in_session.getSession();
191: try {
192: Object user = null;
193: // First search "known locations"
194: for (int i = 0; i < USER_TEST_ATTRIBUTES.length; ++i) {
195: Object obj = httpSession
196: .getAttribute(USER_TEST_ATTRIBUTES[i]);
197: if (null != obj) {
198: user = obj;
199: break;
200: }
201: obj = httpSession.getAttribute(USER_TEST_ATTRIBUTES[i]
202: .toLowerCase());
203: if (null != obj) {
204: user = obj;
205: break;
206: }
207: obj = httpSession.getAttribute(USER_TEST_ATTRIBUTES[i]
208: .toUpperCase());
209: if (null != obj) {
210: user = obj;
211: break;
212: }
213: }
214:
215: if (null != user) {
216: return user;
217: }
218:
219: // Last guess: iterate over all attributes, to find a java.security.Principal or javax.security.auth.Subject
220: // If there is only one, consider it to be /the/ user
221: {
222: final List principalArray = new ArrayList();
223: for (Enumeration enumeration = httpSession
224: .getAttributeNames(); enumeration
225: .hasMoreElements();) {
226: String name = (String) enumeration.nextElement();
227: Object obj = httpSession.getAttribute(name);
228: if (null != obj
229: && (obj instanceof Principal || obj instanceof Subject)) {
230: principalArray.add(obj);
231: }
232: // This workaround for JDK 1.3 compatibility. For JDK 1.4+, use previous (commented) instanceof.
233: // try {
234: // Class subjectClass = Class.forName("javax.security.auth.Subject", true, Thread.currentThread().getContextClassLoader());
235: // if (subjectClass.isInstance(obj)) {
236: // principalArray.add(obj);
237: // }
238: // } catch (ClassNotFoundException cnfe) {
239: // // This is JDK 1.3: javax.security.auth.Subject does not exist; do nothing
240: // }
241: }
242: if (principalArray.size() == 1) {
243: user = principalArray.get(0);
244: }
245: }
246:
247: if (null != user) {
248: return user;
249: }
250:
251: return user;
252: } catch (IllegalStateException ise) {
253: //ignore: invalidated session
254: return null;
255: }
256: }
257:
258: public static long getUsedTimeForSession(Session in_session) {
259: try {
260: long diffMilliSeconds = in_session.getLastAccessedTime()
261: - in_session.getCreationTime();
262: return diffMilliSeconds;
263: } catch (IllegalStateException ise) {
264: //ignore: invalidated session
265: return -1;
266: }
267: }
268:
269: public static long getTTLForSession(Session in_session) {
270: try {
271: long diffMilliSeconds = (1000 * in_session
272: .getMaxInactiveInterval())
273: - (System.currentTimeMillis() - in_session
274: .getLastAccessedTime());
275: return diffMilliSeconds;
276: } catch (IllegalStateException ise) {
277: //ignore: invalidated session
278: return -1;
279: }
280: }
281:
282: public static long getInactiveTimeForSession(Session in_session) {
283: try {
284: long diffMilliSeconds = System.currentTimeMillis()
285: - in_session.getLastAccessedTime();
286: return diffMilliSeconds;
287: } catch (IllegalStateException ise) {
288: //ignore: invalidated session
289: return -1;
290: }
291: }
292: }
|