Top Description Inners Fields Constructors Methods
org.glassfish.tyrus.core.uri.internal

public Class UriComponent

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.net.URI, .URLDecoder, java.nio.Buffer, .ByteBuffer, .CharBuffer, java.nio.charset.Charset, java.util.ArrayList, .Arrays, .LinkedList, .List, org.glassfish.tyrus.core.l10n.LocalizationMessages

Utility class for validating, encoding and decoding components of a URI.
Authors
Paul Sandoz, Marek Potociar

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static enum
UriComponent.Type

The URI component type.

Field Summary

Modifier and TypeField and Description
private static final boolean[][]
private static final char[]
private static final int[]
private static final String[]
private static final String[]
private static final String[]
private static final Charset

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
_encode(final String s, final UriComponent.Type t, final boolean template, final boolean contextualEncode)

private static int
_valid(final String s, final UriComponent.Type t, final boolean template)

private static void
private static void
appendUTF8EncodedCharacter(final StringBuilder sb, final char c)

public static String

Returns:

the encoded string.
contextualEncode
(final String
the string to be encoded.
s
,
final UriComponent.Type
the URI component type identifying the ASCII characters that must be percent-encoded.
t
)

Contextually encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

public static String

Returns:

the encoded string.
contextualEncode
(final String
the string to be encoded.
s
,
final UriComponent.Type
the URI component type identifying the ASCII characters that must be percent-encoded.
t
,
final boolean
true if the encoded string contains URI template variables
template
)

Contextually encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

public static String

Returns:

the decoded string.
decode
(final String
the string to be decoded.
s
,
final UriComponent.Type
the URI component type, may be null.
t
)

Decodes characters of a string that are percent-encoded octets using UTF-8 decoding (if needed).

private static String
decode(final String s, final int n)

private static int
decodeHex(final String s, final int i)

private static int
decodeHex(final char c)

private static String
decodeHost(final String s, final int n)

public static MultivaluedMap<String, String>

Returns:

the multivalued map of matrix parameters.
decodeMatrix
(final String
the path segment component in encoded form.
pathSegment
,
final boolean
true if the matrix parameters of the path segment component should be in decoded form.
decode
)

Decode the matrix component of a URI path segment.

private static void
decodeMatrixParam(final MultivaluedMap<String, String> params, final String param, final boolean decode)

private static int

Returns:

the index to the next unchecked character in the string to decode
decodeOctets
(final int i, final ByteBuffer bb, final StringBuilder sb)

Decodes octets to characters using the UTF-8 decoding and appends the characters to a StringBuffer.

public static List<PathSegment>

Returns:

the list of path segments.
decodePath
(final URI
the URI. If the path component is an absolute path component then the leading '/' is ignored and is not considered a delimiator of a path segment.
u
,
final boolean
true if the path segments of the path component should be in decoded form.
decode
)

Decode the path component of a URI as path segments.

public static List<PathSegment>

Returns:

the list of path segments.
decodePath
(final String
the path component in encoded form.
path
,
final boolean
true if the path segments of the path component should be in decoded form.
decode
)

Decode the path component of a URI as path segments.

public static void
decodePathSegment(final List<PathSegment>
mutable list of path segments.
segments
,
final String
path segment to be decoded.
segment
,
final boolean
true if the path segment should be in a decoded form.
decode
)

Decode the path segment and add it to the list of path segments.

private static ByteBuffer
decodePercentEncodedOctets(final String s, int i, ByteBuffer bb)

Decode a continuous sequence of percent encoded octets.

public static MultivaluedMap<String, String>

Returns:

the multivalued map of query parameters.
decodeQuery
(final URI
the URI.
u
,
final boolean
true if the returned query parameter values of the query component should be in decoded form.
decode
)

Decode the query component of a URI.

public static MultivaluedMap<String, String>

Returns:

the multivalued map of query parameters.
decodeQuery
(final String
the query component in encoded form.
q
,
final boolean
true if the returned query parameter values of the query component should be in decoded form.
decode
)

Decode the query component of a URI.

public static MultivaluedMap<String, String>

Returns:

the multivalued map of query parameters.
decodeQuery
(final String
the query component in encoded form.
q
,
final boolean
true if the returned query parameter names of the query component should be in decoded form.
decodeNames
,
final boolean
true if the returned query parameter values of the query component should be in decoded form.
decodeValues
)

Decode the query component of a URI.

private static void
decodeQueryParam(final MultivaluedMap<String, String> params, final String param, final boolean decodeNames, final boolean decodeValues)

