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