Class HeaderUtils


  • public final class HeaderUtils
    extends java.lang.Object
    Utility class supporting the processing of message headers.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.logging.Logger LOGGER  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private HeaderUtils()
      Preventing instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String asHeaderString​(java.util.List<java.lang.Object> values, javax.ws.rs.ext.RuntimeDelegate rd)
      Converts a list of message header values to a single string value (with individual values separated by ',').
      static java.lang.String asString​(java.lang.Object headerValue, javax.ws.rs.ext.RuntimeDelegate rd)
      Convert a message header value, represented as a general object, to it's string representation.
      static javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> asStringHeaders​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
      Returns string view of passed headers.
      static java.util.Map<java.lang.String,​java.lang.String> asStringHeadersSingleValue​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
      Transforms multi value map of headers to single String value map.
      static java.util.List<java.lang.String> asStringList​(java.util.List<java.lang.Object> headerValues, javax.ws.rs.ext.RuntimeDelegate rd)
      Returns string view of list of header values.
      static void checkHeaderChanges​(java.util.Map<java.lang.String,​java.lang.String> headersSnapshot, javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> currentHeaders, java.lang.String connectorName)
      Compares two snapshots of headers from jersey ClientRequest and logs WARNING in case of difference.
      static javax.ws.rs.core.AbstractMultivaluedMap<java.lang.String,​java.lang.String> createInbound()
      Create an empty inbound message headers container.
      static javax.ws.rs.core.AbstractMultivaluedMap<java.lang.String,​java.lang.Object> createOutbound()
      Create an empty outbound message headers container.
      static <V> javax.ws.rs.core.MultivaluedMap<java.lang.String,​V> empty()
      Get immutable empty message headers container.
      • Methods inherited from class java.lang.Object

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

      • LOGGER

        private static final java.util.logging.Logger LOGGER
    • Constructor Detail

      • HeaderUtils

        private HeaderUtils()
        Preventing instantiation.
    • Method Detail

      • createInbound

        public static javax.ws.rs.core.AbstractMultivaluedMap<java.lang.String,​java.lang.String> createInbound()
        Create an empty inbound message headers container. Created container is mutable.
        Returns:
        a new empty mutable container for storing inbound message headers.
      • empty

        public static <V> javax.ws.rs.core.MultivaluedMap<java.lang.String,​V> empty()
        Get immutable empty message headers container. The factory method can be used to for both message header container types  –  inbound as well as outbound.
        Type Parameters:
        V - header value type. Typically Object in case of the outbound headers and String in case of the inbound headers.
        Returns:
        an immutable empty message headers container.
      • createOutbound

        public static javax.ws.rs.core.AbstractMultivaluedMap<java.lang.String,​java.lang.Object> createOutbound()
        Create an empty outbound message headers container. Created container is mutable.
        Returns:
        a new empty mutable container for storing outbound message headers.
      • asString

        public static java.lang.String asString​(java.lang.Object headerValue,
                                                javax.ws.rs.ext.RuntimeDelegate rd)
        Convert a message header value, represented as a general object, to it's string representation. If the supplied header value is null, this method returns null.

        This method defers to RuntimeDelegate.createHeaderDelegate(java.lang.Class<T>) to obtain a RuntimeDelegate.HeaderDelegate to convert the value to a String. If a RuntimeDelegate.HeaderDelegate is not found then the toString() method on the header object is utilized.

        Parameters:
        headerValue - the header value represented as an object.
        rd - runtime delegate instance to be used for header delegate retrieval. If null, a default RuntimeDelegate instance will be obtained and used.
        Returns:
        the string representation of the supplied header value or null if the supplied header value is null.
      • asStringList

        public static java.util.List<java.lang.String> asStringList​(java.util.List<java.lang.Object> headerValues,
                                                                    javax.ws.rs.ext.RuntimeDelegate rd)
        Returns string view of list of header values. Any modifications to the underlying list are visible to the view, the view also supports removal of elements. Does not support other modifications.
        Parameters:
        headerValues - header values.
        rd - RuntimeDelegate instance or null (in that case RuntimeDelegate.getInstance() will be called for before element conversion.
        Returns:
        String view of header values.
      • asStringHeaders

        public static javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> asStringHeaders​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
        Returns string view of passed headers. Any modifications to the headers are visible to the view, the view also supports removal of elements. Does not support other modifications.
        Parameters:
        headers - headers.
        Returns:
        String view of headers or null if {code headers} input parameter is null.
      • asStringHeadersSingleValue

        public static java.util.Map<java.lang.String,​java.lang.String> asStringHeadersSingleValue​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
        Transforms multi value map of headers to single String value map. Returned map is immutable. Map values are formatted using method asHeaderString(java.util.List<java.lang.Object>, javax.ws.rs.ext.RuntimeDelegate).
        Parameters:
        headers - headers to be formatted
        Returns:
        immutable single String value map or null if headers input parameter is null.
      • asHeaderString

        public static java.lang.String asHeaderString​(java.util.List<java.lang.Object> values,
                                                      javax.ws.rs.ext.RuntimeDelegate rd)
        Converts a list of message header values to a single string value (with individual values separated by ','). Each single header value is converted to String using a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate.createHeaderDelegate(java.lang.Class) for the header value class or using its toString() method if a header delegate is not available.
        Parameters:
        values - list of individual header values.
        rd - RuntimeDelegate instance or null (in that case RuntimeDelegate.getInstance() will be called for before conversion of elements).
        Returns:
        single string consisting of all the values passed in as a parameter. If values parameter is null, null is returned. If the list of values is empty, an empty string is returned.
      • checkHeaderChanges

        public static void checkHeaderChanges​(java.util.Map<java.lang.String,​java.lang.String> headersSnapshot,
                                              javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> currentHeaders,
                                              java.lang.String connectorName)
        Compares two snapshots of headers from jersey ClientRequest and logs WARNING in case of difference. Current container implementations does not support header modification in WriterInterceptor and MessageBodyWriter. The method checks there are some newly added headers (probably by WI or MBW) and logs WARNING message about it.
        Parameters:
        headersSnapshot - first immutable snapshot of headers
        currentHeaders - current instance of headers tobe compared to
        connectorName - name of connector the method is invoked from, used just in logged message
        See Also:
        JERSEY-2341