org.apache.catalina.manager.util.SessionUtils Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.manager.util;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import javax.security.auth.Subject;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Session;
import org.apache.tomcat.util.ExceptionUtils;
/**
* Utility methods on HttpSessions...
* @author Cédrik LIME
*/
public class SessionUtils {
/**
*
*/
private SessionUtils() {
super();
}
/**
* The session attributes key under which the user's selected
* java.util.Locale
is stored, if any.
*/
// org.apache.struts.Globals.LOCALE_KEY
private static final String STRUTS_LOCALE_KEY = "org.apache.struts.action.LOCALE";//$NON-NLS-1$
// javax.servlet.jsp.jstl.core.Config.FMT_LOCALE
private static final String JSTL_LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale";//$NON-NLS-1$
// org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME
private static final String SPRING_LOCALE_KEY = "org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE";//$NON-NLS-1$
/**
* Lower and upper-case strings will be dynamically generated. Put mid-capitalised strings here!
*/
private static final String[] LOCALE_TEST_ATTRIBUTES = new String[] {
STRUTS_LOCALE_KEY, SPRING_LOCALE_KEY, JSTL_LOCALE_KEY, "Locale", "java.util.Locale" };
/**
* For efficient operation, list the attributes here with the typically used
* capitalisation. This will be tried first and then the auto-generated
* upper and lower case versions will be tried.
*/
private static final String[] USER_TEST_ATTRIBUTES = new String[] {
"Login", "User", "userName", "UserName", "Utilisateur",
"SPRING_SECURITY_LAST_USERNAME"};
/**
* Try to get user locale from the session, if possible.
* IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3, Struts 1.x and Spring
* JSF check the browser meta tag "accept languages" to choose what language to display.
* @param in_session The session
* @return the locale
*/
public static Locale guessLocaleFromSession(final Session in_session) {
return guessLocaleFromSession(in_session.getSession());
}
public static Locale guessLocaleFromSession(final HttpSession in_session) {
if (null == in_session) {
return null;
}
try {
Locale locale = null;
// First search "known locations"
for (String localeTestAttribute : LOCALE_TEST_ATTRIBUTES) {
Object obj = in_session.getAttribute(localeTestAttribute);
if (obj instanceof Locale) {
locale = (Locale) obj;
break;
}
obj = in_session.getAttribute(localeTestAttribute.toLowerCase(Locale.ENGLISH));
if (obj instanceof Locale) {
locale = (Locale) obj;
break;
}
obj = in_session.getAttribute(localeTestAttribute.toUpperCase(Locale.ENGLISH));
if (obj instanceof Locale) {
locale = (Locale) obj;
break;
}
}
if (null != locale) {
return locale;
}
// Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
// TODO: Tapestry 4+
final List