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.validator;
22
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.StringTokenizer;
28
29 import jakarta.servlet.ServletContext;
30 import jakarta.servlet.ServletException;
31 import jakarta.servlet.UnavailableException;
32
33 import org.apache.commons.validator.ValidatorResources;
34 import org.apache.struts.action.ActionServlet;
35 import org.apache.struts.action.PlugIn;
36 import org.apache.struts.config.ModuleConfig;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.xml.sax.SAXException;
40
41
42
43
44
45
46
47
48
49 public class ValidatorPlugIn implements PlugIn {
50
51
52
53
54 private final Logger log =
55 LoggerFactory.getLogger(ValidatorPlugIn.class);
56
57
58
59
60 private final static String RESOURCE_DELIM = ",";
61
62
63
64
65
66 public final static String VALIDATOR_KEY =
67 "org.apache.commons.validator.VALIDATOR_RESOURCES";
68
69
70
71
72
73
74 public final static String STOP_ON_ERROR_KEY =
75 "org.apache.struts.validator.STOP_ON_ERROR";
76
77
78
79
80
81
82
83
84
85 private ActionServlet servlet = null;
86
87
88
89
90
91 protected ValidatorResources resources = null;
92
93
94
95
96
97
98 private String pathnames = null;
99
100
101
102
103
104
105 private boolean stopOnFirstError = true;
106
107
108
109
110
111
112 public String getPathnames() {
113 return pathnames;
114 }
115
116
117
118
119
120
121 public void setPathnames(String pathnames) {
122 this.pathnames = pathnames;
123 }
124
125
126
127
128
129
130
131
132
133 public boolean isStopOnFirstError() {
134 return this.stopOnFirstError;
135 }
136
137
138
139
140
141
142
143
144
145
146 public void setStopOnFirstError(boolean stopOnFirstError) {
147 this.stopOnFirstError = stopOnFirstError;
148 }
149
150
151
152
153
154
155
156
157 public void init(ActionServlet servlet, ModuleConfig config)
158 throws ServletException {
159
160
161
162 this.servlet = servlet;
163
164
165 String validatorModuleKey = VALIDATOR_KEY + config.getPrefix();
166 ServletContext servletContext = servlet.getServletContext();
167 if (servletContext.getAttribute(validatorModuleKey) != null) {
168 throw new UnavailableException("ValidatorPlugIn cannot be " +
169 "redefined for module '" + config.getPrefix() + "'");
170 }
171
172
173 try {
174 this.initResources();
175 servletContext.setAttribute(validatorModuleKey, resources);
176 servletContext.setAttribute(STOP_ON_ERROR_KEY + '.'
177 + config.getPrefix(),
178 (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
179 } catch (Exception e) {
180 log.error(e.getMessage(), e);
181 throw new UnavailableException(
182 "Cannot load a validator resource from '" + pathnames + "'");
183 }
184 }
185
186
187
188
189
190 public void destroy() {
191 log.debug("Destroying ValidatorPlugin");
192
193 servlet = null;
194
195
196 destroyResources();
197 }
198
199
200
201
202
203
204
205 protected void initResources()
206 throws IOException, ServletException {
207 if ((pathnames == null) || (pathnames.length() <= 0)) {
208 return;
209 }
210
211 StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
212
213 List<URL> urlList = new ArrayList<>();
214
215 try {
216 while (st.hasMoreTokens()) {
217 String validatorRules = st.nextToken().trim();
218
219 log.info("Loading validation rules file from '{}'",
220 validatorRules);
221
222 URL input =
223 servlet.getServletContext().getResource(validatorRules);
224
225
226
227 if (input == null) {
228 input = getClass().getResource(validatorRules);
229 }
230
231 if (input != null) {
232 urlList.add(input);
233 } else {
234 throw new ServletException(
235 "Skipping validation rules file from '"
236 + validatorRules + "'. No url could be located.");
237 }
238 }
239
240 URL[] urlArray = urlList.toArray(new URL[0]);
241
242 this.resources = new ValidatorResources(urlArray);
243 } catch (SAXException sex) {
244 log.error("Skipping all validation", sex);
245 throw new ServletException(sex);
246 }
247 }
248
249
250
251
252 protected void destroyResources() {
253 resources = null;
254 }
255 }