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