private static String
decodeQueryParam(final String s, final int n)

public static String

Returns:

the encoded string.
encode
(final String
the string to be encoded.
s
,
final UriComponent.Type
the URI component type identifying the ASCII characters that must be percent-encoded.
t
)

Encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

public static String

Returns:

the encoded string.
encode
(final String
the string to be encoded.
s
,
final UriComponent.Type
the URI component type identifying the ASCII characters that must be percent-encoded.
t
,
final boolean
true if the encoded string contains URI template variables
template
)

Encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

public static String

Returns:

the string with encoded template parameters names.
encodeTemplateNames
(String
the string with zero or more template parameters names
s
)

Encodes a string with template parameters names present, specifically the characters '{' and '}' will be percent-encoded.

public static String

Returns:

Request-Uri representation or null if uri is not provided.
fullRelativeUri
(final URI
uri to obtain Request-Uri from.
uri
)

Return the Request-Uri representation as defined by HTTP spec.

private static boolean[]
initEncodingTable(final List<String> allowed)

private static boolean[][]
private static int[]
public static boolean

Returns:

The is c is a hexadecimal character (e.g. 0, 5, a, A, f, ...)
isHexCharacter
(final char
Any character
c
)

Checks whether the character c is hexadecimal character.

public static boolean

Returns:

true if the encoded string is valid, otherwise false.
valid
(final String
the encoded string.
s
,
final UriComponent.Type
the URI component type identifying the legal characters.
t
)

Validates the legal characters of a percent-encoded string that represents a URI component type.

public static boolean

Returns:

true if the encoded string is valid, otherwise false.
valid
(final String
the encoded string.
s
,
final UriComponent.Type
the URI component type identifying the legal characters.
t
,
final boolean
true if the encoded string contains URI template variables
template
)

Validates the legal characters of a percent-encoded string that represents a URI component type.

public static void
validate(final String
the encoded string.
s
,
final UriComponent.Type
the URI component type identifying the legal characters.
t
)

Validates the legal characters of a percent-encoded string that represents a URI component type.

public static void
validate(final String
the encoded string.
s
,
final UriComponent.Type
the URI component type identifying the legal characters.
t
,
final boolean
true if the encoded string contains URI template variables
template
)

Validates the legal characters of a percent-encoded string that represents a URI component type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ENCODING_TABLESback to summary
private static final boolean[][] ENCODING_TABLES
HEX_DIGITSback to summary
private static final char[] HEX_DIGITS
HEX_TABLEback to summary
private static final int[] HEX_TABLE
SCHEMEback to summary
private static final String[] SCHEME
SUB_DELIMSback to summary
private static final String[] SUB_DELIMS
UNRESERVEDback to summary
private static final String[] UNRESERVED
UTF_8_CHARSETback to summary
private static final Charset UTF_8_CHARSET

Constructor Detail

UriComponentback to summary
private UriComponent()

Method Detail

_encodeback to summary
private static String _encode(final String s, final UriComponent.Type t, final boolean template, final boolean contextualEncode)
_validback to summary
private static int _valid(final String s, final UriComponent.Type t, final boolean template)
appendPercentEncodedOctetback to summary
private static void appendPercentEncodedOctet(final StringBuilder sb, final int b)
appendUTF8EncodedCharacterback to summary
private static void appendUTF8EncodedCharacter(final StringBuilder sb, final char c)
contextualEncodeback to summary
public static String contextualEncode(final String s, final UriComponent.Type t)

Contextually encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding. Percent-encoded characters will be recognized and not double encoded.

Parameters
s:String

the string to be encoded.

t:UriComponent.Type

the URI component type identifying the ASCII characters that must be percent-encoded.

Returns:String

the encoded string.

contextualEncodeback to summary
public static String contextualEncode(final String s, final UriComponent.Type t, final boolean template)

Contextually encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding. Percent-encoded characters will be recognized and not double encoded.

Parameters
s:String

the string to be encoded.

t:UriComponent.Type

the URI component type identifying the ASCII characters that must be percent-encoded.

template:boolean

true if the encoded string contains URI template variables

Returns:String

the encoded string.

decodeback to summary
public static String decode(final String s, final UriComponent.Type t)

Decodes characters of a string that are percent-encoded octets using UTF-8 decoding (if needed).

It is assumed that the string is valid according to an (unspecified) URI component type. If a sequence of contiguous percent-encoded octets is not a valid UTF-8 character then the octets are replaced with '�'.

