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

public Class Utils

extends Object
Class Inheritance
Imports
java.net.URI, java.nio.Buffer, .ByteBuffer, java.text.ParseException, .SimpleDateFormat, java.util.ArrayList, .Arrays, .Date, .Iterator, .List, .Locale, .Map, java.util.logging.Level, .Logger, org.glassfish.tyrus.core.l10n.LocalizationMessages, org.glassfish.tyrus.spi.UpgradeRequest, .UpgradeResponse

Utility methods shared among Tyrus modules.
Author
Pavel Bucek

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Utils.Stringifier<
type for which is conversion defined.
T
>

Define to String conversion for various types.

Field Summary

Modifier and TypeField and Description
private static final List<String>
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static ByteBuffer

Returns:

concatenation.
appendBuffers
(ByteBuffer
first buffer.
buffer
,
ByteBuffer
second buffer.
buffer1
,
int
incoming buffer size. Concatenation length cannot be bigger than this value.
incomingBufferSize
,
int
buffer step size.
BUFFER_STEP_SIZE
)

Concatenates two buffers into one.

private static void
appendHeader(StringBuilder message, String key, String value)

private static void
public static <
object type.
T
>
void
checkNotNull(T
object to check.
reference
,
String
name of parameter to be formatted into localized message of thrown IllegalArgumentException.
parameterName
)

Check for null.

public static <
item type.
T
>
String

Returns:

single String containing all items from provided list.
getHeaderFromList
(List<T>
to be serialized.
list
)

Creates single String value from provided List by calling Object#toString() on each item and separating existing ones with ", ".

public static <
type to be converted.
T
>
String

Returns:

serialized list.
getHeaderFromList
(List<T>
list of values.
list
,
Utils.Stringifier<T>
strignifier used for conversion. When null, Object#toString() method will be used.
stringifier
)

Convert list of values to singe String usable as HTTP header value.

public static <
type of value to be retrieved.
T
>
T

Returns:

typed value or null if property is not set or value is not assignable.
getProperty
(Map<String, Object>
property map.
properties
,
String
key of value to be retrieved.
key
,
Class<T>
type of value to be retrieved.
type
)

Get typed property from generic property map.

public static <
type of value to be retrieved.
T
>
T

Returns:

typed value or null if property is not set or value is not assignable.
getProperty
(final Map<String, Object>
property map.
properties
,
final String
key of value to be retrieved.
key
,
final Class<T>
type of value to be retrieved.
type
,
final T
value returned when record does not exist in supplied map.
defaultValue
)

Get typed property from generic property map.

public static byte[]

Returns:

array of bytes containing the bytes from the position to the limit of the ByteBuffer.
getRemainingArray
(ByteBuffer
where the bytes are taken from.
buffer
)

Creates the array of bytes containing the bytes from the position to the limit of the ByteBuffer.

public static <
type to be converted.
T
>
List<String>

Returns:

converted list.
getStringList
(List<T>
list to be converted.
list
,
Utils.Stringifier<T>
strignifier used for conversion. When null, Object#toString() method will be used.
stringifier
)

Get list of strings from List<T>.

public static int

Returns:

port number which should be used for creating connections/etc.
getWsPort
(URI
provided uri.
uri
)

Get port from provided URI.

public static int

Returns:

port number which should be used for creating connections/etc.
getWsPort
(URI
provided uri.
uri
,
String
scheme to be used when checking for "ws" and "wss".
scheme
)

Get port from provided URI.

public static List<String>

Returns:

split list of values.
parseHeaderValue
(String
string containing header values.
headerValue
)

Parse header value - splits multiple values (quoted, unquoted) separated by comma.

public static Date

Returns:

A Date parsed from the string.
parseHttpDate
(String
String value to be parsed.
stringValue
)

Parse HTTP date.

public static String

Returns:

stringified upgrade request.
stringifyUpgradeRequest
(UpgradeRequest
upgrade request to be formatted.
upgradeRequest
)

Converts upgrade request to a HTTP-formatted string.

public static String

Returns:

stringified upgrade request.
stringifyUpgradeResponse
(UpgradeResponse
upgrade request to be formatted.
upgradeResponse
)

Converts upgrade response to a HTTP-formatted string.

public static byte[]

Returns:

converted value.
toArray
(long
to be converted.
value
)

Convert long to byte[].

public static long

Returns:

converted value.
toLong
(byte[]
to be converted.
bytes
,
int
start index.
start
,
int
end index.
end
)

Convert byte[] to long.

public static List<String>
toString(byte[] bytes)

public static List<String>
toString(byte[] bytes, int start, int end)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FILTERED_HEADERSback to summary
private static final List<String> FILTERED_HEADERS
LOGGERback to summary
private static final Logger LOGGER

Constructor Detail

Utilsback to summary
public Utils()

Method Detail

appendBuffersback to summary
public static ByteBuffer appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, int incomingBufferSize, int BUFFER_STEP_SIZE)

Concatenates two buffers into one. If buffer given as first argument has enough space for putting the other one, it will be done and the original buffer will be returned. Otherwise new buffer will be created.

Parameters
buffer:ByteBuffer

first buffer.

buffer1:ByteBuffer

second buffer.

incomingBufferSize:int

incoming buffer size. Concatenation length cannot be bigger than this value.

BUFFER_STEP_SIZE:int

buffer step size.

Returns:ByteBuffer

concatenation.

Exceptions
IllegalArgumentException:
when the concatenation length is bigger than provided incoming buffer size.
appendHeaderback to summary
private static void appendHeader(StringBuilder message, String key, String value)
appendHeadersback to summary
private static void appendHeaders(StringBuilder message, Map<String, List<String>> headers)
checkNotNullback to summary
public static <T> void checkNotNull(T reference, String parameterName)

