javax.ws.rs.core
Class UriBuilder

java.lang.Object
  extended by javax.ws.rs.core.UriBuilder

public abstract class UriBuilder
extends java.lang.Object

URI template aware utility class for building URIs from their components. See Path.value() for an explanation of URI templates.

Many methods support automatic encoding of illegal characters, see encode(boolean) method. Encoding and validation of URI components follow the rules of the application/x-www-form-urlencoded media type for query parameters and RFC 3986 for all other components. Note that only illegal characters are subject to encoding so, e.g., a path segment 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.

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.

See Also:
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()
          Build a URI, any URI template parameters will be replaced by the empty string.
abstract  java.net.URI build(java.util.Map<java.lang.String,java.lang.Object> values)
          Build a URI, any URI template parameters will be replaced by the value in the supplied map.
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  UriBuilder clone()
          Create a copy of the UriBuilder preserving its state.
abstract  UriBuilder encode(boolean enable)
          Controls whether the UriBuilder will automatically encode URI components added by subsequent operations or not.
abstract  UriBuilder extension(java.lang.String extension)
          Set the extension of the current final path segment to the supplied value appending an initial "." if necessary.
abstract  UriBuilder fragment(java.lang.String fragment)
          Set the URI fragment using an unencoded value.
static UriBuilder fromPath(java.lang.String path)
          Create a new instance representing a relative URI initialized from an unencoded URI path, equivalent to fromPath(path, true).
static UriBuilder fromPath(java.lang.String path, boolean encode)
          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 with automatic encoding (see encode(boolean) method) turned on.
static UriBuilder fromUri(java.lang.String uri)
          Create a new instance initialized from an existing URI with automatic encoding (see encode(boolean) method) turned on.
static UriBuilder fromUri(java.net.URI uri)
          Create a new instance initialized from an existing URI with automatic encoding (see encode(boolean) method) turned on.
abstract  UriBuilder host(java.lang.String host)
          Set the URI host.
abstract  boolean isEncode()
          Get the current state of automatic encoding.
abstract  UriBuilder matrixParam(java.lang.String name, java.lang.String value)
          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 with automatic encoding (see encode(boolean) method) turned on.
abstract  UriBuilder path(java.lang.Class resource)
          Append path segments from a Path-annotated class to the existing list of segments.
abstract  UriBuilder path(java.lang.Class resource, java.lang.String method)
          Append path segments from a Path-annotated method to the existing list of segments.
abstract  UriBuilder path(java.lang.reflect.Method... methods)
          Append path segments from a list of Path-annotated methods to the existing list of segments.
abstract  UriBuilder path(java.lang.String... segments)
          Append path segments to the existing list of segments.
abstract  UriBuilder port(int port)
          Set the URI port.
abstract  UriBuilder queryParam(java.lang.String name, java.lang.String value)
          Append a query parameter to the existing set of query parameters.
abstract  UriBuilder replaceMatrixParams(java.lang.String matrix)
          Set the matrix parameters of the current final segment of the current URI path.
abstract  UriBuilder replacePath(java.lang.String... segments)
          Set the URI path.
abstract  UriBuilder replaceQueryParams(java.lang.String query)
          Set the URI query string.
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 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

UriBuilder

protected UriBuilder()
Protected constructor, use one of the static fromXXX methods to obtain an instance.

Method Detail

newInstance

protected static UriBuilder newInstance()
Creates a new instance of UriBuilder with automatic encoding (see encode(boolean) method) turned on.

Returns:
a new instance of UriBuilder

fromUri

public static UriBuilder fromUri(java.net.URI uri)
                          throws java.lang.IllegalArgumentException
Create a new instance initialized from an existing URI with automatic encoding (see encode(boolean) method) turned on.

Parameters:
uri - a URI that will be used to initialize the UriBuilder.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException - if uri is null

fromUri

public static UriBuilder fromUri(java.lang.String uri)
                          throws java.lang.IllegalArgumentException
Create a new instance initialized from an existing URI with automatic encoding (see encode(boolean) method) turned on.

Parameters:
uri - a URI that will be used to initialize the UriBuilder, may not contain URI parameters.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException - if uri is not a valid URI or is null

fromPath

public static UriBuilder fromPath(java.lang.String path)
                           throws java.lang.IllegalArgumentException
Create a new instance representing a relative URI initialized from an unencoded URI path, equivalent to fromPath(path, true).

Parameters:
path - a URI path that will be used to initialize the UriBuilder, may contain URI template parameters.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException - if path is null

fromPath

