<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document>
    <header>
        <title>Action Interceptors</title>
    </header>
    <body>
        <section id="introduction">
            <title>Introduction</title>
            <p>
                Sometimes it's useful to be able to run code before and/or
                after an action executes. NetUI includes a feature to
                configure "action interceptor" classes to run before/after
                all actions, a particular action, or all actions in a particular
                page flow. The action interceptor can change or cancel the
                destination URI, throw an exception, or "inject" an entire
                nested page flow to run before the action.
            </p>
            <p>
                A simple example of this would be a monitoring infrastructure
                that keeps track of the number of actions raised.  To keep
                track of the number of actions raised, register an interceptor
                that runs the counting code before going to any action. A
                more complex example is an interceptor that might decide
                to take you to a nested page flow that asks you to fill out a
                "satisfaction survey" before sending you to the destination
                page flow.
            </p>
            <p>
                This document describes how to implement and configure
                action interceptors.
            </p>
        </section>
        <section id="action-interceptors">
            <title>Action Interceptors</title>
            <p>
                To create an action interceptor, extend
                <a href="apidocs/javadoc/org/apache/beehive/netui/pageflow/interceptor/action/ActionInterceptor.html">ActionInterceptor</a>
                and implement the abstract methods <code>preAction()</code>,
                <code>postAction()</code>, and <code>afterNestedIntercept()</code>.
            </p>
            <p>
                The action interceptor class is run twice: both before
                <em>and</em> after the execution of the action.
                The method <code>preAction()</code> is called before an
                action is processed. During this callback, the protected
                method <code>setOverrideForward()</code>
                may be called to do three things:
            </p>
            <ul>
                <li>
                    change the destination URI and thus prevent the action
                    from running, or,
                </li>
                <li>
                    set the destination URI to <code>null</code> (no
                    forwarding) and thus prevent the action from running, or,
                </li>
                <li>
                    "inject" an entire nested page flow to run before the
                    action is invoked.  If the override forward URI is a
                    nested page flow, then it will run until it raises one
                    of its return actions.  At that point,
                    <code>afterNestedIntercept()</code> is called on <em>this
                    interceptor</em>, which can again choose to override
                    the forward or allow the original action to run.
                </li>
            </ul>
            <p>
                The method <code>postAction()</code> is called after an
                action is processed. During this callback, the protected
                method <code>setOverrideForward()</code>
                may be called to do two things:
            </p>
            <ul>
                <li>
                    change the destination URI that was returned by the
                    action, or,
                </li>
                <li>
                    set the destination URI to <code>null</code> (no forwarding).
                </li>
            </ul>
            <p>
                Anywhere within the <code>preAction()</code> and
                <code>postAction()</code> methods,
                <code>InterceptorChain.continueChain()</code> is called to
                invoke the rest of the interceptor chain.
            </p>
            <p><strong>See:</strong></p>
            <p>
                <a href="apidocs/javadoc/org/apache/beehive/netui/pageflow/interceptor/action/ActionInterceptor.html">Class org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptor</a>
            </p>
        </section>
        <section id="simple-action-interceptors">
            <title>Simple Action Interceptors</title>
            <p>
                There is nothing to implement for a simple action interceptor.
                Just configure the overriding path or destination URI to
                forward to and whether it should occur before or after
                the action.
            </p>
        </section>
        <section id="config-action-interceptors">
            <title>Configuring Action Interceptors</title>
            <p>
                To run code before or after Page Flow actions, you configure a
                <a href="site:docs/reference/netui/netui_config/config_pageflow_action_interceptor">
                    &lt;pageflow-action-interceptors&gt;
                </a> 
                group in the beehive NetUI configuration file
                <code>/WEB-INF/beehive-netui-config.xml</code>. For example:
            </p>
            <source>
&lt;netui-config xmlns="http://beehive.apache.org/netui/2004/server/config"&gt;
    ...
    &lt;pageflow-action-interceptors&gt;
        &lt;global&gt;
            &lt;action-interceptor&gt;
                &lt;!-- This interceptor is run before/after ALL page flow actions. --&gt;
                &lt;interceptor-class&gt;test.GlobalInterceptor&lt;/interceptor-class&gt;
                &lt;custom-property&gt;
                    &lt;name&gt;someCustomProperty&lt;/name&gt;
                    &lt;value&gt;someValue&lt;/value&gt;
                &lt;/custom-property&gt;
            &lt;/action-interceptor&gt;
        &lt;/global&gt;
        &lt;per-pageflow&gt;
            &lt;pageflow-uri&gt;/example/simpleintercept/Controller.jpf&lt;/pageflow-uri&gt;
            &lt;!-- This interceptor is run before any action in the
                 /example/simpleintercept/Controller.jpf page flow
                 and forwards to /example/nested/SomeController.jpf --&gt;
            &lt;simple-action-interceptor&gt;
                &lt;intercept-path&gt;/example/nested/SomeController.jpf&lt;/intercept-path&gt;
            &lt;/simple-action-interceptor&gt;
        &lt;/per-pageflow&gt;
        &lt;per-pageflow&gt;
            &lt;pageflow-uri&gt;/example/intercept/Controller.jpf&lt;/pageflow-uri&gt;
            &lt;!-- These interceptors are run before/after any action in the
                 /example/intercept/Controller.jpf page flow. --&gt;
            &lt;action-interceptor&gt;
                &lt;interceptor-class&gt;test.ActionInterceptor1&lt;/interceptor-class&gt;
            &lt;/action-interceptor&gt;
            &lt;action-interceptor&gt;
                &lt;interceptor-class&gt;test.ActionInterceptor2&lt;/interceptor-class&gt;
            &lt;/action-interceptor&gt;
            &lt;per-action&gt;
                &lt;!-- This interceptor is run after the action named
                     &quot;testSimpleAction&quot; in the /example/intercept/Controller.jpf
                     page flow. --&gt;
                &lt;action-name&gt;testSimpleAction&lt;/action-name&gt;
                &lt;simple-action-interceptor&gt;
                    &lt;intercept-path&gt;/example/flow/SomeController.jpf&lt;/intercept-path&gt;
                    &lt;after-action&gt;true&lt;/after-action&gt;
                &lt;/simple-action-interceptor&gt;
            &lt;/per-action&gt;
            &lt;per-action&gt;
                &lt;!-- This interceptor is run before/after the action named
                     &quot;testAction&quot; in the /example/intercept/Controller.jpf
                     page flow. --&gt;
                &lt;action-name&gt;testAction&lt;/action-name&gt;
                &lt;action-interceptor&gt;
                    &lt;interceptor-class&gt;test.ActionInterceptor3&lt;/interceptor-class&gt;
                &lt;/action-interceptor&gt;
            &lt;/per-action&gt;
            ...
        &lt;/per-pageflow&gt;
        &lt;per-pageflow&gt;
            ...
        &lt;/per-pageflow&gt;
        ...
    &lt;/pageflow-action-interceptors&gt;
    ...
&lt;/netui-config&gt;</source>
        </section>
    </body>
    <footer>
        <legal>
            Java, J2EE, and JCP are trademarks or registered trademarks of
            Sun Microsystems, Inc. in the United States and other countries.
            <br/>
            &copy; 2004, Apache Software Foundation
        </legal>
    </footer>
</document>
