public final class UrlUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ABOUT
"about".
|
static String |
ABOUT_BLANK
"about:blank".
|
static String |
ABOUT_SCHEME
"about:".
|
static URL |
URL_ABOUT_BLANK
URL for "about:blank".
|
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 hash string.
|
static String |
encodeQuery(String query)
Encodes and escapes the specified URI hash string.
|
static String |
encodeQueryPart(String part) |
static URL |
encodeUrl(URL url,
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 |
getUrlWithoutRef(URL u)
Creates and returns a new URL using only the protocol, authority and path
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 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 |
normalize(URL url)
Helper that constructs a normalized url string
usable as cache key.
|
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.
|
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 final String ABOUT
public static final String ABOUT_SCHEME
public static final String ABOUT_BLANK
public static final URL URL_ABOUT_BLANK
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, 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 encodecharset
- 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 encodeQuery(String query)
query
- the query 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 getUrlWithoutRef(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 URL or -1 to remove itMalformedURLException
- 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 URL or null to remove itMalformedURLException
- 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 name or null to remove itMalformedURLException
- 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 password or null to remove itMalformedURLException
- 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 isValidScheme(String scheme)
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").
scheme
- the scheme string to checkpublic static boolean isSpecialScheme(String scheme)
https://url.spec.whatwg.org/#special-scheme
scheme
- the scheme string to checkpublic 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 authoritypublic static String encodeQueryPart(String part)
part
- the part to encodepublic static URL removeRedundantPort(URL url) throws MalformedURLException
url
- the url to clean upMalformedURLException
- if the URL string cannot be converted to a URL instanceCopyright © 2002–2024 Gargoyle Software Inc.. All rights reserved.