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 | 0 | private String _scriptPath = "/org/apache/tapestry/valid/UrlValidator.script"; |
49 | |
|
50 | |
public UrlValidator() |
51 | 0 | { |
52 | 0 | } |
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 | 0 | if (checkRequired(field, input)) return null; |
76 | |
|
77 | 0 | if (_minimumLength > 0 && input.length() < _minimumLength) |
78 | 0 | throw new ValidatorException(buildMinimumLengthMessage(field), |
79 | |
ValidationConstraint.MINIMUM_WIDTH); |
80 | |
|
81 | 0 | if (!isValidUrl(input)) |
82 | 0 | throw new ValidatorException(buildInvalidUrlFormatMessage(field), |
83 | |
ValidationConstraint.URL_FORMAT); |
84 | |
|
85 | 0 | if (!isAllowedProtocol(input)) { throw new ValidatorException( |
86 | |
buildDisallowedProtocolMessage(field), |
87 | |
ValidationConstraint.DISALLOWED_PROTOCOL); } |
88 | |
|
89 | 0 | return input; |
90 | |
} |
91 | |
|
92 | |
public int getMinimumLength() |
93 | |
{ |
94 | 0 | return _minimumLength; |
95 | |
} |
96 | |
|
97 | |
public void setMinimumLength(int minimumLength) |
98 | |
{ |
99 | 0 | _minimumLength = minimumLength; |
100 | 0 | } |
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 | 0 | new URL(url); |
159 | 0 | bIsValid = true; |
160 | |
} |
161 | 0 | catch (MalformedURLException mue) |
162 | |
{ |
163 | 0 | bIsValid = false; |
164 | 0 | } |
165 | 0 | return bIsValid; |
166 | |
} |
167 | |
|
168 | |
protected boolean isAllowedProtocol(String url) |
169 | |
{ |
170 | 0 | boolean bIsAllowed = false; |
171 | 0 | if (_allowedProtocols != null) |
172 | |
{ |
173 | |
URL oUrl; |
174 | |
try |
175 | |
{ |
176 | 0 | oUrl = new URL(url); |
177 | |
} |
178 | 0 | catch (MalformedURLException e) |
179 | |
{ |
180 | 0 | return false; |
181 | 0 | } |
182 | 0 | String actualProtocol = oUrl.getProtocol(); |
183 | 0 | Iterator iter = _allowedProtocols.iterator(); |
184 | 0 | while(iter.hasNext()) |
185 | |
{ |
186 | 0 | String protocol = (String) iter.next(); |
187 | 0 | if (protocol.equals(actualProtocol)) |
188 | |
{ |
189 | 0 | bIsAllowed = true; |
190 | 0 | break; |
191 | |
} |
192 | 0 | } |
193 | 0 | } |
194 | |
else |
195 | |
{ |
196 | 0 | bIsAllowed = true; |
197 | |
} |
198 | 0 | 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 | 0 | _invalidUrlFormatMessage = string; |
214 | 0 | } |
215 | |
|
216 | |
public String getDisallowedProtocolMessage() |
217 | |
{ |
218 | 0 | return _disallowedProtocolMessage; |
219 | |
} |
220 | |
|
221 | |
public void setDisallowedProtocolMessage(String string) |
222 | |
{ |
223 | 0 | _disallowedProtocolMessage = string; |
224 | 0 | } |
225 | |
|
226 | |
public void setMinimumLengthMessage(String string) |
227 | |
{ |
228 | 0 | _minimumLengthMessage = string; |
229 | 0 | } |
230 | |
|
231 | |
protected String buildMinimumLengthMessage(IFormComponent field) |
232 | |
{ |
233 | 0 | String pattern = getPattern(_minimumLengthMessage, "field-too-short", |
234 | |
field.getPage().getLocale()); |
235 | |
|
236 | 0 | return formatString(pattern, Integer.toString(_minimumLength), field |
237 | |
.getDisplayName()); |
238 | |
} |
239 | |
|
240 | |
protected String buildInvalidUrlFormatMessage(IFormComponent field) |
241 | |
{ |
242 | 0 | String pattern = getPattern(_invalidUrlFormatMessage, |
243 | |
"invalid-url-format", |
244 | |
field.getPage().getLocale()); |
245 | |
|
246 | 0 | return formatString(pattern, field.getDisplayName()); |
247 | |
} |
248 | |
|
249 | |
protected String buildDisallowedProtocolMessage(IFormComponent field) |
250 | |
{ |
251 | 0 | if (_allowedProtocols == null) { return null; } |
252 | 0 | String pattern = getPattern(_disallowedProtocolMessage, |
253 | |
"disallowed-protocol", |
254 | |
field.getPage().getLocale()); |
255 | |
|
256 | 0 | String allowedProtocols = ""; |
257 | 0 | Iterator iter = _allowedProtocols.iterator(); |
258 | 0 | while(iter.hasNext()) |
259 | |
{ |
260 | 0 | String protocol = (String) iter.next(); |
261 | 0 | if (!allowedProtocols.equals("")) { |
262 | 0 | if (iter.hasNext()) |
263 | |
{ |
264 | 0 | allowedProtocols += ", "; |
265 | |
} |
266 | |
else |
267 | |
{ |
268 | 0 | allowedProtocols += " or "; |
269 | |
} |
270 | |
} |
271 | 0 | allowedProtocols += protocol; |
272 | 0 | } |
273 | |
|
274 | 0 | return formatString(pattern, allowedProtocols); |
275 | |
} |
276 | |
|
277 | |
protected String getPattern(String override, String key, Locale locale) |
278 | |
{ |
279 | 0 | if (override != null) return override; |
280 | |
|
281 | 0 | ResourceBundle strings = ResourceBundle.getBundle( |
282 | |
"org.apache.tapestry.valid.ValidationStrings", locale); |
283 | 0 | return strings.getString(key); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
public void setAllowedProtocols(String protocols) |
291 | |
{ |
292 | 0 | StringSplitter spliter = new StringSplitter(','); |
293 | |
|
294 | 0 | String[] aProtocols = spliter.splitToArray(protocols); |
295 | 0 | _allowedProtocols = new Vector(); |
296 | 0 | for(int i = 0; i < aProtocols.length; i++) |
297 | |
{ |
298 | 0 | _allowedProtocols.add(aProtocols[i]); |
299 | |
} |
300 | 0 | } |
301 | |
|
302 | |
} |