View Javadoc

1   /*
2    * $Id: ServletConfigInterceptor.java 728521 2008-12-21 21:51:53Z nilsga $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
97   *     &lt;interceptor-ref name="servletConfig"/&gt;
98   *     &lt;interceptor-ref name="basicStack"/&gt;
99   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
100  * &lt;/action&gt;
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                 // We are in servtlet environment, so principal information resides in HttpServletRequest
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 }