FileDocCategorySizeDatePackage
MessageStoreInterceptor.javaAPI DocExample13020Mon Jul 23 13:26:52 BST 2007org.apache.struts2.interceptor

MessageStoreInterceptor

public class MessageStoreInterceptor extends Object implements com.opensymphony.xwork2.interceptor.Interceptor
An interceptor to store {@link ValidationAware} action's messages / errors and field errors into Http Session, such that it will be retrieveable at a later stage. This allows the action's message / errors and field errors to be available longer that just the particular http request.

In the 'STORE' mode, the interceptor will store the {@link ValidationAware} action's message / errors and field errors into Http session.

In the 'RETRIEVE' mode, the interceptor will retrieve the stored action's message / errors and field errors and put them back into the {@link ValidationAware} action.

The interceptor does nothing in the 'NONE' mode, which is the default.

The operation mode could be switched using :-

1] Setting the iterceptor parameter eg.

<action name="submitApplication" ...>
<interceptor-ref name="store">
<param name="operationMode">l;STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
....
</action>
2] Through request parameter (allowRequestParameterSwitch must be 'true' which is the default)
// the request will have the operation mode in 'STORE'
http://localhost:8080/context/submitApplication.action?operationMode=STORE
  • allowRequestParameterSwitch - To enable request parameter that could switch the operation mode of this interceptor.
  • requestParameterSwitch - The request parameter that will indicate what mode this interceptor is in.
  • operationMode - The operation mode this interceptor should be in (either 'STORE', 'RETRIEVE' or 'NONE'). 'NONE' being the default.

The following method could be overriden :-

  • getRequestOperationMode - get the operation mode of this interceptor based on the request parameters
  • mergeCollection - merge two collections
  • mergeMap - merge two map


<action name="submitApplication" ....>
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="input" type="redirect">applicationFailed.action</result>
<result type="dispatcher">applicationSuccess.jsp</result>
</action>

<action name="applicationFailed" ....>
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<result>applicationFailed.jsp</result>
</action>


With the example above, 'submitApplication.action' will have the action messages / errors / field errors stored in the Http Session. Later when needed, (in this case, when 'applicationFailed.action' is fired, it will get the action messages / errors / field errors stored in the Http Session and put them back into the action.
version
$Date: 2006-11-06 10:01:43 -0500 (Mon, 06 Nov 2006) $ $Id: MessageStoreInterceptor.java 471756 2006-11-06 15:01:43Z husted $

Fields Summary
private static final long
serialVersionUID
private static final Log
_log
public static final String
STORE_MODE
public static final String
RETRIEVE_MODE
public static final String
NONE
private boolean
allowRequestParameterSwitch
private String
requestParameterSwitch
private String
operationMode
public static String
fieldErrorsSessionKey
public static String
actionErrorsSessionKey
public static String
actionMessagesSessionKey
Constructors Summary
Methods Summary
protected voidafter(com.opensymphony.xwork2.ActionInvocation invocation, java.lang.String result)
Handle the storing of field errors / action messages / field errors, which is done after action invocation, and the operationMode is in 'STORE'.

param
invocation
param
result
throws
Exception


        String reqOperationMode = getRequestOperationMode(invocation);
        if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
                STORE_MODE.equalsIgnoreCase(operationMode)) {

            Object action = invocation.getAction();
            if (action instanceof ValidationAware) {
                // store error / messages into session
                Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);

                _log.debug("store action ["+action+"] error/messages into session ");

                ValidationAware validationAwareAction = (ValidationAware) action;
                session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
                session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
                session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
            }
            else {
                _log.debug("Action ["+action+"] is not ValidationAware, no message / error that are storeable");
            }
        }
    
protected voidbefore(com.opensymphony.xwork2.ActionInvocation invocation)
Handle the retrieving of field errors / action messages / field errors, which is done before action invocation, and the operationMode is 'RETRIEVE'.

