PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/tomcat-7.0.2/java/org/apache/catalina/manager/util/SessionUtils.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 264 lines | 174 code | 24 blank | 66 comment | 60 complexity | 085b781638bc3f4df5627a4d3d7311e3 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.catalina.manager.util;
  18. import java.lang.reflect.Method;
  19. import java.security.Principal;
  20. import java.util.ArrayList;
  21. import java.util.Enumeration;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import javax.security.auth.Subject;
  25. import javax.servlet.http.HttpSession;
  26. import org.apache.catalina.Session;
  27. /**
  28. * Utility methods on HttpSessions...
  29. * @author Cédrik LIME
  30. */
  31. public class SessionUtils {
  32. /**
  33. *
  34. */
  35. private SessionUtils() {
  36. super();
  37. }
  38. /**
  39. * The session attributes key under which the user's selected
  40. * <code>java.util.Locale</code> is stored, if any.
  41. */
  42. // org.apache.struts.Globals.LOCALE_KEY
  43. private static final String STRUTS_LOCALE_KEY = "org.apache.struts.action.LOCALE";//$NON-NLS-1$
  44. // javax.servlet.jsp.jstl.core.Config.FMT_LOCALE
  45. private static final String JSTL_LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale";//$NON-NLS-1$
  46. // org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME
  47. private static final String SPRING_LOCALE_KEY = "org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE";//$NON-NLS-1$
  48. /**
  49. * Lower and upper-case strings will be dynamically generated. Put mid-capitalised strings here!
  50. */
  51. private static final String[] LOCALE_TEST_ATTRIBUTES = new String[] {
  52. STRUTS_LOCALE_KEY, SPRING_LOCALE_KEY, JSTL_LOCALE_KEY, "Locale", "java.util.Locale" };
  53. /**
  54. * For efficient operation, list the attributes here with the typically used
  55. * capitalisation. This will be tried first and then the auto-generated
  56. * upper and lower case versions will be tried.
  57. */
  58. private static final String[] USER_TEST_ATTRIBUTES = new String[] {
  59. "Login", "User", "userName", "UserName", "Utilisateur",
  60. "SPRING_SECURITY_LAST_USERNAME"};
  61. /**
  62. * Try to get user locale from the session, if possible.
  63. * IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3, Struts 1.x and Spring
  64. * JSF check the browser meta tag "accept languages" to choose what language to display.
  65. * @param in_session
  66. * @return String
  67. */
  68. public static Locale guessLocaleFromSession(final Session in_session) {
  69. return guessLocaleFromSession(in_session.getSession());
  70. }
  71. public static Locale guessLocaleFromSession(final HttpSession in_session) {
  72. if (null == in_session) {
  73. return null;
  74. }
  75. try {
  76. Locale locale = null;
  77. // First search "known locations"
  78. for (int i = 0; i < LOCALE_TEST_ATTRIBUTES.length; ++i) {
  79. Object obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i]);
  80. if (null != obj && obj instanceof Locale) {
  81. locale = (Locale) obj;
  82. break;
  83. }
  84. obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i].toLowerCase(Locale.ENGLISH));
  85. if (null != obj && obj instanceof Locale) {
  86. locale = (Locale) obj;
  87. break;
  88. }
  89. obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i].toUpperCase(Locale.ENGLISH));
  90. if (null != obj && obj instanceof Locale) {
  91. locale = (Locale) obj;
  92. break;
  93. }
  94. }
  95. if (null != locale) {
  96. return locale;
  97. }
  98. // Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
  99. // TODO: Tapestry 4+
  100. {
  101. final List<Object> tapestryArray = new ArrayList<Object>();
  102. for (Enumeration<String> enumeration = in_session.getAttributeNames(); enumeration.hasMoreElements();) {
  103. String name = enumeration.nextElement();
  104. if (name.indexOf("tapestry") > -1 && name.indexOf("engine") > -1 && null != in_session.getAttribute(name)) {//$NON-NLS-1$ //$NON-NLS-2$
  105. tapestryArray.add(in_session.getAttribute(name));
  106. }
  107. }
  108. if (tapestryArray.size() == 1) {
  109. // found a potential Engine! Let's call getLocale() on it.
  110. Object probableEngine = tapestryArray.get(0);
  111. if (null != probableEngine) {
  112. try {
  113. Method readMethod = probableEngine.getClass().getMethod("getLocale", (Class<?>[])null);//$NON-NLS-1$
  114. if (null != readMethod) {
  115. // Call the property getter and return the value
  116. Object possibleLocale = readMethod.invoke(probableEngine, (Object[]) null);
  117. if (null != possibleLocale && possibleLocale instanceof Locale) {
  118. locale = (Locale) possibleLocale;
  119. }
  120. }
  121. } catch (Exception e) {
  122. // stay silent
  123. }
  124. }
  125. }
  126. }
  127. if (null != locale) {
  128. return locale;
  129. }
  130. // Last guess: iterate over all attributes, to find a Locale
  131. // If there is only one, consider it to be /the/ locale
  132. {
  133. final List<Object> localeArray = new ArrayList<Object>();
  134. for (Enumeration<String> enumeration = in_session.getAttributeNames(); enumeration.hasMoreElements();) {
  135. String name = enumeration.nextElement();
  136. Object obj = in_session.getAttribute(name);
  137. if (null != obj && obj instanceof Locale) {
  138. localeArray.add(obj);
  139. }
  140. }
  141. if (localeArray.size() == 1) {
  142. locale = (Locale) localeArray.get(0);
  143. }
  144. }
  145. return locale;
  146. } catch (IllegalStateException ise) {
  147. //ignore: invalidated session
  148. return null;
  149. }
  150. }
  151. /**
  152. * Try to get user from the session, if possible.
  153. * @param in_session
  154. * @return Object
  155. */
  156. public static Object guessUserFromSession(final Session in_session) {
  157. if (null == in_session) {
  158. return null;
  159. }
  160. if (in_session.getPrincipal() != null) {
  161. return in_session.getPrincipal().getName();
  162. }
  163. HttpSession httpSession = in_session.getSession();
  164. if (httpSession == null)
  165. return null;
  166. try {
  167. Object user = null;
  168. // First search "known locations"
  169. for (int i = 0; i < USER_TEST_ATTRIBUTES.length; ++i) {
  170. Object obj = httpSession.getAttribute(USER_TEST_ATTRIBUTES[i]);
  171. if (null != obj) {
  172. user = obj;
  173. break;
  174. }
  175. obj = httpSession.getAttribute(USER_TEST_ATTRIBUTES[i].toLowerCase(Locale.ENGLISH));
  176. if (null != obj) {
  177. user = obj;
  178. break;
  179. }
  180. obj = httpSession.getAttribute(USER_TEST_ATTRIBUTES[i].toUpperCase(Locale.ENGLISH));
  181. if (null != obj) {
  182. user = obj;
  183. break;
  184. }
  185. }
  186. if (null != user) {
  187. return user;
  188. }
  189. // Last guess: iterate over all attributes, to find a java.security.Principal or javax.security.auth.Subject
  190. // If there is only one, consider it to be /the/ user
  191. {
  192. final List<Object> principalArray = new ArrayList<Object>();
  193. for (Enumeration<String> enumeration = httpSession.getAttributeNames(); enumeration.hasMoreElements();) {
  194. String name = enumeration.nextElement();
  195. Object obj = httpSession.getAttribute(name);
  196. if (null != obj && (obj instanceof Principal || obj instanceof Subject)) {
  197. principalArray.add(obj);
  198. }
  199. }
  200. if (principalArray.size() == 1) {
  201. user = principalArray.get(0);
  202. }
  203. }
  204. if (null != user) {
  205. return user;
  206. }
  207. return user;
  208. } catch (IllegalStateException ise) {
  209. //ignore: invalidated session
  210. return null;
  211. }
  212. }
  213. public static long getUsedTimeForSession(Session in_session) {
  214. try {
  215. long diffMilliSeconds = in_session.getThisAccessedTime() - in_session.getCreationTime();
  216. return diffMilliSeconds;
  217. } catch (IllegalStateException ise) {
  218. //ignore: invalidated session
  219. return -1;
  220. }
  221. }
  222. public static long getTTLForSession(Session in_session) {
  223. try {
  224. long diffMilliSeconds = (1000*in_session.getMaxInactiveInterval()) - (System.currentTimeMillis() - in_session.getThisAccessedTime());
  225. return diffMilliSeconds;
  226. } catch (IllegalStateException ise) {
  227. //ignore: invalidated session
  228. return -1;
  229. }
  230. }
  231. public static long getInactiveTimeForSession(Session in_session) {
  232. try {
  233. long diffMilliSeconds = System.currentTimeMillis() - in_session.getThisAccessedTime();
  234. return diffMilliSeconds;
  235. } catch (IllegalStateException ise) {
  236. //ignore: invalidated session
  237. return -1;
  238. }
  239. }
  240. }