public static UriBuilder fromPath(java.lang.String path,
                                  boolean encode)
                           throws java.lang.IllegalArgumentException
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.
encode - controls whether the supplied value is automatically encoded (true) or not (false). If false, the value must be valid with all illegal characters already escaped. The supplied value will remain in force for subsequent operations and may be altered by calling the encode method.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException - if path is null, or if encode is false and path contains illegal characters

fromResource

public static UriBuilder fromResource(java.lang.Class<?> resource)
                               throws java.lang.IllegalArgumentException
Create a new instance representing a relative URI initialized from a root resource class with automatic encoding (see encode(boolean) method) turned on.

Parameters:
resource - a root resource whose Path value will be used to initialize the UriBuilder. The value of the encode property of the Path annotation will be used when processing the value of the Path but it will not be used to modify the state of automaic encoding for the builder.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException - if resource is not annotated with Path or if resource.encode is false and resource.value, or if resource is null contains illegal characters

clone

public abstract UriBuilder 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 the build() method.

Overrides:
clone in class java.lang.Object
Returns:
a copy of the UriBuilder

encode

public abstract UriBuilder encode(boolean enable)
Controls whether the UriBuilder will automatically encode URI components added by subsequent operations or not. Defaults to true unless overridden during creation or set via this method.

Parameters:
enable - automatic encoding (true) or disable it (false). If false, subsequent components added must be valid with all illegal characters already escaped.
Returns:
the updated UriBuilder

isEncode

public abstract boolean isEncode()
Get the current state of automatic encoding. Defaults to true unless overridden during creation or set via encode(boolean).

Returns:
true if automatic encoding is enable, false otherwise
See Also:
encode(boolean)

uri

public abstract UriBuilder uri(java.net.URI uri)
                        throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException - if uri is null

scheme

public abstract UriBuilder scheme(java.lang.String scheme)
                           throws java.lang.IllegalArgumentException
Set the URI scheme.

Parameters:
scheme - the URI scheme, may contain URI template parameters. A null value will unset the URI scheme.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if scheme is invalid

schemeSpecificPart

public abstract UriBuilder schemeSpecificPart(java.lang.String ssp)
                                       throws java.lang.IllegalArgumentException
Set the URI scheme-specific-part (see URI). 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:
java.lang.IllegalArgumentException - if ssp cannot be parsed or is null

userInfo

public abstract UriBuilder userInfo(java.lang.String ui)
                             throws java.lang.IllegalArgumentException
Set the URI user-info.

Parameters:
ui - the URI user-info, may contain URI template parameters. A null value will unset userInfo component of the URI.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if automatic encoding is disabled and ui contains illegal characters.

host

public abstract UriBuilder host(java.lang.String host)
                         throws java.lang.IllegalArgumentException
Set the URI host.

Parameters:
host - the URI host, may contain URI template parameters. A null value will unset the host component of the URI.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if host is invalid.

port

public abstract UriBuilder port(int port)
                         throws java.lang.IllegalArgumentException
Set the URI port.

Parameters:
port - the URI port, a value of -1 will unset an explicit port.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if port is invalid

replacePath

public abstract UriBuilder replacePath(java.lang.String... segments)
                                throws java.lang.IllegalArgumentException
Set the URI path. This method will overwrite any existing path segments and associated matrix parameters. When constructing the final path, each segment will be separated by '/' if necessary. Existing '/' characters are preserved thus a single segment value can represent multiple URI path segments.

Parameters:
segments - the path segments, may contain URI template parameters. A null value will unset the path component of the URI.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if any element of segments is null, or if automatic encoding is disabled and any element of segments contains illegal characters

path

public abstract UriBuilder path(java.lang.String... segments)
                         throws java.lang.IllegalArgumentException
Append path segments to the existing list of segments. When constructing the final path, each segment will be separated by '/' if necessary. Existing '/' characters are preserved thus a single segment value can represent multiple URI path segments.

Parameters:
segments - the path segments, may contain URI template parameters
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if any element of segments is null, or if automatic encoding is disabled and any element of segments contains illegal characters

path

public abstract UriBuilder path(java.lang.Class resource)
                         throws java.lang.IllegalArgumentException
Append path segments from a Path-annotated class to the existing list of segments. When constructing the final path, each segment will be separated by '/' if necessary. The value of the encode property of the Path annotation will be used when processing the value of the Path but it will not be used to modify the state of automaic encoding for the builder.

Parameters:
resource - a resource whose Path value will be used to obtain the path segment.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if resource is null, or if resource.encode is false and resource.value contains illegal characters, or if resource is not annotated with Path

path

