public final class DomainValidator extends AbstractValidator
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.)
Modifier and Type | Class and Description |
---|---|
static class |
DomainValidator.ArrayType
enum used by
updateTLDOverride(ArrayType, String[])
to determine which override array to update / fetch |
private static class |
DomainValidator.IdnBugHolder |
Modifier and Type | Field and Description |
---|---|
private boolean |
allowLocal |
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 |
private static DomainValidator |
DOMAIN_VALIDATOR
Singleton instance of this validator, which
doesn't consider local addresses as valid.
|
private static DomainValidator |
DOMAIN_VALIDATOR_WITH_LOCAL
Singleton instance of this validator, which does
consider local addresses valid.
|
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 hostname
|
private static java.lang.String[] |
INFRASTRUCTURE_TLDS |
private static boolean |
inUse |
private static java.lang.String[] |
LOCAL_TLDS |
private static int |
MAX_DOMAIN_LENGTH |
private static java.lang.String |
TOP_LABEL_REGEX |
Modifier | Constructor and Description |
---|---|
private |
DomainValidator(boolean allowLocal)
Private constructor.
|
Modifier and Type | Method and Description |
---|---|
private static boolean |
arrayContains(java.lang.String[] sortedArray,
java.lang.String key)
Check if a sorted array contains the specified key
|
private static java.lang.String |
chompLeadingDot(java.lang.String str) |
(package private) static void |
clearTLDOverrides() |
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 java.lang.String[] |
getTLDEntries(DomainValidator.ArrayType table)
Get a copy of the internal array.
|
java.lang.String |
getValidatorName()
Returns the name of this validator
|
private static boolean |
isOnlyASCII(java.lang.String input) |
boolean |
isValid(java.lang.String domain)
Returns true if the specified
String parses
as a valid domain name with a recognized top-level domain. |
boolean |
isValidCountryCodeTld(java.lang.String ccTld)
Returns true if the specified
String 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 specified
String matches any
IANA-defined generic top-level domain. |
boolean |
isValidInfrastructureTld(java.lang.String iTld)
Returns true if the specified
String matches any
IANA-defined infrastructure top-level domain. |
boolean |
isValidLocalTld(java.lang.String lTld)
Returns true if the specified
String matches any
widely used "local" domains (localhost or localdomain). |
boolean |
isValidTld(java.lang.String tld)
Returns true if the specified
String matches any
IANA-defined top-level domain. |
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.
|
getErrorMessage, setErrorMessage
private static final int MAX_DOMAIN_LENGTH
private static final java.lang.String[] EMPTY_STRING_ARRAY
private static final java.lang.String DOMAIN_LABEL_REGEX
private static final java.lang.String TOP_LABEL_REGEX
private static final java.lang.String DOMAIN_NAME_REGEX
private final boolean allowLocal
private static final DomainValidator DOMAIN_VALIDATOR
private static final DomainValidator DOMAIN_VALIDATOR_WITH_LOCAL
private final RegexValidator domainRegex
private final RegexValidator hostnameRegex
private static final java.lang.String[] INFRASTRUCTURE_TLDS
private static final java.lang.String[] GENERIC_TLDS
private static final java.lang.String[] COUNTRY_CODE_TLDS
private static final java.lang.String[] LOCAL_TLDS
private static boolean inUse
private static volatile java.lang.String[] countryCodeTLDsPlus
private static volatile java.lang.String[] genericTLDsPlus
private static volatile java.lang.String[] countryCodeTLDsMinus
private static volatile java.lang.String[] genericTLDsMinus
private DomainValidator(boolean allowLocal)
allowLocal
- whether to allow local domainspublic static DomainValidator getInstance()
public static DomainValidator getInstance(boolean allowLocal)
allowLocal
- Should local addresses be considered valid?public boolean isValid(java.lang.String domain)
String
parses
as a valid domain name with a recognized top-level domain.
The parsing is case-insensitive.isValid
in class AbstractValidator
domain
- the parameter to check for domain name syntaxpublic java.lang.String getValidatorName()
AbstractValidator
getValidatorName
in class AbstractValidator
boolean isValidDomainSyntax(java.lang.String domain)
public boolean isValidTld(java.lang.String tld)
String
matches any
IANA-defined top-level domain. Leading dots are ignored if present.
The search is case-insensitive.tld
- the parameter to check for TLD status, not nullpublic boolean isValidInfrastructureTld(java.lang.String iTld)
String
matches any
IANA-defined infrastructure top-level domain. Leading dots are
ignored if present. The search is case-insensitive.iTld
- the parameter to check for infrastructure TLD status, not nullpublic boolean isValidGenericTld(java.lang.String gTld)
String
matches any
IANA-defined generic top-level domain. Leading dots are ignored
if present. The search is case-insensitive.gTld
- the parameter to check for generic TLD status, not nullpublic boolean isValidCountryCodeTld(java.lang.String ccTld)
String
matches any
IANA-defined country code top-level domain. Leading dots are
ignored if present. The search is case-insensitive.ccTld
- the parameter to check for country code TLD status, not nullpublic boolean isValidLocalTld(java.lang.String lTld)
String
matches any
widely used "local" domains (localhost or localdomain). Leading dots are
ignored if present. The search is case-insensitive.lTld
- the parameter to check for local TLD status, not nullprivate static java.lang.String chompLeadingDot(java.lang.String str)
static void clearTLDOverrides()
public static void updateTLDOverride(DomainValidator.ArrayType table, java.lang.String... tlds)
For example:
DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"apache"})}
To clear an override array, provide an empty array.
table
- the table to update, see DomainValidator.ArrayType
Must be one of the following
tlds
- the array of TLDs, must not be nulljava.lang.IllegalStateException
- if the method is called after getInstancejava.lang.IllegalArgumentException
- if one of the read-only tables is requestedpublic static java.lang.String[] getTLDEntries(DomainValidator.ArrayType table)
table
- the array type (any of the enum values)java.lang.IllegalArgumentException
- if the table type is unexpected (should not happen)public static java.lang.String unicodeToASCII(java.lang.String input)
input
- the string to convert, not nullprivate static boolean isOnlyASCII(java.lang.String input)
private static boolean arrayContains(java.lang.String[] sortedArray, java.lang.String key)
sortedArray
- the array to searchkey
- the key to findtrue
if the array contains the key