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