param
invocation
throws
Exception

        String reqOperationMode = getRequestOperationMode(invocation);

        if (RETRIEVE_MODE.equalsIgnoreCase(reqOperationMode) ||
                RETRIEVE_MODE.equalsIgnoreCase(operationMode)) {

            Object action = invocation.getAction();
            if (action instanceof ValidationAware) {
                // retrieve error / message from session
                Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
                ValidationAware validationAwareAction = (ValidationAware) action;

                _log.debug("retrieve error / message from session to populate into action ["+action+"]");

                Collection actionErrors = (Collection) session.get(actionErrorsSessionKey);
                Collection actionMessages = (Collection) session.get(actionMessagesSessionKey);
                Map fieldErrors = (Map) session.get(fieldErrorsSessionKey);

                if (actionErrors != null && actionErrors.size() > 0) {
                    Collection mergedActionErrors = mergeCollection(validationAwareAction.getActionErrors(), actionErrors);
                    validationAwareAction.setActionErrors(mergedActionErrors);
                }

                if (actionMessages != null && actionMessages.size() > 0) {
                    Collection mergedActionMessages = mergeCollection(validationAwareAction.getActionMessages(), actionMessages);
                    validationAwareAction.setActionMessages(mergedActionMessages);
                }

                if (fieldErrors != null && fieldErrors.size() > 0) {
                    Map mergedFieldErrors = mergeMap(validationAwareAction.getFieldErrors(), fieldErrors);
                    validationAwareAction.setFieldErrors(mergedFieldErrors);
                }
                session.remove(actionErrorsSessionKey);
                session.remove(actionMessagesSessionKey);
                session.remove(fieldErrorsSessionKey);
            }
        }
    
public voiddestroy()

    
public booleangetAllowRequestParameterSwitch()

        return this.allowRequestParameterSwitch;
    
public java.lang.StringgetOperationModel()

        return this.operationMode;
    
protected java.lang.StringgetRequestOperationMode(com.opensymphony.xwork2.ActionInvocation invocation)
Get the operationMode through request paramter, if allowRequestParameterSwitch is 'true', else it simply returns 'NONE', meaning its neither in the 'STORE_MODE' nor 'RETRIEVE_MODE'.

return
String

        String reqOperationMode = NONE;
        if (allowRequestParameterSwitch) {
            Map reqParams = (Map) invocation.getInvocationContext().get(ActionContext.PARAMETERS);
            boolean containsParameter = reqParams.containsKey(requestParameterSwitch);
            if (containsParameter) {
                String[] reqParamsArr = (String[]) reqParams.get(requestParameterSwitch);
                if (reqParamsArr != null && reqParamsArr.length > 0) {
                    reqOperationMode = reqParamsArr[0];
                }
            }
        }
        return reqOperationMode;
    
public java.lang.StringgetRequestParameterSwitch()

        return this.requestParameterSwitch;
    
public voidinit()

    
public java.lang.Stringintercept(com.opensymphony.xwork2.ActionInvocation invocation)

        _log.debug("entering MessageStoreInterceptor ...");

        before(invocation);
        String result = invocation.invoke();
        after(invocation, result);

        _log.debug("exit executing MessageStoreInterceptor");
        return result;
    
protected java.util.CollectionmergeCollection(java.util.Collection col1, java.util.Collection col2)
Merge col1 and col2 and return the composite Collection.

param
col1
param
col2
return
Collection

        Collection _col1 = (col1 == null ? new ArrayList() : col1);
        Collection _col2 = (col2 == null ? new ArrayList() : col2);
        _col1.addAll(_col2);
        return _col1;
    
protected java.util.MapmergeMap(java.util.Map map1, java.util.Map map2)
Merge map1 and map2 and return the composite Map

param
map1
param
map2
return
Map

        Map _map1 = (map1 == null ? new LinkedHashMap() : map1);
        Map _map2 = (map2 == null ? new LinkedHashMap() : map2);
        _map1.putAll(_map2);
        return _map1;
    
public voidsetAllowRequestParameterSwitch(boolean allowRequestParameterSwitch)




        
        this.allowRequestParameterSwitch = allowRequestParameterSwitch;
    
public voidsetOperationMode(java.lang.String operationMode)

        this.operationMode = operationMode;
    
public voidsetRequestParameterSwitch(java.lang.String requestParameterSwitch)

        this.requestParameterSwitch = requestParameterSwitch;