Commits

Anonymous committed b58adcb

Added ability to exclude parameters from being applied to the stack
WW-1551

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1288e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java

 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.*;
 
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
     private static final Log LOG = LogFactory.getLog(ParametersInterceptor.class);
 
     boolean ordered = false;
+    Set<Pattern> excludeParams = Collections.EMPTY_SET;
     static boolean devMode = false;
     
     @Inject("devMode")
 
     protected boolean acceptableName(String name) {
         if (name.indexOf('=') != -1 || name.indexOf(',') != -1 || name.indexOf('#') != -1
-                || name.indexOf(':') != -1) {
+                || name.indexOf(':') != -1 || isExcluded(name)) {
             return false;
         } else {
             return true;
         }
     }
+    
+    protected boolean isExcluded(String paramName) {
+        if (!this.excludeParams.isEmpty()) {
+            for (Pattern pattern : excludeParams) {
+                Matcher matcher = pattern.matcher(paramName);
+                if (matcher.matches()) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
     /**
      * Whether to order the parameters or not
     public void setOrdered(boolean ordered) {
         this.ordered = ordered;
     }
+    
+    /**
+     * Gets a set of regular expressions of parameters to remove
+     * from the parameter map
+     * 
+     * @return A set of compiled regular expression patterns
+     */
+    protected Set getExcludeParamsSet() {
+        return excludeParams;
+    }
+
+    /**
+     * Sets a comma-delimited list of regular expressions to match 
+     * parameters that should be removed from the parameter map.
+     * 
+     * @param commaDelim A comma-delimited list of regular expressions
+     */
+    public void setExcludeParams(String commaDelim) {
+        Collection<String> excludePatterns = asCollection(commaDelim);
+        if (excludePatterns != null) {
+            excludeParams = new HashSet<Pattern>();
+            for (String pattern : excludePatterns) {
+                excludeParams.add(Pattern.compile(pattern));
+            }
+        }
+    }
+
+    /**
+     * Return a collection from the comma delimited String.
+     *
+     * @param commaDelim
+     * @return A collection from the comma delimited String.
+     */
+    private Collection asCollection(String commaDelim) {
+        if (commaDelim == null || commaDelim.trim().length() == 0) {
+            return null;
+        }
+        return TextParseUtil.commaDelimitedStringToSet(commaDelim);
+    }
 }

src/test/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java

         interceptor.intercept(mai);
         interceptor.destroy();
     }
+    
+    public void testExcludedParametersAreIgnored() throws Exception {
+        ParametersInterceptor pi = new ParametersInterceptor();
+        pi.setExcludeParams("dojo\\..*");
+        final Map actual = new HashMap();
+        ValueStack stack = new OgnlValueStack() {
+            public void setValue(String expr, Object value) {
+                actual.put(expr, value);
+            }
+        };
+        final Map expected = new HashMap() {
+            {
+                put("fooKey", "fooValue");
+            }
+        };
+
+        Map parameters = new HashMap() {
+            {
+                put("dojo.test", "dojoValue");
+                put("fooKey", "fooValue");
+            }
+        };
+        pi.setParameters(new NoParametersAction(), stack, parameters);
+        assertEquals(expected, actual);
+    }
+
 
     private class NoParametersAction implements Action, NoParameters {