If the URI component is of type HOST then any "%" found between "[]" is left alone. It is an IPv6 literal with a scope_id.

If the URI component is of type QUERY_PARAM then any "+" is decoded as as ' '.

Parameters
s:String

the string to be decoded.

t:UriComponent.Type

the URI component type, may be null.

Returns:String

the decoded string.

Exceptions
IllegalArgumentException:
if a malformed percent-encoded octet is detected
decodeback to summary
private static String decode(final String s, final int n)
decodeHexback to summary
private static int decodeHex(final String s, final int i)
decodeHexback to summary
private static int decodeHex(final char c)
decodeHostback to summary
private static String decodeHost(final String s, final int n)
decodeMatrixback to summary
public static MultivaluedMap<String, String> decodeMatrix(final String pathSegment, final boolean decode)

Decode the matrix component of a URI path segment.

Parameters
pathSegment:String

the path segment component in encoded form.

decode:boolean

true if the matrix parameters of the path segment component should be in decoded form.

Returns:MultivaluedMap<String, String>

the multivalued map of matrix parameters.

decodeMatrixParamback to summary
private static void decodeMatrixParam(final MultivaluedMap<String, String> params, final String param, final boolean decode)
Annotations
@SuppressWarnings:StatementWithEmptyBody
decodeOctetsback to summary
private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb)

Decodes octets to characters using the UTF-8 decoding and appends the characters to a StringBuffer.

Returns:int

the index to the next unchecked character in the string to decode

decodePathback to summary
public static List<PathSegment> decodePath(final URI u, final boolean decode)

Decode the path component of a URI as path segments.

Parameters
u:URI

the URI. If the path component is an absolute path component then the leading '/' is ignored and is not considered a delimiator of a path segment.

decode:boolean

true if the path segments of the path component should be in decoded form.

Returns:List<PathSegment>

the list of path segments.

decodePathback to summary
public static List<PathSegment> decodePath(final String path, final boolean decode)

Decode the path component of a URI as path segments.

Any '/' character in the path is considered to be a deliminator between two path segments. Thus if the path is '/' then the path segment list will contain two empty path segments. If the path is "//" then the path segment list will contain three empty path segments. If the path is "/a/" the path segment list will consist of the following path segments in order: "", "a" and "".

Parameters
path:String

the path component in encoded form.

decode:boolean

true if the path segments of the path component should be in decoded form.

Returns:List<PathSegment>

the list of path segments.

decodePathSegmentback to summary
public static void decodePathSegment(final List<PathSegment> segments, final String segment, final boolean decode)

Decode the path segment and add it to the list of path segments.

Parameters
segments:List<PathSegment>

mutable list of path segments.

segment:String

path segment to be decoded.

decode:boolean

true if the path segment should be in a decoded form.

decodePercentEncodedOctetsback to summary
private static ByteBuffer decodePercentEncodedOctets(final String s, int i, ByteBuffer bb)

Decode a continuous sequence of percent encoded octets.

Assumes the index, i, starts that the first hex digit of the first percent-encoded octet.

decodeQueryback to summary
public static MultivaluedMap<String, String> decodeQuery(final URI u, final boolean decode)

Decode the query component of a URI.

Query parameter names in the returned map are always decoded. Decoding of query parameter values can be controlled using the decode parameter flag.

Parameters
u:URI

the URI.

decode:boolean

true if the returned query parameter values of the query component should be in decoded form.

Returns:MultivaluedMap<String, String>

the multivalued map of query parameters.

decodeQueryback to summary
public static MultivaluedMap<String, String> decodeQuery(final String q, final boolean decode)

Decode the query component of a URI.

Query parameter names in the returned map are always decoded. Decoding of query parameter values can be controlled using the decode parameter flag.

Parameters
q:String

the query component in encoded form.

decode:boolean

true if the returned query parameter values of the query component should be in decoded form.

Returns:MultivaluedMap<String, String>

the multivalued map of query parameters.

decodeQueryback to summary
public static MultivaluedMap<String, String> decodeQuery(final String q, final boolean decodeNames, final boolean decodeValues)

Decode the query component of a URI.

Decoding of query parameter names and values can be controlled using the decodeNames and decodeValues parameter flags.

Parameters
q:String

the query component in encoded form.

decodeNames:boolean

true if the returned query parameter names of the query component should be in decoded form.

decodeValues:boolean

true if the returned query parameter values of the query component should be in decoded form.

Returns:MultivaluedMap<String, String>

the multivalued map of query parameters.

