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