Class HeaderUtils


  • public final class HeaderUtils
    extends Object
    Utility class supporting the processing of message headers.
    Author:
    Marek Potociar, Michal Gajdos, Libor Kramolis
    • Method Detail

      • createInbound

        public static jakarta.ws.rs.core.AbstractMultivaluedMap<String,​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> jakarta.ws.rs.core.MultivaluedMap<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 jakarta.ws.rs.core.AbstractMultivaluedMap<String,​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 String asString​(Object headerValue,
                                      jakarta.ws.rs.core.Configuration configuration)
        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 or HeaderDelegateProvider to convert the value to a String. If neither is found then the toString() method on the header object is utilized.

        Parameters:
        headerValue - the header value represented as an object.
        configuration - the Configuration that may contain CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE property preventing the HeaderDelegateProvider to be used by the default RuntimeDelegate instance.
        Returns:
        the string representation of the supplied header value or null if the supplied header value is null.
      • asStringList

        public static List<String> asStringList​(List<Object> headerValues,
                                                jakarta.ws.rs.core.Configuration configuration)
        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.
        configuration - the Configuration that may contain CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE property preventing the HeaderDelegateProvider to be used by the default RuntimeDelegate instance.
        Returns:
        String view of header values.
      • asStringHeaders

        public static jakarta.ws.rs.core.MultivaluedMap<String,​String> asStringHeaders​(jakarta.ws.rs.core.MultivaluedMap<String,​Object> headers,
                                                                                             jakarta.ws.rs.core.Configuration configuration)
        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.
        configuration - the Configuration that may contain CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE property preventing the HeaderDelegateProvider to be used by the default RuntimeDelegate instance.
        Returns:
        String view of headers or null if {code headers} input parameter is null.
      • asStringHeadersSingleValue

        public static Map<String,​String> asStringHeadersSingleValue​(jakarta.ws.rs.core.MultivaluedMap<String,​Object> headers,
                                                                          jakarta.ws.rs.core.Configuration configuration)
        Transforms multi value map of headers to single String value map. Returned map is immutable. Map values are formatted using method asHeaderString(List, RuntimeDelegate).
        Parameters:
        headers - headers to be formatted
        configuration - the Configuration that may contain CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE property preventing the HeaderDelegateProvider to be used by the default RuntimeDelegate instance.
        Returns:
        immutable single String value map or null if headers input parameter is null.
      • asHeaderString

        public static String asHeaderString​(List<Object> values,
                                            jakarta.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​(Map<String,​String> headersSnapshot,
                                              jakarta.ws.rs.core.MultivaluedMap<String,​Object> currentHeaders,
                                              String connectorName,
                                              jakarta.ws.rs.core.Configuration configuration)
        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
        configuration - the Configuration that may contain CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE property preventing the HeaderDelegateProvider to be used by the default RuntimeDelegate instance.
        See Also:
        JERSEY-2341
      • getPreferredCookie

        public static jakarta.ws.rs.core.NewCookie getPreferredCookie​(jakarta.ws.rs.core.NewCookie first,
                                                                      jakarta.ws.rs.core.NewCookie second)
        Compare two NewCookies having the same name. See documentation RFC.
        Parameters:
        first - NewCookie to be compared.
        second - NewCookie to be compared.
        Returns:
        the preferred NewCookie according to rules : - the latest maxAge. - if equal, compare the expiry date. - if equal, compare path length.
      • asString

        @Deprecated
        public static String asString​(Object headerValue)
        Deprecated.
        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.
        Parameters:
        headerValue - the header value represented as an object.
        Returns:
        the string representation of the supplied header value or null if the supplied header value is null.
        See Also:
        asString(Object, Configuration)
      • asStringList

        @Deprecated
        public static List<String> asStringList​(List<Object> headerValues)
        Deprecated.
        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.
        Returns:
        String view of header values.
        See Also:
        asStringList(List, Configuration)
      • asStringHeaders

        @Deprecated
        public static jakarta.ws.rs.core.MultivaluedMap<String,​String> asStringHeaders​(jakarta.ws.rs.core.MultivaluedMap<String,​Object> headers)
        Deprecated.
        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.
        See Also:
        asStringHeaders(MultivaluedMap, Configuration)
      • checkHeaderChanges

        @Deprecated
        public static void checkHeaderChanges​(Map<String,​String> headersSnapshot,
                                              jakarta.ws.rs.core.MultivaluedMap<String,​Object> currentHeaders,
                                              String connectorName)
        Deprecated.
        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:
        checkHeaderChanges(Map, MultivaluedMap, String, Configuration)