/
ActionSupport.java
354 lines (303 loc) · 11.3 KB
/
ActionSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.opensymphony.xwork2;
import com.opensymphony.xwork2.conversion.impl.ConversionData;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.ValidationAware;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import java.io.Serializable;
import java.util.*;
/**
* Provides a default implementation for the most common actions.
* See the documentation for all the interfaces this class implements for more detailed information.
*/
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
private static final Logger LOG = LogManager.getLogger(ActionSupport.class);
private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
private transient TextProvider textProvider;
private transient LocaleProvider localeProvider;
protected Container container;
@Override
public void setActionErrors(Collection<String> errorMessages) {
validationAware.setActionErrors(errorMessages);
}
@Override
public Collection<String> getActionErrors() {
return validationAware.getActionErrors();
}
@Override
public void setActionMessages(Collection<String> messages) {
validationAware.setActionMessages(messages);
}
@Override
public Collection<String> getActionMessages() {
return validationAware.getActionMessages();
}
@Override
public void setFieldErrors(Map<String, List<String>> errorMap) {
validationAware.setFieldErrors(errorMap);
}
@Override
public Map<String, List<String>> getFieldErrors() {
return validationAware.getFieldErrors();
}
@Override
public Locale getLocale() {
return getLocaleProvider().getLocale();
}
@Override
public boolean isValidLocaleString(String localeStr) {
return getLocaleProvider().isValidLocaleString(localeStr);
}
@Override
public boolean isValidLocale(Locale locale) {
return getLocaleProvider().isValidLocale(locale);
}
@Override
public boolean hasKey(String key) {
return getTextProvider().hasKey(key);
}
@Override
public String getText(String aTextName) {
return getTextProvider().getText(aTextName);
}
@Override
public String getText(String aTextName, String defaultValue) {
return getTextProvider().getText(aTextName, defaultValue);
}
@Override
public String getText(String aTextName, String defaultValue, String obj) {
return getTextProvider().getText(aTextName, defaultValue, obj);
}
@Override
public String getText(String aTextName, List<?> args) {
return getTextProvider().getText(aTextName, args);
}
@Override
public String getText(String key, String[] args) {
return getTextProvider().getText(key, args);
}
@Override
public String getText(String aTextName, String defaultValue, List<?> args) {
return getTextProvider().getText(aTextName, defaultValue, args);
}
@Override
public String getText(String key, String defaultValue, String[] args) {
return getTextProvider().getText(key, defaultValue, args);
}
@Override
public String getText(String key, String defaultValue, List<?> args, ValueStack stack) {
return getTextProvider().getText(key, defaultValue, args, stack);
}
@Override
public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
return getTextProvider().getText(key, defaultValue, args, stack);
}
/**
* Dedicated method to support I10N and conversion errors
*
* @param key message which contains formatting string
* @param expr that should be formatted
* @return formatted expr with format specified by key
*/
public String getFormatted(String key, String expr) {
Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
if (conversionErrors.containsKey(expr)) {
String[] vals = (String[]) conversionErrors.get(expr).getValue();
return vals[0];
} else {
final ValueStack valueStack = ActionContext.getContext().getValueStack();
final Object val = valueStack.findValue(expr);
return getText(key, Arrays.asList(val));
}
}
@Override
public ResourceBundle getTexts() {
return getTextProvider().getTexts();
}
@Override
public ResourceBundle getTexts(String aBundleName) {
return getTextProvider().getTexts(aBundleName);
}
@Override
public void addActionError(String anErrorMessage) {
validationAware.addActionError(anErrorMessage);
}
@Override
public void addActionMessage(String aMessage) {
validationAware.addActionMessage(aMessage);
}
@Override
public void addFieldError(String fieldName, String errorMessage) {
validationAware.addFieldError(fieldName, errorMessage);
}
public String input() throws Exception {
return INPUT;
}
/**
* A default implementation that does nothing an returns "success".
*
* <p>
* Subclasses should override this method to provide their business logic.
* </p>
*
* <p>
* See also {@link com.opensymphony.xwork2.Action#execute()}.
* </p>
*
* @return returns {@link #SUCCESS}
* @throws Exception can be thrown by subclasses.
*/
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public boolean hasActionErrors() {
return validationAware.hasActionErrors();
}
@Override
public boolean hasActionMessages() {
return validationAware.hasActionMessages();
}
@Override
public boolean hasErrors() {
return validationAware.hasErrors();
}
@Override
public boolean hasFieldErrors() {
return validationAware.hasFieldErrors();
}
/**
* Clears field errors. Useful for Continuations and other situations
* where you might want to clear parts of the state on the same action.
*/
public void clearFieldErrors() {
validationAware.clearFieldErrors();
}
/**
* Clears action errors. Useful for Continuations and other situations
* where you might want to clear parts of the state on the same action.
*/
public void clearActionErrors() {
validationAware.clearActionErrors();
}
/**
* Clears messages. Useful for Continuations and other situations
* where you might want to clear parts of the state on the same action.
*/
public void clearMessages() {
validationAware.clearMessages();
}
/**
* Clears all errors. Useful for Continuations and other situations
* where you might want to clear parts of the state on the same action.
*/
public void clearErrors() {
validationAware.clearErrors();
}
/**
* Clears all errors and messages. Useful for Continuations and other situations
* where you might want to clear parts of the state on the same action.
*/
public void clearErrorsAndMessages() {
validationAware.clearErrorsAndMessages();
}
/**
* A default implementation that validates nothing.
* Subclasses should override this method to provide validations.
*/
@Override
public void validate() {
// A default implementation that validates nothing
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* <!-- START SNIPPET: pause-method -->
* Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return
* the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc.
*
* <p>
* The next time this action is invoked (and using the same continuation ID), the method will resume immediately
* after where this method was called, with the entire call stack in the execute method restored.
* </p>
*
* <p>
* Note: this method can <b>only</b> be called within the {@link #execute()} method.
* </p>
*
* <!-- END SNIPPET: pause-method -->
*
* @param result the result to return - the same type of return value in the {@link #execute()} method.
*/
public void pause(String result) {
}
/**
* If called first time it will create {@link com.opensymphony.xwork2.TextProviderFactory},
* inject dependency (if {@link com.opensymphony.xwork2.inject.Container} is accesible) into in,
* then will create new {@link com.opensymphony.xwork2.TextProvider} and store it in a field
* for future references and at the returns reference to that field
*
* @return reference to field with TextProvider
*/
protected TextProvider getTextProvider() {
if (textProvider == null) {
final TextProviderFactory tpf = getContainer().getInstance(TextProviderFactory.class);
textProvider = tpf.createInstance(getClass());
}
return textProvider;
}
protected LocaleProvider getLocaleProvider() {
if (localeProvider == null) {
final LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class);
localeProvider = localeProviderFactory.createLocaleProvider();
}
return localeProvider;
}
/**
* TODO: This a temporary solution, maybe we should consider stop injecting container into beans
*/
protected Container getContainer() {
if (container == null) {
container = ActionContext.getContext().getContainer();
if (container != null) {
boolean devMode = Boolean.parseBoolean(container.getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
if (devMode) {
LOG.warn("Container is null, action was created manually? Fallback to ActionContext");
} else {
LOG.debug("Container is null, action was created manually? Fallback to ActionContext");
}
} else {
LOG.warn("Container is null, action was created out of ActionContext scope?!?");
}
}
return container;
}
@Inject
public void setContainer(Container container) {
this.container = container;
}
}