mirror of
https://github.com/apache/struts.git
synced 2026-08-07 23:57:03 +00:00
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
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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<Boolean> devModeOverride = new InheritableThreadLocal<Boolean>();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user