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

Home » xwork-2.1.5 » com.opensymphony » xwork2 » interceptor » [javadoc | source]