Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * The contents of this file are subject to the terms
   * of the Common Development and Distribution License
   * (the "License").  You may not use this file except
   * in compliance with the License.
   * 
   * You can obtain a copy of the license at
   * http://www.opensource.org/licenses/cddl1.php
   * See the License for the specific language governing
  * permissions and limitations under the License.
  */
 
 /*
  * UriBuilder.java
  *
  * Created on July 18, 2007, 11:53 AM
  *
  */
 
 package javax.ws.rs.core;
 
 import java.net.URI;
 import java.util.Map;
URI template aware utility class for building URIs from their components. See javax.ws.rs.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.

 
 public abstract class UriBuilder {
    
    
Protected constructor, use one of the static fromXXX methods to obtain an instance.
 
     protected UriBuilder() {}
    
    
Creates a new instance of UriBuilder.

Returns:
a new instance of UriBuilder
 
     protected static UriBuilder newInstance() {
         UriBuilder b = RuntimeDelegate.getInstance().createUriBuilder();
         return b;
     }
    
    
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:
java.lang.IllegalArgumentException if uri is null
 
     public static UriBuilder fromUri(URI urithrows IllegalArgumentException {
         UriBuilder b = newInstance();
         b.uri(uri);
         return b;
     }
    
    
Create a new instance initialized from an existing URI.

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
 
     public static UriBuilder fromUri(String urithrows IllegalArgumentException {
         URI u;
         try {
             u = URI.create(uri);
         } catch (NullPointerException ex) {
             throw new IllegalArgumentException(ex.getMessage(), ex);
         }
         return fromUri(u);
    }
    
    
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:
java.lang.IllegalArgumentException if path is null
    public static UriBuilder fromPath(String paththrows IllegalArgumentException {
        UriBuilder b = newInstance();
        b.path(path);
        return b;
    }

    
Create a new instance representing a relative URI initialized from a root resource class.

Parameters:
resource a root resource whose javax.ws.rs.Path value will be used to initialize the UriBuilder.
Returns:
a new UriBuilder
Throws:
java.lang.IllegalArgumentException if resource is not annotated with javax.ws.rs.Path or resource is null.
    public static UriBuilder fromResource(Class<?> resourcethrows IllegalArgumentException {
        UriBuilder b = newInstance();
        b.path(resource);
        return b;
    }
    
    
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(java.lang.Object[]) method.

Returns:
a copy of the UriBuilder
    @Override
    public abstract UriBuilder clone();
    
    
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
    public abstract UriBuilder uri(URI urithrows 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
    public abstract UriBuilder scheme(String schemethrows IllegalArgumentException;
    
    
Set the URI scheme-specific-part (see java.net.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
    public abstract UriBuilder schemeSpecificPart(String sspthrows 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
    public abstract UriBuilder userInfo(String ui);
    
    
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.
    public abstract UriBuilder host(String hostthrows 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
    public abstract UriBuilder port(int portthrows IllegalArgumentException;
    
    
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. A null value will unset the path component of the URI.
Returns:
the updated UriBuilder
    public abstract UriBuilder replacePath(String 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:
java.lang.IllegalArgumentException if path is null
    public abstract UriBuilder path(String paththrows IllegalArgumentException;

    
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 whose javax.ws.rs.Path value will be used to obtain the path to append.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if resource is null, or if resource is not annotated with javax.ws.rs.Path
    public abstract UriBuilder path(Class resourcethrows IllegalArgumentException;
    
    
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 to path(Method), it can only be used in cases where there is a single method with the specified name that is annotated with javax.ws.rs.Path.

Parameters:
resource the resource containing the method
method the name of the method whose javax.ws.rs.Path value will be used to obtain the path to append
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if resource or method is null, or there is more than or less than one variant of the method annotated with javax.ws.rs.Path
    public abstract UriBuilder path(Class resourceString methodthrows IllegalArgumentException;
    
    
Append the path from a javax.ws.rs.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.

Parameters:
method a method whose javax.ws.rs.Path value will be used to obtain the path to append to the existing path
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if method is null or is not annotated with a javax.ws.rs.Path
    public abstract UriBuilder path(Method methodthrows IllegalArgumentException;
    
    
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:
java.lang.IllegalArgumentException if segments or any element of segments is null
    public abstract UriBuilder segment(String... segmentsthrows 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
See also:
Matrix URIs
    public abstract UriBuilder replaceMatrix(String matrixthrows IllegalArgumentException;

    
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 a String using its toString() method. Stringified values may contain URI template parameters.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if name or values is null
See also:
Matrix URIs
    public abstract UriBuilder matrixParam(String nameObject... valuesthrows IllegalArgumentException;

    
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 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.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if name is null.
See also:
Matrix URIs
    public abstract UriBuilder replaceMatrixParam(String nameObject... valuesthrows 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
    public abstract UriBuilder replaceQuery(String querythrows IllegalArgumentException;

    
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 a String using its toString() method. Stringified values may contain URI template parameters.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if name or values is null
    public abstract UriBuilder queryParam(String nameObject... valuesthrows IllegalArgumentException;
    
    
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 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.
Returns:
the updated UriBuilder
Throws:
java.lang.IllegalArgumentException if name is null
    public abstract UriBuilder replaceQueryParam(String nameObject... valuesthrows IllegalArgumentException;
    
    
Set the URI fragment.

Parameters:
fragment the URI fragment, may contain URI template parameters. A null value will remove any existing fragment.
Returns:
the updated UriBuilder
    public abstract UriBuilder fragment(String fragment);
    
    
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 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.

Parameters:
values a map of URI template parameter names and values
Returns:
the URI built from the UriBuilder
Throws:
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.
    public abstract URI buildFromMap(Map<String, ? extends Objectvalues
            throws IllegalArgumentExceptionUriBuilderException;
    
    
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 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:
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.
    public abstract URI buildFromEncodedMap(Map<String, ? extends Objectvalues
            throws IllegalArgumentExceptionUriBuilderException;
    
    
Build a URI, using the supplied values in order to replace any URI template parameters. Values are converted to 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".

Parameters:
values a list of URI template parameter values
Returns:
the URI built from the UriBuilder
Throws:
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.
    public abstract URI build(Object... values
            throws IllegalArgumentExceptionUriBuilderException;
    
    
Build a URI. Any URI templates parameters will be replaced with the supplied values in order. Values are converted to 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".

Parameters:
values a list of URI template parameter values
Returns:
the URI built from the UriBuilder
Throws:
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.
    public abstract URI buildFromEncoded(Object... values
            throws IllegalArgumentExceptionUriBuilderException;
New to GrepCode? Check out our FAQ X