Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.interceptor » [javadoc | source]
    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.Action;
    8   import com.opensymphony.xwork2.ActionInvocation;
    9   import com.opensymphony.xwork2.ValidationAware;
   10   import com.opensymphony.xwork2.util.logging.Logger;
   11   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   12   
   13   /**
   14    * <!-- START SNIPPET: description -->
   15    *
   16    * An interceptor that makes sure there are not validation errors before allowing the interceptor chain to continue.
   17    * <b>This interceptor does not perform any validation</b>.
   18    *
   19    * <p/>This interceptor does nothing if the name of the method being invoked is specified in the <b>excludeMethods</b>
   20    * parameter. <b>excludeMethods</b> accepts a comma-delimited list of method names. For example, requests to
   21    * <b>foo!input.action</b> and <b>foo!back.action</b> will be skipped by this interceptor if you set the
   22    * <b>excludeMethods</b> parameter to "input, back".
   23    *
   24    * <b>Note:</b> As this method extends off MethodFilterInterceptor, it is capable of
   25    * deciding if it is applicable only to selective methods in the action class. This is done by adding param tags
   26    * for the interceptor element, naming either a list of excluded method names and/or a list of included method
   27    * names, whereby includeMethods overrides excludedMethods. A single * sign is interpreted as wildcard matching
   28    * all methods for both parameters.
   29    * See {@link MethodFilterInterceptor} for more info.
   30    *
   31    * <!-- END SNIPPET: description -->
   32    *
   33    * <p/> <u>Interceptor parameters:</u>
   34    *
   35    * <!-- START SNIPPET: parameters -->
   36    *
   37    * <ul>
   38    *
   39    * <li>inputResultName - Default to "input". Determine the result name to be returned when
   40    * an action / field error is found.</li>
   41    *
   42    * </ul>
   43    *
   44    * <!-- END SNIPPET: parameters -->
   45    *
   46    * <p/> <u>Extending the interceptor:</u>
   47    *
   48    * <p/>
   49    *
   50    * <!-- START SNIPPET: extending -->
   51    *
   52    * There are no known extension points for this interceptor.
   53    *
   54    * <!-- END SNIPPET: extending -->
   55    *
   56    * <p/> <u>Example code:</u>
   57    *
   58    * <pre>
   59    * <!-- START SNIPPET: example -->
   60    * 
   61    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   62    *     &lt;interceptor-ref name="params"/&gt;
   63    *     &lt;interceptor-ref name="validation"/&gt;
   64    *     &lt;interceptor-ref name="workflow"/&gt;
   65    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   66    * &lt;/action&gt;
   67    * 
   68    * &lt;-- In this case myMethod as well as mySecondMethod of the action class
   69    *        will not pass through the workflow process --&gt;
   70    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   71    *     &lt;interceptor-ref name="params"/&gt;
   72    *     &lt;interceptor-ref name="validation"/&gt;
   73    *     &lt;interceptor-ref name="workflow"&gt;
   74    *         &lt;param name="excludeMethods"&gt;myMethod,mySecondMethod&lt;/param&gt;
   75    *     &lt;/interceptor-ref name="workflow"&gt;
   76    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   77    * &lt;/action&gt;
   78    *
   79    * &lt;-- In this case, the result named "error" will be used when
   80    *        an action / field error is found --&gt;
   81    * &lt;-- The Interceptor will only be applied for myWorkflowMethod method of action
   82    *        classes, since this is the only included method while any others are excluded --&gt;
   83    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   84    *     &lt;interceptor-ref name="params"/&gt;
   85    *     &lt;interceptor-ref name="validation"/&gt;
   86    *     &lt;interceptor-ref name="workflow"&gt;
   87    *        &lt;param name="inputResultName"&gt;error&lt;/param&gt;
   88   *         &lt;param name="excludeMethods"&gt;*&lt;/param&gt;
   89   *         &lt;param name="includeMethods"&gt;myWorkflowMethod&lt;/param&gt;
   90    *     &lt;/interceptor-ref&gt;
   91    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   92    * &lt;/action&gt;
   93    *
   94    * <!-- END SNIPPET: example -->
   95    * </pre>
   96    *
   97    * @author Jason Carreira
   98    * @author Rainer Hermanns
   99    * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  100    * @author Philip Luppens
  101    * @author tm_jee
  102    */
  103   public class DefaultWorkflowInterceptor extends MethodFilterInterceptor {
  104   	
  105   	private static final long serialVersionUID = 7563014655616490865L;
  106   
  107   	private static final Logger LOG = LoggerFactory.getLogger(DefaultWorkflowInterceptor.class);
  108   	
  109   	private String inputResultName = Action.INPUT;
  110   	
  111   	/**
  112   	 * Set the <code>inputResultName</code> (result name to be returned when 
  113   	 * a action / field error is found registered). Default to {@link Action#INPUT}
  114   	 * 
  115   	 * @param inputResultName what result name to use when there was validation error(s).
  116   	 */
  117   	public void setInputResultName(String inputResultName) {
  118   		this.inputResultName = inputResultName;
  119   	}
  120   	
  121   	/**
  122   	 * Intercept {@link ActionInvocation} and returns a <code>inputResultName</code>
  123   	 * when action / field errors is found registered.
  124   	 * 
  125   	 * @return String result name
  126   	 */
  127       protected String doIntercept(ActionInvocation invocation) throws Exception {
  128           Object action = invocation.getAction();
  129           
  130           if (action instanceof ValidationAware) {
  131               ValidationAware validationAwareAction = (ValidationAware) action;
  132   
  133               if (validationAwareAction.hasErrors()) {
  134               	if (LOG.isDebugEnabled()) {
  135               		LOG.debug("Errors on action "+validationAwareAction+", returning result name 'input'");
  136               	}
  137               	return inputResultName;
  138               }
  139           }
  140   
  141           return invocation.invoke();
  142       }
  143   
  144   }

Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.interceptor » [javadoc | source]