Class DomainNameUtils
This class provides various utility methods for validating and manipulating domain names. It includes methods for checking the validity of domain names and labels, removing wildcard prefixes, and determining the base domain and parent domains. The class also handles special cases such as reserved LDH labels and IDNA (Internationalized Domain Names in Applications) processing.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Pattern
Pattern for a domain label.private static final String
Domain name labels consist of up to 63 letters, numbers, and hyphens, but cannot start or end with a hyphen.private static final Pattern
Compiled pattern for a domain.private static final String
Regex pattern for domains.private static final String
Regex for one or more of the allowed char set for the local-part of a valid email address.private static final String
Regex for the local-part of an email addressprivate static final Pattern
Compiled pattern for validating email addresses.private static final int
Maximum length of a domain name.private final PslOverrideSupplier
Supplier for Public Suffix List overrides.static final String
Separator for reserved LDH labels.private static final com.ibm.icu.text.IDNA
IDNA instance for UTS 46.static final String
Per https://datatracker.ietf.org/doc/rfc5890/, labels that start with "?? -
Constructor Summary
ConstructorsConstructorDescriptionDomainNameUtils
(DcvContext dcvContext) Constructs a new DomainNameUtils with the specified DcvContext. -
Method Summary
Modifier and TypeMethodDescription(package private) static boolean
Determines if the domain contains invalid reserved LDH labels, including verifying any punycode present is valid.static boolean
domainMatchesRegex
(String domain) Checks if the given domain matches the domain regex pattern.getBaseDomain
(String domainName) Gets the base domain of the given domain name.getDomainAndParents
(String domain) Gets the domain and its parent domains.static boolean
isValidDomainLabel
(String domainLabel) Checks if the given domain label is matches the DOMAIN_LABEL_PATTERN.static boolean
isValidEmailAddress
(String email) Checks if the given email address is valid.(package private) static String
removeWildCard
(String domainName) Removes the wildcard prefix from the given domain name and ensures it is lowercase.private String
truncateDomainName
(String domainName) Utility function to prevent logging a domain name that is excessively long.void
validateDomainName
(String domainName) Validates the given domain name.
-
Field Details
-
DOMAIN_LABEL_REGEX
Domain name labels consist of up to 63 letters, numbers, and hyphens, but cannot start or end with a hyphen.- See Also:
-
DOMAIN_LABEL_PATTERN
Pattern for a domain label. -
DOMAIN_REGEX
Regex pattern for domains. Domains are two or more "."-separated labels.- See Also:
-
DOMAIN_PATTERN
Compiled pattern for a domain. -
XN_LABEL
Per https://datatracker.ietf.org/doc/rfc5890/, labels that start with "??--" are "tagged domain names". Currently, the only allowed start for such labels is "xn--". This is a regex pattern for the "xn--" label.- See Also:
-
R_LDH_LABEL_SEPARATOR
Separator for reserved LDH labels.- See Also:
-
MAX_DOMAIN_LENGTH
private static final int MAX_DOMAIN_LENGTHMaximum length of a domain name.- See Also:
-
EMAIL_CHAR_REGEX
Regex for one or more of the allowed char set for the local-part of a valid email address.- See Also:
-
EMAIL_LOCAL_PART_REGEX
Regex for the local-part of an email addressThe local-part of an email address can contain '.'s, but they cannot be the first or last character, and they cannot appear consecutively.
- See Also:
-
EMAIL_PATTERN
Compiled pattern for validating email addresses.Email address consist of a local-part and a domain, with an @ separating them.
-
UTS_46_INSTANCE
private static final com.ibm.icu.text.IDNA UTS_46_INSTANCEIDNA instance for UTS 46.This instance of the IDNA class is configured for UTS 46 (Unicode Technical Standard #46) processing. It is used to handle internationalized domain names, converting them between Unicode and ASCII representations.
-
pslOverrideSupplier
Supplier for Public Suffix List overrides.This supplier provides overrides for the Public Suffix List (PSL). It is used to handle special cases where the default PSL does not apply, allowing for custom domain suffix rules.
-
-
Constructor Details
-
DomainNameUtils
Constructs a new DomainNameUtils with the specified DcvContext.- Parameters:
dcvContext
- context where we can find the needed dependencies and configuration
-
-
Method Details
-
validateDomainName
Validates the given domain name.This method validates the provided domain name by checking its length, format, and reserved LDH labels. It also ensures that the domain name is under a valid public suffix.
- Parameters:
domainName
- the domain name to validate- Throws:
InputException
- if the domain name is invalid
-
truncateDomainName
Utility function to prevent logging a domain name that is excessively long.- Parameters:
domainName
- the domain name to truncate- Returns:
- up to MAX_DOMAIN_LENGTH+10 chars of the domain name followed by "..." if the domain name was truncated
-
isValidDomainLabel
Checks if the given domain label is matches the DOMAIN_LABEL_PATTERN.- Parameters:
domainLabel
- the domain label to check- Returns:
- true if the domain label is valid, false otherwise
-
domainMatchesRegex
Checks if the given domain matches the domain regex pattern.- Parameters:
domain
- the domain to check- Returns:
- true if the domain matches the regex pattern, false otherwise
-
removeWildCard
Removes the wildcard prefix from the given domain name and ensures it is lowercase.- Parameters:
domainName
- the domain name to process- Returns:
- the domain name without the wildcard prefix
-
domainContainsInvalidReservedLDHLabel
Determines if the domain contains invalid reserved LDH labels, including verifying any punycode present is valid.- Parameters:
domain
- the domain to check- Returns:
- true if the domain contains invalid reserved LDH labels, false otherwise
-
getDomainAndParents
Gets the domain and its parent domains.This method returns a list of the provided domain name and all parent domains up to the base domain. For example, "a.b.c.d.e.com" would return the list ["a.b.c.d.e.com", "b.c.d.e.com", "c.d.e.com", "d.e.com", "e.com"]
- Parameters:
domain
- the domain to process- Returns:
- a list of the domain and its parent domains
- Throws:
InputException
- if the domain is invalid
-
getBaseDomain
Gets the base domain of the given domain name.This method returns the base domain of the provided domain name. It uses the Public Suffix List (PSL) and any overrides to determine the base domain.
- Parameters:
domainName
- the domain name to process- Returns:
- the base domain
- Throws:
InputException
- if the domain is invalid
-
isValidEmailAddress
Checks if the given email address is valid.This method checks if the provided email address matches the EMAIL_PATTERN.
- Parameters:
email
- the email address to check- Returns:
- true if the email address is valid, false otherwise
-