1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32 import com.opensymphony.xwork2.ActionInvocation;
33 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
34 import com.opensymphony.xwork2.util.CompoundRoot;
35 import com.opensymphony.xwork2.util.TextUtils;
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 (TextUtils.stringSet(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(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 }