Class HeaderGroup

  • All Implemented Interfaces:
    java.io.Serializable, MessageHeaders
    Direct Known Subclasses:
    BasicHttpRequest, BasicHttpResponse

    public class HeaderGroup
    extends java.lang.Object
    implements MessageHeaders, java.io.Serializable
    A class for combining a set of headers. This class allows for multiple headers with the same name and keeps track of the order in which headers were added.
    Since:
    4.0
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static Header[] EMPTY  
      private java.util.List<Header> headers
      The list of headers for this group, in the order in which they were added
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      HeaderGroup()
      Constructor for HeaderGroup.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addHeader​(Header header)
      Adds the given header to the group.
      void clear()
      Removes all headers.
      boolean containsHeader​(java.lang.String name)
      Tests if headers with the given name are contained within this group.
      int countHeaders​(java.lang.String name)
      Checks if a certain header is present in this message and how many times.
      Header getCondensedHeader​(java.lang.String name)
      Gets a header representing all of the header values with the given name.
      Header getFirstHeader​(java.lang.String name)
      Gets the first header with the given name.
      Header getHeader​(java.lang.String name)
      Gets single first header with the given name.
      Header[] getHeaders()
      Gets all of the headers contained within this group.
      Header[] getHeaders​(java.lang.String name)
      Gets all of the headers with the given name.
      Header getLastHeader​(java.lang.String name)
      Gets the last header with the given name.
      private boolean headerEquals​(Header header1, Header header2)  
      java.util.Iterator<Header> headerIterator()
      Returns an iterator over this group of headers.
      java.util.Iterator<Header> headerIterator​(java.lang.String name)
      Returns an iterator over the headers with a given name in this group.
      boolean removeHeader​(Header header)
      Removes the first given header.
      boolean removeHeaders​(java.lang.String name)
      Removes all headers with a given name in this group.
      boolean removeHeaders​(Header header)
      Removes all headers that match the given header.
      void setHeader​(Header header)
      Replaces the first occurrence of the header with the same name.
      void setHeaders​(Header... headers)
      Sets all of the headers contained within this group overriding any existing headers.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • EMPTY

        private static final Header[] EMPTY
      • headers

        private final java.util.List<Header> headers
        The list of headers for this group, in the order in which they were added
    • Constructor Detail

      • HeaderGroup

        public HeaderGroup()
        Constructor for HeaderGroup.
    • Method Detail

      • clear

        public void clear()
        Removes all headers.
      • addHeader

        public void addHeader​(Header header)
        Adds the given header to the group. The order in which this header was added is preserved.
        Parameters:
        header - the header to add
      • removeHeader

        public boolean removeHeader​(Header header)
        Removes the first given header.
        Parameters:
        header - the header to remove
        Returns:
        true if a header was removed as a result of this call.
      • headerEquals

        private boolean headerEquals​(Header header1,
                                     Header header2)
      • removeHeaders

        public boolean removeHeaders​(Header header)
        Removes all headers that match the given header.
        Parameters:
        header - the header to remove
        Returns:
        true if any header was removed as a result of this call.
        Since:
        5.0
      • setHeader

        public void setHeader​(Header header)
        Replaces the first occurrence of the header with the same name. If no header with the same name is found the given header is added to the end of the list.
        Parameters:
        header - the new header that should replace the first header with the same name if present in the list.
        Since:
        5.0
      • setHeaders

        public void setHeaders​(Header... headers)
        Sets all of the headers contained within this group overriding any existing headers. The headers are added in the order in which they appear in the array.
        Parameters:
        headers - the headers to set
      • getCondensedHeader

        public Header getCondensedHeader​(java.lang.String name)
        Gets a header representing all of the header values with the given name. If more that one header with the given name exists the values will be combined with a ",".

        Header name comparison is case insensitive.

        Parameters:
        name - the name of the header(s) to get
        Returns:
        a header with a condensed value or null if no headers by the given name are present
      • getHeaders

        public Header[] getHeaders​(java.lang.String name)
        Gets all of the headers with the given name. The returned array maintains the relative order in which the headers were added.

        Header name comparison is case insensitive.

        Specified by:
        getHeaders in interface MessageHeaders
        Parameters:
        name - the name of the header(s) to get
        Returns:
        an array of length ≥ 0
      • getFirstHeader

        public Header getFirstHeader​(java.lang.String name)
        Gets the first header with the given name.

        Header name comparison is case insensitive.

        Specified by:
        getFirstHeader in interface MessageHeaders
        Parameters:
        name - the name of the header to get
        Returns:
        the first header or null
      • getHeader

        public Header getHeader​(java.lang.String name)
                         throws ProtocolException
        Gets single first header with the given name.

        Header name comparison is case insensitive.

        Specified by:
        getHeader in interface MessageHeaders
        Parameters:
        name - the name of the header to get
        Returns:
        the first header or null
        Throws:
        ProtocolException - in case multiple headers with the given name are found.
      • getLastHeader

        public Header getLastHeader​(java.lang.String name)
        Gets the last header with the given name.

        Header name comparison is case insensitive.

        Specified by:
        getLastHeader in interface MessageHeaders
        Parameters:
        name - the name of the header to get
        Returns:
        the last header or null
      • getHeaders

        public Header[] getHeaders()
        Gets all of the headers contained within this group.
        Specified by:
        getHeaders in interface MessageHeaders
        Returns:
        an array of length ≥ 0
      • containsHeader

        public boolean containsHeader​(java.lang.String name)
        Tests if headers with the given name are contained within this group.

        Header name comparison is case insensitive.

        Specified by:
        containsHeader in interface MessageHeaders
        Parameters:
        name - the header name to test for
        Returns:
        true if at least one header with the name is contained, false otherwise
      • countHeaders

        public int countHeaders​(java.lang.String name)
        Checks if a certain header is present in this message and how many times.

        Header name comparison is case insensitive.

        Specified by:
        countHeaders in interface MessageHeaders
        Parameters:
        name - the header name to check for.
        Returns:
        number of occurrences of the header in the message.
      • headerIterator

        public java.util.Iterator<Header> headerIterator()
        Returns an iterator over this group of headers.
        Specified by:
        headerIterator in interface MessageHeaders
        Returns:
        iterator over this group of headers.
        Since:
        5.0
      • headerIterator

        public java.util.Iterator<Header> headerIterator​(java.lang.String name)
        Returns an iterator over the headers with a given name in this group.
        Specified by:
        headerIterator in interface MessageHeaders
        Parameters:
        name - the name of the headers over which to iterate, or null for all headers
        Returns:
        iterator over some headers in this group.
        Since:
        5.0
      • removeHeaders

        public boolean removeHeaders​(java.lang.String name)
        Removes all headers with a given name in this group.
        Parameters:
        name - the name of the headers to be removed.
        Returns:
        true if any header was removed as a result of this call.
        Since:
        5.0
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object