public final class UrlUtils extends Object
Modifier and Type | Method and Description |
---|---|
static String |
decode(String escaped)
Unescapes and decodes 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 anchor string.
|
static URL |
encodeUrl(URL url,
boolean minimalQueryEncoding,
Charset charset)
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 |
getUrlWithoutPathRefQuery(URL u)
Creates and returns a new URL using only the protocol and authority from the given one.
|
static URL |
getUrlWithProtocolAndAuthority(URL u)
Creates and returns a new URL identical to the specified URL, ignoring path, protocol and query.
|
static String |
normalize(URL url)
Helper that constructs a normalized url string
usable as cache key.
|
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.
|
static boolean |
sameFile(URL u1,
URL u2)
More or less the same as sameFile(URL, URL) but without
resolving the host to an IP address for comparing.
|
static URI |
toURI(URL url,
String query)
Constructs a
URI using the specified URL. |
static URL |
toUrlSafe(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:..."
|
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:..."
|
public static URL toUrlSafe(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 a data:... 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.
url
- the URL string to convert into a URL instancepublic static URL toUrlUnsafe(String url) throws MalformedURLException
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 a data:... URL.
Unlike toUrlSafe(String)
, the caller need not be sure that URL strings passed to this
method will parse correctly as URLs.
url
- the URL string to convert into a URL instanceMalformedURLException
- if the URL string cannot be converted to a URL instancepublic static URL encodeUrl(URL url, boolean minimalQueryEncoding, Charset charset)
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".
url
- the URL to encodeminimalQueryEncoding
- whether or not to perform minimal query encoding, like IE doescharset
- the charsetpublic static String encodeAnchor(String anchor)
anchor
- the anchor string to encode and escapepublic static String encodeHash(String hash)
hash
- the anchor string to encode and escapepublic static String decode(String escaped)
escaped
- the string to be unescaped and decodedpublic static URL getUrlWithoutPathRefQuery(URL u) throws MalformedURLException
u
- the URL on which to base the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewProtocol(URL u, String newProtocol) throws MalformedURLException
u
- the URL on which to base the returned URLnewProtocol
- the new protocol to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewHost(URL u, String newHost) throws MalformedURLException
u
- the URL on which to base the returned URLnewHost
- the new host to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewHostAndPort(URL u, String newHost, int newPort) throws MalformedURLException
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 URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewPort(URL u, int newPort) throws MalformedURLException
u
- the URL on which to base the returned URLnewPort
- the new port to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewPath(URL u, String newPath) throws MalformedURLException
u
- the URL on which to base the returned URLnewPath
- the new path to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewRef(URL u, String newRef) throws MalformedURLException
u
- the URL on which to base the returned URLnewRef
- the new reference to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewQuery(URL u, String newQuery) throws MalformedURLException
u
- the URL on which to base the returned URLnewQuery
- the new query string to use in the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithProtocolAndAuthority(URL u) throws MalformedURLException
u
- the URL on which to base the returned URLMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewUserName(URL u, String newUserName) throws MalformedURLException
u
- the URL on which to base the returned URLnewUserName
- the new user nameMalformedURLException
- if there is a problem creating the new URLpublic static URL getUrlWithNewUserPassword(URL u, String newUserPassword) throws MalformedURLException
u
- the URL on which to base the returned URLnewUserPassword
- the new user passwordMalformedURLException
- if there is a problem creating the new URLpublic static String resolveUrl(String baseUrl, String relativeUrl)
baseUrl
- The base URL in which to resolve the specification.relativeUrl
- The relative URL to resolve against the base URL.public static String resolveUrl(URL baseUrl, String relativeUrl)
baseUrl
- The base URL in which to resolve the specification.relativeUrl
- The relative URL to resolve against the base URL.public static boolean sameFile(URL u1, URL u2)
u1
- a URL objectu2
- a URL objectpublic static String normalize(URL url)
url
- a URL objectpublic static URI toURI(URL url, String query) throws URISyntaxException
URI
using the specified URL.url
- the URLquery
- the queryURISyntaxException
- 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 authorityCopyright © 2002–2020 Gargoyle Software Inc.. All rights reserved.