View Javadoc

1   /*
2    * $Id: ServletConfigInterceptor.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.interceptor;
19  
20  import java.util.Map;
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.StrutsStatics;
27  import org.apache.struts2.util.ServletContextAware;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.ActionInvocation;
31  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
32  
33  
34  /***
35   * <!-- START SNIPPET: description -->
36   *
37   * An interceptor which sets action properties based on the interfaces an action implements. For example, if the action
38   * implements {@link ParameterAware} then the action context's parameter map will be set on it.
39   *
40   * <p/> This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the
41   * servlet context, the session, etc. Interfaces that it supports are:
42   *
43   * <ul>
44   *
45   * <li>{@link ServletContextAware}</li>
46   *
47   * <li>{@link ServletRequestAware}</li>
48   *
49   * <li>{@link ServletResponseAware}</li>
50   *
51   * <li>{@link ParameterAware}</li>
52   * 
53   * <li>{@link RequestAware}</li>
54   *
55   * <li>{@link SessionAware}</li>
56   *
57   * <li>{@link ApplicationAware}</li>
58   *
59   * <li>{@link PrincipalAware}</li>
60   *
61   * </ul>
62   *
63   * <!-- END SNIPPET: description -->
64   *
65   * <p/> <u>Interceptor parameters:</u>
66   *
67   * <!-- START SNIPPET: parameters -->
68   *
69   * <ul>
70   *
71   * <li>None</li>
72   *
73   * </ul>
74   *
75   * <!-- END SNIPPET: parameters -->
76   *
77   * <p/> <u>Extending the interceptor:</u>
78   *
79   * <p/>
80   *
81   * <!-- START SNIPPET: extending -->
82   *
83   * There are no known extension points for this interceptor.
84   *
85   * <!-- END SNIPPET: extending -->
86   *
87   * <p/> <u>Example code:</u>
88   *
89   * <pre>
90   * <!-- START SNIPPET: example -->
91   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
92   *     &lt;interceptor-ref name="servlet-config"/&gt;
93   *     &lt;interceptor-ref name="basicStack"/&gt;
94   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
95   * &lt;/action&gt;
96   * <!-- END SNIPPET: example -->
97   * </pre>
98   *
99   * @see ServletContextAware
100  * @see ServletRequestAware
101  * @see ServletResponseAware
102  * @see ParameterAware
103  * @see SessionAware
104  * @see ApplicationAware
105  * @see PrincipalAware
106  */
107 public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
108 	
109 	private static final long serialVersionUID = 605261777858676638L;
110 
111     /***
112      * Sets action properties based on the interfaces an action implements. Things like application properties,
113      * parameters, session attributes, etc are set based on the implementing interface.
114      *
115      * @param invocation an encapsulation of the action execution state.
116      * @throws Exception if an error occurs when setting action properties.
117      */
118     public String intercept(ActionInvocation invocation) throws Exception {
119         final Object action = invocation.getAction();
120         final ActionContext context = invocation.getInvocationContext();
121 
122         if (action instanceof ServletRequestAware) {
123             HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
124             ((ServletRequestAware) action).setServletRequest(request);
125         }
126 
127         if (action instanceof ServletResponseAware) {
128             HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
129             ((ServletResponseAware) action).setServletResponse(response);
130         }
131 
132         if (action instanceof ParameterAware) {
133             ((ParameterAware) action).setParameters(context.getParameters());
134         }
135         
136         if (action instanceof RequestAware) {
137         	((RequestAware) action).setRequest((Map) context.get("request"));
138         }
139 
140         if (action instanceof SessionAware) {
141             ((SessionAware) action).setSession(context.getSession());
142         }
143 
144         if (action instanceof ApplicationAware) {
145             ((ApplicationAware) action).setApplication(context.getApplication());
146         }
147 
148         if (action instanceof PrincipalAware) {
149             HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
150             ((PrincipalAware) action).setPrincipalProxy(new PrincipalProxy(request));
151         }
152         if (action instanceof ServletContextAware) {
153             ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
154             ((ServletContextAware) action).setServletContext(servletContext);
155         }
156         return invocation.invoke();
157     }
158 }