Class UriTemplate
- Direct Known Subclasses:
PathTemplate
- Author:
- Paul Sandoz, Martin Matula, Gerard Davison (gerard.davison at oracle.com)
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Comparator
<UriTemplate> Order the templates according to JAX-RS specification.static final UriTemplate
The empty URI template that matches thenull
or empty URI path. -
Constructor Summary
ModifierConstructorDescriptionUriTemplate
(String template) Construct a new URI template.protected
UriTemplate
(UriTemplateParser templateParser) Construct a new URI template. -
Method Summary
Modifier and TypeMethodDescriptionfinal String
Create a URI by substituting any template variables for corresponding template values.final String
Create a URI by substituting any template variables for corresponding template values.static String
createURI
(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Object[] values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.static String
createURI
(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Map<String, ?> values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.final String
Create a URI by substituting any template variables for corresponding template values.static String
createURIWithStringValues
(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, String[] values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.static String
createURIWithStringValues
(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Map<String, ?> values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.final boolean
Check if the URI template ends in a slash ('/'
).final boolean
Equality is calculated from the String of the regular expression generated from the templates.final int
Get the number of characters in the regular expression not resulting from conversion of template variables.final int
Get the number of explicit regular expressions declared in the template variables.final int
Get the number of regular expression groupsfinal int
Get the number of template variables.final PatternWithGroups
Get the URI pattern.final String
Get the URI template as a String.Get the list of template variables for the template.final int
hashCode()
Hash code is calculated from String of the regular expression generated from the template.final boolean
Ascertain if a template variable is a member of this template.final boolean
match
(CharSequence uri, List<String> groupValues) Match a URI against the template.final boolean
match
(CharSequence uri, Map<String, String> templateVariableToValue) Match a URI against the template.static URI
Normalize the URI by resolve the dot & dot-dot path segments as described in RFC 3986.static URI
Normalize the URI by resolve the dot & dot-dot path segments as described in RFC 3986.static URI
relativize
(URI baseUri, URI refUri) Relativize URI with respect to a base URI.static URI
Resolve a relative URI reference against a base URI as defined in RFC 3986.static URI
Resolve a relative URI reference against a base URI as defined in RFC 3986.static String
resolveTemplateValues
(UriComponent.Type type, String template, boolean encode, Map<String, ?> _mapValues) Resolves template variables in the giventemplate
from_mapValues
.final String
toString()
-
Field Details
-
COMPARATOR
Order the templates according to JAX-RS specification.Sort the set of matching resource classes using the number of characters in the regular expression not resulting from template variables as the primary key, the number of matching groups as a secondary key, and the number of explicit regular expression declarations as the tertiary key.
-
EMPTY
The empty URI template that matches thenull
or empty URI path.
-
-
Constructor Details
-
UriTemplate
Construct a new URI template.The template will be parsed to extract template variables.
A specific regular expression will be generated from the template to match URIs according to the template and map template variables to template values.
- Parameters:
template
- the template.- Throws:
PatternSyntaxException
- if the specified regular expression could not be generatedIllegalArgumentException
- if the template isnull
or an empty string.
-
UriTemplate
protected UriTemplate(UriTemplateParser templateParser) throws PatternSyntaxException, IllegalArgumentException Construct a new URI template.The template will be parsed to extract template variables.
A specific regular expression will be generated from the template to match URIs according to the template and map template variables to template values.
- Parameters:
templateParser
- the parser to parse the template.- Throws:
PatternSyntaxException
- if the specified regular expression could not be generatedIllegalArgumentException
- if the template isnull
or an empty string.
-
-
Method Details
-
resolve
Resolve a relative URI reference against a base URI as defined in RFC 3986.- Parameters:
baseUri
- base URI to be used for resolution.refUri
- reference URI string to be resolved against the base URI.- Returns:
- resolved URI.
- Throws:
IllegalArgumentException
- If the given string violates the URI specification RFC.
-
resolve
Resolve a relative URI reference against a base URI as defined in RFC 3986.- Parameters:
baseUri
- base URI to be used for resolution.refUri
- reference URI to be resolved against the base URI.- Returns:
- resolved URI.
-
normalize
Normalize the URI by resolve the dot & dot-dot path segments as described in RFC 3986.This method provides a workaround for issues with
URI.normalize()
which is not able to properly normalize absolute paths that start with a".."
segment, e.g."/../a/b"
as required by RFC 3986 (according to RFC 3986 the path"/../a/b"
should resolve to"/a/b"
, whileURI.normalize()
keeps the".."
segment in the URI path.- Parameters:
uri
- the original URI string.- Returns:
- the URI with dot and dot-dot segments resolved.
- Throws:
IllegalArgumentException
- If the given string violates the URI specification RFC.- See Also:
-
normalize
Normalize the URI by resolve the dot & dot-dot path segments as described in RFC 3986.This method provides a workaround for issues with
URI.normalize()
which is not able to properly normalize absolute paths that start with a".."
segment, e.g."/../a/b"
as required by RFC 3986 (according to RFC 3986 the path"/../a/b"
should resolve to"/a/b"
, whileURI.normalize()
keeps the".."
segment in the URI path.- Parameters:
uri
- the original URI.- Returns:
- the URI with dot and dot-dot segments resolved.
- See Also:
-
relativize
Relativize URI with respect to a base URI.After the relativization is done, dots in paths of both URIs are
resolved
.- Parameters:
baseUri
- base URI to be used for relativization.refUri
- URI to be relativized.- Returns:
- relativized URI.
-
getTemplate
Get the URI template as a String.- Returns:
- the URI template.
-
getPattern
Get the URI pattern.- Returns:
- the URI pattern.
-
endsWithSlash
public final boolean endsWithSlash()Check if the URI template ends in a slash ('/'
).- Returns:
true
if the template ends in a '/', otherwise false.
-
getTemplateVariables
Get the list of template variables for the template.- Returns:
- the list of template variables.
-
isTemplateVariablePresent
Ascertain if a template variable is a member of this template.- Parameters:
name
- name The template variable.- Returns:
true
if the template variable is a member of the template, otherwisefalse
.
-
getNumberOfExplicitRegexes
public final int getNumberOfExplicitRegexes()Get the number of explicit regular expressions declared in the template variables.- Returns:
- the number of explicit regular expressions in the template variables.
-
getNumberOfRegexGroups
public final int getNumberOfRegexGroups()Get the number of regular expression groups- Returns:
- the number of regular expressions groups
-
getNumberOfExplicitCharacters
public final int getNumberOfExplicitCharacters()Get the number of characters in the regular expression not resulting from conversion of template variables.- Returns:
- the number of explicit characters
-
getNumberOfTemplateVariables
public final int getNumberOfTemplateVariables()Get the number of template variables.- Returns:
- the number of template variables.
-
match
public final boolean match(CharSequence uri, Map<String, String> templateVariableToValue) throws IllegalArgumentExceptionMatch a URI against the template.If the URI matches against the pattern then the template variable to value map will be filled with template variables as keys and template values as values.
- Parameters:
uri
- the uri to match against the template.templateVariableToValue
- the map where to put template variables (as keys) and template values (as values). The map is cleared before any entries are put.- Returns:
- true if the URI matches the template, otherwise false.
- Throws:
IllegalArgumentException
- if the uri or templateVariableToValue is null.
-
match
public final boolean match(CharSequence uri, List<String> groupValues) throws IllegalArgumentException Match a URI against the template.If the URI matches against the pattern the capturing group values (if any) will be added to a list passed in as parameter.
- Parameters:
uri
- the uri to match against the template.groupValues
- the list to store the values of a pattern's capturing groups is matching is successful. The values are stored in the same order as the pattern's capturing groups.- Returns:
- true if the URI matches the template, otherwise false.
- Throws:
IllegalArgumentException
- if the uri or templateVariableToValue is null.
-
createURI
Create a URI by substituting any template variables for corresponding template values.A URI template variable without a value will be substituted by the empty string.
- Parameters:
values
- the map of template variables to template values.- Returns:
- the URI.
-
createURI
Create a URI by substituting any template variables for corresponding template values.A URI template variable without a value will be substituted by the empty string.
- Parameters:
values
- the array of template values. The values will be substituted in order of occurrence of unique template variables.- Returns:
- the URI.
-
createURI
Create a URI by substituting any template variables for corresponding template values.A URI template variable without a value will be substituted by the empty string.
- Parameters:
values
- the array of template values. The values will be substituted in order of occurrence of unique template variables.offset
- the offset into the template value array.length
- the length of the template value array.- Returns:
- the URI.
-
toString
-
hashCode
public final int hashCode()Hash code is calculated from String of the regular expression generated from the template. -
equals
Equality is calculated from the String of the regular expression generated from the templates. -
createURI
public static String createURI(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Map<String, ?> values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.A template values is an Object instance MUST support the toString() method to convert the template value to a String instance.
- Parameters:
scheme
- the URI scheme component.authority
- the URI authority component.userInfo
- the URI user info component.host
- the URI host component.port
- the URI port component.path
- the URI path component.query
- the URI query component.fragment
- the URI fragment component.values
- the template variable to value map.encode
- if true encode a template value according to the correspond component type of the associated template variable, otherwise contextually encode the template value.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:
- a URI.
-
createURIWithStringValues
public static String createURIWithStringValues(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Map<String, ?> values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.A template value is an Object instance that MUST support the toString() method to convert the template value to a String instance.
- Parameters:
scheme
- the URI scheme component.authority
- the URI authority info component.userInfo
- the URI user info component.host
- the URI host component.port
- the URI port component.path
- the URI path component.query
- the URI query component.fragment
- the URI fragment component.values
- the template variable to value map.encode
- if true encode a template value according to the correspond component type of the associated template variable, otherwise contextually encode the template value.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:
- a URI.
-
createURI
public static String createURI(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Object[] values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.The template values are an array of Object and each Object instance MUST support the toString() method to convert the template value to a String instance.
- Parameters:
scheme
- the URI scheme component.authority
- the URI authority component.userInfo
- the URI user info component.host
- the URI host component.port
- the URI port component.path
- the URI path component.query
- the URI query component.fragment
- ] the URI fragment component.values
- the array of template values.encode
- if true encode a template value according to the correspond component type of the associated template variable, otherwise contextually encode the template value.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:
- a URI.
-
createURIWithStringValues
public static String createURIWithStringValues(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, String[] values, boolean encode, boolean encodeSlashInPath) Construct a URI from the component parts each of which may contain template variables.- Parameters:
scheme
- the URI scheme component.authority
- the URI authority component.userInfo
- the URI user info component.host
- the URI host component.port
- the URI port component.path
- the URI path component.query
- the URI query component.fragment
- the URI fragment component.values
- the array of template values.encode
- if true encode a template value according to the correspond component type of the associated template variable, otherwise contextually encode the template value.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:
- a URI.
-
resolveTemplateValues
public static String resolveTemplateValues(UriComponent.Type type, String template, boolean encode, Map<String, ?> _mapValues) Resolves template variables in the giventemplate
from_mapValues
. Resolves only these variables which are defined in the_mapValues
leaving other variables unchanged.- Parameters:
type
- Type of thetemplate
(port, path, query, ...).template
- Input uri component to resolve.encode
- True if template values from_mapValues
should be percent encoded._mapValues
- Map with template variables as keys and template values as values. None of them should be null.- Returns:
- String with resolved template variables.
- Throws:
IllegalArgumentException
- when_mapValues
value is null.
-