public abstract class UriBuilder extends Object
Path.value()
for an explanation of URI templates.
Builder methods perform contextual encoding of characters not permitted in
the corresponding URI component following the rules of the
application/x-www-form-urlencoded
media type for query parameters and
RFC 3986 for all other
components. Note that only characters not permitted in a particular component
are subject to encoding so, e.g., a path supplied to one of the path
methods may contain matrix parameters or multiple path segments since the
separators are legal characters and will not be encoded. Percent encoded
values are also recognized where allowed and will not be double encoded.
URI templates are allowed in most components of a URI but their value is restricted to a particular component. E.g.
UriBuilder.fromPath("{arg1}").build("foo#bar");
would result in encoding of the '#' such that the resulting URI is
"foo%23bar". To create a URI "foo#bar" use
UriBuilder.fromPath("{arg1}").fragment("{arg2}").build("foo", "bar")
instead. URI template names and delimiters are never encoded but their
values are encoded when a URI is built.
Template parameter regular expressions are ignored when building a URI, i.e.
no validation is performed.Modifier | Constructor and Description |
---|---|
protected |
UriBuilder()
Protected constructor, use one of the static
fromXXX
methods to obtain an instance. |
Modifier and Type | Method and Description |
---|---|
abstract URI |
build(Object... values)
Build a URI, using the supplied values in order to replace any URI
template parameters.
|
abstract URI |
buildFromEncoded(Object... values)
Build a URI.
|
abstract URI |
buildFromEncodedMap(Map<String,? extends Object> values)
Build a URI, any URI template parameters will be replaced by the value in
the supplied map.
|
abstract URI |
buildFromMap(Map<String,? extends Object> values)
Build a URI, any URI template parameters will be replaced by the value in
the supplied map.
|
abstract UriBuilder |
clone()
Create a copy of the UriBuilder preserving its state.
|
abstract UriBuilder |
fragment(String fragment)
Set the URI fragment.
|
static UriBuilder |
fromPath(String path)
Create a new instance representing a relative URI initialized from a
URI path.
|
static UriBuilder |
fromResource(Class<?> resource)
Create a new instance representing a relative URI initialized from a
root resource class.
|
static UriBuilder |
fromUri(String uri)
Create a new instance initialized from an existing URI.
|
static UriBuilder |
fromUri(URI uri)
Create a new instance initialized from an existing URI.
|
abstract UriBuilder |
host(String host)
Set the URI host.
|
abstract UriBuilder |
matrixParam(String name,
Object... values)
Append a matrix parameter to the existing set of matrix parameters of
the current final segment of the URI path.
|
protected static UriBuilder |
newInstance()
Creates a new instance of UriBuilder.
|
abstract UriBuilder |
path(Class resource)
Append the path from a Path-annotated class to the
existing path.
|
abstract UriBuilder |
path(Class resource,
String method)
Append the path from a Path-annotated method to the
existing path.
|
abstract UriBuilder |
path(Method method)
Append the path from a
Path -annotated method to the
existing path. |
abstract UriBuilder |
path(String path)
Append path to the existing path.
|
abstract UriBuilder |
port(int port)
Set the URI port.
|
abstract UriBuilder |
queryParam(String name,
Object... values)
Append a query parameter to the existing set of query parameters.
|
abstract UriBuilder |
replaceMatrix(String matrix)
Set the matrix parameters of the current final segment of the current URI path.
|
abstract UriBuilder |
replaceMatrixParam(String name,
Object... values)
Replace the existing value(s) of a matrix parameter on
the current final segment of the URI path.
|
abstract UriBuilder |
replacePath(String path)
Set the URI path.
|
abstract UriBuilder |
replaceQuery(String query)
Set the URI query string.
|
abstract UriBuilder |
replaceQueryParam(String name,
Object... values)
Replace the existing value(s) of a query parameter.
|
abstract UriBuilder |
scheme(String scheme)
Set the URI scheme.
|
abstract UriBuilder |
schemeSpecificPart(String ssp)
Set the URI scheme-specific-part (see
URI ). |
abstract UriBuilder |
segment(String... segments)
Append path segments to the existing path.
|
abstract UriBuilder |
uri(URI uri)
Copies the non-null components of the supplied URI to the UriBuilder replacing
any existing values for those components.
|
abstract UriBuilder |
userInfo(String ui)
Set the URI user-info.
|
protected UriBuilder()
fromXXX
methods to obtain an instance.protected static UriBuilder newInstance()
public static UriBuilder fromUri(URI uri) throws IllegalArgumentException
uri
- a URI that will be used to initialize the UriBuilder.IllegalArgumentException
- if uri is nullpublic static UriBuilder fromUri(String uri) throws IllegalArgumentException
uri
- a URI that will be used to initialize the UriBuilder, may not
contain URI parameters.IllegalArgumentException
- if uri is not a valid URI or is nullpublic static UriBuilder fromPath(String path) throws IllegalArgumentException
path
- a URI path that will be used to initialize the UriBuilder,
may contain URI template parameters.IllegalArgumentException
- if path is nullpublic static UriBuilder fromResource(Class<?> resource) throws IllegalArgumentException
resource
- a root resource whose Path
value will
be used to initialize the UriBuilder.IllegalArgumentException
- if resource is not annotated with
Path
or resource is null.public abstract UriBuilder clone()
build(java.lang.Object...)
method.public abstract UriBuilder uri(URI uri) throws IllegalArgumentException
uri
- the URI to copy components fromIllegalArgumentException
- if uri is nullpublic abstract UriBuilder scheme(String scheme) throws IllegalArgumentException
scheme
- the URI scheme, may contain URI template parameters.
A null value will unset the URI scheme.IllegalArgumentException
- if scheme is invalidpublic abstract UriBuilder schemeSpecificPart(String ssp) throws IllegalArgumentException
URI
). This
method will overwrite any existing
values for authority, user-info, host, port and path.ssp
- the URI scheme-specific-part, may contain URI template parametersIllegalArgumentException
- if ssp cannot be parsed or is nullpublic abstract UriBuilder userInfo(String ui)
ui
- the URI user-info, may contain URI template parameters.
A null value will unset userInfo component of the URI.public abstract UriBuilder host(String host) throws IllegalArgumentException
host
- the URI host, may contain URI template parameters.
A null value will unset the host component of the URI.IllegalArgumentException
- if host is invalid.public abstract UriBuilder port(int port) throws IllegalArgumentException
port
- the URI port, a value of -1 will unset an explicit port.IllegalArgumentException
- if port is invalidpublic abstract UriBuilder replacePath(String path)
path
- the path, may contain URI template parameters.
A null value will unset the path component of the URI.public abstract UriBuilder path(String path) throws IllegalArgumentException
path
- the path, may contain URI template parametersIllegalArgumentException
- if path is nullpublic abstract UriBuilder path(Class resource) throws IllegalArgumentException
resource
- a resource whose Path
value will be
used to obtain the path to append.IllegalArgumentException
- if resource is null, or
if resource is not annotated with Path
public abstract UriBuilder path(Class resource, String method) throws IllegalArgumentException
path(Method)
, it
can only be used in cases where there is a single method with the
specified name that is annotated with Path
.resource
- the resource containing the methodmethod
- the name of the method whose Path
value will be
used to obtain the path to appendIllegalArgumentException
- if resource or method is null,
or there is more than or less than one variant of the method annotated with
Path
public abstract UriBuilder path(Method method) throws IllegalArgumentException
Path
-annotated method to the
existing path.
When constructing the final path, a '/' separator will be inserted
between the existing path and the supplied path if necessary.method
- a method whose Path
value will be
used to obtain the path to append to the existing pathIllegalArgumentException
- if method is null or is
not annotated with a Path
public abstract UriBuilder segment(String... segments) throws IllegalArgumentException
segments
- the path segment values, each may contain URI template
parametersIllegalArgumentException
- if segments or any element of segments
is nullpublic abstract UriBuilder replaceMatrix(String matrix) throws IllegalArgumentException
matrix
- the matrix parameters, may contain URI template parameters.
A null value will remove all matrix parameters of the current final segment
of the current URI path.IllegalArgumentException
- if matrix cannot be parsedpublic abstract UriBuilder matrixParam(String name, Object... values) throws IllegalArgumentException
name
- the matrix parameter name, may contain URI template parametersvalues
- the matrix parameter value(s), each object will be converted
to a String
using its toString()
method. Stringified
values may contain URI template parameters.IllegalArgumentException
- if name or values is nullpublic abstract UriBuilder replaceMatrixParam(String name, Object... values) throws IllegalArgumentException
name
- the matrix parameter name, may contain URI template parametersvalues
- the matrix parameter value(s), each object will be converted
to a String
using its toString()
method. Stringified
values may contain URI template parameters. If values
is empty
or null then all current values of the parameter are removed.IllegalArgumentException
- if name is null.public abstract UriBuilder replaceQuery(String query) throws IllegalArgumentException
query
- the URI query string, may contain URI template parameters.
A null value will remove all query parameters.IllegalArgumentException
- if query cannot be parsedpublic abstract UriBuilder queryParam(String name, Object... values) throws IllegalArgumentException
name
- the query parameter name, may contain URI template parametersvalues
- the query parameter value(s), each object will be converted
to a String
using its toString()
method. Stringified
values may contain URI template parameters.IllegalArgumentException
- if name or values is nullpublic abstract UriBuilder replaceQueryParam(String name, Object... values) throws IllegalArgumentException
name
- the query parameter name, may contain URI template parametersvalues
- the query parameter value(s), each object will be converted
to a String
using its toString()
method. Stringified
values may contain URI template parameters. If values
is empty
or null then all current values of the parameter are removed.IllegalArgumentException
- if name is nullpublic abstract UriBuilder fragment(String fragment)
fragment
- the URI fragment, may contain URI template parameters.
A null value will remove any existing fragment.public abstract URI buildFromMap(Map<String,? extends Object> values) throws IllegalArgumentException, UriBuilderException
String
using
their toString
method and are then encoded to match the
rules of the URI component to which they pertain. All '%' characters
in the stringified values will be encoded.
The state of the builder is unaffected; this method may be called
multiple times on the same builder instance.values
- a map of URI template parameter names and valuesIllegalArgumentException
- if there are any URI template parameters
without a supplied value, or if a template parameter value is null.UriBuilderException
- if a URI cannot be constructed based on the
current state of the builder.public abstract URI buildFromEncodedMap(Map<String,? extends Object> values) throws IllegalArgumentException, UriBuilderException
String
using
their toString
method and are then encoded to match the
rules of the URI component to which they pertain. All % characters in
the stringified values that are not followed by two hexadecimal numbers
will be encoded.
The state of the builder is unaffected; this method may be called
multiple times on the same builder instance.values
- a map of URI template parameter names and valuesIllegalArgumentException
- if there are any URI template parameters
without a supplied value, or if a template parameter value is null.UriBuilderException
- if a URI cannot be constructed based on the
current state of the builder.public abstract URI build(Object... values) throws IllegalArgumentException, UriBuilderException
String
using
their toString
method and are then encoded to match the
rules of the URI component to which they pertain. All '%' characters
in the stringified values will be encoded.
The state of the builder is unaffected; this method may be called
multiple times on the same builder instance.
All instances of the same template parameter will be replaced by the same value that corresponds to the position of the first instance of the template parameter. e.g. the template "{a}/{b}/{a}" with values {"x", "y", "z"} will result in the the URI "x/y/x", not "x/y/z".
values
- a list of URI template parameter valuesIllegalArgumentException
- if there are any URI template parameters
without a supplied value, or if a value is null.UriBuilderException
- if a URI cannot be constructed based on the
current state of the builder.public abstract URI buildFromEncoded(Object... values) throws IllegalArgumentException, UriBuilderException
String
using
their toString
method and are then encoded to match the
rules of the URI component to which they pertain. All % characters in
the stringified values that are not followed by two hexadecimal numbers
will be encoded.
The state of the builder is unaffected; this method may be called
multiple times on the same builder instance.
All instances of the same template parameter will be replaced by the same value that corresponds to the position of the first instance of the template parameter. e.g. the template "{a}/{b}/{a}" with values {"x", "y", "z"} will result in the the URI "x/y/x", not "x/y/z".
values
- a list of URI template parameter valuesIllegalArgumentException
- if there are any URI template parameters
without a supplied value, or if a value is null.UriBuilderException
- if a URI cannot be constructed based on the
current state of the builder.Copyright © 2012 JBoss by Red Hat. All Rights Reserved.