1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.contexts;
22
23 import org.apache.commons.chain.web.jakarta.servlet.ServletWebContext;
24 import org.apache.struts.Globals;
25 import org.apache.struts.action.ActionMessages;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.chain.Constants;
28 import org.apache.struts.config.ActionConfig;
29 import org.apache.struts.util.MessageResources;
30
31 import jakarta.servlet.ServletContext;
32 import jakarta.servlet.http.HttpServletRequest;
33 import jakarta.servlet.http.HttpServletResponse;
34
35
36
37
38
39 public class ServletActionContext extends WebActionContext {
40
41
42
43
44
45
46 public ServletActionContext(ServletWebContext context) {
47 super(context);
48 }
49
50
51
52
53
54
55
56
57
58 public ServletActionContext(ServletContext context,
59 HttpServletRequest request, HttpServletResponse response) {
60 this(new ServletWebContext(context, request, response));
61 }
62
63
64
65
66
67
68 protected ServletWebContext servletWebContext() {
69 return (ServletWebContext) this.getBaseContext();
70 }
71
72 public void release() {
73 this.servletWebContext().release();
74 super.release();
75 }
76
77
78
79
80
81
82
83
84
85
86 public ServletContext getContext() {
87 return servletWebContext().getContext();
88 }
89
90
91
92
93
94
95 public HttpServletRequest getRequest() {
96 return servletWebContext().getRequest();
97 }
98
99
100
101
102
103
104 public HttpServletResponse getResponse() {
105 return servletWebContext().getResponse();
106 }
107
108
109
110
111
112
113 public ActionServlet getActionServlet() {
114 return (ActionServlet) this.get(Constants.ACTION_SERVLET_KEY);
115 }
116
117
118
119
120
121
122 public void setActionServlet(ActionServlet servlet) {
123 this.put(Constants.ACTION_SERVLET_KEY, servlet);
124 }
125
126
127
128
129 public void setActionConfig(ActionConfig actionConfig) {
130 super.setActionConfig(actionConfig);
131 this.getRequestScope().put(Globals.MAPPING_KEY, actionConfig);
132
133
134 }
135
136 public MessageResources getMessageResources() {
137 return ((MessageResources) getRequest().getAttribute(Globals.MESSAGES_KEY));
138 }
139
140
141
142 public MessageResources getMessageResources(String key) {
143
144 ServletContext context = getActionServlet().getServletContext();
145
146
147 return (MessageResources) context.getAttribute(key
148 + getModuleConfig().getPrefix());
149 }
150
151 public void setMessageResources(MessageResources resources) {
152 super.setMessageResources(resources);
153 this.getRequest().setAttribute(Globals.MESSAGES_KEY, resources);
154 }
155
156
157
158
159
160
161
162
163 public void setMessageResources(String key, MessageResources resources) {
164 this.getRequest().setAttribute(key + getModuleConfig().getPrefix(),
165 resources);
166 }
167
168
169
170
171 public void saveErrors(ActionMessages errors) {
172
173 if ((errors == null) || errors.isEmpty()) {
174 getRequest().removeAttribute(Globals.ERROR_KEY);
175
176 return;
177 }
178
179
180 getRequest().setAttribute(Globals.ERROR_KEY, errors);
181 }
182
183 public void saveMessages(ActionMessages messages) {
184 if ((messages == null) || messages.isEmpty()) {
185 getRequest().removeAttribute(Globals.MESSAGE_KEY);
186
187 return;
188 }
189
190 getRequest().setAttribute(Globals.MESSAGE_KEY, messages);
191 }
192
193 public void addMessages(ActionMessages messages) {
194 if (messages == null) {
195 return;
196 }
197
198 ActionMessages requestMessages = getMessages();
199
200 if (requestMessages == null) {
201 requestMessages = new ActionMessages();
202 }
203
204 requestMessages.add(messages);
205 saveMessages(requestMessages);
206 }
207
208 public void addErrors(ActionMessages errors) {
209 if (errors == null) {
210 return;
211 }
212
213 ActionMessages requestErrors = getErrors();
214
215 if (requestErrors == null) {
216 requestErrors = new ActionMessages();
217 }
218
219 requestErrors.add(errors);
220 saveErrors(requestErrors);
221 }
222
223 public ActionMessages getErrors() {
224 return (ActionMessages) this.getRequest().getAttribute(Globals.ERROR_KEY);
225 }
226
227 public ActionMessages getMessages() {
228 return (ActionMessages) this.getRequest().getAttribute(Globals.MESSAGE_KEY);
229 }
230
231
232
233
234
235
236
237 public void saveToken() {
238 token.saveToken(getRequest());
239 }
240
241 public String generateToken() {
242 return token.generateToken(getRequest());
243 }
244
245 public boolean isTokenValid(boolean reset) {
246 return token.isTokenValid(getRequest(), reset);
247 }
248
249 public void resetToken() {
250 token.resetToken(getRequest());
251 }
252 }