public class Reference
extends java.lang.Object
URI reference = absolute-reference | relative-reference absolute-reference = scheme ":" scheme-specific-part [ "#" fragment ] scheme-specific-part = ( hierarchical-part [ "?" query ] ) | opaque-part hierarchical-part = ( "//" authority path-abempty ) | path-absolute | path-rootless | path-empty authority = [ user-info "@" ] host-domain [ ":" host-port ] relative-reference = relative-part [ "?" query ] [ "#" fragment ] relative-part = ( "//" authority path-abempty ) | path-absolute | path-noscheme | path-empty path-abempty = begins with "/" or is empty path-absolute = begins with "/" but not "//" path-noscheme = begins with a non-colon segment path-rootless = begins with a segment path-empty = zero characters
Note that this class doesn't encode or decode the reserved characters. It assumes that the URIs or the URI parts passed in are properly encoded using the standard URI encoding mechanism. You can use the static "encode()" and "decode()" methods for this purpose. Note that if an invalid URI character is detected by the constructor or one of the setters, a trace will be logged and the character will be automatically encoded.
The fundamental point to underline is the difference between an URI "reference" and an URI. Contrary to an URI (the target identifier of a REST resource), an URI reference can be relative (with or without query and fragment part). This relative URI reference can then be resolved against a base reference via the getTargetRef() method which will return a new resolved Reference instance, an absolute URI reference with no base reference and with no dot-segments (the path segments "." and "..").
You can also apply the getTargetRef() method on absolute references in order to solve the dot-segments. Note that applying the getRelativeRef() method on an absolute reference returns the current reference relatively to a base reference, if any, and solves the dot-segments.
The Reference stores its data as a single string, the one passed to the constructor. This string can always be obtained using the toString() method. A couple of integer indexes are maintained to improve the extraction time of various reference properties (URI components).
When you modify a specific component of the URI reference, via the setPath() method for example, the internal string is simply regenerated by updating only the relevant part. We try as much as possible to protect the bytes given to the Reference class instead of transparently parsing and normalizing the URI data. Our idea is to protect encodings and special characters in all case and reduce the memory size taken by this class while making Reference instances mutable.
Because the base reference is only a property of the Reference ("baseRef"). When you use the "Reference(base, path)"
constructor, it is equivalent to doing:
ref = new Reference(path);
ref.setBaseRef(base);
The base ref is not automatically resolved or "merged" with the rest of the reference information (the path here). For example, this let's you reuse a single reference as the base of several relative references. If you modify the base reference, all relative references are still accurate.
Note that the name and value properties are thread safe, stored in volatile members.Constructor and Description |
---|
Reference()
Empty constructor.
|
Reference(Protocol protocol,
java.lang.String hostName)
Constructor for a protocol and host name.
|
Reference(Protocol protocol,
java.lang.String hostName,
int hostPort)
Constructor for a protocol, host name and host port
|
Reference(Reference ref)
Clone constructor.
|
Reference(Reference baseRef,
Reference uriReference)
Constructor from an URI reference (most likely relative).
|
Reference(Reference baseRef,
java.lang.String uriRef)
Constructor from an URI reference (most likely relative).
|
Reference(Reference baseRef,
java.lang.String relativePart,
java.lang.String query,
java.lang.String fragment)
Constructor of relative reference from its parts.
|
Reference(java.lang.String uriReference)
Constructor from an URI reference.
|
Reference(java.lang.String identifier,
java.lang.String fragment)
Constructor from an identifier and a fragment.
|
Reference(java.lang.String scheme,
java.lang.String hostName,
int hostPort,
java.lang.String path,
java.lang.String query,
java.lang.String fragment)
Constructor of absolute reference from its parts.
|
Modifier and Type | Method and Description |
---|---|
Reference |
addQueryParameter(Parameter parameter)
Adds a parameter to the query component.
|
Reference |
addQueryParameter(java.lang.String name,
java.lang.String value)
Adds a parameter to the query component.
|
Reference |
addQueryParameters(java.lang.Iterable<Parameter> parameters)
Adds several parameters to the query component.
|
Reference |
addSegment(java.lang.String value)
Adds a segment at the end of the path.
|
static java.lang.String |
decode(java.lang.String toDecode)
Decodes a given string using the standard URI encoding mechanism and the
UTF-8 character set.
|
static java.lang.String |
decode(java.lang.String toDecode,
CharacterSet characterSet)
Decodes a given string using the standard URI encoding mechanism.
|
static java.lang.String |
encode(java.lang.String toEncode)
Encodes a given string using the standard URI encoding mechanism and the
UTF-8 character set.
|
static java.lang.String |
encode(java.lang.String toEncode,
boolean queryString)
Encodes a given string using the standard URI encoding mechanism and the
UTF-8 character set.
|
static java.lang.String |
encode(java.lang.String toEncode,
boolean queryString,
CharacterSet characterSet)
Encodes a given string using the standard URI encoding mechanism and the
UTF-8 character set.
|
static java.lang.String |
encode(java.lang.String toEncode,
CharacterSet characterSet)
Encodes a given string using the standard URI encoding mechanism.
|
boolean |
equals(java.lang.Object object)
Indicates whether some other object is "equal to" this one.
|
java.lang.String |
getAuthority()
Returns the authority component for hierarchical identifiers.
|
java.lang.String |
getAuthority(boolean decode)
Returns the optionnally decoded authority component.
|
Reference |
getBaseRef()
Returns the base reference for relative references.
|
java.lang.String |
getExtensions()
Returns the optional extensions for hierarchical identifiers.
|
java.lang.String[] |
getExtensionsAsArray()
Returns the extensions as an array or null if no extension is found.
|
java.lang.String |
getFragment()
Returns the fragment identifier.
|
java.lang.String |
getFragment(boolean decode)
Returns the optionnally decoded fragment identifier.
|
java.lang.String |
getHierarchicalPart()
Returns the hierarchical part which is equivalent to the scheme specific
part less the query component.
|
java.lang.String |
getHierarchicalPart(boolean decode)
Returns the optionnally decoded hierarchical part.
|
java.lang.String |
getHostDomain()
Returns the host domain name component for server based hierarchical
identifiers.
|
java.lang.String |
getHostDomain(boolean decode)
Returns the optionally decoded host domain name component.
|
java.lang.String |
getHostIdentifier()
Returns the host identifier.
|
java.lang.String |
getHostIdentifier(boolean decode)
Returns the optionnally decoded host identifier.
|
int |
getHostPort()
Returns the optional port number for server based hierarchical
identifiers.
|
java.lang.String |
getIdentifier()
Returns the absolute resource identifier, without the fragment.
|
java.lang.String |
getIdentifier(boolean decode)
Returns the optionnally decoded absolute resource identifier.
|
java.lang.String |
getLastSegment()
Returns the last segment of a hierarchical path.
|
java.lang.String |
getLastSegment(boolean decode)
Returns the optionnally decoded last segment.
|
java.lang.String |
getLastSegment(boolean decode,
boolean excludeMatrix)
Returns the optionnally decoded last segment.
|
java.lang.String |
getMatrix()
Returns the optional matrix for hierarchical identifiers.
|
java.lang.String |
getMatrix(boolean decode)
Returns the optionnally decoded matrix.
|
Form |
getMatrixAsForm()
Returns the optional matrix as a form.
|
Form |
getMatrixAsForm(CharacterSet characterSet)
Returns the optional matrix as a form submission.
|
Reference |
getParentRef()
Returns the parent reference of a hierarchical reference.
|
java.lang.String |
getPath()
Returns the path component for hierarchical identifiers.
|
java.lang.String |
getPath(boolean decode)
Returns the optionnally decoded path component.
|
java.lang.String |
getQuery()
Returns the optional query component for hierarchical identifiers.
|
java.lang.String |
getQuery(boolean decode)
Returns the optionnally decoded query component.
|
Form |
getQueryAsForm()
Returns the optional query component as a form.
|
Form |
getQueryAsForm(boolean decode)
Returns the optional query component as a form.
|
Form |
getQueryAsForm(CharacterSet characterSet)
Returns the optional query component as a form submission.
|
java.lang.String |
getRelativePart()
Returns the relative part of relative references, without the query and
fragment.
|
java.lang.String |
getRelativePart(boolean decode)
Returns the optionnally decoded relative part.
|
Reference |
getRelativeRef()
Returns the current reference as a relative reference to the current base
reference.
|
Reference |
getRelativeRef(Reference base)
Returns the current reference relatively to a base reference.
|
java.lang.String |
getRemainingPart()
Returns the part of the resource identifier remaining after the base
reference.
|
java.lang.String |
getRemainingPart(boolean decode)
Returns the optionally decoded remaining part.
|
java.lang.String |
getRemainingPart(boolean decode,
boolean query)
Returns the optionally decoded remaining part with or without the query
part of the reference.
|
java.lang.String |
getScheme()
Returns the scheme component.
|
java.lang.String |
getScheme(boolean decode)
Returns the optionnally decoded scheme component.
|
Protocol |
getSchemeProtocol()
Returns the protocol associated with the scheme component.
|
java.lang.String |
getSchemeSpecificPart()
Returns the scheme specific part.
|
java.lang.String |
getSchemeSpecificPart(boolean decode)
Returns the optionnally decoded scheme specific part.
|
java.util.List<java.lang.String> |
getSegments()
Returns the list of segments in a hierarchical path.
|
java.util.List<java.lang.String> |
getSegments(boolean decode)
Returns the optionnally decoded list of segments.
|
Reference |
getTargetRef()
Returns the target reference.
|
java.lang.String |
getUserInfo()
Returns the user info component for server based hierarchical
identifiers.
|
java.lang.String |
getUserInfo(boolean decode)
Returns the optionnally decoded user info component.
|
boolean |
hasExtensions()
Indicates if this reference has file-like extensions on its last path
segment.
|
boolean |
hasFragment()
Indicates if this reference has a fragment identifier.
|
int |
hashCode()
Returns a hash code value for the object.
|
boolean |
hasMatrix()
Indicates if this reference has a matrix.
|
boolean |
hasQuery()
Indicates if this reference has a query component.
|
boolean |
hasScheme()
Indicates if this reference has a scheme component.
|
boolean |
isAbsolute()
Indicates if the reference is absolute.
|
boolean |
isEquivalentTo(Reference ref)
Returns true if both reference are equivalent, meaning that they resolve
to the same target reference.
|
static boolean |
isGenericDelimiter(int character)
Indicates if the given character is a generic URI component delimiter
character.
|
boolean |
isHierarchical()
Indicates if the identifier is hierarchical.
|
boolean |
isOpaque()
Indicates if the identifier is opaque.
|
boolean |
isParent(Reference childRef)
Indicates if the reference is a parent of the hierarchical child
reference.
|
boolean |
isRelative()
Indicates if the reference is relative.
|
static boolean |
isReserved(int character)
Indicates if the given character is a reserved URI character.
|
static boolean |
isSubDelimiter(int character)
Indicates if the given character is an URI subcomponent delimiter
character.
|
static boolean |
isUnreserved(int character)
Indicates if the given character is an unreserved URI character.
|
static boolean |
isValid(int character)
Indicates if the given character is a valid URI character.
|
Reference |
normalize()
Normalizes the reference.
|
void |
setAuthority(java.lang.String authority)
Sets the authority component for hierarchical identifiers.
|
void |
setBaseRef(Reference baseRef)
Sets the base reference for relative references.
|
void |
setBaseRef(java.lang.String baseUri)
Sets the base reference for relative references.
|
void |
setExtensions(java.lang.String extensions)
Sets the extensions for hierarchical identifiers.
|
void |
setExtensions(java.lang.String[] extensions)
Sets the extensions based on an array of extension tokens (without dots).
|
void |
setFragment(java.lang.String fragment)
Sets the fragment identifier.
|
void |
setHostDomain(java.lang.String domain)
Sets the host domain component for server based hierarchical identifiers.
|
void |
setHostPort(java.lang.Integer port)
Sets the optional port number for server based hierarchical identifiers.
|
void |
setIdentifier(java.lang.String identifier)
Sets the absolute resource identifier.
|
void |
setLastSegment(java.lang.String lastSegment)
Sets the last segment of the path.
|
void |
setPath(java.lang.String path)
Sets the path component for hierarchical identifiers.
|
void |
setProtocol(Protocol protocol)
Sets the scheme component based on this protocol.
|
void |
setQuery(java.lang.String query)
Sets the query component for hierarchical identifiers.
|
void |
setRelativePart(java.lang.String relativePart)
Sets the relative part for relative references only.
|
void |
setScheme(java.lang.String scheme)
Sets the scheme component.
|
void |
setSchemeSpecificPart(java.lang.String schemeSpecificPart)
Sets the scheme specific part.
|
void |
setSegments(java.util.List<java.lang.String> segments)
Sets the segments of a hierarchical path.
|
void |
setUserInfo(java.lang.String userInfo)
Sets the user info component for server based hierarchical identifiers.
|
java.lang.String |
toString()
Returns the reference as an URI string.
|
java.lang.String |
toString(boolean query,
boolean fragment)
Returns the URI reference string.
|
static java.lang.String |
toString(java.lang.String scheme,
java.lang.String hostName,
java.lang.Integer hostPort,
java.lang.String path,
java.lang.String query,
java.lang.String fragment)
Creates a reference string from its parts.
|
static java.lang.String |
toString(java.lang.String relativePart,
java.lang.String query,
java.lang.String fragment)
Creates a relative reference string from its parts.
|
static java.lang.String |
toString(java.lang.String scheme,
java.lang.String host,
java.lang.String path,
java.lang.String query,
java.lang.String fragment)
Creates a reference string from its parts.
|
public Reference()
public Reference(Protocol protocol, java.lang.String hostName)
protocol
- Protocol/scheme to usehostName
- The host name or IP address.public Reference(Protocol protocol, java.lang.String hostName, int hostPort)
protocol
- Protocol/scheme to usehostName
- The host name or IP address.hostPort
- The host port (default ports are correctly ignored).public Reference(Reference ref)
ref
- The reference to clone.public Reference(Reference baseRef, Reference uriReference)
baseRef
- The base reference.uriReference
- The URI reference, either absolute or relative.public Reference(Reference baseRef, java.lang.String uriRef)
baseRef
- The base reference.uriRef
- The URI reference, either absolute or relative.public Reference(Reference baseRef, java.lang.String relativePart, java.lang.String query, java.lang.String fragment)
baseRef
- The base reference.relativePart
- The relative part component (most of the time it is the path
component).query
- The optional query component for hierarchical identifiers.fragment
- The optional fragment identifier.public Reference(java.lang.String uriReference)
uriReference
- The URI reference, either absolute or relative.public Reference(java.lang.String identifier, java.lang.String fragment)
identifier
- The resource identifier.fragment
- The fragment identifier.public Reference(java.lang.String scheme, java.lang.String hostName, int hostPort, java.lang.String path, java.lang.String query, java.lang.String fragment)
scheme
- The scheme ("http", "https" or "ftp").hostName
- The host name or IP address.hostPort
- The host port (default ports are correctly ignored).path
- The path component for hierarchical identifiers.query
- The optional query component for hierarchical identifiers.fragment
- The optional fragment identifier.public static java.lang.String decode(java.lang.String toDecode)
toDecode
- The string to decode.public static java.lang.String decode(java.lang.String toDecode, CharacterSet characterSet)
toDecode
- The string to decode.characterSet
- The name of a supported character encoding.public static java.lang.String encode(java.lang.String toEncode)
toEncode
- The string to encode.public static java.lang.String encode(java.lang.String toEncode, boolean queryString)
toEncode
- The string to encode.queryString
- True if the string to encode is part of a query string instead
of a HTML form post.public static java.lang.String encode(java.lang.String toEncode, boolean queryString, CharacterSet characterSet)
toEncode
- The string to encode.queryString
- True if the string to encode is part of a query string instead
of a HTML form post.characterSet
- The supported character encoding.public static java.lang.String encode(java.lang.String toEncode, CharacterSet characterSet)
toEncode
- The string to encode.characterSet
- The supported character encoding.public static boolean isGenericDelimiter(int character)
character
- The character to test.public static boolean isReserved(int character)
character
- The character to test.public static boolean isSubDelimiter(int character)
character
- The character to test.public static boolean isUnreserved(int character)
character
- The character to test.public static boolean isValid(int character)
character
- The character to test.public static java.lang.String toString(java.lang.String scheme, java.lang.String hostName, java.lang.Integer hostPort, java.lang.String path, java.lang.String query, java.lang.String fragment)
scheme
- The scheme ("http", "https" or "ftp").hostName
- The host name or IP address.hostPort
- The host port (default ports are correctly ignored).path
- The path component for hierarchical identifiers.query
- The optional query component for hierarchical identifiers.fragment
- The optional fragment identifier.public static java.lang.String toString(java.lang.String relativePart, java.lang.String query, java.lang.String fragment)
relativePart
- The relative part component.query
- The optional query component for hierarchical identifiers.fragment
- The optional fragment identifier.public static java.lang.String toString(java.lang.String scheme, java.lang.String host, java.lang.String path, java.lang.String query, java.lang.String fragment)
scheme
- The scheme ("http", "https" or "ftp").host
- The host name or IP address plus the optional port number.path
- The path component for hierarchical identifiers.query
- The optional query component for hierarchical identifiers.fragment
- The optional fragment identifier.public Reference addQueryParameter(Parameter parameter)
parameter
- The parameter to add.public Reference addQueryParameter(java.lang.String name, java.lang.String value)
name
- The parameter name.value
- The optional parameter value.public Reference addQueryParameters(java.lang.Iterable<Parameter> parameters)
parameters
- The parameters to add.public Reference addSegment(java.lang.String value)
value
- The segment value to add.public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- The object to compare to.public java.lang.String getAuthority()
public java.lang.String getAuthority(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getAuthority()
public Reference getBaseRef()
public java.lang.String getExtensions()
getExtensionsAsArray()
,
setExtensions(String)
public java.lang.String[] getExtensionsAsArray()
getExtensions()
public java.lang.String getFragment()
public java.lang.String getFragment(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getFragment()
public java.lang.String getHierarchicalPart()
public java.lang.String getHierarchicalPart(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getHierarchicalPart()
public java.lang.String getHostDomain()
public java.lang.String getHostDomain(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getHostDomain()
public java.lang.String getHostIdentifier()
public java.lang.String getHostIdentifier(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getHostIdentifier()
public int getHostPort()
public java.lang.String getIdentifier()
public java.lang.String getIdentifier(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getIdentifier()
public java.lang.String getLastSegment()
public java.lang.String getLastSegment(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getLastSegment()
public java.lang.String getLastSegment(boolean decode, boolean excludeMatrix)
decode
- Indicates if the result should be decoded using the decode(String)
method.excludeMatrix
- getLastSegment()
public java.lang.String getMatrix()
public java.lang.String getMatrix(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getMatrix()
public Form getMatrixAsForm()
public Form getMatrixAsForm(CharacterSet characterSet)
characterSet
- The supported character encoding.public Reference getParentRef()
public java.lang.String getPath()
public java.lang.String getPath(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getPath()
public java.lang.String getQuery()
public java.lang.String getQuery(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getQuery()
public Form getQueryAsForm()
public Form getQueryAsForm(boolean decode)
decode
- Indicates if the names and values should be automatically
decoded.public Form getQueryAsForm(CharacterSet characterSet)
characterSet
- The supported character encoding.public java.lang.String getRelativePart()
public java.lang.String getRelativePart(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getRelativePart()
public Reference getRelativeRef()
getRelativeRef(Reference)
public Reference getRelativeRef(Reference base)
base
- The base reference to use.java.lang.IllegalArgumentException
- If the relative reference is computed although the reference
or the base reference are not absolute or not hierarchical.public java.lang.String getRemainingPart()
getRemainingPart(boolean)
public java.lang.String getRemainingPart(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getRemainingPart()
public java.lang.String getRemainingPart(boolean decode, boolean query)
decode
- Indicates if the result should be decoded using the decode(String)
method.query
- True if the query part should be returned, false otherwise.getRemainingPart()
public java.lang.String getScheme()
public java.lang.String getScheme(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getScheme()
public Protocol getSchemeProtocol()
public java.lang.String getSchemeSpecificPart()
public java.lang.String getSchemeSpecificPart(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getSchemeSpecificPart()
public java.util.List<java.lang.String> getSegments()
public java.util.List<java.lang.String> getSegments(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getSegments()
public Reference getTargetRef()
java.lang.IllegalArgumentException
- If the base reference (after resolution) is not absolute.java.lang.IllegalArgumentException
- If the reference is relative and not base reference has been
provided.public java.lang.String getUserInfo()
public java.lang.String getUserInfo(boolean decode)
decode
- Indicates if the result should be decoded using the decode(String)
method.getUserInfo()
public boolean hasExtensions()
getExtensions()
public boolean hasFragment()
public int hashCode()
hashCode
in class java.lang.Object
public boolean hasMatrix()
getMatrix()
public boolean hasQuery()
public boolean hasScheme()
public boolean isAbsolute()
public boolean isEquivalentTo(Reference ref)
ref
- The reference to compare.public boolean isHierarchical()
public boolean isOpaque()
public boolean isParent(Reference childRef)
childRef
- The hierarchical reference.public boolean isRelative()
public Reference normalize()
public void setAuthority(java.lang.String authority)
authority
- The authority component for hierarchical identifiers.public void setBaseRef(Reference baseRef)
baseRef
- The base reference for relative references.public void setBaseRef(java.lang.String baseUri)
baseUri
- The base URI for relative references.public void setExtensions(java.lang.String extensions)
extensions
- The extensions to set or null (without leading or trailing
dots).getExtensions()
,
getExtensionsAsArray()
,
setExtensions(String[])
public void setExtensions(java.lang.String[] extensions)
extensions
- The array of extensions.getExtensions()
,
getExtensionsAsArray()
,
setExtensions(String)
public void setFragment(java.lang.String fragment)
fragment
- The fragment identifier.java.lang.IllegalArgumentException
- if the fragment parameter contains the fragment delimiter
('#').public void setHostDomain(java.lang.String domain)
domain
- The host component for server based hierarchical identifiers.public void setHostPort(java.lang.Integer port)
port
- The optional port number for server based hierarchical
identifiers.java.lang.IllegalArgumentException
- If the autority has not been defined.public void setIdentifier(java.lang.String identifier)
identifier
- The absolute resource identifier.java.lang.IllegalArgumentException
- If the identifier parameter contains the fragment delimiter
('#').public void setLastSegment(java.lang.String lastSegment)
lastSegment
- The last segment of a hierarchical path.public void setPath(java.lang.String path)
path
- The path component for hierarchical identifiers.public void setProtocol(Protocol protocol)
protocol
- The protocol of the scheme component.public void setQuery(java.lang.String query)
query
- The query component for hierarchical identifiers.public void setRelativePart(java.lang.String relativePart)
relativePart
- The relative part to set.public void setScheme(java.lang.String scheme)
scheme
- The scheme component.public void setSchemeSpecificPart(java.lang.String schemeSpecificPart)
schemeSpecificPart
- The scheme specific part.public void setSegments(java.util.List<java.lang.String> segments)
segments
- The segments of the hierarchical path.public void setUserInfo(java.lang.String userInfo)
userInfo
- The user info component for server based hierarchical
identifiers.java.lang.IllegalArgumentException
- If the autority part has not been defined.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(boolean query, boolean fragment)
query
- Indicates if the query should be included;fragment
- Indicates if the fragment should be included;Copyright © 2005-2024 Restlet.