001: /*
002: * $Id: ServletConfigInterceptor.java 502196 2007-02-01 11:28:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import java.util.Map;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.portlet.PortletRequest;
029:
030: import org.apache.struts2.StrutsStatics;
031: import org.apache.struts2.servlet.interceptor.ServletPrincipalProxy;
032: import org.apache.struts2.portlet.PortletActionConstants;
033: import org.apache.struts2.portlet.interceptor.PortletPrincipalProxy;
034: import org.apache.struts2.util.ServletContextAware;
035:
036: import com.opensymphony.xwork2.ActionContext;
037: import com.opensymphony.xwork2.ActionInvocation;
038: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
039:
040: /**
041: * <!-- START SNIPPET: description -->
042: *
043: * An interceptor which sets action properties based on the interfaces an action implements. For example, if the action
044: * implements {@link ParameterAware} then the action context's parameter map will be set on it.
045: *
046: * <p/> This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the
047: * servlet context, the session, etc. Interfaces that it supports are:
048: *
049: * <ul>
050: *
051: * <li>{@link ServletContextAware}</li>
052: *
053: * <li>{@link ServletRequestAware}</li>
054: *
055: * <li>{@link ServletResponseAware}</li>
056: *
057: * <li>{@link ParameterAware}</li>
058: *
059: * <li>{@link RequestAware}</li>
060: *
061: * <li>{@link SessionAware}</li>
062: *
063: * <li>{@link ApplicationAware}</li>
064: *
065: * <li>{@link PrincipalAware}</li>
066: *
067: * </ul>
068: *
069: * <!-- END SNIPPET: description -->
070: *
071: * <p/> <u>Interceptor parameters:</u>
072: *
073: * <!-- START SNIPPET: parameters -->
074: *
075: * <ul>
076: *
077: * <li>None</li>
078: *
079: * </ul>
080: *
081: * <!-- END SNIPPET: parameters -->
082: *
083: * <p/> <u>Extending the interceptor:</u>
084: *
085: * <p/>
086: *
087: * <!-- START SNIPPET: extending -->
088: *
089: * There are no known extension points for this interceptor.
090: *
091: * <!-- END SNIPPET: extending -->
092: *
093: * <p/> <u>Example code:</u>
094: *
095: * <pre>
096: * <!-- START SNIPPET: example -->
097: * <action name="someAction" class="com.examples.SomeAction">
098: * <interceptor-ref name="servlet-config"/>
099: * <interceptor-ref name="basicStack"/>
100: * <result name="success">good_result.ftl</result>
101: * </action>
102: * <!-- END SNIPPET: example -->
103: * </pre>
104: *
105: * @see ServletContextAware
106: * @see ServletRequestAware
107: * @see ServletResponseAware
108: * @see ParameterAware
109: * @see SessionAware
110: * @see ApplicationAware
111: * @see PrincipalAware
112: */
113: public class ServletConfigInterceptor extends AbstractInterceptor
114: 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)
126: throws Exception {
127: final Object action = invocation.getAction();
128: final ActionContext context = invocation.getInvocationContext();
129:
130: if (action instanceof ServletRequestAware) {
131: HttpServletRequest request = (HttpServletRequest) context
132: .get(HTTP_REQUEST);
133: ((ServletRequestAware) action).setServletRequest(request);
134: }
135:
136: if (action instanceof ServletResponseAware) {
137: HttpServletResponse response = (HttpServletResponse) context
138: .get(HTTP_RESPONSE);
139: ((ServletResponseAware) action)
140: .setServletResponse(response);
141: }
142:
143: if (action instanceof ParameterAware) {
144: ((ParameterAware) action).setParameters(context
145: .getParameters());
146: }
147:
148: if (action instanceof RequestAware) {
149: ((RequestAware) action).setRequest((Map) context
150: .get("request"));
151: }
152:
153: if (action instanceof SessionAware) {
154: ((SessionAware) action).setSession(context.getSession());
155: }
156:
157: if (action instanceof ApplicationAware) {
158: ((ApplicationAware) action).setApplication(context
159: .getApplication());
160: }
161:
162: if (action instanceof PrincipalAware) {
163: HttpServletRequest request = (HttpServletRequest) context
164: .get(HTTP_REQUEST);
165: Object portletRequest = context
166: .get(PortletActionConstants.REQUEST);
167: if (portletRequest != null) {
168: // We are in portlet environment, so principal information resides in PortletRequest
169: ((PrincipalAware) action)
170: .setPrincipalProxy(new PortletPrincipalProxy(
171: (PortletRequest) portletRequest));
172: } else {
173: // We are in servtlet environment, so principal information resides in HttpServletRequest
174: ((PrincipalAware) action)
175: .setPrincipalProxy(new ServletPrincipalProxy(
176: request));
177: }
178: }
179: if (action instanceof ServletContextAware) {
180: ServletContext servletContext = (ServletContext) context
181: .get(SERVLET_CONTEXT);
182: ((ServletContextAware) action)
183: .setServletContext(servletContext);
184: }
185: return invocation.invoke();
186: }
187: }
|