/**
* This software is based on OLAT, www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS,
* <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) JLS goodsolutions GmbH, Zurich, Switzerland.
* http://www.goodsolutions.ch <br>
* All rights reserved.
* <p>
*/
package org.olat.core.helpers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.olat.core.configuration.PersistedProperties;
import org.olat.core.configuration.PersistedPropertiesChangedEvent;
import org.olat.core.configuration.ServiceLifeCycle;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.control.Event;
import org.olat.core.util.StringHelper;
import org.olat.core.util.event.GenericEventListener;
/**
* Description:<br>
* generic settings related to the gui framework. see also coreconfig.xml for comments
* <P>
* Initial Date: 04.01.2007 <br>
*
* @author Felix Jost
*/
public class Settings implements ServiceLifeCycle, GenericEventListener {
private static boolean debug = false;
private static boolean reusableURLs = false;
private static boolean readOnlyDebug = false;
private static boolean ajaxGloballyOnBoolean = false;
private static int ajaxGloballyOn = 0;//0 -> off, 15 -> on;
private static String guiThemeIdentifyer = "default";
private static Map<String, String> serverconfig = null;
private static List<String> userAgentList;
private static String versionId = "";
private static boolean jUnitTest;
private static final String KEY_SERVER_MODJK_ENABLED = "server_modjk_enabled";
private static final String KEY_SERVER_CORE_JAR_NAME = "server_core_jar_name";
// the persited properties contain user configurable config data (overrides
// default values from spring config)
private static PersistedProperties userConfigurableProperties;
private static final String KEY_GUI_THEME_IDENTIFYER = "layout.theme";
/**
* @return
*/
public static boolean isDebuging() {
return debug;
}
/**
* @return if ajax mode is system-wide enabled or not
*/
public static boolean isAjaxGloballyOn() {
return ajaxGloballyOnBoolean;
}
public static boolean isReusableURLs() {
return reusableURLs;
}
/**
* [spring]
* @param reusableURLs
*/
public void setReusableURLs(boolean reusableURLs) {
Settings.reusableURLs = reusableURLs;
}
public static boolean isReadOnlyDebug() {
return readOnlyDebug;
}
/**
* [spring]
* @param readOnlyDebug
*/
public void setReadOnlyDebug(boolean readOnlyDebug) {
Settings.readOnlyDebug = readOnlyDebug;
}
/**
* affects only new usersessions
* [spring]
* @param
*/
public void setAjaxGloballyOn(int ajaxGloballyOnInt) {
Settings.ajaxGloballyOn = ajaxGloballyOnInt;
Settings.ajaxGloballyOnBoolean = (ajaxGloballyOnInt == 15);
}
/**
* for direct static access from code
* @param ajaxGloballyOn
*/
public static void setAjaxGloballyEnabled(boolean ajaxGloballyOn) {
Settings.ajaxGloballyOn = ajaxGloballyOn ? 15 : 0;
Settings.ajaxGloballyOnBoolean = ajaxGloballyOn;
}
/**
* @param userAgentList the userAgentList to set
*/
public void setUserAgentList(List<String> userAgentList) {
Settings.userAgentList = userAgentList;
}
/**
* checks against a list of browser defined in brasatoconfig.xml whether the browser is ready for
* olat ajax mode or not
* @param ureq
* @return
*/
public static boolean isBrowserOlatAjaxCertified(UserRequest ureq) {
String uag = ureq.getHttpReq().getHeader("user-agent");
if (uag == null) return false;
uag = uag.toLowerCase();
for (Iterator iter = userAgentList.iterator(); iter.hasNext();) {
String agent = (String) iter.next();
if (uag.contains(agent.toLowerCase())) return true;
}
return false;
}
/**
* returns an unmodifiable list of useragents
* @return
*/
public static List<String> getConfiguredUserAgents(){
return Collections.unmodifiableList(userAgentList);
}
public static void setUserAgents(List<String> changedCertifiedUserAgents){
userAgentList = new ArrayList<String>(changedCertifiedUserAgents);
}
/**
* @return the versionId, which is a short string consisting of a-z, 0-9 and _, used to prefix dynamically loaded resources to trick out the buggy cache of some browsers.
*/
public static String getVersionId() {
return Settings.versionId;
}
/**[spring]
* @param versionId the versionId to set
*/
public void setVersionId(String versionId) {
Settings.versionId = versionId;
}
/**
* key='server_name'
key='server_fqdn'
key='server_securePort'
key='server_insecurePort'
key='server_modjk_enabled'
key='server_core_jar_name'
key="serverContextPath"
* @return
*/
public static String getServerconfig(String key){
return Settings.serverconfig.get(key);
}
/**
* [spring]
* @param serverconfig
*/
public void setServerconfig(Map<String,String> serverconfig){
Settings.serverconfig = serverconfig;
}
/**
* [spring]
* @param debug
*/
public void setDebug(boolean debug) {
Settings.debug = debug;
}
/**
* @see org.olat.core.configuration.ServiceLifeCycle#initService()
*/
public void initService() {
// Initialize the user configuration and the spring default configuration
//
// Set the default theme configured in the spring configuration
userConfigurableProperties = new PersistedProperties(this);
userConfigurableProperties.setStringPropertyDefault(KEY_GUI_THEME_IDENTIFYER, guiThemeIdentifyer);
// Override gui theme with value from properties configuration
guiThemeIdentifyer = userConfigurableProperties.getStringPropertyValue(KEY_GUI_THEME_IDENTIFYER, false);
}
/**
* @see org.olat.core.configuration.ServiceLifeCycle#destroy()
*/
public void destroy() {
if (userConfigurableProperties != null) {
userConfigurableProperties.destroy();
userConfigurableProperties = null;
}
}
/**
* @return the CSS theme used for this webapp
*/
public static String getGuiThemeIdentifyer() {
return guiThemeIdentifyer;
}
/**
* Set the CSS theme used for this webapp. Only used by spring. Use static
* method to change the theme at runtime!
*
* @param guiTheme
*/
public void setGuiThemeIdentifyer(String guiThemeIdentifyer) {
Settings.guiThemeIdentifyer = guiThemeIdentifyer;
}
/**
* Set the CSS theme used for this webapp. The configuration is stored in
* the olatdata/system/configuration properties file and overrides the
* spring default configuration.
*
* @param newGuiThemeIdentifyer
*/
public static void setGuiThemeIdentifyerGlobally(String newGuiThemeIdentifyer) {
if (!guiThemeIdentifyer.equals(newGuiThemeIdentifyer)) {
// store new configuration and notify other nodes
userConfigurableProperties.setStringProperty(KEY_GUI_THEME_IDENTIFYER, newGuiThemeIdentifyer, true);
}
}
public void event(Event event) {
if (event instanceof PersistedPropertiesChangedEvent) {
// Override gui theme with value from properties configuration
guiThemeIdentifyer = userConfigurableProperties.getStringPropertyValue(KEY_GUI_THEME_IDENTIFYER, false);
}
}
/**
* check if mod jk is enabled
* @return
*/
public static boolean isModjkEnabled() {
return Settings.serverconfig.containsKey(KEY_SERVER_MODJK_ENABLED)
&& Settings.serverconfig.get(KEY_SERVER_MODJK_ENABLED).equalsIgnoreCase("true");
}
public static String getURIScheme() {
return (isSecurePortAvailable() ? "https:" : "http:");
}
private static boolean isSecurePortAvailable() {
return ! Settings.getServerconfig("server_securePort").equals("0");
}
public static String createServerURI() {
String uri;
String port;
if (isSecurePortAvailable()) {
port = Settings.getServerconfig("server_securePort");
uri = "https://" + Settings.getServerconfig("server_fqdn") + createURIPortPartWithDefaultPortCheck(port, 443);
} else {
port = Settings.getServerconfig("server_insecurePort");
uri = "http://" + Settings.getServerconfig("server_fqdn") + createURIPortPartWithDefaultPortCheck(port, 80);
}
return uri;
}
/**
* @param configuredPort comes from the spring configuration file, null is converted to ""
* @param defaultPort use 80 for http, 443 for https
* @return "" if no port is defined, or a default port. i.e. ":8080" if a port is configured and non-standard
*/
private static String createURIPortPartWithDefaultPortCheck(String configuredPort, int defaultPort){
if( ! StringHelper.containsNonWhitespace(configuredPort)){
return "";
}
int portFromConfig = Integer.valueOf(configuredPort);
if(portFromConfig == defaultPort){
return "";
}else{
return ":" + portFromConfig;
}
}
/**
*
* @return the full server path like https://www.olat.uzh.ch/olat
* depending on settings like secure/insecure, context path, ...
*/
public static String getServerContextPathURI() {
return createServerURI() + Settings.getServerconfig("serverContextPath");
}
/**
* @return True if this is a JUnit test.
*
* core usage: persistence.DB
*/
public static boolean isJUnitTest() {
return jUnitTest;
}
/**
* @param b
*/
public static void setJUnitTest(boolean b) {
jUnitTest = b;
}
}