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