Class ConcurrentHashMapHeaders

  • All Implemented Interfaces:
    Headers, InboundHeaders, OutboundHeaders

    public class ConcurrentHashMapHeaders
    extends java.lang.Object
    implements Headers
    A thread safe implementation of Headers using ConcurrentHashMap as the backing data structure. Concurrent writes are supported. Reads return immutable data types.

    NOTE: If using this implementation of Headers, an agent of the same version should be used. Failure to do so may cause unexpected results at runtime, since the agent's definition may be different than provided by the newrelic-api.

    • Method Detail

      • getMapCopy

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getMapCopy()
        Obtain a copy of the backing map. The result is not guaranteed to be thread safe, or mutable. If it is mutable, changes do not modify the backing headers map.
        Returns:
        The map copy.
      • removeHeader

        public void removeHeader​(java.lang.String name)
        Remove any headers with the name.
        Parameters:
        name - The name of the header.
      • getHeader

        public java.lang.String getHeader​(java.lang.String name)
        Description copied from interface: Headers
        Returns the value of the first specified message header as a String. If the message does not include a header with the specified input name, then this method returns null.
        Specified by:
        getHeader in interface Headers
        Specified by:
        getHeader in interface InboundHeaders
        Parameters:
        name - The name of the desired message header.
        Returns:
        A String containing the value of the requested header, or null if the message header is not present.
      • getHeaders

        public java.util.Collection<java.lang.String> getHeaders​(java.lang.String name)
        Description copied from interface: Headers
        Return all the values of the specified message header as a List of String objects. If the message does not include any headers of the specified name, this method returns an empty list.

        Changes to the returned collection must not affect the Headers instance.

        Specified by:
        getHeaders in interface Headers
        Parameters:
        name - The name of the desired message header.
        Returns:
        A List containing the values of the requested header, or an empty list of the message header is not present.
      • setHeader

        public void setHeader​(java.lang.String name,
                              java.lang.String value)
        Description copied from interface: Headers
        Set a header with the given name and value. If the header had already been set, the new value overwrites the previous one. Headers.containsHeader(String) can be used to test for the presence of a header before setting its value.
        Specified by:
        setHeader in interface Headers
        Specified by:
        setHeader in interface OutboundHeaders
        Parameters:
        name - The name of the header.
        value - The value of the header.
      • addHeader

        public void addHeader​(java.lang.String name,
                              java.lang.String value)
        Description copied from interface: Headers
        Add a header with the given name and value. This method allows headers to have multiple values.
        Specified by:
        addHeader in interface Headers
        Parameters:
        name - The name of the header.
        value - The value of the header.
      • getHeaderNames

        public java.util.Collection<java.lang.String> getHeaderNames()
        Description copied from interface: Headers
        Get the names of the headers.

        Changes to the returned collection must not affect the Headers instance.

        Specified by:
        getHeaderNames in interface Headers
        Returns:
        A possibly empty Collection of the names of present headers.
      • containsHeader

        public boolean containsHeader​(java.lang.String name)
        Description copied from interface: Headers
        Return a boolean indicating whether the named header has already been set.
        Specified by:
        containsHeader in interface Headers
        Parameters:
        name - The name of the header.
        Returns:
        true if the header has already been set, false otherwise.
      • build

        public static ConcurrentHashMapHeaders build​(HeaderType headerType)
        Build an empty instance with the given HeaderType.

        NOTE: If using this implementation of Headers, an agent of the same version should be used. Failure to do so may cause unexpected results at runtime, since the agent's definition may be different than provided by the newrelic-api.

        Parameters:
        headerType - The type of headers the instance represents.
        Returns:
        The instance.
      • buildFromFlatMap

        public static ConcurrentHashMapHeaders buildFromFlatMap​(HeaderType headerType,
                                                                java.util.Map<java.lang.String,​java.lang.String> map)
        Build an instance with the given HeaderType and call setHeader(String, String) with each entry in the map.

        NOTE: If using this implementation of Headers, an agent of the same version should be used. Failure to do so may cause unexpected results at runtime, since the agent's definition may be different than provided by the newrelic-api.

        Parameters:
        headerType - The type of headers the instance represents.
        map - A map of header names and values used to populate the instance.
        Returns:
        The instance.
      • buildFromMap

        public static ConcurrentHashMapHeaders buildFromMap​(HeaderType headerType,
                                                            java.util.Map<java.lang.String,​java.util.List<java.lang.String>> map)
        Build an instance with the given HeaderType and call addHeader(String, String) with each entry in the map.

        NOTE: If using this implementation of Headers, an agent of the same version should be used. Failure to do so may cause unexpected results at runtime, since the agent's definition may be different than provided by the newrelic-api.

        Parameters:
        headerType - The type of headers the instance represents.
        map - A map of header names and potentially several values each used to populate the instance.
        Returns:
        The instance.