|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.ws.rs.core.UriBuilder
public abstract class UriBuilder
URI template-aware utility class for building URIs from their components. See
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.
URI
,
Path
Constructor Summary | |
---|---|
protected |
UriBuilder()
Protected constructor, use one of the static fromXxx(...)
methods to obtain an instance. |
Method Summary | |
---|---|
abstract java.net.URI |
build(java.lang.Object... values)
Build a URI, using the supplied values in order to replace any URI template parameters. |
abstract java.net.URI |
build(java.lang.Object[] values,
boolean encodeSlashInPath)
Build a URI, using the supplied values in order to replace any URI template parameters. |
abstract java.net.URI |
buildFromEncoded(java.lang.Object... values)
Build a URI. |
abstract java.net.URI |
buildFromEncodedMap(java.util.Map<java.lang.String,?> values)
Build a URI. |
abstract java.net.URI |
buildFromMap(java.util.Map<java.lang.String,?> values)
Build a URI. |
abstract java.net.URI |
buildFromMap(java.util.Map<java.lang.String,?> values,
boolean encodeSlashInPath)
Build a URI. |
abstract UriBuilder |
clone()
Create a copy of the UriBuilder preserving its state. |
abstract UriBuilder |
fragment(java.lang.String fragment)
Set the URI fragment. |
static UriBuilder |
fromLink(Link link)
Create a new instance initialized from a Link. |
static UriBuilder |
fromMethod(java.lang.Class<?> resource,
java.lang.String method)
Create a new instance representing a relative URI initialized from a Path -annotated method. |
static UriBuilder |
fromPath(java.lang.String path)
Create a new instance representing a relative URI initialized from a URI path. |
static UriBuilder |
fromResource(java.lang.Class<?> resource)
Create a new instance representing a relative URI initialized from a root resource class. |
static UriBuilder |
fromUri(java.lang.String uriTemplate)
Create a new instance initialized from an existing URI. |
static UriBuilder |
fromUri(java.net.URI uri)
Create a new instance initialized from an existing URI. |
abstract UriBuilder |
host(java.lang.String host)
Set the URI host. |
abstract UriBuilder |
matrixParam(java.lang.String name,
java.lang.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(java.lang.Class<?> resource)
Append the path from a Path-annotated class to the existing path. |
abstract UriBuilder |
path(java.lang.Class<?> resource,
java.lang.String method)
Append the path from a Path-annotated method to the existing path. |
abstract UriBuilder |
path(java.lang.reflect.Method method)
Append the path from a Path -annotated method to the
existing path. |
abstract UriBuilder |
path(java.lang.String path)
Append path to the existing path. |
abstract UriBuilder |
port(int port)
Set the URI port. |
abstract UriBuilder |
queryParam(java.lang.String name,
java.lang.Object... values)
Append a query parameter to the existing set of query parameters. |
abstract UriBuilder |
replaceMatrix(java.lang.String matrix)
Set the matrix parameters of the current final segment of the current URI path. |
abstract UriBuilder |
replaceMatrixParam(java.lang.String name,
java.lang.Object... values)
Replace the existing value(s) of a matrix parameter on the current final segment of the URI path. |
abstract UriBuilder |
replacePath(java.lang.String path)
Set the URI path. |
abstract UriBuilder |
replaceQuery(java.lang.String query)
Set the URI query string. |
abstract UriBuilder |
replaceQueryParam(java.lang.String name,
java.lang.Object... values)
Replace the existing value(s) of a query parameter. |
abstract UriBuilder |
scheme(java.lang.String scheme)
Set the URI scheme. |
abstract UriBuilder |
schemeSpecificPart(java.lang.String ssp)
Set the URI scheme-specific-part (see URI ). |
abstract UriBuilder |
segment(java.lang.String... segments)
Append path segments to the existing path. |
abstract java.lang.String |
toTemplate()
Get the URI template string represented by this URI builder. |
abstract UriBuilder |
uri(java.lang.String uriTemplate)
Parses the uriTemplate string and copies the parsed components of the supplied
URI to the UriBuilder replacing any existing values for those components. |
abstract UriBuilder |
uri(java.net.URI uri)
Copies the non-null components of the supplied URI to the UriBuilder replacing any existing values for those components. |
abstract UriBuilder |
userInfo(java.lang.String ui)
Set the URI user-info. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected UriBuilder()
fromXxx(...)
methods to obtain an instance.
Method Detail |
---|
protected static UriBuilder newInstance()
public static UriBuilder fromUri(java.net.URI uri) throws java.lang.IllegalArgumentException
uri
- a URI that will be used to initialize the UriBuilder.
java.lang.IllegalArgumentException
- if uri is null
.public static UriBuilder fromUri(java.lang.String uriTemplate) throws java.lang.IllegalArgumentException
uriTemplate
- a URI template that will be used to initialize the UriBuilder, may
contain URI parameters.
java.lang.IllegalArgumentException
- if uriTemplate
is not a valid URI template or
is null
.public static UriBuilder fromLink(Link link) throws java.lang.IllegalArgumentException
link
- a Link that will be used to initialize the UriBuilder, only
its URI is used.
java.lang.IllegalArgumentException
- if link is null
public static UriBuilder fromPath(java.lang.String path) throws java.lang.IllegalArgumentException
path
- a URI path that will be used to initialize the UriBuilder,
may contain URI template parameters.
java.lang.IllegalArgumentException
- if path is null
.public static UriBuilder fromResource(java.lang.Class<?> resource) throws java.lang.IllegalArgumentException
resource
- a root resource whose Path
value will
be used to initialize the UriBuilder.
java.lang.IllegalArgumentException
- if resource is not annotated with
Path
or resource is null
.public static UriBuilder fromMethod(java.lang.Class<?> resource, java.lang.String method) throws java.lang.IllegalArgumentException
Path
-annotated method.
This method 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 method.method
- the name of the method whose Path
value will be
used to obtain the path to append.
java.lang.IllegalArgumentException
- 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 clone()
build(Object...)
method.
clone
in class java.lang.Object
public abstract UriBuilder uri(java.net.URI uri) throws java.lang.IllegalArgumentException
uri
- the URI to copy components from.
java.lang.IllegalArgumentException
- if the uri
parameter is null
.public abstract UriBuilder uri(java.lang.String uriTemplate) throws java.lang.IllegalArgumentException
uriTemplate
string and copies the parsed components of the supplied
URI to the UriBuilder replacing any existing values for those components.
uriTemplate
- a URI template that will be used to initialize the UriBuilder, may
contain URI parameters.
java.lang.IllegalArgumentException
- if uriTemplate
is not a valid URI template or
is null
.public abstract UriBuilder scheme(java.lang.String scheme) throws java.lang.IllegalArgumentException
scheme
- the URI scheme, may contain URI template parameters.
A null
value will unset the URI scheme, but will
not unset the any scheme-specific-part components.
java.lang.IllegalArgumentException
- if scheme is invalid.public abstract UriBuilder schemeSpecificPart(java.lang.String ssp) throws java.lang.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 parameters.
java.lang.IllegalArgumentException
- if ssp cannot be parsed or is null
.public abstract UriBuilder userInfo(java.lang.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(java.lang.String host) throws java.lang.IllegalArgumentException
host
- the URI host, may contain URI template parameters.
A null
value will unset the host component of the URI, but
will not unset other authority component parts
(user info
or port
).
java.lang.IllegalArgumentException
- if host is invalid.public abstract UriBuilder port(int port) throws java.lang.IllegalArgumentException
port
- the URI port, a value of -1 will unset an explicit port.
java.lang.IllegalArgumentException
- if port is invalid.public abstract UriBuilder replacePath(java.lang.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(java.lang.String path) throws java.lang.IllegalArgumentException
path
- the path, may contain URI template parameters.
java.lang.IllegalArgumentException
- if path is null
.public abstract UriBuilder path(java.lang.Class<?> resource) throws java.lang.IllegalArgumentException
resource
- a resource whose Path
value will be
used to obtain the path to append.
java.lang.IllegalArgumentException
- if resource is null
, or
if resource is not annotated with Path
.public abstract UriBuilder path(java.lang.Class<?> resource, java.lang.String method) throws java.lang.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 method.method
- the name of the method whose Path
value will be
used to obtain the path to append.
java.lang.IllegalArgumentException
- 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(java.lang.reflect.Method method) throws java.lang.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 path.
java.lang.IllegalArgumentException
- if method is null
or is
not annotated with a Path
.public abstract UriBuilder segment(java.lang.String... segments) throws java.lang.IllegalArgumentException
segments
- the path segment values, each may contain URI template
parameters.
java.lang.IllegalArgumentException
- if segments or any element of segments
is null
.public abstract UriBuilder replaceMatrix(java.lang.String matrix) throws java.lang.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.
java.lang.IllegalArgumentException
- if matrix cannot be parsed.public abstract UriBuilder matrixParam(java.lang.String name, java.lang.Object... values) throws java.lang.IllegalArgumentException
name
- the matrix parameter name, may contain URI template parameters.values
- the matrix parameter value(s), each object will be converted.
to a String
using its toString()
method. Stringified
values may contain URI template parameters.
java.lang.IllegalArgumentException
- if name or values is null
.public abstract UriBuilder replaceMatrixParam(java.lang.String name, java.lang.Object... values) throws java.lang.IllegalArgumentException
name
- the matrix parameter name, may contain URI template parameters.values
- 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.
java.lang.IllegalArgumentException
- if name is null
.public abstract UriBuilder replaceQuery(java.lang.String query) throws java.lang.IllegalArgumentException
query
- the URI query string, may contain URI template parameters.
A null
value will remove all query parameters.
java.lang.IllegalArgumentException
- if query cannot be parsed.public abstract UriBuilder queryParam(java.lang.String name, java.lang.Object... values) throws java.lang.IllegalArgumentException
name
- the query parameter name, may contain URI template parameters.values
- the query parameter value(s), each object will be converted
to a String
using its toString()
method. Stringified
values may contain URI template parameters.
java.lang.IllegalArgumentException
- if name or values is null
.public abstract UriBuilder replaceQueryParam(java.lang.String name, java.lang.Object... values) throws java.lang.IllegalArgumentException
name
- the query parameter name, may contain URI template parameters.values
- 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.
java.lang.IllegalArgumentException
- if name is null
.public abstract UriBuilder fragment(java.lang.String fragment)
fragment
- the URI fragment, may contain URI template parameters.
A null
value will remove any existing fragment.
public abstract java.net.URI buildFromMap(java.util.Map<java.lang.String,?> values) throws java.lang.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.
NOTE: By default all '/'
characters in the stringified values will be
encoded in path templates, i.e. the result is identical to invoking
buildFromMap(valueMap, true)
.
To override this behavior use buildFromMap(valueMap, false)
instead.
values
- a map of URI template parameter names and values.
java.lang.IllegalArgumentException
- 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.buildFromMap(java.util.Map, boolean)
,
buildFromEncodedMap(java.util.Map)
public abstract java.net.URI buildFromMap(java.util.Map<java.lang.String,?> values, boolean encodeSlashInPath) throws java.lang.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.
The encodeSlashInPath
parameter may be used to override the default
encoding of '/'
characters in the stringified template values
in cases when the template is part of the URI path component when using
the buildFromMap(java.util.Map)
method. If the encodeSlashInPath
parameter is set to true
(default), the slash ('/'
) characters in
parameter values will be encoded if the template is placed in the URI path component.
If set to false
the default encoding behavior is overridden an slash characters
in template values will not be encoded when used to substitute path templates.
values
- a map of URI template parameter names and values.encodeSlashInPath
- if true
, the slash ('/'
) characters
in parameter values will be encoded if the template
is placed in the URI path component, otherwise the slash
characters will not be encoded in path templates.
java.lang.IllegalArgumentException
- 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.buildFromMap(java.util.Map)
,
buildFromEncodedMap(java.util.Map)
public abstract java.net.URI buildFromEncodedMap(java.util.Map<java.lang.String,?> values) throws java.lang.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 values.
java.lang.IllegalArgumentException
- 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.buildFromMap(java.util.Map)
,
buildFromMap(java.util.Map, boolean)
public abstract java.net.URI build(java.lang.Object... values) throws java.lang.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".
NOTE: By default all '/'
characters in the stringified values will be
encoded in path templates, i.e. the result is identical to invoking
build(Object[], boolean)
build(values, true)}.
To override this behavior use build(values, false)
instead.
values
- a list of URI template parameter values.
java.lang.IllegalArgumentException
- 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.build(Object[], boolean)
,
buildFromEncoded(Object...)
public abstract java.net.URI build(java.lang.Object[] values, boolean encodeSlashInPath) throws java.lang.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".
The encodeSlashInPath
parameter may be used to override the default
encoding of '/'
characters in the stringified template values
in cases when the template is part of the URI path component when using
the build(Object[])
method. If the encodeSlashInPath
parameter is set to true
(default), the slash ('/'
) characters in
parameter values will be encoded if the template is placed in the URI path component.
If set to false
the default encoding behavior is overridden an slash characters
in template values will not be encoded when used to substitute path templates.
values
- a list of URI template parameter values.encodeSlashInPath
- if true
, the slash ('/'
) characters
in parameter values will be encoded if the template
is placed in the URI path component, otherwise the slash
characters will not be encoded in path templates.
java.lang.IllegalArgumentException
- 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.build(Object[])
,
buildFromEncoded(Object...)
public abstract java.net.URI buildFromEncoded(java.lang.Object... values) throws java.lang.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 values.
java.lang.IllegalArgumentException
- 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.build(Object[])
,
build(Object[], boolean)
public abstract java.lang.String toTemplate()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |