1 /* 2 * Copyright (c) 2002-2006 by OpenSymphony 3 * All rights reserved. 4 */ 5 6 package com.opensymphony.xwork2.interceptor; 7 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.Set; 15 16 17 /** 18 * <!-- START SNIPPET: javadoc --> 19 * 20 * MethodFilterInterceptor is an abstract <code>Interceptor</code> used as 21 * a base class for interceptors that will filter execution based on method 22 * names according to specified included/excluded method lists. 23 * 24 * <p/> 25 * 26 * Settable parameters are as follows: 27 * 28 * <ul> 29 * <li>excludeMethods - method names to be excluded from interceptor processing</li> 30 * <li>includeMethods - method names to be included in interceptor processing</li> 31 * </ul> 32 * 33 * <p/> 34 * 35 * <b>NOTE:</b> If method name are available in both includeMethods and 36 * excludeMethods, it will be considered as an included method: 37 * includeMethods takes precedence over excludeMethods. 38 * 39 * <p/> 40 * 41 * Interceptors that extends this capability include: 42 * 43 * <ul> 44 * <li>TokenInterceptor</li> 45 * <li>TokenSessionStoreInterceptor</li> 46 * <li>DefaultWorkflowInterceptor</li> 47 * <li>ValidationInterceptor</li> 48 * </ul> 49 * 50 * <!-- END SNIPPET: javadoc --> 51 * 52 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a> 53 * @author Rainer Hermanns 54 * 55 * @see org.apache.struts2.interceptor.TokenInterceptor 56 * @see org.apache.struts2.interceptor.TokenSessionStoreInterceptor 57 * @see com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor 58 * @see com.opensymphony.xwork2.validator.ValidationInterceptor 59 * 60 * @version $Date: 2008-06-21 11:29:39 +0200 (Sa, 21 Jun 2008) $ $Id: MethodFilterInterceptor.java 1833 2008-06-21 09:29:39Z rainerh $ 61 */ 62 public abstract class MethodFilterInterceptor extends AbstractInterceptor { 63 protected transient Logger log = LoggerFactory.getLogger(getClass()); 64 65 protected Set<String> excludeMethods = Collections.emptySet(); 66 protected Set<String> includeMethods = Collections.emptySet(); 67 68 public void setExcludeMethods(String excludeMethods) { 69 this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods); 70 } 71 72 public Set<String> getExcludeMethodsSet() { 73 return excludeMethods; 74 } 75 76 public void setIncludeMethods(String includeMethods) { 77 this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods); 78 } 79 80 public Set<String> getIncludeMethodsSet() { 81 return includeMethods; 82 } 83 84 @Override 85 public String intercept(ActionInvocation invocation) throws Exception { 86 if (applyInterceptor(invocation)) { 87 return doIntercept(invocation); 88 } 89 return invocation.invoke(); 90 } 91 92 protected boolean applyInterceptor(ActionInvocation invocation) { 93 String method = invocation.getProxy().getMethod(); 94 // ValidationInterceptor 95 boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method); 96 if (log.isDebugEnabled()) { 97 if (!applyMethod) { 98 log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list."); 99 } 100 } 101 return applyMethod; 102 } 103 104 /** 105 * Subclasses must override to implement the interceptor logic. 106 * 107 * @param invocation the action invocation 108 * @return the result of invocation 109 * @throws Exception 110 */ 111 protected abstract String doIntercept(ActionInvocation invocation) throws Exception; 112 113 }