- Author:
- Daniel Gredler, Martin Tamme, Sudhan Moghe, Marc Guillemot, Ahmed Ashour, Ronald Brill, Joerg Werner, Hartmut Arlt, Vidya m
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String"about".static final String"about:blank".static final String"about:".static final String"about:srcdoc"; the url of the document of an iframe with srcdoc attribute.static final URLURL for "about:blank".static final URLURL for "about:srcdoc". -
Method Summary
Modifier and TypeMethodDescriptionstatic StringUnescapes and decodes the specified string.static byte[]decodeDataUrl(byte[] bytes, boolean removeWhitespace) Decodes an array of URL safe 7-bit characters into an array of original bytes.static byte[]decodeUrl(byte[] bytes) Decodes an array of URL safe 7-bit characters into an array of original bytes.static StringencodeAnchor(String anchor) Encodes and escapes the specified URI anchor string.static StringencodeHash(String hash) Encodes and escapes the specified URI hash string.static StringencodeQuery(String query) Encodes and escapes the specified URI hash string.static StringencodeQueryPart(String part) Returns the encoded string.static URLEncodes illegal characters in the specified URL's path, query string and anchor according to the URL encoding rules observed in real browsers.static byte[]Encodes an array of bytes into an array of URL safe 7-bit characters.static URLgetUrlWithNewHost(URL u, String newHost) Creates and returns a new URL identical to the specified URL, except using the specified host.static URLgetUrlWithNewHostAndPort(URL u, String newHost, int newPort) Creates and returns a new URL identical to the specified URL, except using the specified host.static URLgetUrlWithNewPath(URL u, String newPath) Creates and returns a new URL identical to the specified URL, except using the specified path.static URLgetUrlWithNewPort(URL u, int newPort) Creates and returns a new URL identical to the specified URL, except using the specified port.static URLgetUrlWithNewProtocol(URL u, String newProtocol) Creates and returns a new URL identical to the specified URL, except using the specified protocol.static URLgetUrlWithNewProtocolLikeSetter(URL url, String protocol) Returns the URL with the scheme changed like theprotocolsetter of the URL Standard does it (e.g. forURL.protocol,HTMLAnchorElement.protocolandlocation.protocol).static URLgetUrlWithNewProtocolLikeSetter(URL url, String protocol, boolean fileWithoutHost) Returns the URL with the scheme changed like theprotocolsetter of the URL Standard does it (seegetUrlWithNewProtocolLikeSetter(URL, String)).static URLgetUrlWithNewQuery(URL u, String newQuery) Creates and returns a new URL identical to the specified URL, except using the specified query string.static URLgetUrlWithNewRef(URL u, String newRef) Creates and returns a new URL identical to the specified URL, except using the specified reference.static URLgetUrlWithNewUserName(URL u, String newUserName) Creates and returns a new URL identical to the specified URL but with a changed user name.static URLgetUrlWithNewUserPassword(URL u, String newUserPassword) Creates and returns a new URL identical to the specified URL but with a changed user password.static URLCreates and returns a new URL using only the protocol and authority (without the user info) from the given one.static URLCreates and returns a new URL using only the protocol, authority and path from the given one.static URLCreates and returns a new URL identical to the specified URL, ignoring the user info.static URLCreates and returns a new URL identical to the specified URL, ignoring the ref and the user info; this is the url used as referrer (the Fetch standard 'strip url for use as a referrer' for http(s) urls).static URLCreates and returns a new URL identical to the specified URL, ignoring path, ref, query and user info.static booleanisLocalhostOrLoopback(String host) Returns whether the given host is localhost (also *.localhost, case-insensitive, with an optional trailing dot) or a loopback address (127.0.0.0/8 or [::1]); the origins of these hosts are potentially trustworthy also for http.static booleanisLocalResourceAccessAllowed(URL pageUrl, URL url) Determines whether a page loaded from the given URL is allowed to load the specified URL (e.g. into a frame or a window).static booleanReturns whether the origin of the given URL is potentially trustworthy (a secure context) like defined by the Secure Contexts specification: https, wss and file URLs and http/ws URLs of localhost (also *.localhost) and of the loopback addresses (127.0.0.0/8, [::1]).static booleanisSameOrigin(URL originUrl, URL newUrl) Determines whether two URLs share the same origin according to the Same-Origin Policy.static booleanisSpecialScheme(String scheme) Returns true if specified string is a special scheme.static booleanisValidScheme(String scheme) Returns true if specified string is a valid scheme name.static StringHelper that constructs a normalized url string usable as cache key.static URLReturns the file URL without the host if the host is 'localhost' (like the URL Standard parses file URLs); other URLs are returned unchanged.static URLremoveRedundantPort(URL url) Removes the well known ports if it can be deduced from protocol.static StringresolveUrl(String baseUrl, String relativeUrl) Resolves a given relative URL against a base URL.static StringresolveUrl(URL baseUrl, String relativeUrl) Resolves a given relative URL against a base URL.static booleanMore or less the same as sameFile(URL, URL) but without resolving the host to an IP address for comparing.static StringSerializes the URL like the URL Standard does it; different fromURL.toExternalForm()a file URL without host has an empty host ('file:///C:/a' and not 'file:/C:/a').static URIConstructs aURIusing the specified URL.static URLConstructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an"about:..."static URLtoUrlUnsafe(String url) Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an"about:..."
-
Field Details
-
ABOUT
"about".- See Also:
-
ABOUT_SCHEME
"about:".- See Also:
-
ABOUT_BLANK
"about:blank".- See Also:
-
URL_ABOUT_BLANK
URL for "about:blank". -
ABOUT_SRCDOC
"about:srcdoc"; the url of the document of an iframe with srcdoc attribute.- See Also:
-
URL_ABOUT_SRCDOC
URL for "about:srcdoc".
-
-
Method Details
-
toUrlSafe
Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an
"about:..."URL, a"javascript:..."URL, or adata:...URL.The caller should be sure that URL strings passed to this method will parse correctly as URLs, as this method never expects to have to handle
MalformedURLExceptions.- Parameters:
url- the URL string to convert into a URL instance- Returns:
- the constructed URL instance
-
toUrlUnsafe
Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an
"about:..."URL, a"javascript:..."URL, or adata:...URL.Unlike
toUrlSafe(String), the caller need not be sure that URL strings passed to this method will parse correctly as URLs.- Parameters:
url- the URL string to convert into a URL instance- Returns:
- the constructed URL instance
- Throws:
MalformedURLException- if the URL string cannot be converted to a URL instance
-
encodeUrl
Encodes illegal characters in the specified URL's path, query string and anchor according to the URL encoding rules observed in real browsers.
For example, this method changes
"http://first/?a=b c"to"http://first/?a=b%20c".- Parameters:
url- the URL to encodecharset- the charset- Returns:
- the encoded URL
-
encodeAnchor
Encodes and escapes the specified URI anchor string.- Parameters:
anchor- the anchor string to encode and escape- Returns:
- the encoded and escaped anchor string
-
encodeHash
Encodes and escapes the specified URI hash string.- Parameters:
hash- the anchor string to encode and escape- Returns:
- the encoded and escaped hash string
-
encodeQuery
Encodes and escapes the specified URI hash string.- Parameters:
query- the query string to encode and escape- Returns:
- the encoded and escaped hash string
-
decode
Unescapes and decodes the specified string.- Parameters:
escaped- the string to be unescaped and decoded- Returns:
- the unescaped and decoded string
-
getUrlWithoutPathRefQuery
Creates and returns a new URL using only the protocol and authority (without the user info) from the given one.- Parameters:
u- the URL on which to base the returned URL- Returns:
- a new URL using only the protocol and authority from the given one
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithoutRef
Creates and returns a new URL using only the protocol, authority and path from the given one.- Parameters:
u- the URL on which to base the returned URL- Returns:
- a new URL using only the protocol and authority from the given one
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithoutUserInfo
Creates and returns a new URL identical to the specified URL, ignoring the user info.- Parameters:
u- the URL on which to base the returned URL- Returns:
- a new URL identical to the specified URL, ignoring the user info
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithoutUserInfoAndRef
Creates and returns a new URL identical to the specified URL, ignoring the ref and the user info; this is the url used as referrer (the Fetch standard 'strip url for use as a referrer' for http(s) urls).- Parameters:
u- the URL on which to base the returned URL- Returns:
- a new URL identical to the specified URL, ignoring the ref and the user info
- Throws:
MalformedURLException- if there is a problem creating the new URL- See Also:
-
getUrlWithNewProtocol
Creates and returns a new URL identical to the specified URL, except using the specified protocol.- Parameters:
u- the URL on which to base the returned URLnewProtocol- the new protocol to use in the returned URL- Returns:
- a new URL identical to the specified URL, except using the specified protocol
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewProtocolLikeSetter
Returns the URL with the scheme changed like theprotocolsetter of the URL Standard does it (e.g. forURL.protocol,HTMLAnchorElement.protocolandlocation.protocol). The scheme is not changed- from a special scheme to a not special one (and the other way round),
- to 'file' if the URL has credentials or a port,
- from 'file' if the URL has an empty host.
- Parameters:
url- the URLprotocol- the new protocol (everything from the first ':' is ignored)- Returns:
- the new URL, the given URL if the scheme is not changed or
nullif the protocol is not a valid scheme - See Also:
-
getUrlWithNewProtocolLikeSetter
public static URL getUrlWithNewProtocolLikeSetter(URL url, String protocol, boolean fileWithoutHost) Returns the URL with the scheme changed like theprotocolsetter of the URL Standard does it (seegetUrlWithNewProtocolLikeSetter(URL, String)).- Parameters:
url- the URLprotocol- the new protocol (everything from the first ':' is ignored)fileWithoutHost- whether the host is removed if the scheme is changed to 'file' (like Firefox)- Returns:
- the new URL, the given URL if the scheme is not changed or
nullif the protocol is not a valid scheme
-
serialize
Serializes the URL like the URL Standard does it; different fromURL.toExternalForm()a file URL without host has an empty host ('file:///C:/a' and not 'file:/C:/a').- Parameters:
url- the URL- Returns:
- the serialized URL
- See Also:
-
removeLocalhostFromFileUrl
Returns the file URL without the host if the host is 'localhost' (like the URL Standard parses file URLs); other URLs are returned unchanged.- Parameters:
url- the URL- Returns:
- the URL without the host 'localhost'
-
getUrlWithNewHost
Creates and returns a new URL identical to the specified URL, except using the specified host.- Parameters:
u- the URL on which to base the returned URLnewHost- the new host to use in the returned URL- Returns:
- a new URL identical to the specified URL, except using the specified host
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewHostAndPort
public static URL getUrlWithNewHostAndPort(URL u, String newHost, int newPort) throws MalformedURLException Creates and returns a new URL identical to the specified URL, except using the specified host.- Parameters:
u- the URL on which to base the returned URLnewHost- the new host to use in the returned URLnewPort- the new port to use in the returned URL- Returns:
- a new URL identical to the specified URL, except using the specified host
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewPort
Creates and returns a new URL identical to the specified URL, except using the specified port.- Parameters:
u- the URL on which to base the returned URLnewPort- the new port to use in the returned URL or -1 to remove it- Returns:
- a new URL identical to the specified URL, except using the specified port
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewPath
Creates and returns a new URL identical to the specified URL, except using the specified path.- Parameters:
u- the URL on which to base the returned URLnewPath- the new path to use in the returned URL- Returns:
- a new URL identical to the specified URL, except using the specified path
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewRef
Creates and returns a new URL identical to the specified URL, except using the specified reference.- Parameters:
u- the URL on which to base the returned URLnewRef- the new reference to use in the returned URL or null to remove it- Returns:
- a new URL identical to the specified URL, except using the specified reference
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewQuery
Creates and returns a new URL identical to the specified URL, except using the specified query string.- Parameters:
u- the URL on which to base the returned URLnewQuery- the new query string to use in the returned URL- Returns:
- a new URL identical to the specified URL, except using the specified query string
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithProtocolAndAuthority
Creates and returns a new URL identical to the specified URL, ignoring path, ref, query and user info.- Parameters:
u- the URL on which to base the returned URL- Returns:
- a new URL identical to the specified URL, ignoring path, ref, query and user info
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewUserName
Creates and returns a new URL identical to the specified URL but with a changed user name.- Parameters:
u- the URL on which to base the returned URLnewUserName- the new user name or null to remove it- Returns:
- a new URL identical to the specified URL; only user name updated
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
getUrlWithNewUserPassword
public static URL getUrlWithNewUserPassword(URL u, String newUserPassword) throws MalformedURLException Creates and returns a new URL identical to the specified URL but with a changed user password.- Parameters:
u- the URL on which to base the returned URLnewUserPassword- the new user password or null to remove it- Returns:
- a new URL identical to the specified URL; only user name updated
- Throws:
MalformedURLException- if there is a problem creating the new URL
-
resolveUrl
Resolves a given relative URL against a base URL. See RFC1808 Section 4 for more details.- Parameters:
baseUrl- The base URL in which to resolve the specification.relativeUrl- The relative URL to resolve against the base URL.- Returns:
- the resolved specification.
-
resolveUrl
Resolves a given relative URL against a base URL. See RFC1808 Section 4 for more details.- Parameters:
baseUrl- The base URL in which to resolve the specification.relativeUrl- The relative URL to resolve against the base URL.- Returns:
- the resolved specification.
-
isValidScheme
Returns true if specified string is a valid scheme name.https://tools.ietf.org/html/rfc1738
Scheme names consist of a sequence of characters. The lower case letters "a"--"z", digits, and the characters plus ("+"), period ("."), and hyphen ("-") are allowed. For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names (e.g., allow "HTTP" as well as "http").
- Parameters:
scheme- the scheme string to check- Returns:
- true if valid
-
isSpecialScheme
Returns true if specified string is a special scheme. see https://url.spec.whatwg.org/#special-scheme- Parameters:
scheme- the scheme string to check- Returns:
- true if special
-
sameFile
More or less the same as sameFile(URL, URL) but without resolving the host to an IP address for comparing. Additionally we do some path normalization.- Parameters:
u1- a URL objectu2- a URL object- Returns:
- true if u1 and u2 refer to the same file
-
normalize
Helper that constructs a normalized url string usable as cache key.- Parameters:
url- a URL object- Returns:
- the normalized string
-
toURI
Constructs aURIusing the specified URL.- Parameters:
url- the URLquery- the query- Returns:
- the URI
- Throws:
URISyntaxException- If both a scheme and a path are given but the path is relative, if the URI string constructed from the given components violates RFC 2396, or if the authority component of the string is present but cannot be parsed as a server-based authority
-
encodeQueryPart
Returns the encoded string.- Parameters:
part- the part to encode- Returns:
- the encoded string
-
removeRedundantPort
Removes the well known ports if it can be deduced from protocol.- Parameters:
url- the url to clean up- Returns:
- a new URL without the port or the given one
- Throws:
MalformedURLException- if the URL string cannot be converted to a URL instance
-
decodeDataUrl
public static byte[] decodeDataUrl(byte[] bytes, boolean removeWhitespace) throws IllegalArgumentException Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted back to their original representation.- Parameters:
bytes- array of URL safe charactersremoveWhitespace- if true don't add whitespace chars to the output- Returns:
- array of original bytes
- Throws:
IllegalArgumentException- in case of error
-
decodeUrl
Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted back to their original representation.- Parameters:
bytes- array of URL safe characters- Returns:
- array of original bytes
- Throws:
IllegalArgumentException- in case of error
-
encodeUrl
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.- Parameters:
urlsafe- bitset of characters deemed URL safebytes- array of bytes to convert to URL safe characters- Returns:
- array of bytes containing URL safe characters
-
isSameOrigin
Determines whether two URLs share the same origin according to the Same-Origin Policy. Two URLs are considered to have the same origin if they have the same protocol (scheme), host, and port.The method handles default ports correctly by using the URL's default port when the explicit port is -1 (indicating no port was specified).
- Parameters:
originUrl- the first URL to compare (must not be null)newUrl- the second URL to compare (must not be null)- Returns:
trueif both URLs have the same host and effective port;falseotherwise
-
isLocalResourceAccessAllowed
Determines whether a page loaded from the given URL is allowed to load the specified URL (e.g. into a frame or a window). Like in the browsers, only pages loaded from a file are allowed to access local resources (file URLs).- Parameters:
pageUrl- the URL of the page that wants to load the URL (must not be null)url- the URL to load (must not be null)- Returns:
falseif the URL is a file URL and the page was not loaded from a file
-
isPotentiallyTrustworthy
Returns whether the origin of the given URL is potentially trustworthy (a secure context) like defined by the Secure Contexts specification: https, wss and file URLs and http/ws URLs of localhost (also *.localhost) and of the loopback addresses (127.0.0.0/8, [::1]). Other schemes (e.g. about:, data:, blob:) are not trustworthy; callers have to handle them before (e.g. by using the origin of the creator).- Parameters:
url- the URL to check (must not be null)- Returns:
- whether the origin of the URL is potentially trustworthy
- See Also:
-
isLocalhostOrLoopback
Returns whether the given host is localhost (also *.localhost, case-insensitive, with an optional trailing dot) or a loopback address (127.0.0.0/8 or [::1]); the origins of these hosts are potentially trustworthy also for http.- Parameters:
host- the host to check (as returned byURL.getHost(); may be null)- Returns:
- whether the host is localhost or a loopback address
-