001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import java.util.Map;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import com.opensymphony.webwork.dispatcher.FlashResult;
013: import com.opensymphony.xwork.ActionContext;
014: import com.opensymphony.xwork.ActionInvocation;
015: import com.opensymphony.xwork.interceptor.AroundInterceptor;
016: import com.opensymphony.xwork.interceptor.PreResultListener;
017: import com.opensymphony.xwork.util.OgnlValueStack;
018:
019: /**
020: * <!-- START SNIPPET: description -->
021: * Flash interceptor ({@link FlashInterceptor}) possibly with {@link FlashResult}
022: * allows current action to be available even after a redirect. It does this by
023: * saving the current action into http session and pushing it back into the stack
024: * next request, resulting in the nett effect of the action and its related information
025: * being available across redirect.
026: * <!-- END SNIPPET: description -->
027: *
028: *
029: * <!-- START SNIPPET: parameters -->
030: * <ul>
031: * <li>key - The Http Session key under which the action will be stored,
032: * default to {@link FlashInterceptor#DEFAULT_KEY} which is
033: * the string '__flashAction'.</li>
034: * <li>operation - The operation mode of this interceptor, either
035: * {@link FlashInterceptor#STORE} having a string value of 'Store' or
036: * {@link FlashInterceptor#RETRIEVE} having a string value of 'Retrieve'
037: * The default operation mode is {@link FlashInterceptor#RETRIEVE}</li>
038: * </ul>
039: * <!-- END SNIPPET: parameters -->
040: *
041: *
042: * <!-- START SNIPPET: extending -->
043: * There's no intended extension points
044: * <!-- END SNIPPET: extending -->
045: *
046: *
047: * <pre>
048: * <!-- START SNIPPET: example -->
049: * <!-- Usage 1: (Using only Flash interceptor) -->
050: * <action name="store" ...>
051: * <interceptor-ref name="flash">
052: * <param name="operation">Store</param>
053: * </interceptor-ref>
054: * <interceptor-ref name="defaultStack" />
055: * <result type="redirect">redirectToSomeWhere.jsp</result>
056: * </action>
057: * <action name="retrieve">
058: * <interceptor-ref name="flash">
059: * <param name="operation">Retrieve</param>
060: * </interceptor-ref>
061: * <interceptor-ref name="defaultStack" />
062: * <result>pageWhereWeNeedFlashActionStored.jsp</result>
063: * </action>
064: *
065: *
066: * <!-- Usage 2: (Using Flash Interceptor and Flash Result) -->
067: * <action name="store">
068: * <result type="flash">redirectToSomeWhere.jsp</result>
069: * </action>
070: * <action name="retrieve">
071: * <interceptor-ref name="flash">
072: * <param name="operation">Retrieve</param>
073: * </interceptor-ref>
074: * <interceptor-ref name="defaultStack" />
075: * <result>pageWhereWeNeedFlashActionStored.jsp</result>
076: * </action>
077: *
078: * <!-- END SNIPPET: example -->
079: * </pre>
080: *
081: *
082: * @author tmjee
083: * @version $Date: 2007-02-04 06:33:20 +0100 (Sun, 04 Feb 2007) $ $Id: FlashInterceptor.java 2830 2007-02-04 05:33:20Z tm_jee $
084: */
085: public class FlashInterceptor extends AroundInterceptor {
086:
087: public static final String DEFAULT_KEY = "__flashAction";
088: public static final String STORE = "Store";
089: public static final String RETRIEVE = "Retrieve";
090:
091: private static final Log LOG = LogFactory
092: .getLog(FlashInterceptor.class);
093:
094: private static final long serialVersionUID = -9200319895107209641L;
095:
096: private String key = DEFAULT_KEY;
097: private String operation = RETRIEVE;
098:
099: public void setKey(String key) {
100: this .key = key;
101: }
102:
103: public String getKey() {
104: return this .key;
105: }
106:
107: public void setOperation(String operation) {
108: this .operation = operation;
109: }
110:
111: public String getOperation() {
112: return this .operation;
113: }
114:
115: /**
116: * @see com.opensymphony.xwork.interceptor.AroundInterceptor#after(com.opensymphony.xwork.ActionInvocation, java.lang.String)
117: */
118: protected void after(ActionInvocation invocation, String result)
119: throws Exception {
120: }
121:
122: /**
123: * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
124: */
125: protected void before(ActionInvocation invocation) throws Exception {
126: Map sessionMap = ActionContext.getContext().getSession();
127:
128: if (STORE.equalsIgnoreCase(operation)) {
129: invocation.addPreResultListener(new PreResultListener() {
130: public void beforeResult(ActionInvocation invocation,
131: String resultCode) {
132: Map sessionMap = ActionContext.getContext()
133: .getSession();
134: Object action = invocation.getAction();
135: if (LOG.isDebugEnabled())
136: LOG.debug("inserting action [" + action
137: + "] into session with key [" + key
138: + "]");
139: sessionMap.put(key, action);
140: }
141: });
142: }
143:
144: if (RETRIEVE.equalsIgnoreCase(operation)) {
145: if (sessionMap.get(key) != null) {
146: Object action = sessionMap.get(key);
147: if (LOG.isDebugEnabled()) {
148: LOG.debug("flash action with key [" + key
149: + "] found in session, pushed action ["
150: + action + "] into stack");
151: }
152: OgnlValueStack stack = (OgnlValueStack) ActionContext
153: .getContext().get(ActionContext.VALUE_STACK);
154: stack.push(action);
155: sessionMap.remove(key);
156: if (LOG.isDebugEnabled()) {
157: LOG
158: .debug("flash action with key ["
159: + key
160: + "] with actual type ["
161: + action
162: + "] removed from session after being pushed into the stack ");
163: }
164: } else {
165: if (LOG.isDebugEnabled()) {
166: LOG
167: .debug("flash action key ["
168: + key
169: + "] not found in session, no action is pushed into stack");
170: }
171: }
172: }
173: }
174: }
|