All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.interceptor.ParameterRemoverInterceptor Maven / Gradle / Ivy

Go to download

XWork is an command-pattern framework that is used to power WebWork as well as other applications. XWork provides an Inversion of Control container, a powerful expression language, data type conversion, validation, and pluggable configuration.

There is a newer version: 2.1.3
Show newest version
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;

/**
 * 
 * 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.
 * 
 * 
 * 
 * 
 * 
    *
  • 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
  • *
  • 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
  • *
* * * * * No intended extension point * * *
 * 
 *	
 * <action name="sample" class="org.martingilday.Sample">
 * 	<interceptor-ref name="paramRemover">
 *   		<param name="paramNames">aParam,anotherParam</param>
 *   		<param name="paramValues">--,-1</param>
 * 	</interceptor-ref>
 * 	<interceptor-ref name="default-stack" />
 * 	...
 * </action>
 *  
 * 
 * 
* * * @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 * paramNames and paramValues. * * @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 paramNames 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 paramValues 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy