Home » struts-2.1.8.1-src » org.apache » struts2 » portlet » interceptor » [javadoc | source]

    1   /*
    2    * $Id: PortletStateInterceptor.java 757834 2009-03-24 15:14:07Z wesw $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   
   22   package org.apache.struts2.portlet.interceptor;
   23   
   24   import java.util.Map;
   25   
   26   import javax.portlet.ActionResponse;
   27   import javax.portlet.RenderRequest;
   28   
   29   import org.apache.struts2.portlet.PortletActionConstants;
   30   import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
   31   import org.apache.commons.lang.xwork.StringUtils;
   32   
   33   import com.opensymphony.xwork2.ActionInvocation;
   34   import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
   35   import com.opensymphony.xwork2.util.CompoundRoot;
   36   import com.opensymphony.xwork2.util.ValueStack;
   37   import com.opensymphony.xwork2.util.logging.Logger;
   38   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   39   
   40   public class PortletStateInterceptor extends AbstractInterceptor implements PortletActionConstants {
   41   
   42   	private final static Logger LOG = LoggerFactory.getLogger(PortletStateInterceptor.class);
   43   
   44   	private static final long serialVersionUID = 6138452063353911784L;
   45   
   46   	@Override
   47   	public String intercept(ActionInvocation invocation) throws Exception {
   48   		Integer phase = (Integer) invocation.getInvocationContext().get(PHASE);
   49   		if (RENDER_PHASE.equals(phase)) {
   50   			restoreStack(invocation);
   51   			return invocation.invoke();
   52   		} else if (EVENT_PHASE.equals(phase)) {
   53   			try {
   54   				return invocation.invoke();
   55   			} finally {
   56   				saveStack(invocation);
   57   			}
   58   		} else {
   59   			return invocation.invoke();
   60   		}
   61   	}
   62   
   63   	@SuppressWarnings("unchecked")
   64   	private void saveStack(ActionInvocation invocation) {
   65   		Map session = invocation.getInvocationContext().getSession();
   66   		session.put(STACK_FROM_EVENT_PHASE, invocation.getStack());
   67   		ActionResponse actionResponse = (ActionResponse) invocation.getInvocationContext().get(RESPONSE);
   68   		actionResponse.setRenderParameter(EVENT_ACTION, "true");
   69   	}
   70   
   71   	@SuppressWarnings("unchecked")
   72   	private void restoreStack(ActionInvocation invocation) {
   73   		RenderRequest request = (RenderRequest) invocation.getInvocationContext().get(REQUEST);
   74   		if (StringUtils.isNotEmpty(request.getParameter(EVENT_ACTION))) {
   75   			if(!isProperPrg(invocation)) {
   76   				LOG.debug("Restoring value stack from event phase");
   77   				ValueStack oldStack = (ValueStack) invocation.getInvocationContext().getSession().get(
   78   				STACK_FROM_EVENT_PHASE);
   79   				if (oldStack != null) {
   80   					CompoundRoot oldRoot = oldStack.getRoot();
   81   					ValueStack currentStack = invocation.getStack();
   82   					CompoundRoot root = currentStack.getRoot();
   83   					root.addAll(0, oldRoot);
   84   					LOG.debug("Restored stack");
   85   				}
   86   			}
   87   			else {
   88   				LOG.debug("Won't restore stack from event phase since it's a proper PRG request");
   89   			}
   90   		}
   91   	}
   92   
   93   	private boolean isProperPrg(ActionInvocation invocation) {
   94   		return !(invocation.getAction() instanceof DirectRenderFromEventAction);
   95   	}
   96   
   97   }

Home » struts-2.1.8.1-src » org.apache » struts2 » portlet » interceptor » [javadoc | source]