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 /** 030 * Enumerator for the dev mode value 031 */ 032 public enum DEVMODE 033 { 034 /** Super dev mode */ 035 SUPER_DEVELOPPMENT("super"), 036 /** dev mode */ 037 DEVELOPMENT("true"), 038 /** mode prod */ 039 PRODUCTION("false"); 040 041 private String _value; 042 043 private DEVMODE(String value) 044 { 045 this._value = value; 046 } 047 048 @Override 049 public String toString() 050 { 051 return _value; 052 } 053 054 /** 055 * Converts a string to a DEVMODE 056 * @param size The size to convert 057 * @return The size corresponding to the string or null if unknown 058 */ 059 public static DEVMODE createsFromString(String size) 060 { 061 for (DEVMODE v : DEVMODE.values()) 062 { 063 if (v.toString().equals(size)) 064 { 065 return v; 066 } 067 } 068 return null; 069 } 070 } 071 072 /** The session attribute name for storing the dev mode forced by the connected user. */ 073 public static final String SESSION_ATTRIBUTE_DEVMODE = DevMode.class.toString() + "$DevMode"; 074 075 /** The request parameter name for forcing the dev mode for the connected user. */ 076 public static final String REQUEST_PARAM_FORCE_DEVMODE = "debug.mode"; 077 078 private DevMode() 079 { 080 // static class 081 } 082 083 /** 084 * Check if the current config is in developer mode. 085 * @return True if the developer mode is activated. 086 */ 087 public static DEVMODE getDeveloperMode() 088 { 089 return getDeveloperMode(null); 090 } 091 092 /** 093 * Check if the current config is in developer mode for the current request. 094 * @param request The current request. Can be null. 095 * @return True if the developer mode is activated. 096 */ 097 public static DEVMODE getDeveloperMode(Request request) 098 { 099 if (request != null) 100 { 101 Session session = request.getSession(false); 102 103 String forceDevMode = request.getParameter(REQUEST_PARAM_FORCE_DEVMODE); 104 if (forceDevMode != null) 105 { 106 DEVMODE devMode = DEVMODE.createsFromString(forceDevMode); 107 if (devMode == null) 108 { 109 if (session != null) 110 { 111 // There is an empty request parameter, remove the dev mode for the current session. 112 session.removeAttribute(SESSION_ATTRIBUTE_DEVMODE); 113 } 114 } 115 else 116 { 117 // There is a request parameter, set the dev mode for the current session. 118 if (session != null) 119 { 120 session.setAttribute(SESSION_ATTRIBUTE_DEVMODE, devMode.toString()); 121 } 122 return devMode; 123 } 124 } 125 126 if (session != null) 127 { 128 // Check if there is already a dev mode set in the session. 129 String devMode = (String) session.getAttribute(SESSION_ATTRIBUTE_DEVMODE); 130 if (devMode != null) 131 { 132 return DEVMODE.createsFromString(devMode); 133 } 134 } 135 } 136 137 if (PluginsManager.getInstance().isSafeMode()) 138 { 139 return DEVMODE.DEVELOPMENT; 140 } 141 142 return Config.getInstance().getValue("runtime.mode.dev", true, false) ? DEVMODE.DEVELOPMENT : DEVMODE.PRODUCTION; 143 } 144}