Class InternetDomainName
com
or
foo.co.uk
. Only syntactic analysis is performed; no DNS lookups or other network interactions
take place. Thus there is no guarantee that the domain actually exists on the internet.
One common use of this class is to determine whether a given string is likely to represent an
addressable domain on the web -- that is, for a candidate string "xxx"
, might browsing to
"http://xxx/"
result in a webpage being displayed? In the past, this test was frequently
done by determining whether the domain ended with a public suffix
but was not itself a public suffix. However, this test is no longer accurate. There are many
domains which are both public suffixes and addressable as hosts; "uk.com"
is one example.
As a result, the only useful test to determine if a domain is a plausible web host is
hasPublicSuffix()
. This will return true
for many domains which (currently) are
not hosts, such as "com"
, but given that any public suffix may become a host without
warning, it is better to err on the side of permissiveness and thus avoid spurious rejection of
valid sites.
During construction, names are normalized in two ways:
- ASCII uppercase characters are converted to lowercase.
- Unicode dot separators other than the ASCII period (
'.'
) are converted to the ASCII period.
The normalized values will be returned from toString()
and parts()
, and will
be reflected in the result of equals(Object)
.
Internationalized domain
names such as 网络.cn
are supported, as are the equivalent
IDNA Punycode-encoded
versions.
- Since:
- 5.0
-
Method Summary
Modifier and TypeMethodDescriptionCreates and returns a newInternetDomainName
by prepending the argument and a dot to the current name.boolean
Equality testing is based on the text supplied by the caller, after normalization as described in the class documentation.static InternetDomainName
Returns an instance ofInternetDomainName
after lenient validation.int
hashCode()
boolean
Indicates whether this domain is composed of two or more parts.boolean
Indicates whether this domain name ends in a public suffix, including if it is a public suffix itself.boolean
Indicates whether this domain name represents a public suffix, as defined by the Mozilla Foundation's Public Suffix List (PSL).boolean
Indicates whether this domain name is composed of exactly one subdomain component followed by a public suffix.boolean
Indicates whether this domain name ends in a public suffix, while not being a public suffix itself.static boolean
Indicates whether the argument is a syntactically valid domain name using lenient validation.parent()
Returns anInternetDomainName
that is the immediate ancestor of this one; that is, the current domain with the leftmost part removed.parts()
Returns the individual components of this domain name, normalized to all lower case.Returns the public suffix portion of the domain name, ornull
if no public suffix is present.Returns the portion of this domain name that is one level beneath the public suffix.toString()
Returns the domain name, normalized to all lower case.
-
Method Details
-
from
Returns an instance ofInternetDomainName
after lenient validation. Specifically, validation against RFC 3490 ("Internationalizing Domain Names in Applications") is skipped, while validation against RFC 1035 is relaxed in the following ways:- Any part containing non-ASCII characters is considered valid.
- Underscores ('_') are permitted wherever dashes ('-') are permitted.
- Parts other than the final part may start with a digit, as mandated by RFC 1123.
- Parameters:
domain
- A domain name (not IP address)- Throws:
IllegalArgumentException
- ifname
is not syntactically valid according toisValid(java.lang.String)
- Since:
- 10.0 (previously named
fromLenient
)
-
parts
Returns the individual components of this domain name, normalized to all lower case. For example, for the domain namemail.google.com
, this method returns the list["mail", "google", "com"]
. -
isPublicSuffix
public boolean isPublicSuffix()Indicates whether this domain name represents a public suffix, as defined by the Mozilla Foundation's Public Suffix List (PSL). A public suffix is one under which Internet users can directly register names, such ascom
,co.uk
orpvt.k12.wy.us
. Examples of domain names that are not public suffixes includegoogle
,google.com
andfoo.co.uk
.- Returns:
true
if this domain name appears exactly on the public suffix list- Since:
- 6.0
-
hasPublicSuffix
public boolean hasPublicSuffix()Indicates whether this domain name ends in a public suffix, including if it is a public suffix itself. For example, returnstrue
forwww.google.com
,foo.co.uk
andcom
, but not forgoogle
orgoogle.foo
. This is the recommended method for determining whether a domain is potentially an addressable host.- Since:
- 6.0
-
publicSuffix
Returns the public suffix portion of the domain name, ornull
if no public suffix is present.- Since:
- 6.0
-
isUnderPublicSuffix
public boolean isUnderPublicSuffix()Indicates whether this domain name ends in a public suffix, while not being a public suffix itself. For example, returnstrue
forwww.google.com
,foo.co.uk
andbar.ca.us
, but not forgoogle
,com
, orgoogle.foo
.Warning: a
false
result from this method does not imply that the domain does not represent an addressable host, as many public suffixes are also addressable hosts. UsehasPublicSuffix()
for that test.This method can be used to determine whether it will probably be possible to set cookies on the domain, though even that depends on individual browsers' implementations of cookie controls. See RFC 2109 for details.
- Since:
- 6.0
-
isTopPrivateDomain
public boolean isTopPrivateDomain()Indicates whether this domain name is composed of exactly one subdomain component followed by a public suffix. For example, returnstrue
forgoogle.com
andfoo.co.uk
, but not forwww.google.com
orco.uk
.Warning: A
true
result from this method does not imply that the domain is at the highest level which is addressable as a host, as many public suffixes are also addressable hosts. For example, the domainbar.uk.com
has a public suffix ofuk.com
, so it would returntrue
from this method. Butuk.com
is itself an addressable host.This method can be used to determine whether a domain is probably the highest level for which cookies may be set, though even that depends on individual browsers' implementations of cookie controls. See RFC 2109 for details.
- Since:
- 6.0
-
topPrivateDomain
Returns the portion of this domain name that is one level beneath the public suffix. For example, forx.adwords.google.co.uk
it returnsgoogle.co.uk
, sinceco.uk
is a public suffix.If
isTopPrivateDomain()
is true, the current domain name instance is returned.This method should not be used to determine the topmost parent domain which is addressable as a host, as many public suffixes are also addressable hosts. For example, the domain
foo.bar.uk.com
has a public suffix ofuk.com
, so it would returnbar.uk.com
from this method. Butuk.com
is itself an addressable host.This method can be used to determine the probable highest level parent domain for which cookies may be set, though even that depends on individual browsers' implementations of cookie controls.
- Throws:
IllegalStateException
- if this domain does not end with a public suffix- Since:
- 6.0
-
hasParent
public boolean hasParent()Indicates whether this domain is composed of two or more parts. -
parent
Returns anInternetDomainName
that is the immediate ancestor of this one; that is, the current domain with the leftmost part removed. For example, the parent ofwww.google.com
isgoogle.com
.- Throws:
IllegalStateException
- if the domain has no parent, as determined byhasParent()
-
child
Creates and returns a newInternetDomainName
by prepending the argument and a dot to the current name. For example,InternetDomainName.from("foo.com").child("www.bar")
returns a newInternetDomainName
with the valuewww.bar.foo.com
. Only lenient validation is performed, as describedhere
.- Throws:
NullPointerException
- if leftParts is nullIllegalArgumentException
- if the resulting name is not valid
-
isValid
Indicates whether the argument is a syntactically valid domain name using lenient validation. Specifically, validation against RFC 3490 ("Internationalizing Domain Names in Applications") is skipped.The following two code snippets are equivalent:
domainName = InternetDomainName.isValid(name) ? InternetDomainName.from(name) : DEFAULT_DOMAIN;
try { domainName = InternetDomainName.from(name); } catch (IllegalArgumentException e) { domainName = DEFAULT_DOMAIN; }
- Since:
- 8.0 (previously named
isValidLenient
)
-
toString
Returns the domain name, normalized to all lower case. -
equals
Equality testing is based on the text supplied by the caller, after normalization as described in the class documentation. For example, a non-ASCII Unicode domain name and the Punycode version of the same domain name would not be considered equal. -
hashCode
public int hashCode()
-