From 94629009bbffd2f9a756faea001afcbe05036997 Mon Sep 17 00:00:00 2001 From: John Lindal Date: Mon, 31 Jan 2011 20:00:58 +0000 Subject: [PATCH] WW-3546 provide API for overriding devMode per request, and check for override where appropriate git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1065735 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/struts2/dispatcher/Dispatcher.java | 3 +- .../struts2/dispatcher/FilterDispatcher.java | 29 ++++++++++++++++++- .../debugging/DebuggingInterceptor.java | 3 ++ .../apache/struts2/json/JSONInterceptor.java | 18 ++++++++---- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index a260feb8e..fa05ce96f 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -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 { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java index e267a9aa4..858a2f363 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java @@ -170,10 +170,15 @@ public class FilterDispatcher implements StrutsStatics, Filter { 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 devModeOverride = new InheritableThreadLocal(); + /** * Initializes the filter by creating a default dispatcher * and setting the default packages for static resources. @@ -237,6 +242,27 @@ public class FilterDispatcher implements StrutsStatics, Filter { } } + /** + * 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 StrutsStatics, Filter { } finally { UtilTimerStack.pop(timerKey); } + devModeOverride.remove(); } } } diff --git a/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java index dc5532d92..729ff9cd2 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/debugging/DebuggingInterceptor.java @@ -40,6 +40,7 @@ import javax.servlet.http.HttpServletResponse; 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 implements Interceptor { 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); diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java index 09d2fa193..aa713d840 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java @@ -34,6 +34,7 @@ import javax.servlet.http.HttpServletResponse; 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 AbstractInterceptor { } 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 AbstractInterceptor { 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 AbstractInterceptor { * @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 AbstractInterceptor { 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.