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