1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2;
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 import javax.servlet.jsp.PageContext;
30
31 import org.apache.struts2.dispatcher.mapper.ActionMapping;
32
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.util.ValueStack;
35
36
37 /***
38 * Web-specific context information for actions. This class subclasses <tt>ActionContext</tt> which
39 * provides access to things like the action name, value stack, etc. This class adds access to
40 * web objects like servlet parameters, request attributes and things like the HTTP session.
41 */
42 public class ServletActionContext extends ActionContext implements StrutsStatics {
43
44 private static final long serialVersionUID = -666854718275106687L;
45
46 public static final String STRUTS_VALUESTACK_KEY = "struts.valueStack";
47 public static final String ACTION_MAPPING = "struts.actionMapping";
48
49 @SuppressWarnings("unused")
50 private ServletActionContext(Map context) {
51 super(context);
52 }
53
54 /***
55 * Gets the current action context
56 *
57 * @param req The request
58 * @return The current action context
59 */
60 public static ActionContext getActionContext(HttpServletRequest req) {
61 ValueStack vs = getValueStack(req);
62 if (vs != null) {
63 return new ActionContext(vs.getContext());
64 } else {
65 return null;
66 }
67 }
68
69 /***
70 * Gets the current value stack for this request
71 *
72 * @param req The request
73 * @return The value stack
74 */
75 public static ValueStack getValueStack(HttpServletRequest req) {
76 return (ValueStack) req.getAttribute(STRUTS_VALUESTACK_KEY);
77 }
78
79 /***
80 * Gets the action mapping for this context
81 *
82 * @return The action mapping
83 */
84 public static ActionMapping getActionMapping() {
85 return (ActionMapping) ActionContext.getContext().get(ACTION_MAPPING);
86 }
87
88 /***
89 * Returns the HTTP page context.
90 *
91 * @return the HTTP page context.
92 */
93 public static PageContext getPageContext() {
94 return (PageContext) ActionContext.getContext().get(PAGE_CONTEXT);
95 }
96
97 /***
98 * Sets the HTTP servlet request object.
99 *
100 * @param request the HTTP servlet request object.
101 */
102 public static void setRequest(HttpServletRequest request) {
103 ActionContext.getContext().put(HTTP_REQUEST, request);
104 }
105
106 /***
107 * Gets the HTTP servlet request object.
108 *
109 * @return the HTTP servlet request object.
110 */
111 public static HttpServletRequest getRequest() {
112 return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);
113 }
114
115 /***
116 * Sets the HTTP servlet response object.
117 *
118 * @param response the HTTP servlet response object.
119 */
120 public static void setResponse(HttpServletResponse response) {
121 ActionContext.getContext().put(HTTP_RESPONSE, response);
122 }
123
124 /***
125 * Gets the HTTP servlet response object.
126 *
127 * @return the HTTP servlet response object.
128 */
129 public static HttpServletResponse getResponse() {
130 return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
131 }
132
133 /***
134 * Gets the servlet context.
135 *
136 * @return the servlet context.
137 */
138 public static ServletContext getServletContext() {
139 return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);
140 }
141
142 /***
143 * Sets the current servlet context object
144 *
145 * @param servletContext The servlet context to use
146 */
147 public static void setServletContext(ServletContext servletContext) {
148 ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);
149 }
150 }