Class Utils


  • public class Utils
    extends java.lang.Object
    Utility methods shared among Tyrus modules.
    Author:
    Pavel Bucek
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Utils.Stringifier<T>
      Define to String conversion for various types.
    • Constructor Summary

      Constructors 
      Constructor Description
      Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.nio.ByteBuffer appendBuffers​(java.nio.ByteBuffer buffer, java.nio.ByteBuffer buffer1, int incomingBufferSize, int BUFFER_STEP_SIZE)
      Concatenates two buffers into one.
      static <T> void checkNotNull​(T reference, java.lang.String parameterName)
      Check for null.
      static <T> java.lang.String getHeaderFromList​(java.util.List<T> list)
      Creates single String value from provided List by calling Object.toString() on each item and separating existing ones with ", ".
      static <T> java.lang.String getHeaderFromList​(java.util.List<T> list, Utils.Stringifier<T> stringifier)
      Convert list of values to singe String usable as HTTP header value.
      static <T> T getProperty​(java.util.Map<java.lang.String,​java.lang.Object> properties, java.lang.String key, java.lang.Class<T> type)
      Get typed property from generic property map.
      static <T> T getProperty​(java.util.Map<java.lang.String,​java.lang.Object> properties, java.lang.String key, java.lang.Class<T> type, T defaultValue)
      Get typed property from generic property map.
      static byte[] getRemainingArray​(java.nio.ByteBuffer buffer)
      Creates the array of bytes containing the bytes from the position to the limit of the ByteBuffer.
      static <T> java.util.List<java.lang.String> getStringList​(java.util.List<T> list, Utils.Stringifier<T> stringifier)
      Get list of strings from List<T>.
      static int getWsPort​(java.net.URI uri)
      Get port from provided URI.
      static int getWsPort​(java.net.URI uri, java.lang.String scheme)
      Get port from provided URI.
      static java.util.List<java.lang.String> parseHeaderValue​(java.lang.String headerValue)
      Parse header value - splits multiple values (quoted, unquoted) separated by comma.
      static java.util.Date parseHttpDate​(java.lang.String stringValue)
      Parse HTTP date.
      static java.lang.String stringifyUpgradeRequest​(UpgradeRequest upgradeRequest)
      Converts upgrade request to a HTTP-formatted string.
      static java.lang.String stringifyUpgradeResponse​(UpgradeResponse upgradeResponse)
      Converts upgrade response to a HTTP-formatted string.
      static byte[] toArray​(long value)
      Convert long to byte[].
      static long toLong​(byte[] bytes, int start, int end)
      Convert byte[] to long.
      static java.util.List<java.lang.String> toString​(byte[] bytes)  
      static java.util.List<java.lang.String> toString​(byte[] bytes, int start, int end)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • parseHeaderValue

        public static java.util.List<java.lang.String> parseHeaderValue​(java.lang.String headerValue)
        Parse header value - splits multiple values (quoted, unquoted) separated by comma.
        Parameters:
        headerValue - string containing header values.
        Returns:
        split list of values.
      • getRemainingArray

        public static byte[] getRemainingArray​(java.nio.ByteBuffer buffer)
        Creates the array of bytes containing the bytes from the position to the limit of the ByteBuffer.
        Parameters:
        buffer - where the bytes are taken from.
        Returns:
        array of bytes containing the bytes from the position to the limit of the ByteBuffer.
      • getHeaderFromList

        public static <T> java.lang.String getHeaderFromList​(java.util.List<T> list)
        Creates single String value from provided List by calling Object.toString() on each item and separating existing ones with ", ".
        Type Parameters:
        T - item type.
        Parameters:
        list - to be serialized.
        Returns:
        single String containing all items from provided list.
      • getStringList

        public static <T> java.util.List<java.lang.String> getStringList​(java.util.List<T> list,
                                                                         Utils.Stringifier<T> stringifier)
        Get list of strings from List<T>.
        Type Parameters:
        T - type to be converted.
        Parameters:
        list - list to be converted.
        stringifier - strignifier used for conversion. When null, Object.toString() method will be used.
        Returns:
        converted list.
      • getHeaderFromList

        public static <T> java.lang.String getHeaderFromList​(java.util.List<T> list,
                                                             Utils.Stringifier<T> stringifier)
        Convert list of values to singe String usable as HTTP header value.
        Type Parameters:
        T - type to be converted.
        Parameters:
        list - list of values.
        stringifier - strignifier used for conversion. When null, Object.toString() method will be used.
        Returns:
        serialized list.
      • checkNotNull

        public static <T> void checkNotNull​(T reference,
                                            java.lang.String parameterName)
        Check for null. Throws IllegalArgumentException if provided value is null.
        Type Parameters:
        T - object type.
        Parameters:
        reference - object to check.
        parameterName - name of parameter to be formatted into localized message of thrown IllegalArgumentException.
      • toArray

        public static byte[] toArray​(long value)
        Convert long to byte[].
        Parameters:
        value - to be converted.
        Returns:
        converted value.
      • toLong

        public static long toLong​(byte[] bytes,
                                  int start,
                                  int end)
        Convert byte[] to long.
        Parameters:
        bytes - to be converted.
        start - start index.
        end - end index.
        Returns:
        converted value.
      • toString

        public static java.util.List<java.lang.String> toString​(byte[] bytes)
      • toString

        public static java.util.List<java.lang.String> toString​(byte[] bytes,
                                                                int start,
                                                                int end)
      • appendBuffers

        public static java.nio.ByteBuffer appendBuffers​(java.nio.ByteBuffer buffer,
                                                        java.nio.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 - first buffer.
        buffer1 - second buffer.
        incomingBufferSize - incoming buffer size. Concatenation length cannot be bigger than this value.
        BUFFER_STEP_SIZE - buffer step size.
        Returns:
        concatenation.
        Throws:
        java.lang.IllegalArgumentException - when the concatenation length is bigger than provided incoming buffer size.
      • getProperty

        public static <T> T getProperty​(java.util.Map<java.lang.String,​java.lang.Object> properties,
                                        java.lang.String key,
                                        java.lang.Class<T> type)
        Get typed property from generic property map.
        Type Parameters:
        T - type of value to be retrieved.
        Parameters:
        properties - property map.
        key - key of value to be retrieved.
        type - type of value to be retrieved.
        Returns:
        typed value or null if property is not set or value is not assignable.
      • getProperty

        public static <T> T getProperty​(java.util.Map<java.lang.String,​java.lang.Object> properties,
                                        java.lang.String key,
                                        java.lang.Class<T> type,
                                        T defaultValue)
        Get typed property from generic property map.
        Type Parameters:
        T - type of value to be retrieved.
        Parameters:
        properties - property map.
        key - key of value to be retrieved.
        type - type of value to be retrieved.
        defaultValue - value returned when record does not exist in supplied map.
        Returns:
        typed value or null if property is not set or value is not assignable.
      • getWsPort

        public static int getWsPort​(java.net.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 - provided uri.
        Returns:
        port number which should be used for creating connections/etc.
      • getWsPort

        public static int getWsPort​(java.net.URI uri,
                                    java.lang.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 - provided uri.
        scheme - scheme to be used when checking for "ws" and "wss".
        Returns:
        port number which should be used for creating connections/etc.
      • parseHttpDate

        public static java.util.Date parseHttpDate​(java.lang.String stringValue)
                                            throws java.text.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 value to be parsed.
        Returns:
        A Date parsed from the string.
        Throws:
        java.text.ParseException - if the specified string cannot be parsed in neither of all three HTTP date formats.
      • stringifyUpgradeRequest

        public static java.lang.String stringifyUpgradeRequest​(UpgradeRequest upgradeRequest)
        Converts upgrade request to a HTTP-formatted string.
        Parameters:
        upgradeRequest - upgrade request to be formatted.
        Returns:
        stringified upgrade request.
      • stringifyUpgradeResponse

        public static java.lang.String stringifyUpgradeResponse​(UpgradeResponse upgradeResponse)
        Converts upgrade response to a HTTP-formatted string.
        Parameters:
        upgradeResponse - upgrade request to be formatted.
        Returns:
        stringified upgrade request.