Class DomainValidator
- java.lang.Object
-
- com.networknt.org.apache.commons.validator.routines.DomainValidator
-
- All Implemented Interfaces:
java.io.Serializable
public class DomainValidator extends java.lang.Object implements java.io.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:
- 1.4
- 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 / fetchprivate static class
DomainValidator.IDNBUGHOLDER
static class
DomainValidator.Item
Used to specify overrides when creating a new class.private static class
DomainValidator.LazyHolder
-
Field Summary
Fields Modifier and Type Field Description private boolean
allowLocal
Whether to allow local overrides.private static java.lang.String[]
COUNTRY_CODE_TLDS
private static java.lang.String[]
countryCodeTLDsMinus
private static java.lang.String[]
countryCodeTLDsPlus
private static java.lang.String
DOMAIN_LABEL_REGEX
private static java.lang.String
DOMAIN_NAME_REGEX
The above instances must only be returned via the getInstance() methods.private RegexValidator
domainRegex
RegexValidator for matching domains.private static java.lang.String[]
EMPTY_STRING_ARRAY
private static java.lang.String[]
GENERIC_TLDS
private static java.lang.String[]
genericTLDsMinus
private static java.lang.String[]
genericTLDsPlus
private RegexValidator
hostnameRegex
RegexValidator for matching a local hostnameprivate static java.lang.String[]
INFRASTRUCTURE_TLDS
private static boolean
inUse
private static java.lang.String[]
LOCAL_TLDS
private static java.lang.String[]
localTLDsMinus
private static java.lang.String[]
localTLDsPlus
private static int
MAX_DOMAIN_LENGTH
Maximum allowable length (253) of a domain name(package private) java.lang.String[]
myCountryCodeTLDsMinus
Local override.(package private) java.lang.String[]
myCountryCodeTLDsPlus
Local override.(package private) java.lang.String[]
myGenericTLDsMinus
Local override.(package private) java.lang.String[]
myGenericTLDsPlus
Local override.(package private) java.lang.String[]
myLocalTLDsMinus
Local override.(package private) java.lang.String[]
myLocalTLDsPlus
Local override.private static long
serialVersionUID
private static java.lang.String
TOP_LABEL_REGEX
private static java.lang.String
UNEXPECTED_ENUM_VALUE
-
Constructor Summary
Constructors Modifier Constructor Description private
DomainValidator(boolean allowLocal)
Private constructor.private
DomainValidator(boolean allowLocal, java.util.List<DomainValidator.Item> items)
Private constructor, allowing local overrides
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
arrayContains(java.lang.String[] sortedArray, java.lang.String key)
Check if a sorted array contains the specified keyprivate java.lang.String
chompLeadingDot(java.lang.String str)
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 DomainValidator
getInstance(boolean allowLocal, java.util.List<DomainValidator.Item> items)
Returns a new instance of this validator.java.lang.String[]
getOverrides(DomainValidator.ArrayType table)
Gets a copy of an instance level internal array.static java.lang.String[]
getTLDEntries(DomainValidator.ArrayType table)
Gets a copy of a class level internal array.boolean
isAllowLocal()
Does this instance allow local addresses?private static boolean
isOnlyASCII(java.lang.String input)
boolean
isValid(java.lang.String domain)
Returns true if the specifiedString
parses as a valid domain name with a recognized top-level domain.boolean
isValidCountryCodeTld(java.lang.String ccTld)
Returns true if the specifiedString
matches any IANA-defined country code top-level domain.(package private) boolean
isValidDomainSyntax(java.lang.String domain)
boolean
isValidGenericTld(java.lang.String gTld)
Returns true if the specifiedString
matches any IANA-defined generic top-level domain.boolean
isValidInfrastructureTld(java.lang.String iTld)
Returns true if the specifiedString
matches any IANA-defined infrastructure top-level domain.boolean
isValidLocalTld(java.lang.String lTld)
Returns true if the specifiedString
matches any widely used "local" domains (localhost or localdomain).boolean
isValidTld(java.lang.String tld)
Returns true if the specifiedString
matches any IANA-defined top-level domain.(package private) static java.lang.String
unicodeToASCII(java.lang.String input)
Converts potentially Unicode input to punycode.static void
updateTLDOverride(DomainValidator.ArrayType table, java.lang.String... tlds)
Update one of the TLD override arrays.
-
-
-
Field Detail
-
MAX_DOMAIN_LENGTH
private static final int MAX_DOMAIN_LENGTH
Maximum allowable length (253) of a domain name- See Also:
- Constant Field Values
-
EMPTY_STRING_ARRAY
private static final java.lang.String[] EMPTY_STRING_ARRAY
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
DOMAIN_LABEL_REGEX
private static final java.lang.String DOMAIN_LABEL_REGEX
- See Also:
- Constant Field Values
-
TOP_LABEL_REGEX
private static final java.lang.String TOP_LABEL_REGEX
- See Also:
- Constant Field Values
-
DOMAIN_NAME_REGEX
private static final java.lang.String DOMAIN_NAME_REGEX
The above instances must only be returned via the getInstance() methods. This is to ensure that the override data arrays are properly protected.- See Also:
- Constant Field Values
-
UNEXPECTED_ENUM_VALUE
private static final java.lang.String UNEXPECTED_ENUM_VALUE
- See Also:
- Constant Field Values
-
INFRASTRUCTURE_TLDS
private static final java.lang.String[] INFRASTRUCTURE_TLDS
-
GENERIC_TLDS
private static final java.lang.String[] GENERIC_TLDS
-
COUNTRY_CODE_TLDS
private static final java.lang.String[] COUNTRY_CODE_TLDS
-
LOCAL_TLDS
private static final java.lang.String[] LOCAL_TLDS
-
inUse
private static boolean inUse
-
countryCodeTLDsPlus
private static java.lang.String[] countryCodeTLDsPlus
-
genericTLDsPlus
private static java.lang.String[] genericTLDsPlus
-
countryCodeTLDsMinus
private static java.lang.String[] countryCodeTLDsMinus
-
genericTLDsMinus
private static java.lang.String[] genericTLDsMinus
-
localTLDsMinus
private static java.lang.String[] localTLDsMinus
-
localTLDsPlus
private static java.lang.String[] localTLDsPlus
-
allowLocal
private final boolean allowLocal
Whether to allow local overrides.
-
domainRegex
private final RegexValidator domainRegex
RegexValidator for matching domains.
-
hostnameRegex
private final RegexValidator hostnameRegex
RegexValidator for matching a local hostname
-
myCountryCodeTLDsMinus
final java.lang.String[] myCountryCodeTLDsMinus
Local override.
-
myCountryCodeTLDsPlus
final java.lang.String[] myCountryCodeTLDsPlus
Local override.
-
myGenericTLDsPlus
final java.lang.String[] myGenericTLDsPlus
Local override.
-
myGenericTLDsMinus
final java.lang.String[] myGenericTLDsMinus
Local override.
-
myLocalTLDsPlus
final java.lang.String[] myLocalTLDsPlus
Local override.
-
myLocalTLDsMinus
final java.lang.String[] myLocalTLDsMinus
Local override.
-
-
Constructor Detail
-
DomainValidator
private DomainValidator(boolean allowLocal)
Private constructor.
-
DomainValidator
private DomainValidator(boolean allowLocal, java.util.List<DomainValidator.Item> items)
Private constructor, allowing local overrides- Since:
- 1.7
-
-
Method Detail
-
arrayContains
private static boolean arrayContains(java.lang.String[] sortedArray, java.lang.String key)
Check if a sorted array contains the specified key- Parameters:
sortedArray
- the array to searchkey
- the key to find- Returns:
true
if the array contains the key
-
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
-
getInstance
public static DomainValidator getInstance(boolean allowLocal, java.util.List<DomainValidator.Item> items)
Returns a new instance of this validator. The user can provide a list ofDomainValidator.Item
entries which can be used to override the generic and country code lists. Note that any such entries override values provided by theupdateTLDOverride(ArrayType, String[])
method If an entry for a particular type is not provided, then the class override (if any) is retained.- Parameters:
allowLocal
- Should local addresses be considered valid?items
- - array ofDomainValidator.Item
entries- Returns:
- an instance of this validator
- Since:
- 1.7
-
getTLDEntries
public static java.lang.String[] getTLDEntries(DomainValidator.ArrayType table)
Gets a copy of a class level internal array.- Parameters:
table
- the array type (any of the enum values)- Returns:
- a copy of the array
- Throws:
java.lang.IllegalArgumentException
- if the table type is unexpected (should not happen)- Since:
- 1.5.1
-
isOnlyASCII
private static boolean isOnlyASCII(java.lang.String input)
-
unicodeToASCII
static java.lang.String unicodeToASCII(java.lang.String input)
Converts potentially Unicode input to punycode. If conversion fails, returns the original input.- Parameters:
input
- the string to convert, not null- Returns:
- converted input, or original input if conversion fails
-
updateTLDOverride
public static void updateTLDOverride(DomainValidator.ArrayType table, java.lang.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, "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
- LOCAL_MINUS
- LOCAL_PLUS
tlds
- the array of TLDs, must not be null- Throws:
java.lang.IllegalStateException
- if the method is called after getInstancejava.lang.IllegalArgumentException
- if one of the read-only tables is requested- Since:
- 1.5.0
-
chompLeadingDot
private java.lang.String chompLeadingDot(java.lang.String str)
-
getOverrides
public java.lang.String[] getOverrides(DomainValidator.ArrayType table)
Gets a copy of an instance level internal array.- Parameters:
table
- the array type (any of the enum values)- Returns:
- a copy of the array
- Throws:
java.lang.IllegalArgumentException
- if the table type is unexpected, e.g. GENERIC_RO- Since:
- 1.7
-
isAllowLocal
public boolean isAllowLocal()
Does this instance allow local addresses?- Returns:
- true if local addresses are allowed.
- Since:
- 1.7
-
isValid
public boolean isValid(java.lang.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
-
isValidCountryCodeTld
public boolean isValidCountryCodeTld(java.lang.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
-
isValidDomainSyntax
final boolean isValidDomainSyntax(java.lang.String domain)
-
isValidGenericTld
public boolean isValidGenericTld(java.lang.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
-
isValidInfrastructureTld
public boolean isValidInfrastructureTld(java.lang.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
-
isValidLocalTld
public boolean isValidLocalTld(java.lang.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
-
isValidTld
public boolean isValidTld(java.lang.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.If allowLocal is true, the TLD is checked using
isValidLocalTld(String)
. The TLD is then checked againstisValidInfrastructureTld(String)
,isValidGenericTld(String)
andisValidCountryCodeTld(String)
- Parameters:
tld
- the parameter to check for TLD status, not null- Returns:
- true if the parameter is a TLD
-
-