|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.valid; |
|
16 |
| |
|
17 |
| import java.net.MalformedURLException; |
|
18 |
| import java.net.URL; |
|
19 |
| import java.util.Collection; |
|
20 |
| import java.util.HashMap; |
|
21 |
| import java.util.Iterator; |
|
22 |
| import java.util.Locale; |
|
23 |
| import java.util.Map; |
|
24 |
| import java.util.ResourceBundle; |
|
25 |
| import java.util.Vector; |
|
26 |
| |
|
27 |
| import org.apache.tapestry.IMarkupWriter; |
|
28 |
| import org.apache.tapestry.IRequestCycle; |
|
29 |
| import org.apache.tapestry.form.IFormComponent; |
|
30 |
| import org.apache.tapestry.util.StringSplitter; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| public class UrlValidator extends BaseValidator |
|
36 |
| { |
|
37 |
| private int _minimumLength; |
|
38 |
| |
|
39 |
| private String _minimumLengthMessage; |
|
40 |
| |
|
41 |
| private String _invalidUrlFormatMessage; |
|
42 |
| |
|
43 |
| private String _disallowedProtocolMessage; |
|
44 |
| |
|
45 |
| private Collection _allowedProtocols; |
|
46 |
| |
|
47 |
| private String _scriptPath = "/org/apache/tapestry/valid/UrlValidator.script"; |
|
48 |
| |
|
49 |
18
| public UrlValidator()
|
|
50 |
| { |
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
0
| public UrlValidator(String initializer)
|
|
60 |
| { |
|
61 |
0
| super(initializer);
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| public String toString(IFormComponent field, Object value)
|
|
65 |
| { |
|
66 |
0
| if (value == null)
|
|
67 |
0
| return null;
|
|
68 |
| |
|
69 |
0
| return value.toString();
|
|
70 |
| } |
|
71 |
| |
|
72 |
18
| public Object toObject(IFormComponent field, String input) throws ValidatorException
|
|
73 |
| { |
|
74 |
18
| if (checkRequired(field, input))
|
|
75 |
0
| return null;
|
|
76 |
| |
|
77 |
18
| if (_minimumLength > 0 && input.length() < _minimumLength)
|
|
78 |
6
| throw new ValidatorException(buildMinimumLengthMessage(field),
|
|
79 |
| ValidationConstraint.MINIMUM_WIDTH); |
|
80 |
| |
|
81 |
12
| if (!isValidUrl(input))
|
|
82 |
6
| throw new ValidatorException(buildInvalidUrlFormatMessage(field),
|
|
83 |
| ValidationConstraint.URL_FORMAT); |
|
84 |
| |
|
85 |
6
| if (!isAllowedProtocol(input))
|
|
86 |
| { |
|
87 |
3
| throw new ValidatorException(buildDisallowedProtocolMessage(field),
|
|
88 |
| ValidationConstraint.DISALLOWED_PROTOCOL); |
|
89 |
| } |
|
90 |
| |
|
91 |
3
| return input;
|
|
92 |
| } |
|
93 |
| |
|
94 |
0
| public int getMinimumLength()
|
|
95 |
| { |
|
96 |
0
| return _minimumLength;
|
|
97 |
| } |
|
98 |
| |
|
99 |
6
| public void setMinimumLength(int minimumLength)
|
|
100 |
| { |
|
101 |
6
| _minimumLength = minimumLength;
|
|
102 |
| } |
|
103 |
| |
|
104 |
0
| public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
|
|
105 |
| IRequestCycle cycle) |
|
106 |
| { |
|
107 |
0
| if (!isClientScriptingEnabled())
|
|
108 |
0
| return;
|
|
109 |
| |
|
110 |
0
| Map symbols = new HashMap();
|
|
111 |
| |
|
112 |
0
| if (isRequired())
|
|
113 |
0
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
|
114 |
| |
|
115 |
0
| if (_minimumLength > 0)
|
|
116 |
0
| symbols.put("minimumLengthMessage",
|
|
117 |
| buildMinimumLengthMessage(field)); |
|
118 |
| |
|
119 |
0
| symbols.put("urlFormatMessage", buildInvalidUrlFormatMessage(field));
|
|
120 |
| |
|
121 |
0
| symbols.put("urlDisallowedProtocolMessage",
|
|
122 |
| buildDisallowedProtocolMessage(field)); |
|
123 |
| |
|
124 |
0
| symbols.put("urlRegexpProtocols", buildUrlRegexpProtocols());
|
|
125 |
| |
|
126 |
0
| processValidatorScript(_scriptPath, cycle, field, symbols);
|
|
127 |
| } |
|
128 |
| |
|
129 |
0
| private String buildUrlRegexpProtocols()
|
|
130 |
| { |
|
131 |
0
| if (_allowedProtocols == null)
|
|
132 |
| { |
|
133 |
0
| return null;
|
|
134 |
| } |
|
135 |
0
| String regexp = "/(";
|
|
136 |
0
| Iterator iter = _allowedProtocols.iterator();
|
|
137 |
0
| while (iter.hasNext())
|
|
138 |
| { |
|
139 |
0
| String protocol = (String) iter.next();
|
|
140 |
0
| regexp += protocol;
|
|
141 |
0
| if (iter.hasNext())
|
|
142 |
| { |
|
143 |
0
| regexp += "|";
|
|
144 |
| } |
|
145 |
| } |
|
146 |
0
| regexp += "):///";
|
|
147 |
0
| return regexp;
|
|
148 |
| } |
|
149 |
| |
|
150 |
0
| public String getScriptPath()
|
|
151 |
| { |
|
152 |
0
| return _scriptPath;
|
|
153 |
| } |
|
154 |
| |
|
155 |
0
| public void setScriptPath(String scriptPath)
|
|
156 |
| { |
|
157 |
0
| _scriptPath = scriptPath;
|
|
158 |
| } |
|
159 |
| |
|
160 |
12
| protected boolean isValidUrl(String url)
|
|
161 |
| { |
|
162 |
12
| boolean bIsValid;
|
|
163 |
12
| try
|
|
164 |
| { |
|
165 |
12
| new URL(url);
|
|
166 |
6
| bIsValid = true;
|
|
167 |
| } |
|
168 |
| catch (MalformedURLException mue) |
|
169 |
| { |
|
170 |
6
| bIsValid = false;
|
|
171 |
| } |
|
172 |
12
| return bIsValid;
|
|
173 |
| } |
|
174 |
| |
|
175 |
6
| protected boolean isAllowedProtocol(String url)
|
|
176 |
| { |
|
177 |
6
| boolean bIsAllowed = false;
|
|
178 |
6
| if (_allowedProtocols != null)
|
|
179 |
| { |
|
180 |
3
| URL oUrl;
|
|
181 |
3
| try
|
|
182 |
| { |
|
183 |
3
| oUrl = new URL(url);
|
|
184 |
| } |
|
185 |
| catch (MalformedURLException e) |
|
186 |
| { |
|
187 |
0
| return false;
|
|
188 |
| } |
|
189 |
3
| String actualProtocol = oUrl.getProtocol();
|
|
190 |
3
| Iterator iter = _allowedProtocols.iterator();
|
|
191 |
3
| while (iter.hasNext())
|
|
192 |
| { |
|
193 |
6
| String protocol = (String) iter.next();
|
|
194 |
6
| if (protocol.equals(actualProtocol))
|
|
195 |
| { |
|
196 |
0
| bIsAllowed = true;
|
|
197 |
0
| break;
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| } |
|
201 |
| else |
|
202 |
| { |
|
203 |
3
| bIsAllowed = true;
|
|
204 |
| } |
|
205 |
6
| return bIsAllowed;
|
|
206 |
| } |
|
207 |
| |
|
208 |
0
| public String getInvalidUrlFormatMessage()
|
|
209 |
| { |
|
210 |
0
| return _invalidUrlFormatMessage;
|
|
211 |
| } |
|
212 |
| |
|
213 |
0
| public String getMinimumLengthMessage()
|
|
214 |
| { |
|
215 |
0
| return _minimumLengthMessage;
|
|
216 |
| } |
|
217 |
| |
|
218 |
3
| public void setInvalidUrlFormatMessage(String string)
|
|
219 |
| { |
|
220 |
3
| _invalidUrlFormatMessage = string;
|
|
221 |
| } |
|
222 |
| |
|
223 |
0
| public String getDisallowedProtocolMessage()
|
|
224 |
| { |
|
225 |
0
| return _disallowedProtocolMessage;
|
|
226 |
| } |
|
227 |
| |
|
228 |
0
| public void setDisallowedProtocolMessage(String string)
|
|
229 |
| { |
|
230 |
0
| _disallowedProtocolMessage = string;
|
|
231 |
| } |
|
232 |
| |
|
233 |
3
| public void setMinimumLengthMessage(String string)
|
|
234 |
| { |
|
235 |
3
| _minimumLengthMessage = string;
|
|
236 |
| } |
|
237 |
| |
|
238 |
6
| protected String buildMinimumLengthMessage(IFormComponent field)
|
|
239 |
| { |
|
240 |
6
| String pattern = getPattern(_minimumLengthMessage, "field-too-short",
|
|
241 |
| field.getPage().getLocale()); |
|
242 |
| |
|
243 |
6
| return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
|
|
244 |
| } |
|
245 |
| |
|
246 |
6
| protected String buildInvalidUrlFormatMessage(IFormComponent field)
|
|
247 |
| { |
|
248 |
6
| String pattern = getPattern(_invalidUrlFormatMessage, "invalid-url-format",
|
|
249 |
| field.getPage().getLocale()); |
|
250 |
| |
|
251 |
6
| return formatString(pattern, field.getDisplayName());
|
|
252 |
| } |
|
253 |
| |
|
254 |
3
| protected String buildDisallowedProtocolMessage(IFormComponent field)
|
|
255 |
| { |
|
256 |
3
| if (_allowedProtocols == null)
|
|
257 |
| { |
|
258 |
0
| return null;
|
|
259 |
| } |
|
260 |
3
| String pattern = getPattern(_disallowedProtocolMessage, "disallowed-protocol",
|
|
261 |
| field.getPage().getLocale()); |
|
262 |
| |
|
263 |
3
| String allowedProtocols = "";
|
|
264 |
3
| Iterator iter = _allowedProtocols.iterator();
|
|
265 |
3
| while (iter.hasNext())
|
|
266 |
| { |
|
267 |
6
| String protocol = (String) iter.next();
|
|
268 |
6
| if (!allowedProtocols.equals("")) {
|
|
269 |
3
| if (iter.hasNext())
|
|
270 |
| { |
|
271 |
0
| allowedProtocols += ", ";
|
|
272 |
| } |
|
273 |
| else |
|
274 |
| { |
|
275 |
3
| allowedProtocols += " or ";
|
|
276 |
| } |
|
277 |
| } |
|
278 |
6
| allowedProtocols += protocol;
|
|
279 |
| } |
|
280 |
| |
|
281 |
3
| return formatString(pattern, allowedProtocols);
|
|
282 |
| } |
|
283 |
| |
|
284 |
15
| protected String getPattern(String override, String key, Locale locale)
|
|
285 |
| { |
|
286 |
15
| if (override != null)
|
|
287 |
6
| return override;
|
|
288 |
| |
|
289 |
9
| ResourceBundle strings = ResourceBundle.getBundle(
|
|
290 |
| "org.apache.tapestry.valid.ValidationStrings", |
|
291 |
| locale); |
|
292 |
9
| return strings.getString(key);
|
|
293 |
| } |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
3
| public void setAllowedProtocols(String protocols)
|
|
300 |
| { |
|
301 |
3
| StringSplitter spliter = new StringSplitter(',');
|
|
302 |
| |
|
303 |
3
| String[] aProtocols = spliter.splitToArray(protocols);
|
|
304 |
3
| _allowedProtocols = new Vector();
|
|
305 |
3
| for (int i = 0; i < aProtocols.length; i++)
|
|
306 |
| { |
|
307 |
6
| _allowedProtocols.add(aProtocols[i]);
|
|
308 |
| } |
|
309 |
| } |
|
310 |
| |
|
311 |
| } |