decodeQueryParamback to summary
private static void decodeQueryParam(final MultivaluedMap<String, String> params, final String param, final boolean decodeNames, final boolean decodeValues)
Annotations
@SuppressWarnings:StatementWithEmptyBody
decodeQueryParamback to summary
private static String decodeQueryParam(final String s, final int n)
encodeback to summary
public static String encode(final String s, final UriComponent.Type t)

Encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

Parameters
s:String

the string to be encoded.

t:UriComponent.Type

the URI component type identifying the ASCII characters that must be percent-encoded.

Returns:String

the encoded string.

encodeback to summary
public static String encode(final String s, final UriComponent.Type t, final boolean template)

Encodes the characters of string that are either non-ASCII characters or are ASCII characters that must be percent-encoded using the UTF-8 encoding.

Parameters
s:String

the string to be encoded.

t:UriComponent.Type

the URI component type identifying the ASCII characters that must be percent-encoded.

template:boolean

true if the encoded string contains URI template variables

Returns:String

the encoded string.

encodeTemplateNamesback to summary
public static String encodeTemplateNames(String s)

Encodes a string with template parameters names present, specifically the characters '{' and '}' will be percent-encoded.

Parameters
s:String

the string with zero or more template parameters names

Returns:String

the string with encoded template parameters names.

fullRelativeUriback to summary
public static String fullRelativeUri(final URI uri)

Return the Request-Uri representation as defined by HTTP spec. For example:

<Method> <Request-URI> HTTP/<Version> (e.g. GET /auth;foo=bar/hello?foo=bar HTTP/1.1)
Parameters
uri:URI

uri to obtain Request-Uri from.

Returns:String

Request-Uri representation or null if uri is not provided.

initEncodingTableback to summary
private static boolean[] initEncodingTable(final List<String> allowed)
initEncodingTablesback to summary
private static boolean[][] initEncodingTables()
initHexTableback to summary
private static int[] initHexTable()
isHexCharacterback to summary
public static boolean isHexCharacter(final char c)

Checks whether the character c is hexadecimal character.

Parameters
c:char

Any character

Returns:boolean

The is c is a hexadecimal character (e.g. 0, 5, a, A, f, ...)

validback to summary
public static boolean valid(final String s, final UriComponent.Type t)

Validates the legal characters of a percent-encoded string that represents a URI component type.

Parameters
s:String

the encoded string.

t:UriComponent.Type

the URI component type identifying the legal characters.

Returns:boolean

true if the encoded string is valid, otherwise false.

validback to summary
public static boolean valid(final String s, final UriComponent.Type t, final boolean template)

Validates the legal characters of a percent-encoded string that represents a URI component type.

Parameters
s:String

the encoded string.

t:UriComponent.Type

the URI component type identifying the legal characters.

template:boolean

true if the encoded string contains URI template variables

Returns:boolean

true if the encoded string is valid, otherwise false.

validateback to summary
public static void validate(final String s, final UriComponent.Type t)

Validates the legal characters of a percent-encoded string that represents a URI component type.

Parameters
s:String

the encoded string.

t:UriComponent.Type

the URI component type identifying the legal characters.

Exceptions
IllegalArgumentException:
if the encoded string contains illegal characters.
validateback to summary
public static void validate(final String s, final UriComponent.Type t, final boolean template)

Validates the legal characters of a percent-encoded string that represents a URI component type.

Parameters
s:String

the encoded string.

t:UriComponent.Type

the URI component type identifying the legal characters.

template:boolean

true if the encoded string contains URI template variables

Exceptions
IllegalArgumentException:
if the encoded string contains illegal characters.
org.glassfish.tyrus.core.uri.internal back to summary

private final Class UriComponent.PathSegmentImpl

extends Object
implements PathSegment
Class Inheritance
  • java.lang.Object
  • org.glassfish.tyrus.core.uri.internal.UriComponent.PathSegmentImpl
All Implemented Interfaces
org.glassfish.tyrus.core.uri.internal.PathSegment

Field Summary

Modifier and TypeField and Description
private static final PathSegment
private final MultivaluedMap<String, String>
private final String

Constructor Summary

AccessConstructor and Description
pack-priv
PathSegmentImpl(final String path, final boolean decode)

pack-priv
PathSegmentImpl(final String path, final boolean decode, final MultivaluedMap<String, String> matrixParameters)

Method Summary

Modifier and TypeMethod and Description
public MultivaluedMap<String, String>
getMatrixParameters()

Implements org.glassfish.tyrus.core.uri.internal.PathSegment.getMatrixParameters.

Get a map of the matrix parameters associated with the path segment.

