public final class URI extends Object implements Comparable<URI>
A URI instance represents that defined by RFC3986, with some deviations.
At its highest level, a URI consists of:
[scheme:]scheme-specific-part
[#fragment]
where # and : are literal characters, and those parts enclosed in square brackets are optional.
There are two main types of URI. An opaque URI is one which just consists of the above three parts, and is not further defined. An example of such a URI would be mailto: URI. In contrast, hierarchical URIs give further definition to the scheme-specific part, so as represent some part of a hierarchical structure.
[//authority][path]
[?query]
with / and ? being literal characters. When server-based, the authority section is further subdivided into:
[user-info@]host
[:port]
with @ and : as literal characters. Authority sections that are not server-based are said to be registry-based.
Hierarchical URIs can be either relative or absolute. Absolute URIs always start with a `/', while relative URIs don't specify a scheme. Opaque URIs are always absolute.
Each part of the URI may have one of three states: undefined, empty or
containing some content. The former two of these are represented by
null
and the empty string in Java, respectively. The
scheme-specific part may never be undefined. It also follows from this that
the path sub-part may also not be undefined, so as to ensure the former.
The characters that can be used within a valid URI are restricted. There are two main classes of characters which can't be used as is within the URI:
The set of valid characters differs depending on the section of the URI:
These definitions reference the following sets of characters:
The constructors and accessor methods allow the use and retrieval of URI
components which contain non-US-ASCII characters directly. They are only
escaped when the toASCIIString()
method is used. In contrast,
illegal characters are always quoted, with the exception of the return values
of the non-raw accessors.
Constructor and Description |
---|
URI(String str)
Creates an URI from the given string
|
URI(String scheme,
String ssp,
String fragment)
Create an URI from the given components
|
URI(String scheme,
String userInfo,
String host,
int port,
String path,
String query,
String fragment)
Create an URI from the given components
|
URI(String scheme,
String host,
String path,
String fragment)
Create an URI from the given components
|
URI(String scheme,
String authority,
String path,
String query,
String fragment)
Create an URI from the given components
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(URI uri)
Compare the URI with another URI.
|
static URI |
create(String str)
Create an URI from the given string
|
boolean |
equals(Object obj)
Compares the URI with the given object for equality.
|
String |
getAuthority()
Returns the decoded authority part of this URI
|
String |
getFragment()
Returns the fragment of the URI
|
String |
getHost()
Returns the hostname of the URI
|
String |
getPath()
Returns the path of the URI
|
int |
getPort()
Returns the port number of the URI
|
String |
getQuery()
Returns the query of the URI
|
String |
getRawAuthority()
Returns the raw authority part of this URI
|
String |
getRawFragment()
Return the raw fragment part of this URI
|
String |
getRawPath()
Returns the raw path part of this URI
|
String |
getRawQuery()
Returns the raw query part of this URI
|
String |
getRawSchemeSpecificPart()
Returns the raw scheme specific part of this URI.
|
String |
getRawUserInfo()
Returns the raw user info part of this URI
|
String |
getScheme()
Returns the scheme of the URI
|
String |
getSchemeSpecificPart()
Returns the decoded scheme specific part of this URI.
|
String |
getUserInfo()
Returns the decoded user info part of this URI
|
int |
hashCode()
Computes the hashcode of the URI
|
boolean |
isAbsolute()
Tells whether this URI is absolute or not
|
boolean |
isOpaque()
Tell whether this URI is opaque or not
|
URI |
normalize()
Returns a normalized version of the URI.
|
URI |
relativize(URI uri)
Relativizes the given URI against this URI.
|
URI |
resolve(String str)
Resolves the given URI string against this URI
|
URI |
resolve(URI uri)
Resolves the given URI against this URI
|
String |
toASCIIString()
Returns the URI as US-ASCII string.
|
String |
toString()
Returns the URI as a String.
|
public URI(String str) throws URISyntaxException
str
- The string to create the URI fromURISyntaxException
- If the given string violates RFC 2396NullPointerException
- If str is nullpublic URI(String scheme, String ssp, String fragment) throws URISyntaxException
scheme
- The scheme namessp
- The scheme specific partfragment
- The fragmentURISyntaxException
- If the given string violates RFC 2396public URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment) throws URISyntaxException
scheme
- The scheme nameuserInfo
- The username and authorization infohost
- The hostnameport
- The port numberpath
- The pathquery
- The queryfragment
- The fragmentURISyntaxException
- If the given string violates RFC 2396public URI(String scheme, String host, String path, String fragment) throws URISyntaxException
scheme
- The scheme namehost
- The hostnamepath
- The pathfragment
- The fragmentURISyntaxException
- If the given string violates RFC 2396public URI(String scheme, String authority, String path, String query, String fragment) throws URISyntaxException
scheme
- The scheme nameauthority
- The authoritypath
- The apthquery
- The queryfragment
- The fragmentURISyntaxException
- If the given string violates RFC 2396public int compareTo(URI uri) throws ClassCastException
compareTo
in interface Comparable<URI>
uri
- The other URI to compare this URI withClassCastException
public static URI create(String str)
str
- The string to create the URI fromIllegalArgumentException
- If the given string violates RFC 2396NullPointerException
- If str is nullpublic boolean equals(Object obj)
Compares the URI with the given object for equality. If the object is not a
URI
, then the method returns false. Otherwise, the following
criteria are observed:
public String getAuthority()
public String getFragment()
public String getHost()
public String getPath()
public int getPort()
public String getQuery()
public String getRawAuthority()
public String getRawFragment()
public String getRawPath()
public String getRawQuery()
public String getRawSchemeSpecificPart()
public String getRawUserInfo()
public String getScheme()
public String getSchemeSpecificPart()
public String getUserInfo()
public boolean isAbsolute()
public boolean isOpaque()
public URI normalize()
Returns a normalized version of the URI. If the URI is opaque, or its path is already in normal form, then this URI is simply returned. Otherwise, the following transformation of the path element takes place:
The resulting URI will be free of `.' and `..' segments, barring those that were prepended or which couldn't be paired, respectively.
public URI relativize(URI uri)
Relativizes the given URI against this URI. The following algorithm is used:
uri
- the URI to relativize agsint this URINullPointerException
- if the uri is nullpublic URI resolve(String str) throws IllegalArgumentException
str
- The URI as string to resolve against this URIIllegalArgumentException
- If the given URI string violates RFC 2396NullPointerException
- If uri is nullpublic URI resolve(URI uri)
uri
- The URI to resolve against this URINullPointerException
- if uri is nullpublic String toASCIIString()
toString()
for URIs that don't contain any non-US-ASCII
characters. Otherwise, the non-US-ASCII characters are replaced by their
percent-encoded representations.