public abstract UriBuilder path(java.lang.Class resource,
                                java.lang.String method)
                         throws java.lang.IllegalArgumentException
Append path segments from a Path-annotated method to the existing list of segments. When constructing the final path, each segment will be separated by '/' if necessary. This method is a convenience shortcut to path(Method), it can only be used in cases where there is a single method with the specified name that is annotated with Path.

Parameters:
resource - the resource containing the method
method - the name of the method whose Path value will be used to obtain the path segment
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if resource or method is null, or if the specified method does not exist, or there is more than or less than one variant of the method annotated with Path

path

public abstract UriBuilder path(java.lang.reflect.Method... methods)
                         throws java.lang.IllegalArgumentException
Append path segments from a list of Path-annotated methods to the existing list of segments. When constructing the final path, each segment will be separated by '/' if necessary. The value of the encode property of the Path annotation will be used when processing the value of the Path but it will not be used to modify the state of automaic encoding for the builder.

Parameters:
methods - a list of methods whose Path values will be used to obtain the path segments
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if any element of methods is null or is not annotated with a Path

extension

public abstract UriBuilder extension(java.lang.String extension)
Set the extension of the current final path segment to the supplied value appending an initial "." if necessary. The extension is everything following the first "." in the current final path segment of the URI excluding any matrix parameters that might be present after the extension

Parameters:
extension - the extension, a null value will unset an existing extension including a trailing "." if necessary
Returns:
the updated UriBuilder
See Also:
UriInfo.getPathExtension()

replaceMatrixParams

public abstract UriBuilder replaceMatrixParams(java.lang.String matrix)
                                        throws java.lang.IllegalArgumentException
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. A null value will remove all matrix parameters of the current final segment of the current URI path.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if matrix cannot be parsed, or if automatic encoding is disabled and any matrix parameter name or value contains illegal characters

matrixParam

public abstract UriBuilder matrixParam(java.lang.String name,
                                       java.lang.String value)
                                throws java.lang.IllegalArgumentException
Append a matrix parameter to the existing set of matrix parameters of the current final segment of the 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:
name - the matrix parameter name, may contain URI template parameters
value - the matrix parameter value, may contain URI template parameters
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if name or value is null, or if automatic encoding is disabled and name or value contains illegal characters

replaceQueryParams

public abstract UriBuilder replaceQueryParams(java.lang.String query)
                                       throws java.lang.IllegalArgumentException
Set the URI query string. This method will overwrite any existing query parameters.

Parameters:
query - the URI query string, may contain URI template parameters. A null value will remove all query parameters.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if query cannot be parsed or is null, or if automatic encoding is disabled and any query parameter name or value contains illegal characters

queryParam

public abstract UriBuilder queryParam(java.lang.String name,
                                      java.lang.String value)
                               throws java.lang.IllegalArgumentException
Append a query parameter to the existing set of query parameters.

Parameters:
name - the query parameter name, may contain URI template parameters
value - the query parameter value, may contain URI template parameters
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if name or value is null, or if automatic encoding is disabled and name or value contains illegal characters

fragment

public abstract UriBuilder fragment(java.lang.String fragment)
                             throws java.lang.IllegalArgumentException
Set the URI fragment using an unencoded value.

Parameters:
fragment - the URI fragment, may contain URI template parameters. A null value will remove any existing fragment.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException - if fragment is null, or if automatic encoding is disabled and fragment contains illegal characters

build

public abstract java.net.URI build()
                            throws UriBuilderException
Build a URI, any URI template parameters will be replaced by the empty string. The build method does not change the state of the UriBuilder and it may be called multiple times on the same builder instance.

Returns:
the URI built from the UriBuilder
Throws:
UriBuilderException - if there are any URI template parameters, or if a URI cannot be constructed based on the current state of the builder.

build

public abstract java.net.URI build(java.util.Map<java.lang.String,java.lang.Object> values)
                            throws java.lang.IllegalArgumentException,
                                   UriBuilderException
Build a URI, any URI template parameters will be replaced by the value in the supplied map. Values are converted to String using their toString method. The build method does not change the state of the UriBuilder and it 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:
java.lang.IllegalArgumentException - if automatic encoding is disabled and a supplied value contains illegal characters, or 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.

build

public abstract java.net.URI build(java.lang.Object... values)
                            throws java.lang.IllegalArgumentException,
                                   UriBuilderException
Build a URI, using the supplied values in order to replace any URI template parameters. Values are converted to String using their toString method. The build method does not change the state of the UriBuilder and it 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:
java.lang.IllegalArgumentException - if automatic encoding is disabled and a supplied value contains illegal characters, or 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 © 2008. All Rights Reserved.