Commits

Anonymous committed f019041

ported XW-462 ParamRemoverInterceptor

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

  • Participants
  • Parent commits 2f8d3c5

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork2/interceptor/ParameterRemoverInterceptor.java

+package com.opensymphony.xwork2.interceptor;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.util.TextParseUtil;
+
+/**
+ * <!-- START SNIPPET: description -->
+ * This is a simple XWork interceptor that allows parameters (matching
+ * one of the paramNames attribute csv value) to be 
+ * removed from the parameter map if they match a certain value
+ * (matching one of the paramValues attribute csv value), before they 
+ * are set on the action. A typical usage would be to want a dropdown/select 
+ * to map onto a boolean value on an action. The select had the options 
+ * none, yes and no with values -1, true and false. The true and false would 
+ * map across correctly. However the -1 would be set to false. 
+ * This was not desired as one might needed the value on the action to stay null. 
+ * This interceptor fixes this by preventing the parameter from ever reaching 
+ * the action.
+ * <!-- END SNIPPET: description -->
+ * 
+ * 
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * 	<li>paramNames - A comma separated value (csv) indicating the parameter name 
+ * 								    whose param value should be considered that if they match any of the
+ *                                     comma separated value (csv) from paramValues attribute, shall be 
+ *                                     removed from the parameter map such that they will not be applied
+ *                                     to the action</li>
+ * 	<li>paramValues - A comma separated value (csv) indicating the parameter value that if 
+ * 							      matched shall have its parameter be removed from the parameter map 
+ * 							      such that they will not be applied to the action</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ * 
+ * 
+ * <!-- START SNIPPET: extending -->
+ * No intended extension point
+ * <!-- END SNIPPET: extending -->
+ * 
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ *	
+ * &lt;action name="sample" class="org.martingilday.Sample"&gt;
+ * 	&lt;interceptor-ref name="paramRemover"&gt;
+ *   		&lt;param name="paramNames"&gt;aParam,anotherParam&lt;/param&gt;
+ *   		&lt;param name="paramValues"&gt;--,-1&lt;/param&gt;
+ * 	&lt;/interceptor-ref&gt;
+ * 	&lt;interceptor-ref name="default-stack" /&gt;
+ * 	...
+ * &lt;/action&gt;
+ *  
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *  
+ *  
+ * @author martin.gilday
+ * @version $Date$ $Id$
+ */
+public class ParameterRemoverInterceptor extends AbstractInterceptor {
+
+	private static final Log LOG = LogFactory.getLog(ParameterRemoverInterceptor.class);
+
+	private static final long serialVersionUID = 1;
+
+	private Set paramNames = Collections.EMPTY_SET;
+
+	private Set paramValues = Collections.EMPTY_SET;
+
+	
+	/**
+	 * Decide if the parameter should be removed from the parameter map based on
+	 * <code>paramNames</code> and <code>paramValues</code>.
+	 * 
+	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor
+	 */
+	@Override
+	public String intercept(ActionInvocation invocation) throws Exception {
+		if (!(invocation.getAction() instanceof NoParameters)
+				&& (null != this.paramNames)) {
+			ActionContext ac = invocation.getInvocationContext();
+			final Map parameters = ac.getParameters();
+
+			if (parameters != null) {
+				for (Iterator i = paramNames.iterator(); i.hasNext(); ) {
+					Object removeName = i.next();
+					
+					// see if the field is in the parameter map
+					if (parameters.containsKey(removeName)) {
+
+						try {
+							String[] values = (String[]) parameters
+									.get(removeName);
+							String value = values[0];
+							if (null != value && this.paramValues.contains(value)) {
+								parameters.remove(removeName);
+							}
+						} catch (Exception e) {
+							LOG.error("Failed to convert parameter to string", e);
+						}
+					}
+				}
+			}
+		}
+		return invocation.invoke();
+	}
+
+	/**
+	 * Allows <code>paramNames</code> attribute to be set as comma-separated-values (csv).
+	 * 
+	 * @param paramNames the paramNames to set
+	 */
+	public void setParamNames(String paramNames) {
+		this.paramNames = TextParseUtil.commaDelimitedStringToSet(paramNames);
+	}
+
+
+	/**
+	 * Allows <code>paramValues</code> attribute to be set as a comma-separated-values (csv).
+	 * 
+	 * @param paramValues the paramValues to set
+	 */
+	public void setParamValues(String paramValues) {
+		this.paramValues = TextParseUtil.commaDelimitedStringToSet(paramValues);
+	}
+}
+

