KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > interceptor > ServletConfigInterceptor


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.interceptor;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.webwork.WebWorkStatics;
9 import com.opensymphony.webwork.util.ServletContextAware;
10 import com.opensymphony.xwork.ActionContext;
11 import com.opensymphony.xwork.ActionInvocation;
12 import com.opensymphony.xwork.interceptor.AroundInterceptor;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17
18 /**
19  * An interceptor which sets action properties based on the interfaces an action implements.
20  * For example, if the action implements {@link ParameterAware} then the action
21  * context's parameter map will be set on it. <p>
22  * <p/>
23  * This interceptor is designed to set all properties an action needs if it's aware of servlet
24  * parametes, the servlet context, the session, etc.
25  *
26  * @author Patrick Lightbody
27  * @author Bill Lynch (docs)
28  * @see ServletContextAware
29  * @see ServletRequestAware
30  * @see ServletResponseAware
31  * @see ParameterAware
32  * @see SessionAware
33  * @see ApplicationAware
34  * @see PrincipalAware
35  */

36 public class ServletConfigInterceptor extends AroundInterceptor implements WebWorkStatics {
37     //~ Methods ////////////////////////////////////////////////////////////////
38

39     /**
40      * Does nothing.
41      */

42     protected void after(ActionInvocation dispatcher, String JavaDoc result) throws Exception JavaDoc {
43     }
44
45     /**
46      * Sets action properties based on the interfaces an action implements. Things like
47      * application properties, parameters, session attributes, etc are set based on
48      * the implementing interface.
49      *
50      * @param invocation an encapsulation of the action execution state.
51      * @throws Exception if an error occurs when setting action properties.
52      */

53     protected void before(ActionInvocation invocation) throws Exception JavaDoc {
54         final Object JavaDoc action = invocation.getAction();
55         final ActionContext context = ActionContext.getContext();
56
57         if (action instanceof ServletRequestAware) {
58             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) context.get(HTTP_REQUEST);
59             ((ServletRequestAware) action).setServletRequest(request);
60         }
61
62         if (action instanceof ServletResponseAware) {
63             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) context.get(HTTP_RESPONSE);
64             ((ServletResponseAware) action).setServletResponse(response);
65         }
66
67         if (action instanceof ParameterAware) {
68             ((ParameterAware) action).setParameters(context.getParameters());
69         }
70
71         if (action instanceof SessionAware) {
72             ((SessionAware) action).setSession(context.getSession());
73         }
74
75         if (action instanceof ApplicationAware) {
76             ((ApplicationAware) action).setApplication(context.getApplication());
77         }
78
79         if (action instanceof PrincipalAware) {
80             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) context.get(HTTP_REQUEST);
81             ((PrincipalAware) action).setPrincipalProxy(new PrincipalProxy(request));
82         }
83         if (action instanceof ServletContextAware) {
84             ((ServletContextAware) action).setServletContext(ServletActionContext.getServletContext());
85         }
86     }
87 }
88
Popular Tags