Class UrlValidator
- java.lang.Object
-
- org.apache.wicket.validation.validator.UrlValidator
-
- All Implemented Interfaces:
java.io.Serializable,org.apache.wicket.util.io.IClusterable,IValidator<java.lang.String>
public class UrlValidator extends java.lang.Object implements IValidator<java.lang.String>
Validator for checking URLs. The default schemes allowed arehttp://,https://, andftp://.The behavior of validation is modified by passing in one of these options:
ALLOW_2_SLASHES - [FALSE]: Allows double '/' characters in the path component.NO_FRAGMENT- [FALSE]: By default fragments are allowed. If this option is included then fragments are flagged as illegal.ALLOW_ALL_SCHEMES - [FALSE]: By default only http, https, and ftp are considered valid schemes. Enabling this option will let any scheme pass validation.
This was originally based
org.apache.commons.validator.UrlValidator, but the dependency on Jakarta-ORO was removed and it now uses java.util.regexp instead. Usage example:<code> Component.add(new UrlValidator({"http", "https"})); </code>- Since:
- 1.2.6
- Author:
- Vincent Demay
- See Also:
- "http://www.ietf.org/rfc/rfc2396.txt", Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intALLOW_2_SLASHESAllow two slashes in the path component of theURL.static intALLOW_ALL_SCHEMESAllows all validly-formatted schemes to pass validation instead of supplying a set of valid schemes.protected java.lang.String[]defaultSchemesIf no schemes are provided, default to this set of protocols.static intNO_FRAGMENTSEnabling this option disallows anyURLfragments.
-
Constructor Summary
Constructors Constructor Description UrlValidator()Constructs aUrlValidatorwith default properties.UrlValidator(int options)Constructs aUrlValidatorwith the given validation options.UrlValidator(java.lang.String[] schemes)Constructs aUrlValidatorwith the givenStringarray of scheme options.UrlValidator(java.lang.String[] schemes, int options)Constructs aUrlValidatorwith the given scheme and validation options (see class description).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intcountToken(java.lang.String token, java.lang.String target)Returns the number of times the token appears in the target.protected IValidationErrordecorate(IValidationError error, IValidatable<java.lang.String> validatable)Allows subclasses to decorate reported errorsstatic booleanisBlankOrNull(java.lang.String value)Checks if the field isn'tnulland if length of the field is greater than zero, not including whitespace.booleanisOff(long flag)Tests whether the given flag is off.booleanisOn(long flag)Tests whether the given flag is on.booleanisValid(java.lang.String value)Checks if a field has a validURL.protected booleanisValidAuthority(java.lang.String authority)Returnstrueif the authority is properly formatted.protected booleanisValidFragment(java.lang.String fragment)Returnstrueif the given fragment isnullor fragments are allowed.protected booleanisValidPath(java.lang.String path)Returnstrueif the path is valid.protected booleanisValidQuery(java.lang.String query)Returnstrueif the query isnullor if it's a properly-formatted query string.protected booleanisValidScheme(java.lang.String scheme)Validates a scheme.voidvalidate(IValidatable<java.lang.String> validatable)Validates theIValidatableinstance.
-
-
-
Field Detail
-
ALLOW_ALL_SCHEMES
public static final int ALLOW_ALL_SCHEMES
Allows all validly-formatted schemes to pass validation instead of supplying a set of valid schemes.- See Also:
- Constant Field Values
-
ALLOW_2_SLASHES
public static final int ALLOW_2_SLASHES
Allow two slashes in the path component of theURL.- See Also:
- Constant Field Values
-
NO_FRAGMENTS
public static final int NO_FRAGMENTS
Enabling this option disallows anyURLfragments.- See Also:
- Constant Field Values
-
defaultSchemes
protected java.lang.String[] defaultSchemes
If no schemes are provided, default to this set of protocols.
-
-
Constructor Detail
-
UrlValidator
public UrlValidator()
Constructs aUrlValidatorwith default properties.
-
UrlValidator
public UrlValidator(java.lang.String[] schemes)
Constructs aUrlValidatorwith the givenStringarray of scheme options. The validation is modified by passing in options in theschemesargument.- Parameters:
schemes- Pass in one or moreURLschemes to consider valid. Passing in anullwill default to "http,https,ftp" being used. If a non-nullscheme is specified, then all valid schemes must be specified. Setting theALLOW_ALL_SCHEMESoption will ignore the contents ofschemes.
-
UrlValidator
public UrlValidator(int options)
Constructs aUrlValidatorwith the given validation options.- Parameters:
options- The options should be set using the public constants declared in this class. To set multiple options you simply add them together. For example,ALLOW_2_SLASHES+NO_FRAGMENTSenables both of those options.
-
UrlValidator
public UrlValidator(java.lang.String[] schemes, int options)Constructs aUrlValidatorwith the given scheme and validation options (see class description).- Parameters:
schemes- Pass in one or moreURLschemes to consider valid. Passing in anullwill default to "http,https,ftp" being used. If a non-nullscheme is specified, then all valid schemes must be specified. Setting theALLOW_ALL_SCHEMESoption will ignore the contents ofschemes.options- The options should be set using the public constants declared in this class. To set multiple options you simply add them together. For example,ALLOW_2_SLASHES+NO_FRAGMENTSenables both of those options.
-
-
Method Detail
-
validate
public void validate(IValidatable<java.lang.String> validatable)
Description copied from interface:IValidatorValidates theIValidatableinstance. Validation errors should be reported using theIValidatable.error(IValidationError)method.- Specified by:
validatein interfaceIValidator<java.lang.String>- Parameters:
validatable- theIValidatableinstance being validated
-
decorate
protected IValidationError decorate(IValidationError error, IValidatable<java.lang.String> validatable)
Allows subclasses to decorate reported errors- Parameters:
error-- Returns:
- decorated error
-
isValid
public final boolean isValid(java.lang.String value)
Checks if a field has a validURL. This method is public because it is directly used in tests.- Parameters:
value- The value validation is being performed on. Anullvalue is considered invalid.- Returns:
trueif theURLis valid
-
isValidScheme
protected boolean isValidScheme(java.lang.String scheme)
Validates a scheme. If schemes[] was initialized to non-null, then only those schemes are allowed. Note that this is slightly different than for the constructor.- Parameters:
scheme- The scheme to validate. Anullvalue is considered invalid.- Returns:
trueif theURLis valid
-
isValidAuthority
protected boolean isValidAuthority(java.lang.String authority)
Returnstrueif the authority is properly formatted. An authority is the combination of host name and port. Anullauthority value is considered invalid.- Parameters:
authority- an authority value to validate- Returns:
- true if authority (host name and port) is valid.
-
isValidPath
protected boolean isValidPath(java.lang.String path)
Returnstrueif the path is valid. Anullvalue is considered invalid.- Parameters:
path- a path value to validate.- Returns:
trueif path is valid.
-
isValidQuery
protected boolean isValidQuery(java.lang.String query)
Returnstrueif the query isnullor if it's a properly-formatted query string.- Parameters:
query- a query value to validate- Returns:
trueif the query is valid
-
isValidFragment
protected boolean isValidFragment(java.lang.String fragment)
Returnstrueif the given fragment isnullor fragments are allowed.- Parameters:
fragment- a fragment value to validate- Returns:
trueif the fragment is valid
-
countToken
protected int countToken(java.lang.String token, java.lang.String target)Returns the number of times the token appears in the target.- Parameters:
token- a token value to be countedtarget- a targetStringto count tokens in- Returns:
- the number of tokens
-
isBlankOrNull
public static boolean isBlankOrNull(java.lang.String value)
Checks if the field isn'tnulland if length of the field is greater than zero, not including whitespace.- Parameters:
value- the value validation is being performed on- Returns:
trueif blank ornull
-
isOn
public boolean isOn(long flag)
Tests whether the given flag is on. If the flag is not a power of 2 (ie. 3) this tests whether the combination of flags is on.- Parameters:
flag- flag value to check- Returns:
- whether the specified flag value is on
-
isOff
public boolean isOff(long flag)
Tests whether the given flag is off. If the flag is not a power of 2 (ie. 3) this tests whether the combination of flags is off.- Parameters:
flag- flag value to check.- Returns:
- whether the specified flag value is off
-
-