001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core;
017
018import org.apache.cocoon.environment.Request;
019import org.apache.cocoon.environment.Session;
020
021import org.ametys.runtime.config.Config;
022import org.ametys.runtime.plugin.PluginsManager;
023
024/**
025 * Static class for retrieving and setting the development mode of the application.
026 */
027public final class DevMode
028{
029    /** The session attribute name for storing the dev mode forced by the connected user. */
030    public static final String SESSION_ATTRIBUTE_DEVMODE = DevMode.class.toString() + "$DevMode";
031
032    /** The request parameter name for forcing the dev mode for the connected user. */
033    public static final String REQUEST_PARAM_FORCE_DEVMODE = "debug.mode";
034
035    private DevMode()
036    {
037        // static class
038    }
039    
040    /**
041     * Check if the current config is in developer mode.
042     * @return True if the developer mode is activated.
043     */
044    public static boolean isDeveloperMode()
045    {
046        return isDeveloperMode(null);
047    }
048    
049    /**
050     * Check if the current config is in developer mode for the current request.
051     * @param request The current request. Can be null.
052     * @return True if the developer mode is activated.
053     */
054    public static boolean isDeveloperMode(Request request)
055    {
056        if (request != null)
057        {
058            Session session = request.getSession(false);
059
060            String forceDevMode = request.getParameter(REQUEST_PARAM_FORCE_DEVMODE);
061            if (forceDevMode != null)
062            {
063                // There is a request parameter, set the dev mode for the current session.
064                boolean devMode = Boolean.valueOf(forceDevMode);
065                if (session != null)
066                {
067                    session.setAttribute(SESSION_ATTRIBUTE_DEVMODE, devMode);
068                }
069                
070                return devMode;
071            }
072            
073            if (session != null)
074            {
075                // Check if there is already a dev mode set in the session.
076                Boolean devMode = (Boolean) session.getAttribute(SESSION_ATTRIBUTE_DEVMODE);
077                if (devMode != null)
078                {
079                    return devMode;
080                }
081            }
082        }
083        
084        if (PluginsManager.getInstance().isSafeMode())
085        {
086            return true;
087        }
088        
089        Boolean devMode = Config.getInstance().getValueAsBoolean("runtime.mode.dev");
090        return devMode != null ? devMode : false;
091    }
092}