001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.interceptor;
006:
007: import java.util.Collections;
008: import java.util.Iterator;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.opensymphony.xwork.ActionContext;
016: import com.opensymphony.xwork.ActionInvocation;
017: import com.opensymphony.xwork.interceptor.AroundInterceptor;
018: import com.opensymphony.xwork.interceptor.NoParameters;
019: import com.opensymphony.xwork.util.TextParseUtil;
020:
021: /**
022: * <!-- START SNIPPET: description -->
023: * This is a simple WebWork interceptor that allows parameters (matching
024: * one of the paramNames attribute csv value) to be
025: * removed from the parameter map if they match a certain value
026: * (matching one of the paramValues attribute csv value), before they
027: * are set on the action. A typical usage would be to want a dropdown/select
028: * to map onto a boolean value on an action. The select had the options
029: * none, yes and no with values -1, true and false. The true and false would
030: * map across correctly. However the -1 would be set to false.
031: * This was not desired as one might needed the value on the action to stay null.
032: * This interceptor fixes this by preventing the parameter from ever reaching
033: * the action.
034: * <!-- END SNIPPET: description -->
035: *
036: *
037: * <!-- START SNIPPET: parameters -->
038: * <ul>
039: * <li>paramNames - A comma separated value (csv) indicating the parameter name
040: * whose param value should be considered that if they match any of the
041: * comma separated value (csv) from paramValues attribute, shall be
042: * removed from the parameter map such that they will not be applied
043: * to the action</li>
044: * <li>paramValues - A comma separated value (csv) indicating the parameter value that if
045: * matched shall have its parameter be removed from the parameter map
046: * such that they will not be applied to the action</li>
047: * </ul>
048: * <!-- END SNIPPET: parameters -->
049: *
050: *
051: * <!-- START SNIPPET: extending -->
052: * No intended extension point
053: * <!-- END SNIPPET: extending -->
054: *
055: * <pre>
056: * <!-- START SNIPPET: example -->
057: *
058: * <action name="sample" class="org.martingilday.Sample">
059: * <interceptor-ref name="paramRemover">
060: * <param name="paramNames">aParam,anotherParam</param>
061: * <param name="paramValues">--,-1</param>
062: * </interceptor-ref>
063: * <interceptor-ref name="default-stack" />
064: * ...
065: * </action>
066: *
067: * <!-- END SNIPPET: example -->
068: * </pre>
069: *
070: *
071: * @author martin.gilday
072: * @version $Date$ $Id$
073: */
074: public class ParameterRemoverInterceptor extends AroundInterceptor {
075:
076: private static final Log LOG = LogFactory
077: .getLog(ParameterRemoverInterceptor.class);
078:
079: private static final long serialVersionUID = 1;
080:
081: private Set paramNames = Collections.EMPTY_SET;
082:
083: private Set paramValues = Collections.EMPTY_SET;
084:
085: /**
086: * Empty implementation.
087: */
088: protected void after(ActionInvocation dispatcher, String result)
089: throws Exception {
090: // do nothing
091: }
092:
093: /**
094: * Decide if the parameter should be removed from the parameter map based on
095: * <code>paramNames</code> and <code>paramValues</code>.
096: *
097: * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
098: */
099: protected void before(ActionInvocation invocation) throws Exception {
100:
101: if (!(invocation.getAction() instanceof NoParameters)
102: && (null != this .paramNames)) {
103: ActionContext ac = invocation.getInvocationContext();
104: final Map parameters = ac.getParameters();
105:
106: if (parameters != null) {
107: for (Iterator i = paramNames.iterator(); i.hasNext();) {
108: Object removeName = i.next();
109:
110: // see if the field is in the parameter map
111: if (parameters.containsKey(removeName)) {
112:
113: try {
114: String[] values = (String[]) parameters
115: .get(removeName);
116: String value = values[0];
117: if (null != value
118: && this .paramValues.contains(value)) {
119: parameters.remove(removeName);
120: }
121: } catch (Exception e) {
122: LOG
123: .error(
124: "Failed to convert parameter to string",
125: e);
126: }
127: }
128: }
129: }
130: }
131: }
132:
133: /**
134: * Allows <code>paramNames</code> attribute to be set as comma-separated-values (csv).
135: *
136: * @param paramNames the paramNames to set
137: */
138: public void setParamNames(String paramNames) {
139: this .paramNames = TextParseUtil
140: .commaDelimitedStringToSet(paramNames);
141: }
142:
143: /**
144: * Allows <code>paramValues</code> attribute to be set as a comma-separated-values (csv).
145: *
146: * @param paramValues the paramValues to set
147: */
148: public void setParamValues(String paramValues) {
149: this.paramValues = TextParseUtil
150: .commaDelimitedStringToSet(paramValues);
151: }
152:
153: }
|