Class UrlUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
ClassUrl
represents a Uniform Resource Locator. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
"about".static final String
"about:blank".private static final URLStreamHandler
static final String
"about:".private static final BitSet
private static final URLStreamHandler
private static final BitSet
private static final URLStreamHandler
private static final BitSet
private static final BitSet
static final URL
URL for "about:blank". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static URL
createNewUrl
(String protocol, String userInfo, String host, int port, String path, String ref, String query) Creates a new URL based on the specified fragments.private static URL
Creates a new URL based on the specified fragments.static String
Unescapes and decodes the specified string.private static String
Escapes and encodes the specified string.static String
encodeAnchor
(String anchor) Encodes and escapes the specified URI anchor string.static String
encodeHash
(String hash) Encodes and escapes the specified URI hash string.private static String
encodePercentSign
(byte[] input) Encodes every occurrence of the escape character '%' in the given input string that is not followed by two hexadecimal characters.static String
encodeQuery
(String query) Encodes and escapes the specified URI hash string.static String
encodeQueryPart
(String part) static URL
Encodes illegal characters in the specified URL's path, query string and anchor according to the URL encoding rules observed in real browsers.static URL
getUrlWithNewHost
(URL u, String newHost) Creates and returns a new URL identical to the specified URL, except using the specified host.static URL
getUrlWithNewHostAndPort
(URL u, String newHost, int newPort) Creates and returns a new URL identical to the specified URL, except using the specified host.static URL
getUrlWithNewPath
(URL u, String newPath) Creates and returns a new URL identical to the specified URL, except using the specified path.static URL
getUrlWithNewPort
(URL u, int newPort) Creates and returns a new URL identical to the specified URL, except using the specified port.static URL
getUrlWithNewProtocol
(URL u, String newProtocol) Creates and returns a new URL identical to the specified URL, except using the specified protocol.static URL
getUrlWithNewQuery
(URL u, String newQuery) Creates and returns a new URL identical to the specified URL, except using the specified query string.static URL
getUrlWithNewRef
(URL u, String newRef) Creates and returns a new URL identical to the specified URL, except using the specified reference.static URL
getUrlWithNewUserName
(URL u, String newUserName) Creates and returns a new URL identical to the specified URL but with a changed user name.static URL
getUrlWithNewUserPassword
(URL u, String newUserPassword) Creates and returns a new URL identical to the specified URL but with a changed user password.static URL
Creates and returns a new URL using only the protocol and authority from the given one.static URL
Creates and returns a new URL using only the protocol, authority and path from the given one.static URL
Creates and returns a new URL identical to the specified URL, ignoring path, protocol and query.(package private) static boolean
isNormalUrlProtocol
(String protocol) static boolean
isSpecialScheme
(String scheme) Returns true if specified string is a special scheme.static boolean
isValidScheme
(String scheme) Returns true if specified string is a valid scheme name.static String
Helper that constructs a normalized url string usable as cache key.private static UrlUtils.Url
Parses a given specification using the algorithm depicted in RFC1808:private static String
"../" after the leading "/" should be removed as browsers do (not in RFC)static URL
removeRedundantPort
(URL url) Removes the well known ports if it can be deduced from protocol.static String
resolveUrl
(String baseUrl, String relativeUrl) Resolves a given relative URL against a base URL.static String
resolveUrl
(URL baseUrl, String relativeUrl) Resolves a given relative URL against a base URL.private static UrlUtils.Url
resolveUrl
(UrlUtils.Url baseUrl, String relativeUrl) Resolves a given relative URL against a base URL using the algorithm depicted in RFC1808:static boolean
More or less the same as sameFile(URL, URL) but without resolving the host to an IP address for comparing.static URI
Constructs aURI
using the specified URL.static 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:..."
URL, a"javascript:..."
URL, or adata:...
URL.static URL
toUrlUnsafe
(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:..."
URL, a"javascript:..."
URL, or adata:...
URL.
-
Field Details
-
ABOUT
"about".- See Also:
-
ABOUT_SCHEME
"about:".- See Also:
-
ABOUT_BLANK
"about:blank".- See Also:
-
URL_ABOUT_BLANK
URL for "about:blank". -
JS_HANDLER
-
ABOUT_HANDLER
-
DATA_HANDLER
-
PATH_ALLOWED_CHARS
-
QUERY_ALLOWED_CHARS
-
ANCHOR_ALLOWED_CHARS
-
HASH_ALLOWED_CHARS
-
-
Constructor Details
-
UrlUtils
private UrlUtils()Disallow instantiation of this class.
-
-
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
MalformedURLException
s.- 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
-
encode
Escapes and encodes the specified string. Based on HttpClient 3.1'sURIUtil.encode()
method.- Parameters:
unescaped
- the string to encodeallowed
- allowed characters that shouldn't be escapedcharset
- the charset to use- Returns:
- the escaped string
-
encodePercentSign
Encodes every occurrence of the escape character '%' in the given input string that is not followed by two hexadecimal characters.- Parameters:
input
- the input bytes- Returns:
- the given input string where every occurrence of
%
in invalid escape sequences has been replace by%25
-
getUrlWithoutPathRefQuery
Creates and returns a new URL using only the protocol and authority 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
-
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
-
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, protocol and query.- Parameters:
u
- the URL on which to base the returned URL- Returns:
- a new URL identical to the specified URL, ignoring path, protocol and query
- 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
-
createNewUrl
private static URL createNewUrl(String protocol, String userInfo, String host, int port, String path, String ref, String query) throws MalformedURLException Creates a new URL based on the specified fragments.- Parameters:
protocol
- the protocol to use (may not benull
)userInfo
- the user info to use (may benull
)host
- the host to use (may not benull
)port
- the port to use (may be-1
if no port is specified)path
- the path to use (may benull
and may omit the initial'/'
)ref
- the reference to use (may benull
and must not include the'#'
)query
- the query to use (may benull
and must not include the'?'
)- Returns:
- a new URL based on the specified fragments
- Throws:
MalformedURLException
- if there is a problem creating the new URL
-
createNewUrl
private static URL createNewUrl(String protocol, String authority, String path, String ref, String query) throws MalformedURLException Creates a new URL based on the specified fragments.- Parameters:
protocol
- the protocol to use (may not benull
)authority
- the authority to use (may not benull
)path
- the path to use (may benull
and may omit the initial'/'
)ref
- the reference to use (may benull
and must not include the'#'
)query
- the query to use (may benull
and must not include the'?'
)- Returns:
- a new URL based on the specified fragments
- 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.
-
parseUrl
Parses a given specification using the algorithm depicted in RFC1808:Section 2.4: Parsing a URL
An accepted method for parsing URLs is useful to clarify the generic-RL syntax of Section 2.2 and to describe the algorithm for resolving relative URLs presented in Section 4. This section describes the parsing rules for breaking down a URL (relative or absolute) into the component parts described in Section 2.1. The rules assume that the URL has already been separated from any surrounding text and copied to a "parse string". The rules are listed in the order in which they would be applied by the parser.
- Parameters:
spec
- The specification to parse.- Returns:
- the parsed 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.https://url.spec.whatwg.org/#special-scheme
- Parameters:
scheme
- the scheme string to check- Returns:
- true if special
-
resolveUrl
Resolves a given relative URL against a base URL using the algorithm depicted in RFC1808:Section 4: Resolving Relative URLs
This section describes an example algorithm for resolving URLs within a context in which the URLs may be relative, such that the result is always a URL in absolute form. Although this algorithm cannot guarantee that the resulting URL will equal that intended by the original author, it does guarantee that any valid URL (relative or absolute) can be consistently transformed to an absolute form given a valid base URL.
- 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.
-
removeLeadingSlashPoints
"../" after the leading "/" should be removed as browsers do (not in RFC) -
isNormalUrlProtocol
-
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 aURI
using 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
- Parameters:
part
- the part to encode- Returns:
- the ecoded 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
-