Commits

Anonymous committed cf99f2a

Issue number:
Obtained from:
Submitted by:
Reviewed by:
CVS: ----------------------------------------------------------------------
CVS: Issue number:
CVS: If this change addresses one or more issues,
CVS: then enter the issue number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to the project by someone else; i.e.,
CVS: they sent us a patch or a set of diffs, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.

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

  • Participants
  • Parent commits 1fa9f0f

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/interceptor/ParameterFilterInterceptor.java

+/*
+ * Created on Dec 12, 2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package com.opensymphony.xwork.interceptor;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeMap;
+
+import com.opensymphony.xwork.ActionInvocation;
+
+/**
+ * @author Gabe
+ *
+ * Filters parameters
+ */
+public class ParameterFilterInterceptor implements Interceptor {
+
+    private Collection allowed;
+    
+    private Collection blocked;
+    
+    private TreeMap includesExcludesMap;
+    
+    private boolean defaultBlock=false;
+    
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.interceptor.Interceptor#destroy()
+     */
+    public void destroy() {
+        // TODO Auto-generated method stub
+
+    }
+
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.interceptor.Interceptor#init()
+     */
+    public void init() {
+        // TODO Auto-generated method stub
+
+    }
+
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
+     */
+    public String intercept(ActionInvocation invocation) throws Exception {
+        
+        Map parameters=invocation.getInvocationContext().getParameters();
+        HashSet paramsToRemove=new HashSet();
+        
+        TreeMap includesExcludesMap=getIncludesExcludesMap();
+        
+        for (Iterator i=parameters.keySet().iterator();i.hasNext(); ) {
+            
+            String param=(String)i.next();
+            
+            boolean currentAllowed=!isDefaultBlock();
+            
+            boolean foundApplicableRule=false;
+            for (Iterator j=includesExcludesMap.keySet().iterator(); j.hasNext(); ) {
+                String currRule=(String)j.next();
+                
+                if (param.startsWith(currRule)
+                        && (param.length()==currRule.length()
+                                || isPropSeperator(param.charAt(currRule.length())))) {
+                    currentAllowed=((Boolean)includesExcludesMap.get(currRule)).booleanValue();
+                }	else {
+                    if (foundApplicableRule) {
+                        foundApplicableRule=false;
+                        break;
+                    }
+                }
+            }
+            if (!currentAllowed) {
+                paramsToRemove.add(param);
+            }
+        }
+        for (Iterator i=paramsToRemove.iterator(); i.hasNext();) {
+            parameters.remove(i.next()); 
+        }
+        
+        return invocation.invoke();
+    }
+    
+    /**
+     * @param c
+     * @return
+     */
+    private boolean isPropSeperator(char c) {
+        // TODO Auto-generated method stub
+        return c=='.' || c=='(' || c=='[';
+    }
+
+    private TreeMap getIncludesExcludesMap() {
+        if (this.includesExcludesMap==null) {
+            this.includesExcludesMap=new TreeMap();
+            
+            if (getAllowed()!=null) {
+	            for (Iterator i=getAllowed().iterator();i.hasNext(); ) {
+	                this.includesExcludesMap.put(i.next(), Boolean.TRUE);
+	            }
+            }
+            if (getBlocked()!=null) {
+	            for (Iterator i=getBlocked().iterator();i.hasNext(); ) {
+	                this.includesExcludesMap.put(i.next(), Boolean.FALSE);
+	            }  
+            }
+        }
+        
+        return this.includesExcludesMap;
+    }
+    
+    /**
+     * @return Returns the defaultBlock.
+     */
+    public boolean isDefaultBlock() {
+        return defaultBlock;
+    }
+    /**
+     * @param defaultBlock The defaultBlock to set.
+     */
+    public void setDefaultBlock(boolean defaultExclude) {
+        this.defaultBlock = defaultExclude;
+    }
+    /**
+     * @return Returns the blocked.
+     */
+    public Collection getBlocked() {
+        return blocked;
+    }
+    /**
+     * @param blocked The blocked to set.
+     */
+    public void setBlocked(Collection blocked) {
+        this.blocked = blocked;
+    }
+    
+    public void setBlocked(String blocked) {
+        setBlocked(asCollection(blocked));
+    }
+    /**
+     * @return Returns the allowed.
+     */
+    public Collection getAllowed() {
+        return allowed;
+    }
+    /**
+     * @param allowed The allowed to set.
+     */
+    public void setAllowed(Collection allowed) {
+        this.allowed = allowed;
+    }
+    
+    public void setAllowed(String allowed) {
+        
+        setAllowed(asCollection(allowed));
+    }
+    
+    private Collection asCollection(String commaDelim) {
+        if (commaDelim==null || commaDelim.trim().length()==0) {
+            return null;
+        }
+        String [] splitString=commaDelim.split(",");
+        
+        HashSet set=new HashSet();
+        for (int i=0; i<splitString.length;i++) {
+            set.add(splitString[i].trim());
+        }
+        return set;
+    }
+}