public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

EMPTY_PATH_SEGMENTback to summary
private static final PathSegment EMPTY_PATH_SEGMENT
matrixParametersback to summary
private final MultivaluedMap<String, String> matrixParameters
pathback to summary
private final String path

Constructor Detail

PathSegmentImplback to summary
pack-priv PathSegmentImpl(final String path, final boolean decode)
PathSegmentImplback to summary
pack-priv PathSegmentImpl(final String path, final boolean decode, final MultivaluedMap<String, String> matrixParameters)

Method Detail

getMatrixParametersback to summary
public MultivaluedMap<String, String> getMatrixParameters()

Implements org.glassfish.tyrus.core.uri.internal.PathSegment.getMatrixParameters.

Doc from org.glassfish.tyrus.core.uri.internal.PathSegment.getMatrixParameters.

Get a map of the matrix parameters associated with the path segment. The map keys are the names of the matrix parameters with any percent-escaped octets decoded.

Returns:MultivaluedMap<String, String>

the map of matrix parameters

Annotations
@Override
getPathback to summary
public String getPath()

Implements org.glassfish.tyrus.core.uri.internal.PathSegment.getPath.

Doc from org.glassfish.tyrus.core.uri.internal.PathSegment.getPath.

Get the path segment.

Returns:String

the path segment

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.glassfish.tyrus.core.uri.internal back to summary

public final Enum UriComponent.Type

extends Enum<UriComponent.Type>
Class Inheritance

The URI component type.

Field Summary

Modifier and TypeField and Description
public static final UriComponent.Type
AUTHORITY

The URI authority component type.

public static final UriComponent.Type
FRAGMENT

The URI fragment component type.

public static final UriComponent.Type
HOST

The URI host component type.

public static final UriComponent.Type
MATRIX_PARAM

The URI path component type that is a matrix parameter.

public static final UriComponent.Type
PATH

The URI path component type.

public static final UriComponent.Type
PATH_SEGMENT

The URI path component type that is a path segment.

public static final UriComponent.Type
PORT

The URI port component type.

public static final UriComponent.Type
QUERY

The URI query component type.

public static final UriComponent.Type
QUERY_PARAM

The URI query component type that is a query parameter, space character is encoded as +.

public static final UriComponent.Type
QUERY_PARAM_SPACE_ENCODED

The URI query component type that is a query parameter, space character is encoded as %20.

public static final UriComponent.Type
SCHEME

The URI scheme component type.

public static final UriComponent.Type
UNRESERVED

ALPHA / DIGIT / "-" / "." / "_" / "~" characters.

public static final UriComponent.Type
USER_INFO

The URI user info component type.

Constructor Summary

AccessConstructor and Description
private
Type()

Method Summary

Modifier and TypeMethod and Description
public static UriComponent.Type
public static UriComponent.Type[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

AUTHORITYback to summary
public static final UriComponent.Type AUTHORITY

The URI authority component type.

FRAGMENTback to summary
public static final UriComponent.Type FRAGMENT

The URI fragment component type.

HOSTback to summary
public static final UriComponent.Type HOST

The URI host component type.

MATRIX_PARAMback to summary
public static final UriComponent.Type MATRIX_PARAM

The URI path component type that is a matrix parameter.

PATHback to summary
public static final UriComponent.Type PATH

The URI path component type.

PATH_SEGMENTback to summary
public static final UriComponent.Type PATH_SEGMENT

The URI path component type that is a path segment.

PORTback to summary
public static final UriComponent.Type PORT

The URI port component type.

QUERYback to summary
public static final UriComponent.Type QUERY

The URI query component type.

QUERY_PARAMback to summary
public static final UriComponent.Type QUERY_PARAM

The URI query component type that is a query parameter, space character is encoded as +.

QUERY_PARAM_SPACE_ENCODEDback to summary
public static final UriComponent.Type QUERY_PARAM_SPACE_ENCODED

The URI query component type that is a query parameter, space character is encoded as %20.

SCHEMEback to summary
public static final UriComponent.Type SCHEME

The URI scheme component type.

UNRESERVEDback to summary
public static final UriComponent.Type UNRESERVED

ALPHA / DIGIT / "-" / "." / "_" / "~" characters.

USER_INFOback to summary
public static final UriComponent.Type USER_INFO

The URI user info component type.

Constructor Detail

Typeback to summary
private Type()

Method Detail

valueOfback to summary
public static UriComponent.Type valueOf(String name)
valuesback to summary
public static UriComponent.Type[] values()