001: /*
002: * $Id: $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.interceptor;
022:
023: import com.opensymphony.xwork2.ActionInvocation;
024: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
025: import com.opensymphony.xwork2.util.CompoundRoot;
026: import com.opensymphony.xwork2.util.TextUtils;
027: import com.opensymphony.xwork2.util.ValueStack;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.struts2.portlet.PortletActionConstants;
031: import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
032:
033: import javax.portlet.ActionResponse;
034: import javax.portlet.RenderRequest;
035: import java.util.Map;
036:
037: public class PortletStateInterceptor extends AbstractInterceptor
038: implements PortletActionConstants {
039:
040: private final static Log LOG = LogFactory
041: .getLog(PortletStateInterceptor.class);
042:
043: private static final long serialVersionUID = 6138452063353911784L;
044:
045: @Override
046: public String intercept(ActionInvocation invocation)
047: throws Exception {
048: Integer phase = (Integer) invocation.getInvocationContext()
049: .get(PHASE);
050: if (RENDER_PHASE.equals(phase)) {
051: restoreStack(invocation);
052: return invocation.invoke();
053: } else if (EVENT_PHASE.equals(phase)) {
054: try {
055: return invocation.invoke();
056: } finally {
057: saveStack(invocation);
058: }
059: } else {
060: return invocation.invoke();
061: }
062: }
063:
064: @SuppressWarnings("unchecked")
065: private void saveStack(ActionInvocation invocation) {
066: Map session = invocation.getInvocationContext().getSession();
067: session.put(STACK_FROM_EVENT_PHASE, invocation.getStack());
068: ActionResponse actionResponse = (ActionResponse) invocation
069: .getInvocationContext().get(RESPONSE);
070: actionResponse.setRenderParameter(EVENT_ACTION, "true");
071: }
072:
073: @SuppressWarnings("unchecked")
074: private void restoreStack(ActionInvocation invocation) {
075: RenderRequest request = (RenderRequest) invocation
076: .getInvocationContext().get(REQUEST);
077: if (TextUtils.stringSet(request.getParameter(EVENT_ACTION))) {
078: Map session = invocation.getInvocationContext()
079: .getSession();
080: if (!isProperPrg(invocation)) {
081: LOG.debug("Restoring value stack from event phase");
082: ValueStack oldStack = (ValueStack) invocation
083: .getInvocationContext().getSession().get(
084: STACK_FROM_EVENT_PHASE);
085: if (oldStack != null) {
086: CompoundRoot oldRoot = oldStack.getRoot();
087: ValueStack currentStack = invocation.getStack();
088: CompoundRoot root = currentStack.getRoot();
089: root.addAll(oldRoot);
090: LOG.debug("Restored stack");
091: }
092: } else {
093: LOG
094: .debug("Won't restore stack from event phase since it's a proper PRG request");
095: }
096: }
097: }
098:
099: private boolean isProperPrg(ActionInvocation invocation) {
100: return !(invocation.getAction() instanceof DirectRenderFromEventAction);
101: }
102:
103: }
|