001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import com.opensymphony.webwork.dispatcher.SessionMap;
011: import com.opensymphony.xwork.ActionContext;
012: import com.opensymphony.xwork.ActionInvocation;
013: import com.opensymphony.xwork.interceptor.AroundInterceptor;
014:
015: /**
016: * <!-- START SNIPPET: description -->
017: * This interceptor invalidates http session based on the type of operation it is in.
018: * There's three type of operations:-
019: * <ul>
020: * <li>NextRequest - This causes the interceptor to invalidate the session in
021: * the next comming request where this interceptor is present in the
022: * interceptor stack. This interceptor mark this in the http session
023: * using a key determined by the key attribute of this interceptor</li>
024: * <li>Now - This causes this interceptor to invalidate the session at the end of
025: * this interceptor's interception</li>
026: * <li>NoOperation - This causes this interceptor to basically do nothing.
027: * It is here such that users could have this interceptor in their
028: * default stack and still allows it to do nothing</li>
029: * </ul>
030: * <!-- END SNIPPET: description -->
031: *
032: * <!-- START SNIPPET: extending -->
033: * No intended extension points.
034: * <!-- END SNIPPET: extending -->
035: *
036: * <!-- START SNIPPET: parameters -->
037: * <ul>
038: * <li>type - indicate the operation of this interceptor, valid values are 'NextRequest', 'Now' and 'NoOperation'
039: * See description above for more information.</li>
040: * <li>key - this is the http session key used by the interceptor to mark the situation
041: * whereby the next comming request with this interceptor present in the
042: * interception stack, it will invalidate the http session.</li>
043: * </ul>
044: * <!-- END SNIPPET: parameters -->
045: *
046: * <pre>
047: * <!-- START SNIPPET: example -->
048: *
049: * <action name="logout" ... >
050: * <intereptor-ref name="sessionInvalidate">
051: * <param name="type">Now</param>
052: * </interceptor-ref>
053: * ....
054: * </action>
055: *
056: * or
057: *
058: * <action name="sayByeByeNextRequestWillHaveSessionLost" ... >
059: * <interceptor-ref name="sessionInvalidate">
060: * <param name="type"<NextRequest</param>
061: * </interceptor-ref>
062: * ....
063: * </action>
064: *
065: * <!-- This is the next request, "sessionInvalidate" will find the marker inserted
066: * by the action above and invalidate the session -->
067: * <!-- The type="NoOperation" is just there so that the type is a valid one, and
068: * we don't get a warning log meessage -->
069: * <action name="nextRequest" ... >
070: * <interceptor-ref name="sessionInvalidate">
071: * <param name="type">NoOperation</param>
072: * </interceptor-ref>
073: * ...
074: * </action>
075: *
076: * <!-- END SNIPPET: example -->
077: * </pre>
078: *
079: * @author tmjee
080: * @version $Date: 2006-12-11 13:45:46 +0100 (Mon, 11 Dec 2006) $ $Id: SessionInvalidationInterceptor.java 2757 2006-12-11 12:45:46Z tmjee $
081: */
082: public class SessionInvalidationInterceptor extends AroundInterceptor {
083:
084: private static final Log LOG = LogFactory
085: .getLog(SessionInvalidationInterceptor.class);
086:
087: private static final long serialVersionUID = 1L;
088:
089: public static String NEXT_REQUEST = "NextRequest";
090: public static String NOW = "Now";
091: public static String NO_OPERATION = "NoOperation";
092:
093: protected String key = "___invalidateSession";
094: protected String type = NO_OPERATION;
095:
096: /**
097: * Set the session key, of which this interceptor will use to mark if the next request
098: * with this interceptor in the stack should have the session invalidated.
099: * @param key
100: */
101: public void setKey(String key) {
102: this .key = key;
103: }
104:
105: /**
106: * Get the session key, of which this interceptor will use to mark if the next request
107: * with this interceptor in the stack should have the session invalidated.
108: * @return String
109: */
110: public String getKey() {
111: return this .key;
112: }
113:
114: /**
115: * Set the operation type, either 'NextRequest', 'Now', or 'NoOperation' (default).
116: * @param key
117: */
118: public void setType(String type) {
119: this .type = type;
120: }
121:
122: /**
123: * Returns the operation type.
124: * @return String
125: */
126: public String getType() {
127: return this .type;
128: }
129:
130: /**
131: * Decides if this interceptor should invalidate the session or mark the session
132: * to be invalidated upon the next request that contains this interceptor in the stack.
133: *
134: * @see com.opensymphony.xwork.interceptor.AroundInterceptor#after(com.opensymphony.xwork.ActionInvocation, java.lang.String)
135: */
136: protected void after(ActionInvocation invocation, String result)
137: throws Exception {
138: SessionMap sessionMap = (SessionMap) invocation
139: .getInvocationContext().get(ActionContext.SESSION);
140: if (NOW.equalsIgnoreCase(type)) {
141: if (LOG.isDebugEnabled())
142: LOG.debug("type=now, invalidating session now");
143: sessionMap.invalidate();
144: LOG.info("session invalidated");
145: } else if (NEXT_REQUEST.equalsIgnoreCase(type)) {
146: if (LOG.isDebugEnabled())
147: LOG
148: .debug("type=NextRequest, mark key in session, such that next request that have this interceptor will invalidate the session");
149: sessionMap.put(key, "true");
150: } else if (NO_OPERATION.equalsIgnoreCase(type)) {
151: if (LOG.isDebugEnabled())
152: LOG.debug("no operation");
153: } else {
154: LOG.warn("unrecognized type, type should be either " + NOW
155: + ", " + NEXT_REQUEST + " or " + NO_OPERATION);
156: }
157: }
158:
159: /**
160: * Invalidate this session if it is marked to be invalidated from previous request.
161: *
162: * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
163: */
164: protected void before(ActionInvocation invocation) throws Exception {
165: SessionMap sessionMap = (SessionMap) invocation
166: .getInvocationContext().get(ActionContext.SESSION);
167: if (sessionMap.containsKey(key)
168: && sessionMap.get(key).equals("true")) {
169: if (LOG.isDebugEnabled())
170: LOG
171: .debug("found marker in session indicating this is the 'next request', session should be invalidated");
172: sessionMap.invalidate();
173: LOG.info("session invalidated");
174: }
175: }
176: }
|