edu.internet2.middleware.grouper.ui.SessionInitialiser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grouper-ui Show documentation
Show all versions of grouper-ui Show documentation
Internet2 Groups Management User Interface
The newest version!
/*******************************************************************************
* Copyright 2012 Internet2
*
* Licensed 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.
******************************************************************************/
/*
Copyright 2004-2008 University Corporation for Advanced Internet Development, Inc.
Copyright 2004-2008 The University Of Bristol
Licensed 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 edu.internet2.middleware.grouper.ui;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import edu.internet2.middleware.grouperClient.collections.MultiKey;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import edu.internet2.middleware.grouper.Group;
import edu.internet2.middleware.grouper.GroupFinder;
import edu.internet2.middleware.grouper.GrouperHelper;
import edu.internet2.middleware.grouper.GrouperSession;
import edu.internet2.middleware.grouper.cache.GrouperCache;
import edu.internet2.middleware.grouper.cfg.GrouperConfig;
import edu.internet2.middleware.grouper.ui.util.ChainedResourceBundle;
import edu.internet2.middleware.grouper.ui.util.DOMHelper;
import edu.internet2.middleware.grouper.ui.util.GrouperUiConfig;
import edu.internet2.middleware.grouper.ui.util.MapBundleWrapper;
import edu.internet2.middleware.grouper.ui.util.MembershipExporter;
import edu.internet2.middleware.grouper.ui.util.MembershipImportManager;
/**
* Initialises HttpSession after login. Should probably make an interface
* and allow site specific initialisation
*
*
* @author Gary Brown.
* @version $Id: SessionInitialiser.java,v 1.24 2009-10-16 08:06:26 isgwb Exp $
*/
public class SessionInitialiser {
/**
*
*/
public static final String RESOURCE_BUNDLE_KEY = "resourceBundleKey";
/** logger */
protected static Log LOG = edu.internet2.middleware.grouper.util.GrouperUtil.getLog(SessionInitialiser.class);
private static Group debuggers;
private static boolean attemptedDebuggers=false;
/** cache the locale and resource bundles. the multikey is the module, and the locale.
* this gets the BundleBean, one for nav resource bundle, one for MapBundleWrapper, one for MapBundleWrapper (null),
* media, media wrapper, media wrapper null */
private static GrouperCache resourceBundleCache =
new GrouperCache(SessionInitialiser.class.getName(), 500, false, 120, 120, false);
/**
* get a resource bundle based on multikey
* @param multiKey
* @param isNav true for nav, false for media
* @return the resource bundle
*/
public static LocalizationContext retrieveLocalizationContext(MultiKey multiKey, boolean isNav) {
if (isNav) {
return resourceBundles(multiKey).getNav();
}
return resourceBundles(multiKey).getMedia();
}
/**
* get a resource bundle based on multikey
* @param multiKey
* @param isNav true for nav, false for media
* @param returnNullsIfNotFound false if normal, true, is nulls if not found (e.g. mediaNullMap)
* @return the resource bundle
*/
public static MapBundleWrapper retrieveMapBundleWrapper(MultiKey multiKey, boolean isNav, boolean returnNullsIfNotFound) {
// session.setAttribute("nav",
// new javax.servlet.jsp.jstl.fmt.LocalizationContext(
// chainedBundle));
// session.setAttribute("navMap", new MapBundleWrapper(chainedBundle, false));
// session.setAttribute("navNullMap", navBundleWrapperNull);
// session.setAttribute("mediaMap", new MapBundleWrapper(
// chainedMediaBundle, false));
// //returns null if not there, not question marks
// session.setAttribute("mediaNullMap", new MapBundleWrapper(
// chainedMediaBundle, true));
if (isNav) {
if (!returnNullsIfNotFound) {
return resourceBundles(multiKey).getNavMap();
}
return resourceBundles(multiKey).getNavMapNull();
}
if (!returnNullsIfNotFound) {
return resourceBundles(multiKey).getMediaMap();
}
return resourceBundles(multiKey).getMediaMapNull();
}
/**
* get the resource bundle by name and local
* @param multiKey is the module, name (nav or media), and locale
* @return the resource bundle
*/
public static BundleBean resourceBundles( MultiKey multiKey) {
//new MultiKey(module, name, locale);
BundleBean resourceBundles = resourceBundleCache.get(multiKey);
if (resourceBundles == null) {
synchronized(SessionInitialiser.class) {
resourceBundles = resourceBundleCache.get(multiKey);
if (resourceBundles == null) {
String module = (String)multiKey.getKey(0);
Locale localeObj = (Locale)multiKey.getKey(1);
ResourceBundle grouperBundle = new GrouperNavResourceBundle(localeObj);
ResourceBundle grouperMediaBundle = ResourceBundle.getBundle(
"resources.grouper.media", localeObj);
ChainedResourceBundle chainedMediaBundle = null;
ChainedResourceBundle chainedBundle = new ChainedResourceBundle(grouperBundle,
"navResource");
if (module.equals("i2mi") || module.equals("grouper")) {
chainedMediaBundle = new ChainedResourceBundle(grouperMediaBundle,
"mediaResource");
} else {
ResourceBundle moduleMediaBundle = ResourceBundle.getBundle(
"resources." + module + ".media", localeObj);
chainedMediaBundle = new ChainedResourceBundle(moduleMediaBundle,
"mediaResource");
chainedMediaBundle.addBundle(grouperMediaBundle);
}
MapBundleWrapper navBundleWrapperNull = new MapBundleWrapper(chainedBundle, true);
addIncludeExcludeDefaults(chainedBundle, navBundleWrapperNull);
//add in grouper-ui.properties for media.properties
GrouperUiConfig grouperUiConfig = GrouperUiConfig.retrieveConfig();
for (String key : grouperUiConfig.propertyNames()) {
chainedMediaBundle.addToCache(key, grouperUiConfig.propertyValueString(key, ""));
}
resourceBundles = new BundleBean();
resourceBundles.setNav(
new javax.servlet.jsp.jstl.fmt.LocalizationContext(chainedBundle));
resourceBundles.setNavMap(
new MapBundleWrapper(chainedBundle, false));
resourceBundles.setNavMapNull(
navBundleWrapperNull);
resourceBundles.setMedia(
new javax.servlet.jsp.jstl.fmt.LocalizationContext(chainedMediaBundle));
resourceBundles.setMediaMap(
new MapBundleWrapper(chainedMediaBundle, false));
resourceBundles.setMediaMapNull(
new MapBundleWrapper(chainedMediaBundle, true));
resourceBundleCache.put(multiKey, resourceBundles);
}
}
}
return resourceBundles;
}
/**
* Sets locale and calls any module specific initialisation
*
* @param request
* current HttpServletRequest
*/
public static void init(HttpServletRequest request) {
try {
String localeStr = request.getParameter("lang");
HttpSession session = request.getSession();
Locale locale = null;
if(localeStr!=null && !"".equals(localeStr)) {
locale=createLocale(localeStr);
}
session.setAttribute("locale", locale);
SessionInitialiser.init(null, session);
if(locale!=null) {
SessionInitialiser.init(null, locale.toString(), session);
}else{
SessionInitialiser.init(null, null, session);
}
session.setAttribute("javax.servlet.jsp.jstl.fmt.locale", locale);
//session.setAttribute("sessionInited", localeStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Module specific initialisation with no locale specified
*
* @param module
* Struts's module
* @param session
* current HttpSession
* @throws Exception
*/
public static void init(String module, HttpSession session)
throws Exception {
init(module, null, session);
}
/**
* Module and locale specific initialisation
*
* @param module
* Strut's module
* @param locale
* selected locale
* @param session
* current HttpSession
* @throws Exception
*/
public static void init(String module, String locale, HttpSession session)
throws Exception {
if(Boolean.TRUE.equals(session.getAttribute("sessionInited"))) return;
if (module != null)
module = module.replaceAll("^/", "");
ResourceBundle defaultInit = ResourceBundle
.getBundle("resources/init");
if (module == null || module.equals("")) {
module = defaultInit.getString("default.module");
}
ResourceBundle moduleInit = ResourceBundle.getBundle("resources/"
+ module + "/init");
if (locale == null || locale.equals("")) {
locale = moduleInit.getString("default.locale");
}
Locale localeObj = createLocale(locale);
MultiKey resourceBundlesKey = new MultiKey(module, localeObj);
session.setAttribute(RESOURCE_BUNDLE_KEY, resourceBundlesKey);
ResourceBundle chainedMediaBundle = retrieveLocalizationContext(resourceBundlesKey, false).getResourceBundle();
String pageSizes = chainedMediaBundle
.getString("pager.pagesize.selection");
String[] pageSizeSelections = pageSizes.split(" ");
session.setAttribute("pageSizeSelections", pageSizeSelections);
session.setAttribute("stemSeparator", GrouperHelper.HIER_DELIM);
try {
String initialStems = chainedMediaBundle
.getString("plugin.initialstems");
if (initialStems != null && !"".equals(initialStems))
session.setAttribute("isQuickLinks", Boolean.TRUE);
} catch (Exception e) {
}
initDebugging(session);
GrouperSession s = getGrouperSession(session);
try {
if(GrouperHelper.isRoot(s) && "true".equals(chainedMediaBundle.getString("act-as-admin.default"))) {
session.setAttribute("activeWheelGroupMember",true);
}
}catch(Exception e) {
}
//@TODO: should we split the personalStemRoot and create
//any stems which are missing
try {
String personalStem = chainedMediaBundle
.getString("plugin.personalstem");
if (personalStem != null && !"".equals(personalStem)) {
PersonalStem personalStemInstance = (PersonalStem) Class
.forName(personalStem).newInstance();
GrouperHelper.createIfAbsentPersonalStem(s,personalStemInstance);
}
} catch (Exception e) {
//e.printStackTrace();
}
initThread(session);
if(getAuthUser(session)!=null) session.setAttribute("sessionInited", Boolean.TRUE);
try{
session.setAttribute("fieldList",GrouperHelper.getFieldsAsMap());
}catch(Exception e) {
LOG.error("Error retrieving Fields: " + e.getMessage());
}
session.setAttribute("MembershipExporter",new MembershipExporter());
session.setAttribute("MembershipImportManager",new MembershipImportManager());
Document doc = null;
try {
doc = DOMHelper.getDomFromResourceOnClassPath("resources/grouper/ui-permissions.xml");
}catch(Exception e){
LOG.info("resources/grouper/ui-permissions.xml not found. Default permissions apply.");
}
if(doc==null) {
doc=DOMHelper.newDocument();
}
if(s==null) return;
UiPermissions uip = new UiPermissions(s,doc);
session.setAttribute("uiPermissions", uip);
Set menuFilters = new LinkedHashSet();
session.setAttribute("menuFilters",menuFilters);
String mFilters = null;
try {
mFilters=chainedMediaBundle.getString("menu.filters");
String[] parts = mFilters.split(" ");
Class claz;
MenuFilter filter;
for(int i=0;i