Author: jafl Date: Mon Jan 31 19:16:35 2011 New Revision: 1065716 URL: http://svn.apache.org/viewvc?rev=1065716&view=rev Log: WW-3500 If top of stack is not the action, assume it is the model and check for annotiations.
Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java (with props) struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java (with props) Modified: struts/struts2/trunk/xwork-core/ (props changed) struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterIntereptor.java struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterUnitTest.java Propchange: struts/struts2/trunk/xwork-core/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Mon Jan 31 19:16:35 2011 @@ -5,6 +5,7 @@ cobertura.ser *.iml .project target +test-output ivyrep.properties build .settings Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterIntereptor.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterIntereptor.java?rev=1065716&r1=1065715&r2=1065716&view=diff ============================================================================== --- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterIntereptor.java (original) +++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterIntereptor.java Mon Jan 31 19:16:35 2011 @@ -37,12 +37,20 @@ public class AnnotationParameterFilterIn final Object action = invocation.getAction(); Map<String, Object> parameters = invocation.getInvocationContext().getParameters(); + Object model = invocation.getStack().peek(); + if (model == action) { + model = null; + } + boolean blockByDefault = action.getClass().isAnnotationPresent(BlockByDefault.class); List<Field> annotatedFields = new ArrayList<Field>(); HashSet<String> paramsToRemove = new HashSet<String>(); if (blockByDefault) { AnnotationUtils.addAllFields(Allowed.class, action.getClass(), annotatedFields); + if (model != null) { + AnnotationUtils.addAllFields(Allowed.class, model.getClass(), annotatedFields); + } for (String paramName : parameters.keySet()) { boolean allowed = false; @@ -61,6 +69,9 @@ public class AnnotationParameterFilterIn } } else { AnnotationUtils.addAllFields(Blocked.class, action.getClass(), annotatedFields); + if (model != null) { + AnnotationUtils.addAllFields(Blocked.class, model.getClass(), annotatedFields); + } for (String paramName : parameters.keySet()) { Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java?rev=1065716&view=auto ============================================================================== --- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java (added) +++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java Mon Jan 31 19:16:35 2011 @@ -0,0 +1,23 @@ +package com.opensymphony.xwork2.interceptor.annotations; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * @author jafl + * + */ +public class AllowingByDefaultModel { + + @Blocked + private String m1; + private String m2; + + public void setM1(String s) { + m1 = s; + } + + public void setM2(String s) { + m2 = s; + } + +} Propchange: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AllowingByDefaultModel.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterUnitTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterUnitTest.java?rev=1065716&r1=1065715&r2=1065716&view=diff ============================================================================== --- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterUnitTest.java (original) +++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationParameterFilterUnitTest.java Mon Jan 31 19:16:35 2011 @@ -4,6 +4,8 @@ import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.StubValueStack; +import com.opensymphony.xwork2.util.ValueStack; import junit.framework.TestCase; import java.util.HashMap; @@ -11,10 +13,19 @@ import java.util.Map; /** * @author martin.gilday + * @author jafl * */ public class AnnotationParameterFilterUnitTest extends TestCase { + ValueStack stack; + + @Override + protected void setUp() throws Exception { + super.setUp(); + stack = new StubValueStack(); + } + /** * Only "name" should remain in the parameter map. All others * should be removed @@ -30,11 +41,15 @@ public class AnnotationParameterFilterUn contextMap.put(ActionContext.PARAMETERS, parameterMap); + Action action = new BlockingByDefaultAction(); + stack.push(action); + Mock mockInvocation = new Mock(ActionInvocation.class); mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap)); - mockInvocation.matchAndReturn("getAction", new BlockingByDefaultAction()); - mockInvocation.expectAndReturn("invoke", Action.SUCCESS); - + mockInvocation.matchAndReturn("getAction", action); + mockInvocation.matchAndReturn("getStack", stack); + mockInvocation.expectAndReturn("invoke", Action.SUCCESS); + ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy(); AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor(); @@ -61,11 +76,15 @@ public class AnnotationParameterFilterUn contextMap.put(ActionContext.PARAMETERS, parameterMap); + Action action = new AllowingByDefaultAction(); + stack.push(action); + Mock mockInvocation = new Mock(ActionInvocation.class); mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap)); - mockInvocation.matchAndReturn("getAction", new AllowingByDefaultAction()); - mockInvocation.expectAndReturn("invoke", Action.SUCCESS); - + mockInvocation.matchAndReturn("getAction", action); + mockInvocation.matchAndReturn("getStack", stack); + mockInvocation.expectAndReturn("invoke", Action.SUCCESS); + ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy(); AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor(); @@ -76,5 +95,79 @@ public class AnnotationParameterFilterUn assertNull(parameterMap.get("name")); } + + /** + * Only "name" should remain in the parameter map. All others + * should be removed + * @throws Exception + */ + public void testBlockingByDefaultWithModel() throws Exception { + + Map contextMap = new HashMap(); + Map parameterMap = new HashMap(); + + parameterMap.put("job", "Baker"); + parameterMap.put("name", "Martin"); + parameterMap.put("m1", "s1"); + parameterMap.put("m2", "s2"); + + contextMap.put(ActionContext.PARAMETERS, parameterMap); + stack.push(new BlockingByDefaultModel()); + + Mock mockInvocation = new Mock(ActionInvocation.class); + mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap)); + mockInvocation.matchAndReturn("getAction", new BlockingByDefaultAction()); + mockInvocation.matchAndReturn("getStack", stack); + mockInvocation.expectAndReturn("invoke", Action.SUCCESS); + + ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy(); + + AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor(); + intereptor.intercept(invocation); + + assertEquals("Paramter map should contain two entries", 2, parameterMap.size()); + assertNull(parameterMap.get("job")); + assertNotNull(parameterMap.get("name")); + assertNotNull(parameterMap.get("m1")); + assertNull(parameterMap.get("m2")); + + } + + /** + * "name" should be removed from the map, as it is blocked. + * All other parameters should remain + * @throws Exception + */ + public void testAllowingByDefaultWithModel() throws Exception { + + Map contextMap = new HashMap(); + Map parameterMap = new HashMap(); + + parameterMap.put("job", "Baker"); + parameterMap.put("name", "Martin"); + parameterMap.put("m1", "s1"); + parameterMap.put("m2", "s2"); + + contextMap.put(ActionContext.PARAMETERS, parameterMap); + stack.push(new AllowingByDefaultModel()); + + Mock mockInvocation = new Mock(ActionInvocation.class); + mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(contextMap)); + mockInvocation.matchAndReturn("getAction", new AllowingByDefaultAction()); + mockInvocation.matchAndReturn("getStack", stack); + mockInvocation.expectAndReturn("invoke", Action.SUCCESS); + + ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy(); + + AnnotationParameterFilterIntereptor intereptor = new AnnotationParameterFilterIntereptor(); + intereptor.intercept(invocation); + + assertEquals("Paramter map should contain two entries", 2, parameterMap.size()); + assertNotNull(parameterMap.get("job")); + assertNull(parameterMap.get("name")); + assertNull(parameterMap.get("m1")); + assertNotNull(parameterMap.get("m2")); + + } } Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java?rev=1065716&view=auto ============================================================================== --- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java (added) +++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java Mon Jan 31 19:16:35 2011 @@ -0,0 +1,22 @@ +package com.opensymphony.xwork2.interceptor.annotations; + +/** + * @author jafl + * + */ +@BlockByDefault +public class BlockingByDefaultModel { + + @Allowed + private String m1; + private String m2; + + public void setM1(String s) { + m1 = s; + } + + public void setM2(String s) { + m2 = s; + } + +} Propchange: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/annotations/BlockingByDefaultModel.java ------------------------------------------------------------------------------ svn:eol-style = native