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