Class UriComponent
- java.lang.Object
-
- org.glassfish.jersey.uri.UriComponent
-
public class UriComponent extends java.lang.Object
Utility class for validating, encoding and decoding components of a URI.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
UriComponent.PathSegmentImpl
static class
UriComponent.Type
The URI component type.
-
Field Summary
Fields Modifier and Type Field Description private static boolean[][]
ENCODING_TABLES
private static char[]
HEX_DIGITS
private static int[]
HEX_TABLE
private static java.lang.String[]
SCHEME
private static java.lang.String[]
SUB_DELIMS
private static java.lang.String[]
UNRESERVED
private static java.nio.charset.Charset
UTF_8_CHARSET
-
Constructor Summary
Constructors Modifier Constructor Description private
UriComponent()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.lang.String
_encode(java.lang.String s, UriComponent.Type t, boolean template, boolean contextualEncode)
private static int
_valid(java.lang.String s, UriComponent.Type t, boolean template)
private static void
appendPercentEncodedOctet(java.lang.StringBuilder sb, int b)
private static void
appendUTF8EncodedCharacter(java.lang.StringBuilder sb, int codePoint)
static java.lang.String
contextualEncode(java.lang.String s, 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.static java.lang.String
contextualEncode(java.lang.String s, UriComponent.Type t, 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.private static java.lang.String
decode(java.lang.String s, int n)
static java.lang.String
decode(java.lang.String s, UriComponent.Type t)
Decodes characters of a string that are percent-encoded octets using UTF-8 decoding (if needed).private static int
decodeHex(char c)
private static int
decodeHex(java.lang.String s, int i)
private static java.lang.String
decodeHost(java.lang.String s, int n)
static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String>
decodeMatrix(java.lang.String pathSegment, boolean decode)
Decode the matrix component of a URI path segment.private static void
decodeMatrixParam(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> params, java.lang.String param, boolean decode)
private static int
decodeOctets(int i, java.nio.ByteBuffer bb, java.lang.StringBuilder sb)
Decodes octets to characters using the UTF-8 decoding and appends the characters to a StringBuffer.static java.util.List<javax.ws.rs.core.PathSegment>
decodePath(java.lang.String path, boolean decode)
Decode the path component of a URI as path segments.static java.util.List<javax.ws.rs.core.PathSegment>
decodePath(java.net.URI u, boolean decode)
Decode the path component of a URI as path segments.static void
decodePathSegment(java.util.List<javax.ws.rs.core.PathSegment> segments, java.lang.String segment, boolean decode)
Decode the path segment and add it to the list of path segments.private static java.nio.ByteBuffer
decodePercentEncodedOctets(java.lang.String s, int i, java.nio.ByteBuffer bb)
Decode a continuous sequence of percent encoded octets.static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String>
decodeQuery(java.lang.String q, boolean decode)
Decode the query component of a URI.static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String>
decodeQuery(java.lang.String q, boolean decodeNames, boolean decodeValues)
Decode the query component of a URI.static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String>
decodeQuery(java.net.URI u, boolean decode)
Decode the query component of a URI.private static java.lang.String
decodeQueryParam(java.lang.String s, int n)
private static void
decodeQueryParam(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> params, java.lang.String param, boolean decodeNames, boolean decodeValues)
static java.lang.String
encode(java.lang.String s, 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.static java.lang.String
encode(java.lang.String s, UriComponent.Type t, 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.static java.lang.String
encodeTemplateNames(java.lang.String s)
Encodes a string with template parameters names present, specifically the characters '{' and '}' will be percent-encoded.static java.lang.String
fullRelativeUri(java.net.URI uri)
Return theRequest-Uri
representation as defined by HTTP spec.private static boolean[]
initEncodingTable(java.util.List<java.lang.String> allowed)
private static boolean[][]
initEncodingTables()
private static int[]
initHexTable()
static boolean
isHexCharacter(char c)
Checks whether the characterc
is hexadecimal character.static boolean
valid(java.lang.String s, UriComponent.Type t)
Validates the legal characters of a percent-encoded string that represents a URI component type.static boolean
valid(java.lang.String s, UriComponent.Type t, boolean template)
Validates the legal characters of a percent-encoded string that represents a URI component type.static void
validate(java.lang.String s, UriComponent.Type t)
Validates the legal characters of a percent-encoded string that represents a URI component type.static void
validate(java.lang.String s, UriComponent.Type t, boolean template)
Validates the legal characters of a percent-encoded string that represents a URI component type.
-
-
-
Field Detail
-
HEX_DIGITS
private static final char[] HEX_DIGITS
-
SCHEME
private static final java.lang.String[] SCHEME
-
UNRESERVED
private static final java.lang.String[] UNRESERVED
-
SUB_DELIMS
private static final java.lang.String[] SUB_DELIMS
-
ENCODING_TABLES
private static final boolean[][] ENCODING_TABLES
-
UTF_8_CHARSET
private static final java.nio.charset.Charset UTF_8_CHARSET
-
HEX_TABLE
private static final int[] HEX_TABLE
-
-
Method Detail
-
validate
public static void validate(java.lang.String s, UriComponent.Type t)
Validates the legal characters of a percent-encoded string that represents a URI component type.- Parameters:
s
- the encoded string.t
- the URI component type identifying the legal characters.- Throws:
java.lang.IllegalArgumentException
- if the encoded string contains illegal characters.
-
validate
public static void validate(java.lang.String s, UriComponent.Type t, boolean template)
Validates the legal characters of a percent-encoded string that represents a URI component type.- Parameters:
s
- the encoded string.t
- the URI component type identifying the legal characters.template
- true if the encoded string contains URI template variables- Throws:
java.lang.IllegalArgumentException
- if the encoded string contains illegal characters.
-
valid
public static boolean valid(java.lang.String s, UriComponent.Type t)
Validates the legal characters of a percent-encoded string that represents a URI component type.- Parameters:
s
- the encoded string.t
- the URI component type identifying the legal characters.- Returns:
- true if the encoded string is valid, otherwise false.
-
valid
public static boolean valid(java.lang.String s, UriComponent.Type t, boolean template)
Validates the legal characters of a percent-encoded string that represents a URI component type.- Parameters:
s
- the encoded string.t
- the URI component type identifying the legal characters.template
- true if the encoded string contains URI template variables- Returns:
- true if the encoded string is valid, otherwise false.
-
_valid
private static int _valid(java.lang.String s, UriComponent.Type t, boolean template)
-
contextualEncode
public static java.lang.String contextualEncode(java.lang.String s, 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
- the string to be encoded.t
- the URI component type identifying the ASCII characters that must be percent-encoded.- Returns:
- the encoded string.
-
contextualEncode
public static java.lang.String contextualEncode(java.lang.String s, UriComponent.Type t, 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
- the string to be encoded.t
- the URI component type identifying the ASCII characters that must be percent-encoded.template
- true if the encoded string contains URI template variables- Returns:
- the encoded string.
-
encode
public static java.lang.String encode(java.lang.String s, 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
- the string to be encoded.t
- the URI component type identifying the ASCII characters that must be percent-encoded.- Returns:
- the encoded string.
-
encode
public static java.lang.String encode(java.lang.String s, UriComponent.Type t, 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
- the string to be encoded.t
- the URI component type identifying the ASCII characters that must be percent-encoded.template
- true if the encoded string contains URI template variables- Returns:
- the encoded string.
-
encodeTemplateNames
public static java.lang.String encodeTemplateNames(java.lang.String s)
Encodes a string with template parameters names present, specifically the characters '{' and '}' will be percent-encoded.- Parameters:
s
- the string with zero or more template parameters names- Returns:
- the string with encoded template parameters names.
-
_encode
private static java.lang.String _encode(java.lang.String s, UriComponent.Type t, boolean template, boolean contextualEncode)
-
appendPercentEncodedOctet
private static void appendPercentEncodedOctet(java.lang.StringBuilder sb, int b)
-
appendUTF8EncodedCharacter
private static void appendUTF8EncodedCharacter(java.lang.StringBuilder sb, int codePoint)
-
initEncodingTables
private static boolean[][] initEncodingTables()
-
initEncodingTable
private static boolean[] initEncodingTable(java.util.List<java.lang.String> allowed)
-
decode
public static java.lang.String decode(java.lang.String s, 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
- the string to be decoded.t
- the URI component type, may be null.- Returns:
- the decoded string.
- Throws:
java.lang.IllegalArgumentException
- if a malformed percent-encoded octet is detected
-
decodeQuery
public static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> decodeQuery(java.net.URI u, 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
- the URI.decode
-true
if the returned query parameter values of the query component should be in decoded form.- Returns:
- the multivalued map of query parameters.
-
decodeQuery
public static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> decodeQuery(java.lang.String q, 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
- the query component in encoded form.decode
-true
if the returned query parameter values of the query component should be in decoded form.- Returns:
- the multivalued map of query parameters.
-
decodeQuery
public static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> decodeQuery(java.lang.String q, boolean decodeNames, boolean decodeValues)
Decode the query component of a URI.Decoding of query parameter names and values can be controlled using the
decodeNames
anddecodeValues
parameter flags.- Parameters:
q
- the query component in encoded form.decodeNames
-true
if the returned query parameter names of the query component should be in decoded form.decodeValues
-true
if the returned query parameter values of the query component should be in decoded form.- Returns:
- the multivalued map of query parameters.
-
decodeQueryParam
private static void decodeQueryParam(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> params, java.lang.String param, boolean decodeNames, boolean decodeValues)
-
decodePath
public static java.util.List<javax.ws.rs.core.PathSegment> decodePath(java.net.URI u, boolean decode)
Decode the path component of a URI as path segments.- Parameters:
u
- 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
- true if the path segments of the path component should be in decoded form.- Returns:
- the list of path segments.
-
decodePath
public static java.util.List<javax.ws.rs.core.PathSegment> decodePath(java.lang.String path, 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
- the path component in encoded form.decode
- true if the path segments of the path component should be in decoded form.- Returns:
- the list of path segments.
-
decodePathSegment
public static void decodePathSegment(java.util.List<javax.ws.rs.core.PathSegment> segments, java.lang.String segment, boolean decode)
Decode the path segment and add it to the list of path segments.- Parameters:
segments
- mutable list of path segments.segment
- path segment to be decoded.decode
-true
if the path segment should be in a decoded form.
-
decodeMatrix
public static javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> decodeMatrix(java.lang.String pathSegment, boolean decode)
Decode the matrix component of a URI path segment.- Parameters:
pathSegment
- the path segment component in encoded form.decode
- true if the matrix parameters of the path segment component should be in decoded form.- Returns:
- the multivalued map of matrix parameters.
-
decodeMatrixParam
private static void decodeMatrixParam(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> params, java.lang.String param, boolean decode)
-
decode
private static java.lang.String decode(java.lang.String s, int n)
-
decodeQueryParam
private static java.lang.String decodeQueryParam(java.lang.String s, int n)
-
decodeHost
private static java.lang.String decodeHost(java.lang.String s, int n)
-
decodePercentEncodedOctets
private static java.nio.ByteBuffer decodePercentEncodedOctets(java.lang.String s, int i, java.nio.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.
-
decodeOctets
private static int decodeOctets(int i, java.nio.ByteBuffer bb, java.lang.StringBuilder sb)
Decodes octets to characters using the UTF-8 decoding and appends the characters to a StringBuffer.- Returns:
- the index to the next unchecked character in the string to decode
-
decodeHex
private static int decodeHex(java.lang.String s, int i)
-
initHexTable
private static int[] initHexTable()
-
decodeHex
private static int decodeHex(char c)
-
isHexCharacter
public static boolean isHexCharacter(char c)
Checks whether the characterc
is hexadecimal character.- Parameters:
c
- Any character- Returns:
- The is
c
is a hexadecimal character (e.g. 0, 5, a, A, f, ...)
-
fullRelativeUri
public static java.lang.String fullRelativeUri(java.net.URI uri)
Return theRequest-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 to obtainRequest-Uri
from.- Returns:
Request-Uri
representation ornull
ifuri
is not provided.
-
-