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.PortletRequest;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts2.StrutsStatics;
28 import org.apache.struts2.portlet.context.PortletActionContext;
29
30 import com.opensymphony.xwork2.ActionContext;
31 import com.opensymphony.xwork2.ActionInvocation;
32 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
33
34
35 /***
36 * <!-- START SNIPPET: description -->
37 *
38 * An interceptor which provides an implementation of PortletPreferences if the Action implements
39 * PortletPreferencesAware.
40 *
41 * If running in a servlet environment, a testing implementation of PortletPreferences will be
42 * created and provided, but it should not be used in a production environment.
43 *
44 * <!-- END SNIPPET: description -->
45 *
46 * <p/> <u>Interceptor parameters:</u>
47 *
48 * <!-- START SNIPPET: parameters -->
49 *
50 * <ul>
51 *
52 * <li>None</li>
53 *
54 * </ul>
55 *
56 * <!-- END SNIPPET: parameters -->
57 *
58 * <p/> <u>Extending the interceptor:</u>
59 *
60 * <p/>
61 *
62 * <!-- START SNIPPET: extending -->
63 *
64 * There are no known extension points for this interceptor.
65 *
66 * <!-- END SNIPPET: extending -->
67 *
68 * <p/> <u>Example code:</u>
69 *
70 * <pre>
71 * <!-- START SNIPPET: example -->
72 * <action name="someAction" class="com.examples.SomeAction">
73 * <interceptor-ref name="portlet-preferences"/>
74 * <interceptor-ref name="basicStack"/>
75 * <result name="success">good_result.ftl</result>
76 * </action>
77 * <!-- END SNIPPET: example -->
78 * </pre>
79 *
80 * @see PortletPreferencesAware
81 */
82 public class PortletPreferencesInterceptor extends AbstractInterceptor implements StrutsStatics {
83
84 private static final Log LOG = LogFactory.getLog(PortletPreferencesInterceptor.class);
85
86 public String intercept(ActionInvocation invocation) throws Exception {
87 final Object action = invocation.getAction();
88 final ActionContext context = invocation.getInvocationContext();
89
90 if (action instanceof PortletPreferencesAware) {
91 PortletRequest request = PortletActionContext.getRequest();
92 PortletPreferencesAware awareAction = (PortletPreferencesAware) action;
93
94
95 if (request == null) {
96 LOG.warn("This portlet preferences implementation should only be used during development");
97 awareAction.setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession()));
98 } else {
99 awareAction.setPortletPreferences(request.getPreferences());
100 }
101 }
102 return invocation.invoke();
103 }
104 }