Package com. ehi. struts. interceptor. servicemanagement; Import java. util. ArrayList; Import java. util. List; Import java. util. Map; Import com. opensymphony. xwork2.interceptor. MethodFilterInterceptor; Import com. opensymphony. xwork2.ActionInvocation; Import org. apache. commons. lang. StringUtils; /** * An interceptor to trim all the parameters. <br> * It's bascially adapted from the book, <br/> * 'Apache Struts 2 Web Application development' * * @ Author candicew * */ @ SuppressWarnings ("serial ") Public class TrimInterceptor extends MethodFilterInterceptor { Private List <String> excluded = new ArrayList <String> (); Protected String doIntercept (ActionInvocation invocation) throws Exception { Map <String, Object> parameters = invocation. getInvocationContext (). getParameters (); For (String param: parameters. keySet ()){ If (shouldTrim (param )){ DoTrim (parameters, param ); } } Return invocation. invoke (); } Void doTrim (Map <String, Object> parameters, String param ){ Object val = parameters. get (param ); If (val = null ){ Return; } If (val instanceof String ){ TrimString (parameters, param, val ); } Else { TrimStringArray (parameters, param, val ); } } Private void trimString (Map <String, Object> parameters, String param, Object val ){ String value = (String) val; Value = StringUtils. trimToNull (value ); Parameters. put (param, value ); } Private void trimStringArray (Map <String, Object> parameters, String param, Object val ){ String [] vals = (String []) val; Boolean allNull = true; For (int I = 0; I <vals. length; I ++ ){ Vals [I] = StringUtils. trimToNull (vals [I]); AllNull = allNull & (vals [I] = null ); } If (allNull ){ Parameters. put (param, null ); } } Private boolean shouldTrim (String param ){ For (String exclude: excluded ){ If (param. Fig (exclude )){ Return false; } } Return true; } Public void setExcludedParams (String excludedParams ){ For (String s: StringUtils. split (excludedParams ,",")){ Excluded. add (s. trim ()); } } } |