Author: jafl
Date: Mon Jan 31 20:00:58 2011
New Revision: 1065735

URL: http://svn.apache.org/viewvc?rev=1065735&view=rev
Log:
WW-3546
provide API for overriding devMode per request, and check for override where 
appropriate

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java?rev=1065735&r1=1065734&r2=1065735&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
 Mon Jan 31 20:00:58 2011
@@ -725,7 +725,8 @@ public class Dispatcher {
      */
     public void sendError(HttpServletRequest request, HttpServletResponse 
response,
             ServletContext ctx, int code, Exception e) {
-        if (devMode) {
+        Boolean devModeOverride = FilterDispatcher.getDevModeOverride();
+        if (devModeOverride != null ? devModeOverride.booleanValue() : 
devMode) {
             response.setContentType("text/html");
 
             try {

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java?rev=1065735&r1=1065734&r2=1065735&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java
 Mon Jan 31 20:00:58 2011
@@ -170,11 +170,16 @@ public class FilterDispatcher implements
     protected Dispatcher dispatcher;
 
     /**
-     * Loads stattic resources, set by injection
+     * Loads static resources, set by injection.
      */
     protected StaticContentLoader staticResourceLoader;
 
     /**
+     * Maintains per-request override of devMode configuration.
+     */
+    private static ThreadLocal<Boolean> devModeOverride = new 
InheritableThreadLocal<Boolean>();
+
+    /**
      * Initializes the filter by creating a default dispatcher
      * and setting the default packages for static resources.
      *
@@ -238,6 +243,27 @@ public class FilterDispatcher implements
     }
 
     /**
+     * Set an override of the static devMode value.  Do not set this via a
+     * request parameter or any other unprotected method.  Using a signed
+     * cookie is one safe way to turn it on per request.
+     * 
+     * @param devMode   the override value
+     */
+    public static void overrideDevMode(
+        boolean devMode)
+    {
+        devModeOverride.set(Boolean.valueOf(devMode));
+    }
+
+    /**
+     * @return Boolean override value, or null if no override
+     */
+    public static Boolean getDevModeOverride()
+    {
+        return devModeOverride.get();
+    }
+
+    /**
      * Create a default {@link Dispatcher} that subclasses can override
      * with a custom Dispatcher, if needed.
      *
@@ -400,6 +426,7 @@ public class FilterDispatcher implements
             } finally {
                 UtilTimerStack.pop(timerKey);
             }
+            devModeOverride.remove();
         }
     }
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java?rev=1065735&r1=1065734&r2=1065735&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java
 Mon Jan 31 20:00:58 2011
@@ -40,6 +40,7 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.dispatcher.FilterDispatcher;
 import org.apache.struts2.views.freemarker.FreemarkerManager;
 import org.apache.struts2.views.freemarker.FreemarkerResult;
 
@@ -157,6 +158,8 @@ public class DebuggingInterceptor implem
     public String intercept(ActionInvocation inv) throws Exception {
         boolean actionOnly = false;
         boolean cont = true;
+        Boolean devModeOverride = FilterDispatcher.getDevModeOverride();
+        boolean devMode = devModeOverride != null ? 
devModeOverride.booleanValue() : this.devMode;
         if (devMode) {
             final ActionContext ctx = ActionContext.getContext();
             String type = getParameter(DEBUG_PARAM);

Modified: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java?rev=1065735&r1=1065734&r2=1065735&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
 (original)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
 Mon Jan 31 20:00:58 2011
@@ -34,6 +34,7 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.dispatcher.FilterDispatcher;
 import org.apache.struts2.json.annotations.SMDMethod;
 import org.apache.struts2.json.rpc.RPCError;
 import org.apache.struts2.json.rpc.RPCErrorCode;
@@ -126,7 +127,7 @@ public class JSONInterceptor extends Abs
                     } catch (Exception e) {
                         RPCResponse rpcResponse = new RPCResponse();
                         rpcResponse.setId(smd.get("id").toString());
-                        rpcResponse.setError(new RPCError(e, 
RPCErrorCode.EXCEPTION, debug));
+                        rpcResponse.setError(new RPCError(e, 
RPCErrorCode.EXCEPTION, getDebug()));
 
                         result = rpcResponse;
                     }
@@ -164,9 +165,8 @@ public class JSONInterceptor extends Abs
             return Action.NONE;
         } else {
             if (LOG.isDebugEnabled()) {
-                LOG
-                        .debug("Content type must be 'application/json' or 
'application/json-rpc'. Ignoring request with content type "
-                                + contentType);
+                LOG.debug("Content type must be 'application/json' or 
'application/json-rpc'. " +
+                          "Ignoring request with content type " + contentType);
             }
         }
 
@@ -367,7 +367,8 @@ public class JSONInterceptor extends Abs
      * @return true if debugging is turned on
      */
     public boolean getDebug() {
-        return this.debug;
+        Boolean devModeOverride = FilterDispatcher.getDevModeOverride();
+        return devModeOverride != null ? devModeOverride.booleanValue() : 
this.debug;
     }
 
     /**
@@ -380,6 +381,13 @@ public class JSONInterceptor extends Abs
         this.debug = debug;
     }
 
+    @Inject(StrutsConstants.STRUTS_DEVMODE)
+    public void setDevMode(
+        String mode)
+    {
+        setDebug("true".equalsIgnoreCase(mode));
+    }
+
     /**
      * Sets a comma-delimited list of regular expressions to match properties
      * that should be excluded from the JSON output.


Reply via email to