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.interceptor;
22
23 import java.util.Map;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.portlet.PortletRequest;
29
30 import org.apache.struts2.StrutsStatics;
31 import org.apache.struts2.servlet.interceptor.ServletPrincipalProxy;
32 import org.apache.struts2.portlet.PortletActionConstants;
33 import org.apache.struts2.portlet.interceptor.PortletPrincipalProxy;
34 import org.apache.struts2.util.ServletContextAware;
35
36 import com.opensymphony.xwork2.ActionContext;
37 import com.opensymphony.xwork2.ActionInvocation;
38 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
39
40
41 /***
42 * <!-- START SNIPPET: description -->
43 *
44 * An interceptor which sets action properties based on the interfaces an action implements. For example, if the action
45 * implements {@link ParameterAware} then the action context's parameter map will be set on it.
46 *
47 * <p/> This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the
48 * servlet context, the session, etc. Interfaces that it supports are:
49 *
50 * <ul>
51 *
52 * <li>{@link ServletContextAware}</li>
53 *
54 * <li>{@link ServletRequestAware}</li>
55 *
56 * <li>{@link ServletResponseAware}</li>
57 *
58 * <li>{@link ParameterAware}</li>
59 *
60 * <li>{@link RequestAware}</li>
61 *
62 * <li>{@link SessionAware}</li>
63 *
64 * <li>{@link ApplicationAware}</li>
65 *
66 * <li>{@link PrincipalAware}</li>
67 *
68 * </ul>
69 *
70 * <!-- END SNIPPET: description -->
71 *
72 * <p/> <u>Interceptor parameters:</u>
73 *
74 * <!-- START SNIPPET: parameters -->
75 *
76 * <ul>
77 *
78 * <li>None</li>
79 *
80 * </ul>
81 *
82 * <!-- END SNIPPET: parameters -->
83 *
84 * <p/> <u>Extending the interceptor:</u>
85 *
86 * <p/>
87 *
88 * <!-- START SNIPPET: extending -->
89 *
90 * There are no known extension points for this interceptor.
91 *
92 * <!-- END SNIPPET: extending -->
93 *
94 * <p/> <u>Example code:</u>
95 *
96 * <pre>
97 * <!-- START SNIPPET: example -->
98 * <action name="someAction" class="com.examples.SomeAction">
99 * <interceptor-ref name="servlet-config"/>
100 * <interceptor-ref name="basicStack"/>
101 * <result name="success">good_result.ftl</result>
102 * </action>
103 * <!-- END SNIPPET: example -->
104 * </pre>
105 *
106 * @see ServletContextAware
107 * @see ServletRequestAware
108 * @see ServletResponseAware
109 * @see ParameterAware
110 * @see SessionAware
111 * @see ApplicationAware
112 * @see PrincipalAware
113 */
114 public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
115
116 private static final long serialVersionUID = 605261777858676638L;
117
118 /***
119 * Sets action properties based on the interfaces an action implements. Things like application properties,
120 * parameters, session attributes, etc are set based on the implementing interface.
121 *
122 * @param invocation an encapsulation of the action execution state.
123 * @throws Exception if an error occurs when setting action properties.
124 */
125 public String intercept(ActionInvocation invocation) throws Exception {
126 final Object action = invocation.getAction();
127 final ActionContext context = invocation.getInvocationContext();
128
129 if (action instanceof ServletRequestAware) {
130 HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
131 ((ServletRequestAware) action).setServletRequest(request);
132 }
133
134 if (action instanceof ServletResponseAware) {
135 HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
136 ((ServletResponseAware) action).setServletResponse(response);
137 }
138
139 if (action instanceof ParameterAware) {
140 ((ParameterAware) action).setParameters(context.getParameters());
141 }
142
143 if (action instanceof RequestAware) {
144 ((RequestAware) action).setRequest((Map) context.get("request"));
145 }
146
147 if (action instanceof SessionAware) {
148 ((SessionAware) action).setSession(context.getSession());
149 }
150
151 if (action instanceof ApplicationAware) {
152 ((ApplicationAware) action).setApplication(context.getApplication());
153 }
154
155 if (action instanceof PrincipalAware) {
156 HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
157 Object portletRequest = context.get(PortletActionConstants.REQUEST);
158 if (portletRequest != null) {
159
160 ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy((PortletRequest) portletRequest));
161 } else {
162
163 ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
164 }
165 }
166 if (action instanceof ServletContextAware) {
167 ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
168 ((ServletContextAware) action).setServletContext(servletContext);
169 }
170 return invocation.invoke();
171 }
172 }