1 /* 2 * Copyright (c) 2002-2007 by OpenSymphony 3 * All rights reserved. 4 */ 5 package com.opensymphony.xwork2.interceptor; 6 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionInvocation; 9 import com.opensymphony.xwork2.util.TextParseUtil; 10 import com.opensymphony.xwork2.util.logging.Logger; 11 import com.opensymphony.xwork2.util.logging.LoggerFactory; 12 13 import java.util.Collections; 14 import java.util.Map; 15 import java.util.Set; 16 17 /** 18 * <!-- START SNIPPET: description --> 19 * This is a simple XWork interceptor that allows parameters (matching 20 * one of the paramNames attribute csv value) to be 21 * removed from the parameter map if they match a certain value 22 * (matching one of the paramValues attribute csv value), before they 23 * are set on the action. A typical usage would be to want a dropdown/select 24 * to map onto a boolean value on an action. The select had the options 25 * none, yes and no with values -1, true and false. The true and false would 26 * map across correctly. However the -1 would be set to false. 27 * This was not desired as one might needed the value on the action to stay null. 28 * This interceptor fixes this by preventing the parameter from ever reaching 29 * the action. 30 * <!-- END SNIPPET: description --> 31 * 32 * 33 * <!-- START SNIPPET: parameters --> 34 * <ul> 35 * <li>paramNames - A comma separated value (csv) indicating the parameter name 36 * whose param value should be considered that if they match any of the 37 * comma separated value (csv) from paramValues attribute, shall be 38 * removed from the parameter map such that they will not be applied 39 * to the action</li> 40 * <li>paramValues - A comma separated value (csv) indicating the parameter value that if 41 * matched shall have its parameter be removed from the parameter map 42 * such that they will not be applied to the action</li> 43 * </ul> 44 * <!-- END SNIPPET: parameters --> 45 * 46 * 47 * <!-- START SNIPPET: extending --> 48 * No intended extension point 49 * <!-- END SNIPPET: extending --> 50 * 51 * <pre> 52 * <!-- START SNIPPET: example --> 53 * 54 * <action name="sample" class="org.martingilday.Sample"> 55 * <interceptor-ref name="paramRemover"> 56 * <param name="paramNames">aParam,anotherParam</param> 57 * <param name="paramValues">--,-1</param> 58 * </interceptor-ref> 59 * <interceptor-ref name="defaultStack" /> 60 * ... 61 * </action> 62 * 63 * <!-- END SNIPPET: example --> 64 * </pre> 65 * 66 * 67 * @author martin.gilday 68 */ 69 public class ParameterRemoverInterceptor extends AbstractInterceptor { 70 71 private static final Logger LOG = LoggerFactory.getLogger(ParameterRemoverInterceptor.class); 72 73 private static final long serialVersionUID = 1; 74 75 private Set<String> paramNames = Collections.emptySet(); 76 77 private Set<String> paramValues = Collections.emptySet(); 78 79 80 /** 81 * Decide if the parameter should be removed from the parameter map based on 82 * <code>paramNames</code> and <code>paramValues</code>. 83 * 84 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor 85 */ 86 @Override 87 public String intercept(ActionInvocation invocation) throws Exception { 88 if (!(invocation.getAction() instanceof NoParameters) 89 && (null != this.paramNames)) { 90 ActionContext ac = invocation.getInvocationContext(); 91 final Map<String, Object> parameters = ac.getParameters(); 92 93 if (parameters != null) { 94 for (String removeName : paramNames) { 95 // see if the field is in the parameter map 96 if (parameters.containsKey(removeName)) { 97 98 try { 99 String[] values = (String[]) parameters 100 .get(removeName); 101 String value = values[0]; 102 if (null != value && this.paramValues.contains(value)) { 103 parameters.remove(removeName); 104 } 105 } catch (Exception e) { 106 LOG.error("Failed to convert parameter to string", e); 107 } 108 } 109 } 110 } 111 } 112 return invocation.invoke(); 113 } 114 115 /** 116 * Allows <code>paramNames</code> attribute to be set as comma-separated-values (csv). 117 * 118 * @param paramNames the paramNames to set 119 */ 120 public void setParamNames(String paramNames) { 121 this.paramNames = TextParseUtil.commaDelimitedStringToSet(paramNames); 122 } 123 124 125 /** 126 * Allows <code>paramValues</code> attribute to be set as a comma-separated-values (csv). 127 * 128 * @param paramValues the paramValues to set 129 */ 130 public void setParamValues(String paramValues) { 131 this.paramValues = TextParseUtil.commaDelimitedStringToSet(paramValues); 132 } 133 } 134