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

    1   /*
    2    * Copyright (c) 2002-2006 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.interceptor;
    6   
    7   import com.opensymphony.xwork2.ActionInvocation;
    8   
    9   import java.io.Serializable;
   10   
   11   
   12   /**
   13    * <!-- START SNIPPET: introduction -->
   14    * <p/>
   15    * An interceptor is a stateless class that follows the interceptor pattern, as
   16    * found in {@link  javax.servlet.Filter} and in AOP languages.
   17    * <p/>
   18    * <p/>
   19    * <p/>
   20    * Interceptors are objects that dynamically intercept Action invocations.
   21    * They provide the developer with the opportunity to define code that can be executed
   22    * before and/or after the execution of an action. They also have the ability
   23    * to prevent an action from executing. Interceptors provide developers a way to
   24    * encapulate common functionality in a re-usable form that can be applied to
   25    * one or more Actions.
   26    * <p/>
   27    * <p/>
   28    * <p/>
   29    * Interceptors <b>must</b> be stateless and not assume that a new instance will be created for each request or Action.
   30    * Interceptors may choose to either short-circuit the {@link ActionInvocation} execution and return a return code
   31    * (such as {@link com.opensymphony.xwork2.Action#SUCCESS}), or it may choose to do some processing before
   32    * and/or after delegating the rest of the procesing using {@link ActionInvocation#invoke()}.
   33    * <p/>
   34    * <!-- END SNIPPET: introduction -->
   35    * <p/>
   36    * <p/>
   37    * <p/>
   38    * <!-- START SNIPPET: parameterOverriding -->
   39    * <p/>
   40    * Interceptor's parameter could be overriden through the following ways :-
   41    * <p/>
   42    * <p/>
   43    * <p/>
   44    * <b>Method 1:</b>
   45    * <pre>
   46    * &lt;action name="myAction" class="myActionClass"&gt;
   47    *     &lt;interceptor-ref name="exception"/&gt;
   48    *     &lt;interceptor-ref name="alias"/&gt;
   49    *     &lt;interceptor-ref name="params"/&gt;
   50    *     &lt;interceptor-ref name="servletConfig"/&gt;
   51    *     &lt;interceptor-ref name="prepare"/&gt;
   52    *     &lt;interceptor-ref name="i18n"/&gt;
   53    *     &lt;interceptor-ref name="chain"/&gt;
   54    *     &lt;interceptor-ref name="modelDriven"/&gt;
   55    *     &lt;interceptor-ref name="fileUpload"/&gt;
   56    *     &lt;interceptor-ref name="staticParams"/&gt;
   57    *     &lt;interceptor-ref name="params"/&gt;
   58    *     &lt;interceptor-ref name="conversionError"/&gt;
   59    *     &lt;interceptor-ref name="validation"&gt;
   60    *     &lt;param name="excludeMethods"&gt;myValidationExcudeMethod&lt;/param&gt;
   61    *     &lt;/interceptor-ref&gt;
   62    *     &lt;interceptor-ref name="workflow"&gt;
   63    *     &lt;param name="excludeMethods"&gt;myWorkflowExcludeMethod&lt;/param&gt;
   64    *     &lt;/interceptor-ref&gt;
   65    * &lt;/action&gt;
   66    * </pre>
   67    * <p/>
   68    * <b>Method 2:</b>
   69    * <pre>
   70    * &lt;action name="myAction" class="myActionClass"&gt;
   71    *   &lt;interceptor-ref name="defaultStack"&gt;
   72    *     &lt;param name="validation.excludeMethods"&gt;myValidationExcludeMethod&lt;/param&gt;
   73    *     &lt;param name="workflow.excludeMethods"&gt;myWorkflowExcludeMethod&lt;/param&gt;
   74    *   &lt;/interceptor-ref&gt;
   75    * &lt;/action&gt;
   76    * </pre>
   77    * <p/>
   78    * <p/>
   79    * <p/>
   80    * In the first method, the whole default stack is copied and the parameter then
   81    * changed accordingly.
   82    * <p/>
   83    * <p/>
   84    * <p/>
   85    * In the second method, the 'interceptor-ref' refer to an existing
   86    * interceptor-stack, namely defaultStack in this example, and override the validator
   87    * and workflow interceptor excludeMethods typically in this case. Note that in the
   88    * 'param' tag, the name attribute contains a dot (.) the word before the dot(.)
   89    * specifies the interceptor name whose parameter is to be overridden and the word after
   90    * the dot (.) specifies the parameter itself. Essetially it is as follows :-
   91    * <p/>
   92    * <pre>
   93    *    &lt;interceptor-name&gt;.&lt;parameter-name&gt;
   94    * </pre>
   95    * <p/>
   96    * <b>Note</b> also that in this case the 'interceptor-ref' name attribute
   97    * is used to indicate an interceptor stack which makes sense as if it is referring
   98    * to the interceptor itself it would be just using Method 1 describe above.
   99    * <p/>
  100    * <!-- END SNIPPET: parameterOverriding -->
  101    * <p/>
  102    * <p/>
  103    * <b>Nested Interceptor param overriding</b>
  104    * <p/>
  105    * <!-- START SNIPPET: nestedParameterOverriding -->
  106    * <p/>
  107    * Interceptor stack parameter overriding could be nested into as many level as possible, though it would
  108    * be advisable not to nest it too deep as to avoid confusion, For example,
  109    * <pre>
  110    * &lt;interceptor name="interceptor1" class="foo.bar.Interceptor1" /&gt;
  111    * &lt;interceptor name="interceptor2" class="foo.bar.Interceptor2" /&gt;
  112    * &lt;interceptor name="interceptor3" class="foo.bar.Interceptor3" /&gt;
  113    * &lt;interceptor name="interceptor4" class="foo.bar.Interceptor4" /&gt;
  114    * &lt;interceptor-stack name="stack1"&gt;
  115    *     &lt;interceptor-ref name="interceptor1" /&gt;
  116    * &lt;/interceptor-stack&gt;
  117    * &lt;interceptor-stack name="stack2"&gt;
  118    *     &lt;interceptor-ref name="intercetor2" /&gt;
  119    *     &lt;interceptor-ref name="stack1" /&gt;
  120    * &lt;/interceptor-stack&gt;
  121    * &lt;interceptor-stack name="stack3"&gt;
  122    *     &lt;interceptor-ref name="interceptor3" /&gt;
  123    *     &lt;interceptor-ref name="stack2" /&gt;
  124    * &lt;/interceptor-stack&gt;
  125    * &lt;interceptor-stack name="stack4"&gt;
  126    *     &lt;interceptor-ref name="interceptor4" /&gt;
  127    *     &lt;interceptor-ref name="stack3" /&gt;
  128    *  &lt;/interceptor-stack&gt;
  129    * </pre>
  130    * Assuming the interceptor has the following properties
  131    * <table border="1" width="100%">
  132    * <tr>
  133    * <td>Interceptor</td>
  134    * <td>property</td>
  135    * </tr>
  136    * <tr>
  137    * <td>Interceptor1</td>
  138    * <td>param1</td>
  139    * </tr>
  140    * <tr>
  141    * <td>Interceptor2</td>
  142    * <td>param2</td>
  143    * </tr>
  144    * <tr>
  145    * <td>Interceptor3</td>
  146    * <td>param3</td>
  147    * </tr>
  148    * <tr>
  149    * <td>Interceptor4</td>
  150    * <td>param4</td>
  151    * </tr>
  152    * </table>
  153    * We could override them as follows :-
  154    * <pre>
  155    *    &lt;action ... &gt;
  156    *        &lt;!-- to override parameters of interceptor located directly in the stack  --&gt;
  157    *        &lt;interceptor-ref name="stack4"&gt;
  158    *           &lt;param name="interceptor4.param4"&gt; ... &lt;/param&gt;
  159    *        &lt;/interceptor-ref&gt;
  160    *    &lt;/action&gt;
  161    * <p/>
  162    *    &lt;action ... &gt;
  163    *        &lt;!-- to override parameters of interceptor located under nested stack --&gt;
  164    *        &lt;interceptor-ref name="stack4"&gt;
  165    *            &lt;param name="stack3.interceptor3.param3"&gt; ... &lt;/param&gt;
  166    *            &lt;param name="stack3.stack2.interceptor2.param2"&gt; ... &lt;/param&gt;
  167    *            &lt;param name="stack3.stack2.stack1.interceptor1.param1"&gt; ... &lt;/param&gt;
  168    *        &lt;/interceptor-ref&gt;
  169    *    &lt;/action&gt;
  170    *  </pre>
  171    * <p/>
  172    * <!-- END SNIPPET: nestedParameterOverriding -->
  173    *
  174    * @author Jason Carreira
  175    * @author tmjee
  176    * @version $Date: 2008-06-21 15:36:59 +0200 (Sa, 21 Jun 2008) $ $Id: Interceptor.java 1837 2008-06-21 13:36:59Z musachy $
  177    */
  178   public interface Interceptor extends Serializable {
  179   
  180       /**
  181        * Called to let an interceptor clean up any resources it has allocated.
  182        */
  183       void destroy();
  184   
  185       /**
  186        * Called after an interceptor is created, but before any requests are processed using
  187        * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
  188        * the Interceptor a chance to initialize any needed resources.
  189        */
  190       void init();
  191   
  192       /**
  193        * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
  194        * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
  195        *
  196        * @param invocation the action invocation
  197        * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
  198        * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
  199        */
  200       String intercept(ActionInvocation invocation) throws Exception;
  201   
  202   }

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