WW-3514 remove JSONUtil.PATTERN_PREFIX; add JSONInterceptor.setExcludeWildcards()

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1070550 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Lindal
2011-02-14 16:18:28 +00:00
parent c5ead127ea
commit a98be307b4
4 changed files with 44 additions and 30 deletions
@@ -189,8 +189,8 @@ public class JSONInterceptor extends AbstractInterceptor {
// could be a numeric value
response.setId(id.toString());
// the map is going to have: 'params', 'method' and 'id' (what is the id
// for?)
// the map is going to have: 'params', 'method' and 'id' (for the
// client to identify the response)
Class clazz = object.getClass();
// parameters
@@ -404,6 +404,23 @@ public class JSONInterceptor extends AbstractInterceptor {
}
}
/**
* Sets a comma-delimited list of wildcard expressions to match
* properties that should be excluded from the JSON output.
*
* @param commaDelim
* A comma-delimited list of wildcard expressions
*/
public void setExcludeWildcards(String commaDelim) {
Set<String> excludePatterns = JSONUtil.asSet(commaDelim);
if (excludePatterns != null) {
this.excludeProperties = new ArrayList<Pattern>(excludePatterns.size());
for (String pattern : excludePatterns) {
this.excludeProperties.add(WildcardUtil.compileWildcardPattern(pattern));
}
}
}
/**
* Sets a comma-delimited list of regular expressions to match properties
* that should be included from the JSON output.
@@ -412,24 +429,21 @@ public class JSONInterceptor extends AbstractInterceptor {
* A comma-delimited list of regular expressions
*/
public void setIncludeProperties(String commaDelim) {
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.REGEXP_PATTERN, JSONUtil.getIncludePatternData());
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.REGEXP_PATTERN);
}
/**
* Sets a comma-delimited list of wildcard expressions to match
* properties that should be included from the JSON output. Since the
* patterns are only used for the JSON-RPC response, you only need to
* specify the elements inside your result object (and "result." is
* automatically prepended).
* properties that should be included from the JSON output. The
* standard boilerplate (id, error, debug) are automatically included,
* as appropriate, so you only need to provide patterns for the
* contents of "result".
*
* @param commaDelim
* A comma-delimited list of regular expressions
* A comma-delimited list of wildcard expressions
*/
public void setIncludeWildcards(String commaDelim) {
Map<String, Map<String, String>> includePatternData = JSONUtil.getIncludePatternData();
includePatternData.get(JSONUtil.PATTERN_PREFIX).put(JSONUtil.WILDCARD_PATTERN, "result.");
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.WILDCARD_PATTERN, includePatternData);
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.WILDCARD_PATTERN);
if (includeProperties != null) {
includeProperties.add(Pattern.compile("id"));
includeProperties.add(Pattern.compile("result"));
@@ -439,16 +453,17 @@ public class JSONInterceptor extends AbstractInterceptor {
}
/**
* Returns the appropriate set of includes.
* Returns the appropriate set of includes, based on debug setting.
* Derived classes can override if there are additional, custom
* debug-only parameters.
*/
private List getIncludeProperties()
{
protected List getIncludeProperties() {
if (includeProperties != null && getDebug()) {
List<Pattern> list = new ArrayList<Pattern>(includeProperties);
list.add(Pattern.compile("debug"));
list.add(WildcardUtil.compileWildcardPattern("error.*"));
return list;
}
else {
} else {
return includeProperties;
}
}
@@ -148,7 +148,7 @@ public class JSONResult implements Result {
* @param commaDelim A comma-delimited list of regular expressions
*/
public void setIncludeProperties(String commaDelim) {
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.REGEXP_PATTERN, JSONUtil.getIncludePatternData());
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.REGEXP_PATTERN);
}
/**
@@ -158,7 +158,7 @@ public class JSONResult implements Result {
* @param commaDelim A comma-delimited list of wildcard patterns
*/
public void setIncludeWildcards(String commaDelim) {
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.WILDCARD_PATTERN, JSONUtil.getIncludePatternData());
includeProperties = JSONUtil.processIncludePatterns(JSONUtil.asSet(commaDelim), JSONUtil.WILDCARD_PATTERN);
}
public void execute(ActionInvocation invocation) throws Exception {
@@ -390,13 +390,12 @@ public class JSONUtil {
return (header != null) && (header.indexOf("gzip") >= 0);
}
/* package */ static final String REGEXP_PATTERN = "regexp";
/* package */ static final String WILDCARD_PATTERN = "wildcard";
public static final String REGEXP_PATTERN = "regexp";
public static final String WILDCARD_PATTERN = "wildcard";
/* package */ static final String SPLIT_PATTERN = "split";
/* package */ static final String JOIN_STRING = "join";
/* package */ static final String ARRAY_BEGIN_STRING = "array-begin";
/* package */ static final String ARRAY_END_STRING = "array-end";
/* package */ static final String PATTERN_PREFIX = "pattern-prefix";
/* package */ static Map<String, Map<String, String>> getIncludePatternData()
{
@@ -422,14 +421,15 @@ public class JSONUtil {
data.put(WILDCARD_PATTERN, "]");
includePatternData.put(ARRAY_END_STRING, data);
data = new HashMap<String, String>();
data.put(REGEXP_PATTERN, "");
data.put(WILDCARD_PATTERN, "");
includePatternData.put(PATTERN_PREFIX, data);
return includePatternData;
}
private static final Map<String, Map<String, String>> defaultIncludePatternData = getIncludePatternData();
public static List<Pattern> processIncludePatterns(Set<String> includePatterns, String type) {
return processIncludePatterns(includePatterns, type, defaultIncludePatternData);
}
/* package */ static List<Pattern> processIncludePatterns(Set<String> includePatterns, String type, Map<String, Map<String, String>> includePatternData) {
if (includePatterns != null) {
List<Pattern> results = new ArrayList<Pattern>(includePatterns.size());
@@ -479,7 +479,6 @@ public class JSONUtil {
}
private static void addPattern(List<Pattern> results, String pattern, String type, Map<String, Map<String, String>> includePatternData) {
pattern = includePatternData.get(PATTERN_PREFIX).get(type) + pattern;
results.add(
type == REGEXP_PATTERN ?
Pattern.compile(pattern) :
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="json-default" extends="struts-default">