001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.interceptor.action;
020:
021: import org.apache.beehive.netui.pageflow.interceptor.AbstractInterceptor;
022: import org.apache.beehive.netui.pageflow.interceptor.InterceptorChain;
023: import org.apache.beehive.netui.pageflow.interceptor.InterceptorContext;
024: import org.apache.beehive.netui.pageflow.interceptor.InterceptorException;
025:
026: /**
027: * Base class for Page Flow action interceptors. These are configured in /WEB-INF/beehive-netui-config.xml like this:
028: * <pre>
029: * <netui-config xmlns="http://beehive.apache.org/netui/2004/server/config">
030: * ...
031: *
032: * <pageflow-action-interceptors>
033: * <global>
034: * <before-action>
035: * <action-interceptor>
036: * <interceptor-class>test.BeforeActionInterceptor1</interceptor-class>
037: * </action-interceptor>
038: * <action-interceptor>
039: * <interceptor-class>test.BeforeActionInterceptor2</interceptor-class>
040: * </action-interceptor>
041: * ...
042: * </before-action>
043: * <after-action>
044: * <action-interceptor>
045: * <interceptor-class>test.AfterActionInterceptor1</interceptor-class>
046: * </action-interceptor>
047: * <action-interceptor>
048: * <interceptor-class>test.AfterActionInterceptor2</interceptor-class>
049: * </action-interceptor>
050: * </after-action>
051: * </global>
052: * </pageflow-action-interceptors>
053: *
054: * ...
055: * </netui-config>
056: *
057: * </pre>
058: *
059: * Note that a registered ActionInterceptor is created and cached as a single instance per ServletContext.
060: * It should not hold any per-request or per-session state.
061: */
062: public abstract class ActionInterceptor extends AbstractInterceptor {
063: /**
064: * Callback invoked before the action is processed. During this method, {@link #setOverrideForward} may be called
065: * to:
066: * <ul>
067: * <li>change the destination URI and thus prevent the action from running, or,</li>
068: * <li>set the destination URI to <code>null</code> (no forwarding) and thus prevent the action from running, or,</li>
069: * <li>
070: * "inject" an entire nested page flow to run before the action is invoked. If the override forward URI
071: * is a nested page flow, then it will run until it raises one of its return actions. At that point,
072: * {@link #afterNestedIntercept} is called on <i>this interceptor</i>, which can again choose to override
073: * the forward or allow the original action to run.
074: * </li>
075: * </ul>
076: *
077: * {@link InterceptorChain#continueChain} is called to invoke the rest of the
078: * interceptor chain, anywhere within this method (e.g., at the end, or within a try/finally).
079: *
080: * @param context the current ActionInterceptorContext.
081: * @param chain the interceptor chain. Calling <code>continueChain</code> on this runs the rest of the interceptors.
082: */
083: public abstract void preAction(ActionInterceptorContext context,
084: InterceptorChain chain) throws InterceptorException;
085:
086: /**
087: * Callback invoked before the action is processed. {@link #preAction} may be used instead.
088: */
089: public void preInvoke(InterceptorContext context,
090: InterceptorChain chain) throws InterceptorException {
091: preAction((ActionInterceptorContext) context, chain);
092: }
093:
094: /**
095: * Callback invoked after the action is processed. During this method, {@link #setOverrideForward} may be called
096: * to:
097: * <ul>
098: * <li>change the destination URI and thus override the one returned from the action, or,</li>
099: * <li>set the destination URI to <code>null</code> (no forwarding).</li>
100: * </ul>
101: *
102: * {@link InterceptorChain#continueChain} is called to invoke the rest of the
103: * interceptor chain, anywhere within this method (e.g., at the end, or within a try/finally).
104: *
105: * @param context the current ActionInterceptorContext.
106: * @param chain the interceptor chain. Calling <code>continueChain</code> on this runs the rest of the interceptors.
107: */
108: public abstract void postAction(ActionInterceptorContext context,
109: InterceptorChain chain) throws InterceptorException;
110:
111: /**
112: * Callback invoked after the action is processed. {@link #postAction} may be used instead.
113: */
114: public void postInvoke(InterceptorContext context,
115: InterceptorChain chain) throws InterceptorException {
116: postAction((ActionInterceptorContext) context, chain);
117: }
118:
119: /**
120: * Callback invoked after a nested page flow has been "injected" by {@link #preAction}, and before the original
121: * action has run. During this method, {@link #setOverrideForward} may be called to:
122: *
123: * <ul>
124: * <li>change the destination URI that was returned by the action, or,</li>
125: * <li>set the destination URI to <code>null</code> (no forwarding).</li>
126: * </ul>
127: *
128: * {@link InterceptorChain#continueChain} is called to invoke the rest of the
129: * interceptor chain, anywhere within this method (e.g., at the end, or within a try/finally).
130: *
131: * @param context an extension of {@link ActionInterceptorContext} which contains the return action from the
132: * injected nested page flow.
133: * @throws InterceptorException
134: */
135: public abstract void afterNestedIntercept(
136: AfterNestedInterceptContext context)
137: throws InterceptorException;
138:
139: /**
140: * Override the Forward, either before or after the target action is run. See {@link #preAction} and
141: * {@link #postAction} for information on how this is used.
142: *
143: * @param forward an InterceptorForward that will override the target action's forward; or <code>null</code> to
144: * cancel navigation.
145: * @param context the current ActionInterceptorContext.
146: */
147: protected void setOverrideForward(InterceptorForward forward,
148: ActionInterceptorContext context) {
149: context.setOverrideForward(forward, this );
150: }
151:
152: /**
153: * Optional method that "wraps" the target action invocation. This is mainly useful for surrounding an action
154: * (and the rest of the interceptor chain) with try/catch/finally. This default implementation simply <i>returns</i>
155: * <code>continueChain</code> on the passed-in InterceptorChain, which allows the rest of the interceptors
156: * <i>and</i> the action to run.
157: *
158: * @param context the current ActionInterceptorContext.
159: * @param chain the interceptor chain. This chain is different from the ones passed to {@link #preAction} and
160: * {@link #postAction} in that the action invocation itself is included in it.
161: * @return the ActionForward returned by the action.
162: */
163: public Object wrapAction(ActionInterceptorContext context,
164: InterceptorChain chain) throws InterceptorException {
165: return chain.continueChain();
166: }
167: }
|