File src/test/com/opensymphony/xwork/interceptor/ParameterFilterInterceptorTest.java

+/*
+ * Created on Dec 24, 2005
+ */
+package com.opensymphony.xwork.interceptor;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.mockobjects.dynamic.Mock;
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.SimpleAction;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Gabe
+ *
+ */
+public class ParameterFilterInterceptorTest extends TestCase {
+
+    ActionInvocation invocation;
+    ParameterFilterInterceptor interceptor;
+    Mock mockInvocation;
+    OgnlValueStack stack;
+    Map contextMap;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        contextMap=new HashMap();
+        stack = new OgnlValueStack();
+        mockInvocation = new Mock(ActionInvocation.class);
+        mockInvocation.expectAndReturn("getStack", stack);
+        mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
+        mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap));
+        mockInvocation.matchAndReturn("getAction", new SimpleAction());
+        invocation = (ActionInvocation) mockInvocation.proxy();
+        interceptor = new ParameterFilterInterceptor();
+    }
+    
+    public void testBasicBlockAll() throws Exception {
+        runFilterTest(null,null,true,new String[] {"blah", "bladeblah", "bladebladeblah"});
+        assertEquals(0, getParameterNames().size()); 
+    }
+    
+    public void testBasicAllowed() throws Exception {
+        runFilterTest("blah",null,true,new String[] {"blah"});
+        assertEquals(1, getParameterNames().size()); 
+    }
+    
+    public void testBasicBlocked() throws Exception {
+        runFilterTest(null,"blah",false,new String[] {"blah"});
+        assertEquals(0, getParameterNames().size()); 
+    }      
+    public void testAllSubpropertiesBlocked() throws Exception {
+        runFilterTest(null,"blah",false,new String[] {"blah.deblah", "blah.somethingelse", "blah(22)"});
+        assertEquals(0, getParameterNames().size()); 
+    }
+
+    public void testAllSubpropertiesAllowed() throws Exception {
+        runFilterTest("blah",null,true,
+                new String[] {"blah.deblah", "blah.somethingelse", "blah(22)"});
+        assertEquals(3, getParameterNames().size()); 
+    }
+    
+    public void testTreeBlocking() throws Exception {
+        runFilterTest("blah.deblah","blah,blah.deblah.deblah",false,
+                new String[] {"blah", "blah.deblah", "blah.deblah.deblah"});
+        Collection paramNames=getParameterNames();
+        assertEquals(1, paramNames.size());
+        assertEquals(paramNames.iterator().next(),"blah.deblah");
+    }
+    
+    public void testEnsureOnlyPropsBlocked() throws Exception {
+        runFilterTest(null,"blah",false,new String[] {"blahdeblah"});
+        assertEquals(1, getParameterNames().size()); 
+    }
+  
+    
+    private void runFilterTest(String allowed, String blocked, boolean defaultBlocked, String[] paramNames) throws Exception {
+        interceptor.setAllowed(allowed);
+        interceptor.setBlocked(blocked);
+        interceptor.setDefaultBlock(defaultBlocked);
+        setUpParameters(paramNames);
+        runAction();
+        
+    }
+    
+    private void setUpParameters(String [] paramNames) {
+        Map params=new HashMap();
+        for (int i=0;i<paramNames.length;i++){
+            params.put(paramNames[i], "irrelevant what this is");
+            
+        }
+        contextMap.put(ActionContext.PARAMETERS, params);
+    }
+    
+    private Collection getParameterNames() {
+        
+        return ((Map)contextMap.get(ActionContext.PARAMETERS)).keySet();
+    }
+    
+    public void runAction() throws Exception  {
+        interceptor.intercept(invocation);
+        
+    }
+    
+}

