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.-
Constructor Summary
ModifierConstructorDescriptionprotected
Protected constructor, use one of the staticfromXxx(...)
methods to obtain an instance. -
Method Summary
Modifier and TypeMethodDescriptionabstract URI
Build a URI, using the supplied values in order to replace any URI template parameters.abstract URI
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, ?> values) Build a URI.abstract URI
buildFromMap
(Map<String, ?> values) Build a URI.abstract URI
buildFromMap
(Map<String, ?> values, boolean encodeSlashInPath) Build a URI.abstract UriBuilder
clone()
Create a copy of the UriBuilder preserving its state.abstract UriBuilder
Set the URI fragment.static UriBuilder
Create a new instance initialized from a Link.static UriBuilder
fromMethod
(Class<?> resource, String method) Create a new instance representing a relative URI initialized from aPath
-annotated method.static UriBuilder
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
Create a new instance initialized from an existing URI.static UriBuilder
Create a new instance initialized from an existing URI.abstract UriBuilder
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.static UriBuilder
Creates a new instance of UriBuilder.abstract UriBuilder
Append the path from a Path-annotated class to the existing path.abstract UriBuilder
Append the path from a Path-annotated method to the existing path.abstract UriBuilder
Append the path from aPath
-annotated method to the existing path.abstract UriBuilder
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
resolveTemplate
(String name, Object value) Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied value.abstract UriBuilder
resolveTemplate
(String name, Object value, boolean encodeSlashInPath) Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied value.abstract UriBuilder
resolveTemplateFromEncoded
(String name, Object value) Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied encoded value.abstract UriBuilder
resolveTemplates
(Map<String, Object> templateValues) Resolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs.abstract UriBuilder
resolveTemplates
(Map<String, Object> templateValues, boolean encodeSlashInPath) Resolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs.abstract UriBuilder
resolveTemplatesFromEncoded
(Map<String, Object> templateValues) Resolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs.abstract UriBuilder
Set the URI scheme.abstract UriBuilder
schemeSpecificPart
(String ssp) Set the URI scheme-specific-part (seeURI
).abstract UriBuilder
Append path segments to the existing path.abstract String
Get the URI template string represented by this URI builder.abstract UriBuilder
Parses theuriTemplate
string and copies the parsed components of the supplied URI to the UriBuilder replacing any existing values for those components.abstract UriBuilder
Copies the non-null components of the supplied URI to the UriBuilder replacing any existing values for those components.abstract UriBuilder
Set the URI user-info.
-
Constructor Details
-
UriBuilder
protected UriBuilder()Protected constructor, use one of the staticfromXxx(...)
methods to obtain an instance.
-
-
Method Details
-
newInstance
Creates a new instance of UriBuilder.- Returns:
- a new instance of UriBuilder.
- Since:
- 3.0
-
fromUri
Create a new instance initialized from an existing URI.- Parameters:
uri
- a URI that will be used to initialize the UriBuilder.- Returns:
- a new UriBuilder.
- Throws:
IllegalArgumentException
- if uri isnull
.
-
fromUri
Create a new instance initialized from an existing URI.- Parameters:
uriTemplate
- a URI template that will be used to initialize the UriBuilder, may contain URI parameters.- Returns:
- a new UriBuilder.
- Throws:
IllegalArgumentException
- ifuriTemplate
is not a valid URI template or isnull
.
-
fromLink
Create a new instance initialized from a Link.- Parameters:
link
- a Link that will be used to initialize the UriBuilder, only its URI is used.- Returns:
- a new UriBuilder
- Throws:
IllegalArgumentException
- if link isnull
- Since:
- 2.0
-
fromPath
Create a new instance representing a relative URI initialized from a URI path.- Parameters:
path
- a URI path that will be used to initialize the UriBuilder, may contain URI template parameters.- Returns:
- a new UriBuilder.
- Throws:
IllegalArgumentException
- if path isnull
.
-
fromResource
Create a new instance representing a relative URI initialized from a root resource class.- Parameters:
resource
- a root resource whosePath
value will be used to initialize the UriBuilder.- Returns:
- a new UriBuilder.
- Throws:
IllegalArgumentException
- if resource is not annotated withPath
or resource isnull
.
-
fromMethod
Create a new instance representing a relative URI initialized from aPath
-annotated method. This method can only be used in cases where there is a single method with the specified name that is annotated withPath
.- Parameters:
resource
- the resource containing the method.method
- the name of the method whosePath
value will be used to obtain the path to append.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if resource or method isnull
, or there is more than or less than one variant of the method annotated withPath
.- Since:
- 2.0
-
clone
Create a copy of the UriBuilder preserving its state. This is a more efficient means of creating a copy than constructing a new UriBuilder from a URI returned by thebuild(Object...)
method. -
uri
Copies the non-null components of the supplied URI to the UriBuilder replacing any existing values for those components.- Parameters:
uri
- the URI to copy components from.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if theuri
parameter isnull
.
-
uri
Parses theuriTemplate
string and copies the parsed components of the supplied URI to the UriBuilder replacing any existing values for those components.- Parameters:
uriTemplate
- a URI template that will be used to initialize the UriBuilder, may contain URI parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- ifuriTemplate
is not a valid URI template or isnull
.- Since:
- 2.0
-
scheme
Set the URI scheme.- Parameters:
scheme
- the URI scheme, may contain URI template parameters. Anull
value will unset the URI scheme, but will not unset the any scheme-specific-part components.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if scheme is invalid.
-
schemeSpecificPart
Set the URI scheme-specific-part (seeURI
). This method will overwrite any existing values for authority, user-info, host, port and path.- Parameters:
ssp
- the URI scheme-specific-part, may contain URI template parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if ssp cannot be parsed or isnull
.
-
userInfo
Set the URI user-info.- Parameters:
ui
- the URI user-info, may contain URI template parameters. Anull
value will unset userInfo component of the URI.- Returns:
- the updated UriBuilder.
-
host
Set the URI host.- Parameters:
host
- the URI host, may contain URI template parameters. Anull
value will unset the host component of the URI, but will not unset other authority component parts (user info
orport
).- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if host is invalid.
-
port
Set the URI port.- Parameters:
port
- the URI port, a value of -1 will unset an explicit port.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if port is invalid.
-
replacePath
Set the URI path. This method will overwrite any existing path and associated matrix parameters. Existing '/' characters are preserved thus a single value can represent multiple URI path segments.- Parameters:
path
- the path, may contain URI template parameters. Anull
value will unset the path component of the URI.- Returns:
- the updated UriBuilder.
-
path
Append path to the existing path. When constructing the final path, a '/' separator will be inserted between the existing path and the supplied path if necessary. Existing '/' characters are preserved thus a single value can represent multiple URI path segments.- Parameters:
path
- the path, may contain URI template parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if path isnull
.
-
path
Append the path from a Path-annotated class to the existing path. When constructing the final path, a '/' separator will be inserted between the existing path and the supplied path if necessary.- Parameters:
resource
- a resource whosePath
value will be used to obtain the path to append.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if resource isnull
, or if resource is not annotated withPath
.
-
path
Append the path from a 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. This method is a convenience shortcut topath(Method)
, it can only be used in cases where there is a single method with the specified name that is annotated withPath
.- Parameters:
resource
- the resource containing the method.method
- the name of the method whosePath
value will be used to obtain the path to append.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if resource or method isnull
, or there is more than or less than one variant of the method annotated withPath
.
-
path
Append the path from aPath
-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.- Parameters:
method
- a method whosePath
value will be used to obtain the path to append to the existing path.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if method isnull
or is not annotated with aPath
.
-
segment
Append path segments to the existing path. When constructing the final path, a '/' separator will be inserted between the existing path and the first path segment if necessary and each supplied segment will also be separated by '/'. Existing '/' characters are encoded thus a single value can only represent a single URI path segment.- Parameters:
segments
- the path segment values, each may contain URI template parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if segments or any element of segments isnull
.
-
replaceMatrix
Set the matrix parameters of the current final segment of the current URI path. This method will overwrite any existing matrix parameters on the current final segment of the current URI path. Note that the matrix parameters are tied to a particular path segment; subsequent addition of path segments will not affect their position in the URI path.- Parameters:
matrix
- the matrix parameters, may contain URI template parameters. Anull
value will remove all matrix parameters of the current final segment of the current URI path.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if matrix cannot be parsed.- See Also:
-
matrixParam
Append a matrix parameter to the existing set of matrix parameters of the current final segment of the URI path. If multiple values are supplied the parameter will be added once per value. Note that the matrix parameters are tied to a particular path segment; subsequent addition of path segments will not affect their position in the URI path.- Parameters:
name
- the matrix parameter name, may contain URI template parameters.values
- the matrix parameter value(s), each object will be converted. to aString
using itstoString()
method. Stringified values may contain URI template parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if name or values isnull
.- See Also:
-
replaceMatrixParam
Replace the existing value(s) of a matrix parameter on the current final segment of the URI path. If multiple values are supplied the parameter will be added once per value. Note that the matrix parameters are tied to a particular path segment; subsequent addition of path segments will not affect their position in the URI path.- Parameters:
name
- the matrix parameter name, may contain URI template parameters.values
- the matrix parameter value(s), each object will be converted. to aString
using itstoString()
method. Stringified values may contain URI template parameters. Ifvalues
is empty ornull
then all current values of the parameter are removed.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if name isnull
.- See Also:
-
replaceQuery
Set the URI query string. This method will overwrite any existing query parameters.- Parameters:
query
- the URI query string, may contain URI template parameters. Anull
value will remove all query parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if query cannot be parsed.
-
queryParam
Append a query parameter to the existing set of query parameters. If multiple values are supplied the parameter will be added once per value.- Parameters:
name
- the query parameter name, may contain URI template parameters.values
- the query parameter value(s), each object will be converted to aString
using itstoString()
method. Stringified values may contain URI template parameters.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if name or values isnull
.
-
replaceQueryParam
Replace the existing value(s) of a query parameter. If multiple values are supplied the parameter will be added once per value.- Parameters:
name
- the query parameter name, may contain URI template parameters.values
- the query parameter value(s), each object will be converted to aString
using itstoString()
method. Stringified values may contain URI template parameters. Ifvalues
is empty ornull
then all current values of the parameter are removed.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if name isnull
.
-
fragment
Set the URI fragment.- Parameters:
fragment
- the URI fragment, may contain URI template parameters. Anull
value will remove any existing fragment.- Returns:
- the updated UriBuilder.
-
resolveTemplate
Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied value. In case anull
template name or value is entered aIllegalArgumentException
is thrown.- Parameters:
name
- name of the URI template.value
- value to be used to resolve the template.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the resolved template name or value isnull
.- Since:
- 2.0
-
resolveTemplate
Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied value. In case anull
template name or value is entered aIllegalArgumentException
is thrown.- Parameters:
name
- name of the URI template.value
- value to be used to resolve the template.encodeSlashInPath
- iftrue
, the slash ('/'
) characters in template 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.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the resolved template name or value isnull
.- Since:
- 2.0
-
resolveTemplateFromEncoded
Resolve a URI template with a givenname
in thisUriBuilder
instance using a supplied encoded value. A template with a matching name will be replaced by the supplied value. Value is converted toString
using itstoString()
method and is 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. In case anull
template name or encoded value is entered aIllegalArgumentException
is thrown.- Parameters:
name
- name of the URI template.value
- encoded value to be used to resolve the template.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the resolved template name or encoded value isnull
.- Since:
- 2.0
-
resolveTemplates
Resolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs. A call to the method with an empty parameter map is ignored.- Parameters:
templateValues
- a map of URI template names and their values.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the name-value map or any of the names or values in the map isnull
.- Since:
- 2.0
-
resolveTemplates
public abstract UriBuilder resolveTemplates(Map<String, Object> templateValues, boolean encodeSlashInPath) throws IllegalArgumentExceptionResolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs. A call to the method with an empty parameter map is ignored.- Parameters:
templateValues
- a map of URI template names and their values.encodeSlashInPath
- iftrue
, the slash ('/'
) characters in template 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.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the name-value map or any of the names or values in the map isnull
.- Since:
- 2.0
-
resolveTemplatesFromEncoded
Resolve one or more URI templates in thisUriBuilder
instance using supplied name-value pairs. All templates with their name matching one of the keys in the supplied map will be replaced by the value in the supplied map. Values are converted toString
using theirtoString()
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. A call to the method with an empty parameter map is ignored.- Parameters:
templateValues
- a map of URI template names and their values.- Returns:
- the updated UriBuilder.
- Throws:
IllegalArgumentException
- if the name-value map or any of the names or values in the map isnull
.- Since:
- 2.0
-
buildFromMap
Build a URI. Any URI template parameters will be replaced by the value in the supplied map. Values are converted toString
using theirtoString()
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 invokingbuildFromMap(valueMap, true)
. To override this behavior usebuildFromMap(valueMap, false)
instead.- Parameters:
values
- a map of URI template parameter names and values.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a template parameter value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- See Also:
-
buildFromMap
public abstract URI buildFromMap(Map<String, ?> values, boolean encodeSlashInPath) throws IllegalArgumentException, UriBuilderExceptionBuild a URI. Any URI template parameters will be replaced by the value in the supplied map. Values are converted toString
using theirtoString()
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 thebuildFromMap(java.util.Map)
method. If theencodeSlashInPath
parameter is set totrue
(default), the slash ('/'
) characters in parameter values will be encoded if the template is placed in the URI path component. If set tofalse
the default encoding behavior is overridden an slash characters in template values will not be encoded when used to substitute path templates.- Parameters:
values
- a map of URI template parameter names and values.encodeSlashInPath
- iftrue
, 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.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a template parameter value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- See Also:
-
buildFromEncodedMap
public abstract URI buildFromEncodedMap(Map<String, ?> values) throws IllegalArgumentException, UriBuilderExceptionBuild a URI. Any URI template parameters will be replaced by the value in the supplied map. Values are converted toString
using theirtoString()
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.- Parameters:
values
- a map of URI template parameter names and values.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a template parameter value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- Since:
- 2.0
- See Also:
-
build
Build a URI, using the supplied values in order to replace any URI template parameters. Values are converted toString
using theirtoString()
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 invokingbuild(Object[], boolean)
build(values, true)}. To override this behavior usebuild(values, false)
instead.- Parameters:
values
- a list of URI template parameter values.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- See Also:
-
build
public abstract URI build(Object[] values, boolean encodeSlashInPath) throws IllegalArgumentException, UriBuilderException Build a URI, using the supplied values in order to replace any URI template parameters. Values are converted toString
using theirtoString()
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 thebuild(Object[])
method. If theencodeSlashInPath
parameter is set totrue
(default), the slash ('/'
) characters in parameter values will be encoded if the template is placed in the URI path component. If set tofalse
the default encoding behavior is overridden an slash characters in template values will not be encoded when used to substitute path templates.- Parameters:
values
- a list of URI template parameter values.encodeSlashInPath
- iftrue
, 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.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- Since:
- 2.0
- See Also:
-
buildFromEncoded
public abstract URI buildFromEncoded(Object... values) throws IllegalArgumentException, UriBuilderException Build a URI. Any URI templates parameters will be replaced with the supplied values in order. Values are converted toString
using theirtoString()
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".
- Parameters:
values
- a list of URI template parameter values.- Returns:
- the URI built from the UriBuilder.
- Throws:
IllegalArgumentException
- if there are any URI template parameters without a supplied value, or if a value isnull
.UriBuilderException
- if a URI cannot be constructed based on the current state of the builder.- See Also:
-
toTemplate
Get the URI template string represented by this URI builder.- Returns:
- the URI template string for this URI builder.
- Since:
- 2.0
-