@NotThreadSafe
public final class UrlBuilder
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
UrlBuilder |
clearQuery()
Clear the unstructured query and any query params.
|
UrlBuilder |
forceTrailingSlash()
Force the generated URL to have a trailing slash at the end of the path.
|
static UrlBuilder |
forHost(java.lang.String scheme,
java.lang.String host)
Create a URL with an null port and UTF-8 encoding.
|
static UrlBuilder |
forHost(java.lang.String scheme,
java.lang.String host,
int port) |
UrlBuilder |
fragment(java.lang.String fragment)
Set the fragment.
|
static UrlBuilder |
fromUrl(java.net.URL url)
Calls
fromUrl(URL, CharsetDecoder) with a UTF-8 CharsetDecoder. |
static UrlBuilder |
fromUrl(java.net.URL url,
java.nio.charset.CharsetDecoder charsetDecoder)
Create a UrlBuilder initialized with the contents of a
URL. |
UrlBuilder |
matrixParam(java.lang.String name,
java.lang.String value)
Add a matrix param to the last added path segment.
|
UrlBuilder |
pathSegment(java.lang.String segment)
Add a path segment.
|
UrlBuilder |
pathSegments(java.lang.String... segments)
Add multiple path segments.
|
UrlBuilder |
queryParam(java.lang.String name,
java.lang.String value)
Add an HTML query parameter.
|
java.lang.String |
toUrlString()
Encode the current builder state into a URL string.
|
UrlBuilder |
unstructuredQuery(java.lang.String query)
Set the complete query string of arbitrary structure.
|
public static UrlBuilder forHost(@Nonnull java.lang.String scheme, @Nonnull java.lang.String host)
scheme - scheme (e.g. http)host - host in any of the valid syntaxes: reg-name (a dns name), ipv4 literal (1.2.3.4), ipv6 literal
([::1]), excluding IPvFuture since no one uses that in practiceforHost(String scheme, String host, int port)public static UrlBuilder forHost(@Nonnull java.lang.String scheme, @Nonnull java.lang.String host, int port)
scheme - scheme (e.g. http)host - host in any of the valid syntaxes: reg-name ( a dns name), ipv4 literal (1.2.3.4), ipv6 literal
([::1]), excluding IPvFuture since no one uses that in practiceport - port@Nonnull public static UrlBuilder fromUrl(@Nonnull java.net.URL url) throws java.nio.charset.CharacterCodingException
fromUrl(URL, CharsetDecoder) with a UTF-8 CharsetDecoder. The same semantics about the
query string apply.url - url to initialize builder withjava.nio.charset.CharacterCodingException - if char decoding failsfromUrl(URL, CharsetDecoder)@Nonnull public static UrlBuilder fromUrl(@Nonnull java.net.URL url, @Nonnull java.nio.charset.CharsetDecoder charsetDecoder) throws java.nio.charset.CharacterCodingException
URL.
The query string will be parsed into HTML4 query params if it can be separated into a
&-separated sequence of key=value pairs. The sequence of query params can then be
appended to by continuing to call queryParam(String, String). The concept of query params is
only part of the HTML spec (and common HTTP usage), though, so it's perfectly legal to have a query string that
is in some other form. To represent this case, if the aforementioned param-parsing attempt fails, the query
string will be treated as just a monolithic, unstructured, string. In this case, calls to queryParam(String, String) on the resulting instance will throw IllegalStateException, and only calls
to unstructuredQuery(String)}, which replaces the entire query string, are allowed.url - url to initialize builder withcharsetDecoder - the decoder to decode encoded bytes with (except for reg names, which are always UTF-8)java.nio.charset.CharacterCodingException - if decoding percent-encoded bytes fails and charsetDecoder is configured to
report errorsfromUrl(URL, CharsetDecoder)@Nonnull public UrlBuilder pathSegment(@Nonnull java.lang.String segment)
segment - a path segment@Nonnull public UrlBuilder pathSegments(java.lang.String... segments)
pathSegment(String).segments - path segments@Nonnull public UrlBuilder queryParam(@Nonnull java.lang.String name, @Nonnull java.lang.String value)
unstructuredQuery(String). See fromUrl(URL, CharsetDecoder).name - param namevalue - param value@Nonnull public UrlBuilder unstructuredQuery(@Nonnull java.lang.String query)
queryParam(String, String). See fromUrl(URL,
CharsetDecoder).query - Complete URI query, as specified by https://tools.ietf.org/html/rfc3986#section-3.4@Nonnull public UrlBuilder clearQuery()
@Nonnull public UrlBuilder matrixParam(@Nonnull java.lang.String name, @Nonnull java.lang.String value)
name - param namevalue - param value@Nonnull public UrlBuilder fragment(@Nonnull java.lang.String fragment)
fragment - fragment string@Nonnull public UrlBuilder forceTrailingSlash()
public java.lang.String toUrlString()
throws java.nio.charset.CharacterCodingException
java.nio.charset.CharacterCodingException - if character encoding fails and the encoder is configured to report errors