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 javax.portlet.PortletContext;
25 import javax.portlet.PortletRequest;
26 import javax.portlet.PortletResponse;
27
28 import org.apache.struts2.StrutsStatics;
29 import org.apache.struts2.interceptor.PrincipalAware;
30 import org.apache.struts2.portlet.PortletActionConstants;
31
32 import com.opensymphony.xwork2.ActionContext;
33 import com.opensymphony.xwork2.ActionInvocation;
34 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
35 import com.opensymphony.xwork2.util.logging.Logger;
36 import com.opensymphony.xwork2.util.logging.LoggerFactory;
37
38 public class PortletAwareInterceptor extends AbstractInterceptor implements PortletActionConstants, StrutsStatics {
39
40 private static final long serialVersionUID = 2476509721059587700L;
41
42 private static final Logger LOG = LoggerFactory.getLogger(PortletAwareInterceptor.class);
43
44 /***
45 * Sets action properties based on the interfaces an action implements. Things like application properties,
46 * parameters, session attributes, etc are set based on the implementing interface.
47 *
48 * @param invocation an encapsulation of the action execution state.
49 * @throws Exception if an error occurs when setting action properties.
50 */
51 public String intercept(ActionInvocation invocation) throws Exception {
52 final Object action = invocation.getAction();
53 final ActionContext context = invocation.getInvocationContext();
54
55 if (action instanceof PortletRequestAware) {
56 PortletRequest request = (PortletRequest) context.get(REQUEST);
57 ((PortletRequestAware) action).setPortletRequest(request);
58 }
59
60 if (action instanceof PortletResponseAware) {
61 PortletResponse response = (PortletResponse) context.get(RESPONSE);
62 ((PortletResponseAware) action).setPortletResponse(response);
63 }
64 if (action instanceof PrincipalAware) {
65 PortletRequest request = (PortletRequest) context.get(REQUEST);
66 ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request));
67 }
68 if (action instanceof PortletContextAware) {
69 PortletContext portletContext = (PortletContext) context.get(STRUTS_PORTLET_CONTEXT);
70 ((PortletContextAware) action).setPortletContext(portletContext);
71 }
72 if (action instanceof PortletPreferencesAware) {
73 PortletRequest request = (PortletRequest) context.get(REQUEST);
74
75
76 if (request == null) {
77 LOG.warn("This portlet preferences implementation should only be used during development");
78 ((PortletPreferencesAware)action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession()));
79 } else {
80 ((PortletPreferencesAware)action).setPortletPreferences(request.getPreferences());
81 }
82 }
83 return invocation.invoke();
84 }
85 }