File src/test/com/opensymphony/xwork2/interceptor/ParameterRemoverInterceptorTest.java

+package com.opensymphony.xwork2.interceptor;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.easymock.MockControl;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ActionSupport;
+
+import junit.framework.TestCase;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class ParameterRemoverInterceptorTest extends TestCase {
+
+	protected Map contextMap;
+	protected ActionContext context;
+	protected MockControl actionInvocationControl;
+	protected ActionInvocation actionInvocation;
+	
+	protected void setUp() throws Exception {
+		contextMap = new LinkedHashMap();
+		context = new ActionContext(contextMap);
+		
+		actionInvocationControl = MockControl.createControl(ActionInvocation.class);
+		actionInvocation = (ActionInvocation) actionInvocationControl.getMock();
+		actionInvocationControl.expectAndDefaultReturn(actionInvocation.getAction(),  new SampleAction());
+		actionInvocationControl.expectAndDefaultReturn(actionInvocation.getInvocationContext(), context);
+		actionInvocationControl.expectAndDefaultReturn(actionInvocation.invoke(), "success");
+	}
+	
+	public void testInterception1() throws Exception {
+		contextMap.put(ActionContext.PARAMETERS, new LinkedHashMap() {
+			private static final long serialVersionUID = 0L;
+			{
+				put("param1", new String[] { "paramValue1" });
+				put("param2", new String[] { "paramValue2" });
+				put("param3", new String[] { "paramValue3" });
+				put("param", new String[] { "paramValue" });
+			}
+		});
+		
+		actionInvocationControl.replay();
+		
+		ParameterRemoverInterceptor interceptor = new ParameterRemoverInterceptor();
+		interceptor.setParamNames("param1,param2");
+		interceptor.setParamValues("paramValue1,paramValue2");
+		interceptor.intercept(actionInvocation);
+		
+		Map params = (Map) contextMap.get(ActionContext.PARAMETERS);
+		assertEquals(params.size(), 2);
+		assertTrue(params.containsKey("param3"));
+		assertTrue(params.containsKey("param"));
+		assertEquals(((String[])params.get("param3"))[0], "paramValue3");
+		assertEquals(((String[])params.get("param"))[0], "paramValue");
+		
+		actionInvocationControl.verify();
+	}
+	
+	
+	public void testInterception2() throws Exception {
+		contextMap.put(ActionContext.PARAMETERS, new LinkedHashMap() {
+			private static final long serialVersionUID = 0L;
+			{
+				put("param1", new String[] { "paramValue2" });
+				put("param2", new String[] { "paramValue1" });
+			}
+		});
+		
+		actionInvocationControl.replay();
+		
+		ParameterRemoverInterceptor interceptor = new ParameterRemoverInterceptor();
+		interceptor.setParamNames("param1,param2");
+		interceptor.setParamValues("paramValue1,paramValue2");
+		interceptor.intercept(actionInvocation);
+		
+		Map params = (Map) contextMap.get(ActionContext.PARAMETERS);
+		assertEquals(params.size(), 0);
+		
+		actionInvocationControl.verify();
+	}
+	
+	
+	public void testInterception3() throws Exception {
+		contextMap.put(ActionContext.PARAMETERS, new LinkedHashMap() {
+			private static final long serialVersionUID = 0L;
+			{
+				put("param1", new String[] { "paramValueOne" });
+				put("param2", new String[] { "paramValueTwo" });
+			}
+		});
+		
+		actionInvocationControl.replay();
+		
+		ParameterRemoverInterceptor interceptor = new ParameterRemoverInterceptor();
+		interceptor.setParamNames("param1,param2");
+		interceptor.setParamValues("paramValue1,paramValue2");
+		interceptor.intercept(actionInvocation);
+		
+		Map params = (Map) contextMap.get(ActionContext.PARAMETERS);
+		assertEquals(params.size(), 2);
+		assertTrue(params.containsKey("param1"));
+		assertTrue(params.containsKey("param2"));
+		assertEquals(((String[])params.get("param1"))[0], "paramValueOne");
+		assertEquals(((String[])params.get("param2"))[0], "paramValueTwo");
+		
+		actionInvocationControl.verify();
+	}
+	
+	class SampleAction extends ActionSupport {
+		private static final long serialVersionUID = 7489487258845368260L;
+	}
+}