File src/test/com/opensymphony/xwork/util/MockObjectTypeDeterminer.java

+/*
+ * Created on Dec 12, 2005
+ *
+ */
+package com.opensymphony.xwork.util;
+
+/**
+ * @author Gabe
+ *
+ * Mocks the function of an ObjectTypeDeterminer for testing purposes
+ */
+public class MockObjectTypeDeterminer implements ObjectTypeDeterminer {
+
+    private Class keyClass;
+    private Class elementClass;
+    private String keyProperty;
+    private boolean shouldCreateIfNew;
+    
+    public MockObjectTypeDeterminer() {}
+    
+    
+    /**
+     * @param keyClass
+     * @param elementClass
+     * @param keyProperty
+     * @param shouldCreateIfNew
+     */
+    public MockObjectTypeDeterminer(Class keyClass, Class elementClass,
+            String keyProperty, boolean shouldCreateIfNew) {
+        super();
+        this.keyClass = keyClass;
+        this.elementClass = elementClass;
+        this.keyProperty = keyProperty;
+        this.shouldCreateIfNew = shouldCreateIfNew;
+    }
+    
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.util.ObjectTypeDeterminer#getKeyClass(java.lang.Class, java.lang.String)
+     */
+    public Class getKeyClass(Class parentClass, String property) {
+        
+        return getKeyClass();
+    }
+
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.util.ObjectTypeDeterminer#getElementClass(java.lang.Class, java.lang.String, java.lang.Object)
+     */
+    public Class getElementClass(Class parentClass, String property, Object key) {
+        
+        return getElementClass();
+    }
+
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.util.ObjectTypeDeterminer#getKeyProperty(java.lang.Class, java.lang.String)
+     */
+    public String getKeyProperty(Class parentClass, String property) {
+        
+        return getKeyProperty();
+    }
+
+    /* (non-Javadoc)
+     * @see com.opensymphony.xwork.util.ObjectTypeDeterminer#shouldCreateIfNew(java.lang.Class, java.lang.String, java.lang.Object, java.lang.String, boolean)
+     */
+    public boolean shouldCreateIfNew(Class parentClass, String property,
+            Object target, String keyProperty, boolean isIndexAccessed) {
+        
+        return isShouldCreateIfNew();
+    }
+
+    /**
+     * @return Returns the elementClass.
+     */
+    public Class getElementClass() {
+        return elementClass;
+    }
+    /**
+     * @param elementClass The elementClass to set.
+     */
+    public void setElementClass(Class elementClass) {
+        this.elementClass = elementClass;
+    }
+    /**
+     * @return Returns the keyClass.
+     */
+    public Class getKeyClass() {
+        return keyClass;
+    }
+    /**
+     * @param keyClass The keyClass to set.
+     */
+    public void setKeyClass(Class keyClass) {
+        this.keyClass = keyClass;
+    }
+    /**
+     * @return Returns the keyProperty.
+     */
+    public String getKeyProperty() {
+        return keyProperty;
+    }
+    /**
+     * @param keyProperty The keyProperty to set.
+     */
+    public void setKeyProperty(String keyProperty) {
+        this.keyProperty = keyProperty;
+    }
+    /**
+     * @return Returns the shouldCreateIfNew.
+     */
+    public boolean isShouldCreateIfNew() {
+        return shouldCreateIfNew;
+    }
+    /**
+     * @param shouldCreateIfNew The shouldCreateIfNew to set.
+     */
+    public void setShouldCreateIfNew(boolean shouldCreateIfNew) {
+        this.shouldCreateIfNew = shouldCreateIfNew;
+    }
+}