Check for null. Throws IllegalArgumentException if provided value is null.

Parameters
<T>
object type.
reference:T

object to check.

parameterName:String

name of parameter to be formatted into localized message of thrown IllegalArgumentException.

getHeaderFromListback to summary
public static <T> String getHeaderFromList(List<T> list)

Creates single String value from provided List by calling Object#toString() on each item and separating existing ones with ", ".

Parameters
<T>
item type.
list:List<T>

to be serialized.

Returns:String

single String containing all items from provided list.

getHeaderFromListback to summary
public static <T> String getHeaderFromList(List<T> list, Utils.Stringifier<T> stringifier)

Convert list of values to singe String usable as HTTP header value.

Parameters
<T>
type to be converted.
list:List<T>

list of values.

stringifier:Utils.Stringifier<T>

strignifier used for conversion. When null, Object#toString() method will be used.

Returns:String

serialized list.

getPropertyback to summary
public static <T> T getProperty(Map<String, Object> properties, String key, Class<T> type)

Get typed property from generic property map.

Parameters
<T>
type of value to be retrieved.
properties:Map<String, Object>

property map.

key:String

key of value to be retrieved.

type:Class<T>

type of value to be retrieved.

Returns:T

typed value or null if property is not set or value is not assignable.

getPropertyback to summary
public static <T> T getProperty(final Map<String, Object> properties, final String key, final Class<T> type, final T defaultValue)

Get typed property from generic property map.

Parameters
<T>
type of value to be retrieved.
properties:Map<String, Object>

property map.

key:String

key of value to be retrieved.

type:Class<T>

type of value to be retrieved.

defaultValue:T

value returned when record does not exist in supplied map.

Returns:T

typed value or null if property is not set or value is not assignable.

Annotations
@SuppressWarnings:unchecked
getRemainingArrayback to summary
public static byte[] getRemainingArray(ByteBuffer buffer)

Creates the array of bytes containing the bytes from the position to the limit of the ByteBuffer.

Parameters
buffer:ByteBuffer

where the bytes are taken from.

Returns:byte[]

array of bytes containing the bytes from the position to the limit of the ByteBuffer.

getStringListback to summary
public static <T> List<String> getStringList(List<T> list, Utils.Stringifier<T> stringifier)

Get list of strings from List<T>.

Parameters
<T>
type to be converted.
list:List<T>

list to be converted.

stringifier:Utils.Stringifier<T>

strignifier used for conversion. When null, Object#toString() method will be used.

Returns:List<String>

converted list.

getWsPortback to summary
public static int getWsPort(URI uri)

Get port from provided URI.

Expected schemes are "ws" and "wss" and this method will return 80 or 443 when the port is not explicitly set in the provided URI.

Parameters
uri:URI

provided uri.

Returns:int

port number which should be used for creating connections/etc.

getWsPortback to summary
public static int getWsPort(URI uri, String scheme)

Get port from provided URI.

Expected schemes are "ws" and "wss" and this method will return 80 or 443 when the port is not explicitly set in the provided URI.

Parameters
uri:URI

provided uri.

scheme:String

scheme to be used when checking for "ws" and "wss".

Returns:int

port number which should be used for creating connections/etc.

parseHeaderValueback to summary
public static List<String> parseHeaderValue(String headerValue)

Parse header value - splits multiple values (quoted, unquoted) separated by comma.

Parameters
headerValue:String

string containing header values.

Returns:List<String>

split list of values.

parseHttpDateback to summary
public static Date parseHttpDate(String stringValue) throws ParseException

Parse HTTP date.

HTTP applications have historically allowed three different formats for the representation of date/time stamps:

  • Sun, 06 Nov 1994 08:49:37 GMT (RFC 822, updated by RFC 1123)
  • Sunday, 06-Nov-94 08:49:37 GMT (RFC 850, obsoleted by RFC 1036)
  • Sun Nov 6 08:49:37 1994 (ANSI C's asctime() format)
Parameters
stringValue:String

String value to be parsed.

Returns:Date

A Date parsed from the string.

Exceptions
ParseException:
if the specified string cannot be parsed in neither of all three HTTP date formats.
stringifyUpgradeRequestback to summary
public static String stringifyUpgradeRequest(UpgradeRequest upgradeRequest)

Converts upgrade request to a HTTP-formatted string.

Parameters
upgradeRequest:UpgradeRequest

upgrade request to be formatted.

Returns:String

stringified upgrade request.

stringifyUpgradeResponseback to summary
public static String stringifyUpgradeResponse(UpgradeResponse upgradeResponse)

Converts upgrade response to a HTTP-formatted string.

Parameters
upgradeResponse:UpgradeResponse

upgrade request to be formatted.

Returns:String

stringified upgrade request.

toArrayback to summary
public static byte[] toArray(long value)

Convert long to byte[].

Parameters
value:long

to be converted.

Returns:byte[]

converted value.

toLongback to summary
public static long toLong(byte[] bytes, int start, int end)

Convert byte[] to long.

Parameters
bytes:byte[]

to be converted.

start:int

start index.

end:int

end index.

Returns:long

converted value.

toStringback to summary
public static List<String> toString(byte[] bytes)
toStringback to summary
public static List<String> toString(byte[] bytes, int start, int end)
org.glassfish.tyrus.core back to summary

public abstract Class Utils.Stringifier<T>

extends Object
Class Inheritance
Type Parameters
<T>
type for which is conversion defined.

Define to String conversion for various types.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract String

Returns:

String representation of given object.
toString
(T
object to be converted.
t
)

Convert object to String.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Stringifierback to summary
public Stringifier()

Method Detail

toStringback to summary
pack-priv abstract String toString(T t)

Convert object to String.

Parameters
t:T

object to be converted.

Returns:String

String representation of given object.