001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import com.opensymphony.webwork.WebWorkStatics;
008: import com.opensymphony.webwork.util.ServletContextAware;
009: import com.opensymphony.xwork.ActionContext;
010: import com.opensymphony.xwork.ActionInvocation;
011: import com.opensymphony.xwork.interceptor.AroundInterceptor;
012:
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015: import javax.servlet.ServletContext;
016:
017: /**
018: * <!-- START SNIPPET: description -->
019: *
020: * An interceptor which sets action properties based on the interfaces an action implements. For example, if the action
021: * implements {@link ParameterAware} then the action context's parameter map will be set on it.
022: *
023: * <p/> This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the
024: * servlet context, the session, etc. Interfaces that it supports are:
025: *
026: * <ul>
027: *
028: * <li>{@link ServletContextAware}</li>
029: *
030: * <li>{@link ServletRequestAware}</li>
031: *
032: * <li>{@link ServletResponseAware}</li>
033: *
034: * <li>{@link ParameterAware}</li>
035: *
036: * <li>{@link SessionAware}</li>
037: *
038: * <li>{@link ApplicationAware}</li>
039: *
040: * <li>{@link PrincipalAware}</li>
041: *
042: * </ul>
043: *
044: * <!-- END SNIPPET: description -->
045: *
046: * <p/> <u>Interceptor parameters:</u>
047: *
048: * <!-- START SNIPPET: parameters -->
049: *
050: * <ul>
051: *
052: * <li>None</li>
053: *
054: * </ul>
055: *
056: * <!-- END SNIPPET: parameters -->
057: *
058: * <p/> <u>Extending the interceptor:</u>
059: *
060: * <p/>
061: *
062: * <!-- START SNIPPET: extending -->
063: *
064: * There are no known extension points for this interceptor.
065: *
066: * <!-- END SNIPPET: extending -->
067: *
068: * <p/> <u>Example code:</u>
069: *
070: * <pre>
071: * <!-- START SNIPPET: example -->
072: * <action name="someAction" class="com.examples.SomeAction">
073: * <interceptor-ref name="servlet-config"/>
074: * <interceptor-ref name="basicStack"/>
075: * <result name="success">good_result.ftl</result>
076: * </action>
077: * <!-- END SNIPPET: example -->
078: * </pre>
079: *
080: * @author Patrick Lightbody
081: * @author Bill Lynch (docs)
082: * @see ServletContextAware
083: * @see ServletRequestAware
084: * @see ServletResponseAware
085: * @see ParameterAware
086: * @see SessionAware
087: * @see ApplicationAware
088: * @see PrincipalAware
089: */
090: public class ServletConfigInterceptor extends AroundInterceptor
091: implements WebWorkStatics {
092:
093: private static final long serialVersionUID = -1226074297258786788L;
094:
095: protected void after(ActionInvocation dispatcher, String result)
096: throws Exception {
097: }
098:
099: /**
100: * Sets action properties based on the interfaces an action implements. Things like application properties,
101: * parameters, session attributes, etc are set based on the implementing interface.
102: *
103: * @param invocation an encapsulation of the action execution state.
104: * @throws Exception if an error occurs when setting action properties.
105: */
106: protected void before(ActionInvocation invocation) throws Exception {
107: final Object action = invocation.getAction();
108: final ActionContext context = invocation.getInvocationContext();
109:
110: if (action instanceof ServletRequestAware) {
111: HttpServletRequest request = (HttpServletRequest) context
112: .get(HTTP_REQUEST);
113: ((ServletRequestAware) action).setServletRequest(request);
114: }
115:
116: if (action instanceof ServletResponseAware) {
117: HttpServletResponse response = (HttpServletResponse) context
118: .get(HTTP_RESPONSE);
119: ((ServletResponseAware) action)
120: .setServletResponse(response);
121: }
122:
123: if (action instanceof ParameterAware) {
124: ((ParameterAware) action).setParameters(context
125: .getParameters());
126: }
127:
128: if (action instanceof SessionAware) {
129: ((SessionAware) action).setSession(context.getSession());
130: }
131:
132: if (action instanceof ApplicationAware) {
133: ((ApplicationAware) action).setApplication(context
134: .getApplication());
135: }
136:
137: if (action instanceof PrincipalAware) {
138: HttpServletRequest request = (HttpServletRequest) context
139: .get(HTTP_REQUEST);
140: ((PrincipalAware) action)
141: .setPrincipalProxy(new PrincipalProxy(request));
142: }
143: if (action instanceof ServletContextAware) {
144: ServletContext servletContext = (ServletContext) context
145: .get(SERVLET_CONTEXT);
146: ((ServletContextAware) action)
147: .setServletContext(servletContext);
148: }
149: }
150: }
|