Class DomainValidator
- java.lang.Object
-
- com.networknt.schema.format.DomainValidator
-
- All Implemented Interfaces:
Serializable
public class DomainValidator extends Object implements Serializable
Domain name validation routines.
This validator provides methods for validating Internet domain names and top-level domains.
Domain names are evaluated according to the standards RFC1034, section 3, and RFC1123, section 2.1. No accommodation is provided for the specialized needs of other applications; if the domain name has been URL-encoded, for example, validation will fail even though the equivalent plaintext version of the same name would have passed.
Validation is also provided for top-level domains (TLDs) as defined and maintained by the Internet Assigned Numbers Authority (IANA):
isValidInfrastructureTld(java.lang.String)
- validates infrastructure TLDs (.arpa
, etc.)isValidGenericTld(java.lang.String)
- validates generic TLDs (.com, .org
, etc.)isValidCountryCodeTld(java.lang.String)
- validates country code TLDs (.us, .uk, .cn
, etc.)
(NOTE: This class does not provide IP address lookup for domain names or methods to ensure that a given domain name matches a specific IP; see
InetAddress
for that functionality.)- Since:
- Validator 1.4
- Version:
- $Revision$
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DomainValidator.ArrayType
enum used byupdateTLDOverride(ArrayType, String[])
to determine which override array to update / fetch
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DomainValidator
getInstance()
Returns the singleton instance of this validator.static DomainValidator
getInstance(boolean allowLocal)
Returns the singleton instance of this validator, with local validation as required.static String[]
getTLDEntries(DomainValidator.ArrayType table)
Get a copy of the internal array.boolean
isValid(String domain)
Returns true if the specifiedString
parses as a valid domain name with a recognized top-level domain.boolean
isValidCountryCodeTld(String ccTld)
Returns true if the specifiedString
matches any IANA-defined country code top-level domain.boolean
isValidGenericTld(String gTld)
Returns true if the specifiedString
matches any IANA-defined generic top-level domain.boolean
isValidInfrastructureTld(String iTld)
Returns true if the specifiedString
matches any IANA-defined infrastructure top-level domain.boolean
isValidLocalTld(String lTld)
Returns true if the specifiedString
matches any widely used "local" domains (localhost or localdomain).boolean
isValidTld(String tld)
Returns true if the specifiedString
matches any IANA-defined top-level domain.static void
updateTLDOverride(DomainValidator.ArrayType table, String[] tlds)
Update one of the TLD override arrays.
-
-
-
Method Detail
-
getInstance
public static DomainValidator getInstance()
Returns the singleton instance of this validator. It will not consider local addresses as valid.- Returns:
- the singleton instance of this validator
-
getInstance
public static DomainValidator getInstance(boolean allowLocal)
Returns the singleton instance of this validator, with local validation as required.- Parameters:
allowLocal
- Should local addresses be considered valid?- Returns:
- the singleton instance of this validator
-
isValid
public boolean isValid(String domain)
Returns true if the specifiedString
parses as a valid domain name with a recognized top-level domain. The parsing is case-insensitive.- Parameters:
domain
- the parameter to check for domain name syntax- Returns:
- true if the parameter is a valid domain name
-
isValidTld
public boolean isValidTld(String tld)
Returns true if the specifiedString
matches any IANA-defined top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
tld
- the parameter to check for TLD status, not null- Returns:
- true if the parameter is a TLD
-
isValidInfrastructureTld
public boolean isValidInfrastructureTld(String iTld)
Returns true if the specifiedString
matches any IANA-defined infrastructure top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
iTld
- the parameter to check for infrastructure TLD status, not null- Returns:
- true if the parameter is an infrastructure TLD
-
isValidGenericTld
public boolean isValidGenericTld(String gTld)
Returns true if the specifiedString
matches any IANA-defined generic top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
gTld
- the parameter to check for generic TLD status, not null- Returns:
- true if the parameter is a generic TLD
-
isValidCountryCodeTld
public boolean isValidCountryCodeTld(String ccTld)
Returns true if the specifiedString
matches any IANA-defined country code top-level domain. Leading dots are ignored if present. The search is case-insensitive.- Parameters:
ccTld
- the parameter to check for country code TLD status, not null- Returns:
- true if the parameter is a country code TLD
-
isValidLocalTld
public boolean isValidLocalTld(String lTld)
Returns true if the specifiedString
matches any widely used "local" domains (localhost or localdomain). Leading dots are ignored if present. The search is case-insensitive.- Parameters:
lTld
- the parameter to check for local TLD status, not null- Returns:
- true if the parameter is an local TLD
-
updateTLDOverride
public static void updateTLDOverride(DomainValidator.ArrayType table, String[] tlds)
Update one of the TLD override arrays. This must only be done at program startup, before any instances are accessed using getInstance.For example:
DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"apache"})
To clear an override array, provide an empty array.
- Parameters:
table
- the table to update, seeDomainValidator.ArrayType
Must be one of the following- COUNTRY_CODE_MINUS
- COUNTRY_CODE_PLUS
- GENERIC_MINUS
- GENERIC_PLUS
tlds
- the array of TLDs, must not be null- Throws:
IllegalStateException
- if the method is called after getInstanceIllegalArgumentException
- if one of the read-only tables is requested- Since:
- 1.5.0
-
getTLDEntries
public static String[] getTLDEntries(DomainValidator.ArrayType table)
Get a copy of the internal array.- Parameters:
table
- the array type (any of the enum values)- Returns:
- a copy of the array
- Throws:
IllegalArgumentException
- if the table type is unexpected (should not happen)- Since:
- 1.5.1
-
-