Package io.muserver

Interface Headers

  • All Superinterfaces:
    java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.String>>

    public interface Headers
    extends java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.String>>
    HTTP headers
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Deprecated Methods 
      Modifier and Type Method Description
      java.util.List<ParameterizedHeaderWithValue> accept()
      Gets the Accept-Charset header value.
      java.util.List<ParameterizedHeaderWithValue> acceptCharset()
      Gets the Accept header value.
      java.util.List<ParameterizedHeaderWithValue> acceptEncoding()
      Gets the Accept-Encoding header value.
      java.util.List<ParameterizedHeaderWithValue> acceptLanguage()
      Gets the Accept-Language header value.
      Headers add​(Headers headers)
      Adds all headers from another headers object to this
      Headers add​(java.lang.CharSequence name, java.lang.Iterable<?> values)
      Adds an item to these headers.
      Headers add​(java.lang.CharSequence name, java.lang.Object value)
      Adds an item to these headers.
      Headers add​(java.lang.String name, java.lang.Iterable<?> values)
      Adds an item to these headers.
      Headers add​(java.lang.String name, java.lang.Object value)
      Adds an item to these headers.
      Headers addInt​(java.lang.CharSequence name, int value)
      Adds an integer value
      Headers addShort​(java.lang.CharSequence name, short value)
      Deprecated.
      use an alternative such as addInt(CharSequence, int)
      ParameterizedHeader cacheControl()
      Gets the Cache-Control header value.
      Headers clear()  
      boolean contains​(java.lang.CharSequence name)
      Returns true if the given parameter is specified with any value
      boolean contains​(java.lang.CharSequence name, java.lang.CharSequence value, boolean ignoreCase)
      Checks if a header with the given name exists with the given value.
      boolean contains​(java.lang.String name)
      Returns true if the given parameter is specified with any value
      boolean contains​(java.lang.String name, java.lang.String value, boolean ignoreCase)
      Checks if a header with the given name exists with the given value.
      boolean containsValue​(java.lang.CharSequence name, java.lang.CharSequence value, boolean ignoreCase)
      Similar to contains(String, String, boolean) but returns true even if a value occurs in a comma-separated header list.
      javax.ws.rs.core.MediaType contentType()
      Gets the parsed Content-Type header value.
      java.util.List<java.util.Map.Entry<java.lang.String,​java.lang.String>> entries()  
      java.util.List<ForwardedHeader> forwarded()
      Gets the Forwarded header value.
      java.lang.String get​(java.lang.CharSequence name)
      Gets the value with the given name, or null if there is no parameter with that name.
      java.lang.String get​(java.lang.CharSequence name, java.lang.String defaultValue)
      Gets the value with the given name, or the default value if there is no parameter with that name.
      java.lang.String get​(java.lang.String name)
      Gets the value with the given name, or null if there is no parameter with that name.
      java.util.List<java.lang.String> getAll​(java.lang.CharSequence name)
      Gets all the parameters with the given name, or an empty list if none are found.
      java.util.List<java.lang.String> getAll​(java.lang.String name)
      Gets all the parameters with the given name, or an empty list if none are found.
      java.util.List<java.lang.String> getAllAsString​(java.lang.CharSequence name)
      Deprecated.
      use an alternative
      java.lang.String getAsString​(java.lang.CharSequence name)
      Deprecated.
      use an alternative
      boolean getBoolean​(java.lang.String name)
      Gets a parameter as a boolean, where values such as true, on and yes as considered true, and other values (or no parameter with the name) is considered false.
      double getDouble​(java.lang.String name, double defaultValue)
      Gets the parameter as a double, or returns the default value if it was not specified or was in an invalid format.
      float getFloat​(java.lang.String name, float defaultValue)
      Gets the parameter as a float, or returns the default value if it was not specified or was in an invalid format.
      java.lang.Integer getInt​(java.lang.CharSequence name)
      Deprecated.
      int getInt​(java.lang.CharSequence name, int defaultValue)
      Gets the parameter as an integer, or returns the default value if it was not specified or was in an invalid format.
      long getLong​(java.lang.String name, long defaultValue)
      Gets the parameter as a long, or returns the default value if it was not specified or was in an invalid format.
      java.lang.Short getShort​(java.lang.CharSequence name)
      Deprecated.
      Please use an alternate accessor
      short getShort​(java.lang.CharSequence name, short defaultValue)
      Deprecated.
      Please use an alternate accessor
      java.lang.Long getTimeMillis​(java.lang.CharSequence name)
      Gets a date header.
      long getTimeMillis​(java.lang.CharSequence name, long defaultValue)
      Gets a date header.
      boolean hasBody()
      Returns true if the headers suggest there is a message body by checking if there is a HeaderNames.TRANSFER_ENCODING header or the HeaderNames.CONTENT_LENGTH is greater than 0.
      static Headers http1Headers()
      Creates new headers for HTTP1 requests
      static Headers http2Headers()
      Creates new headers for HTTP2 requests
      boolean isEmpty()  
      java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.String>> iterator()  
      java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.String>> iteratorAsString()
      Deprecated.
      use an alternative
      java.util.Set<java.lang.String> names()  
      Headers remove​(java.lang.CharSequence name)
      Removes a header.
      Headers remove​(java.lang.String name)
      Removes a header.
      Headers set​(Headers headers)
      Removes all the current headers and adds them from the given headers.
      Headers set​(java.lang.CharSequence name, java.lang.Iterable<?> values)
      Sets a header value list.
      Headers set​(java.lang.CharSequence name, java.lang.Object value)
      Sets a header value.
      Headers set​(java.lang.String name, java.lang.Iterable<?> values)
      Sets a header value list.
      Headers set​(java.lang.String name, java.lang.Object value)
      Sets a header value.
      Headers setAll​(Headers headers)
      Sets all the values from the given headers object, overwriting any existing headers with the same names.
      Headers setInt​(java.lang.CharSequence name, int value)
      Sets the given integer value, replacing the existing value if it is already set.
      Headers setShort​(java.lang.CharSequence name, short value)
      Deprecated.
      int size()  
      java.lang.String toString()
      Returns a string representation of the headers.
      java.lang.String toString​(java.util.Collection<java.lang.String> toSuppress)
      Returns a string representation of the headers with selected header values replaced with the string (hidden).
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • get

        java.lang.String get​(java.lang.String name)

        Gets the value with the given name, or null if there is no parameter with that name.

        If there are multiple parameters with the same name, the first one is returned.

        Parameters:
        name - The name of the parameter to get
        Returns:
        The value, or null
      • get

        java.lang.String get​(java.lang.CharSequence name)

        Gets the value with the given name, or null if there is no parameter with that name.

        If there are multiple parameters with the same name, the first one is returned.

        Parameters:
        name - The name of the parameter to get
        Returns:
        The value, or null
      • get

        java.lang.String get​(java.lang.CharSequence name,
                             java.lang.String defaultValue)

        Gets the value with the given name, or the default value if there is no parameter with that name.

        If there are multiple parameters with the same name, the first one is returned.

        Parameters:
        name - The name of the parameter to get
        defaultValue - The default value to use if there is no given value
        Returns:
        The value of the parameter, or the default value
      • getInt

        @Deprecated
        java.lang.Integer getInt​(java.lang.CharSequence name)
        Deprecated.
        Parameters:
        name - The name
        Returns:
        The value
      • getInt

        int getInt​(java.lang.CharSequence name,
                   int defaultValue)
        Gets the parameter as an integer, or returns the default value if it was not specified or was in an invalid format.
        Parameters:
        name - The name of the parameter.
        defaultValue - The value to use if none was specified, or an invalid format was used.
        Returns:
        Returns the parameter value as an integer.
      • getLong

        long getLong​(java.lang.String name,
                     long defaultValue)
        Gets the parameter as a long, or returns the default value if it was not specified or was in an invalid format.
        Parameters:
        name - The name of the parameter.
        defaultValue - The value to use if none was specified, or an invalid format was used.
        Returns:
        Returns the parameter value as a long.
      • getFloat

        float getFloat​(java.lang.String name,
                       float defaultValue)
        Gets the parameter as a float, or returns the default value if it was not specified or was in an invalid format.
        Parameters:
        name - The name of the parameter.
        defaultValue - The value to use if none was specified, or an invalid format was used.
        Returns:
        Returns the parameter value as a float.
      • getDouble

        double getDouble​(java.lang.String name,
                         double defaultValue)
        Gets the parameter as a double, or returns the default value if it was not specified or was in an invalid format.
        Parameters:
        name - The name of the parameter.
        defaultValue - The value to use if none was specified, or an invalid format was used.
        Returns:
        Returns the parameter value as a double.
      • getBoolean

        boolean getBoolean​(java.lang.String name)

        Gets a parameter as a boolean, where values such as true, on and yes as considered true, and other values (or no parameter with the name) is considered false.

        This can be used to access checkbox values as booleans.

        Parameters:
        name - The name of the parameter.
        Returns:
        Returns true if the value was truthy, or false if it was falsy or not specified.
      • getShort

        @Deprecated
        java.lang.Short getShort​(java.lang.CharSequence name)
        Deprecated.
        Please use an alternate accessor
        Deprecated
        Parameters:
        name - The name
        Returns:
        The value
      • getShort

        @Deprecated
        short getShort​(java.lang.CharSequence name,
                       short defaultValue)
        Deprecated.
        Please use an alternate accessor
        Deprecated
        Parameters:
        name - The name
        defaultValue - Default
        Returns:
        The value
      • getTimeMillis

        java.lang.Long getTimeMillis​(java.lang.CharSequence name)
        Gets a date header.

        This converts the string date values into milliseconds

        Parameters:
        name - The header name
        Returns:
        The value in milliseconds of the date header, or null if not found
      • getTimeMillis

        long getTimeMillis​(java.lang.CharSequence name,
                           long defaultValue)
        Gets a date header.

        This converts the string date values into milliseconds

        Parameters:
        name - The header name
        defaultValue - The default to use if no date header is available
        Returns:
        The value in milliseconds of the date header, or the default if not found
      • getAll

        java.util.List<java.lang.String> getAll​(java.lang.String name)
        Gets all the parameters with the given name, or an empty list if none are found.
        Parameters:
        name - The parameter name to get
        Returns:
        All values of the parameter with the given name
      • getAll

        java.util.List<java.lang.String> getAll​(java.lang.CharSequence name)
        Gets all the parameters with the given name, or an empty list if none are found.
        Parameters:
        name - The parameter name to get
        Returns:
        All values of the parameter with the given name
      • entries

        java.util.List<java.util.Map.Entry<java.lang.String,​java.lang.String>> entries()
        Returns:
        All the headers
      • contains

        boolean contains​(java.lang.String name)
        Returns true if the given parameter is specified with any value
        Parameters:
        name - The name of the value
        Returns:
        True if it's specified; otherwise false.
      • contains

        boolean contains​(java.lang.CharSequence name)
        Returns true if the given parameter is specified with any value
        Parameters:
        name - The name of the value
        Returns:
        True if it's specified; otherwise false.
      • iterator

        java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.String>> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.String>>
        Returns:
        An iterator to iterate through the headers
      • isEmpty

        boolean isEmpty()
        Returns:
        True if there are no headers
      • size

        int size()
        Returns:
        The number of headers. Repeated headers are counted twice.
      • names

        java.util.Set<java.lang.String> names()
        Returns:
        The header names in these headers
      • add

        Headers add​(java.lang.String name,
                    java.lang.Object value)
        Adds an item to these headers. If a header with the given name already exists then this value is added rather than replaced.
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • add

        Headers add​(java.lang.CharSequence name,
                    java.lang.Object value)
        Adds an item to these headers. If a header with the given name already exists then this value is added rather than replaced.
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • add

        Headers add​(java.lang.String name,
                    java.lang.Iterable<?> values)
        Adds an item to these headers. If a header with the given name already exists then this value is added rather than replaced.
        Parameters:
        name - The header name
        values - The values
        Returns:
        This headers object
      • add

        Headers add​(java.lang.CharSequence name,
                    java.lang.Iterable<?> values)
        Adds an item to these headers. If a header with the given name already exists then this value is added rather than replaced.
        Parameters:
        name - The header name
        values - The value
        Returns:
        This headers object
      • add

        Headers add​(Headers headers)
        Adds all headers from another headers object to this
        Parameters:
        headers - The headers to add
        Returns:
        This headers object
      • addInt

        Headers addInt​(java.lang.CharSequence name,
                       int value)
        Adds an integer value
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • addShort

        @Deprecated
        Headers addShort​(java.lang.CharSequence name,
                         short value)
        Deprecated.
        use an alternative such as addInt(CharSequence, int)
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • set

        Headers set​(java.lang.String name,
                    java.lang.Object value)
        Sets a header value. If a header with the given name already exists then this replaces it.
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • set

        Headers set​(java.lang.CharSequence name,
                    java.lang.Object value)
        Sets a header value. If a header with the given name already exists then this replaces it.
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • set

        Headers set​(java.lang.String name,
                    java.lang.Iterable<?> values)
        Sets a header value list. If a header with the given name already exists then this replaces it.
        Parameters:
        name - The header name
        values - The value
        Returns:
        This headers object
      • set

        Headers set​(java.lang.CharSequence name,
                    java.lang.Iterable<?> values)
        Sets a header value list. If a header with the given name already exists then this replaces it.
        Parameters:
        name - The header name
        values - The value
        Returns:
        This headers object
      • set

        Headers set​(Headers headers)
        Removes all the current headers and adds them from the given headers.
        Parameters:
        headers - The headers to use
        Returns:
        This headers object
      • setAll

        Headers setAll​(Headers headers)
        Sets all the values from the given headers object, overwriting any existing headers with the same names.
        Parameters:
        headers - The headers to use
        Returns:
        This headers object
      • setInt

        Headers setInt​(java.lang.CharSequence name,
                       int value)
        Sets the given integer value, replacing the existing value if it is already set.
        Parameters:
        name - The header name
        value - The value to set
        Returns:
        This headers object
      • setShort

        @Deprecated
        Headers setShort​(java.lang.CharSequence name,
                         short value)
        Deprecated.
        Parameters:
        name - The header name
        value - The value
        Returns:
        This headers object
      • remove

        Headers remove​(java.lang.String name)
        Removes a header. Does nothing if the header is not set.
        Parameters:
        name - The header to remove
        Returns:
        This headers object
      • remove

        Headers remove​(java.lang.CharSequence name)
        Removes a header. Does nothing if the header is not set.
        Parameters:
        name - The header to remove
        Returns:
        This headers object
      • clear

        Headers clear()
        Returns:
        Removes all the headers from this object
      • contains

        boolean contains​(java.lang.String name,
                         java.lang.String value,
                         boolean ignoreCase)
        Checks if a header with the given name exists with the given value.
        Parameters:
        name - The case-insensitive header name to check.
        value - The value to check for.
        ignoreCase - If true, the case of the value is ignored.
        Returns:
        True if a header name with the given value exists.
      • contains

        boolean contains​(java.lang.CharSequence name,
                         java.lang.CharSequence value,
                         boolean ignoreCase)
        Checks if a header with the given name exists with the given value.
        Parameters:
        name - The case-insensitive header name to check.
        value - The value to check for.
        ignoreCase - If true, the case of the value is ignored.
        Returns:
        True if a header name with the given value exists.
      • containsValue

        boolean containsValue​(java.lang.CharSequence name,
                              java.lang.CharSequence value,
                              boolean ignoreCase)
        Similar to contains(String, String, boolean) but returns true even if a value occurs in a comma-separated header list.
        Parameters:
        name - The case-insensitive header name to check.
        value - The value to check for.
        ignoreCase - If true, the case of the value is ignored.
        Returns:
        True if a header name with the given value exists.
      • getAsString

        @Deprecated
        java.lang.String getAsString​(java.lang.CharSequence name)
        Deprecated.
        use an alternative
        Parameters:
        name - The header name
        Returns:
        This headers object
      • getAllAsString

        @Deprecated
        java.util.List<java.lang.String> getAllAsString​(java.lang.CharSequence name)
        Deprecated.
        use an alternative
        Parameters:
        name - The header name
        Returns:
        This headers object
      • iteratorAsString

        @Deprecated
        java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.String>> iteratorAsString()
        Deprecated.
        use an alternative
        Returns:
        Am iterator
      • accept

        java.util.List<ParameterizedHeaderWithValue> accept()

        Gets the Accept-Charset header value.

        For example, if a client sends text/html,application/xml;q=0.9 then this would return a list of two values: text/html, and application/xml where application/xml has a parameter q of value 0.9

        Returns:
        Returns a parsed Accept header, or an empty list if none specified.
      • acceptCharset

        java.util.List<ParameterizedHeaderWithValue> acceptCharset()

        Gets the Accept header value.

        For example, if a client sends iso-8859-5, unicode-1-1;q=0.8 then this would return a list of two values: iso-8859-5, and unicode-1-1 where unicode-1-1 has a parameter q of value 0.8

        Returns:
        Returns a parsed Accept-Charset header, or an empty list if none specified.
      • acceptEncoding

        java.util.List<ParameterizedHeaderWithValue> acceptEncoding()

        Gets the Accept-Encoding header value.

        For example, if a client sends gzip, deflate then this would return a list of two values: gzip and deflate

        Returns:
        Returns a parsed Accept-Encoding header, or an empty list if none specified.
      • forwarded

        java.util.List<ForwardedHeader> forwarded()

        Gets the Forwarded header value.

        For example, if a client sends for=192.0.2.60;proto=http;by=203.0.113.43 then this would return a list of length one that has for, proto, and by values.

        If there is no Forwarded header, however there are X-Forwarded-* headers, then those will be used to generate pseudo-forwarded headers.

        Returns:
        Returns a parsed Forwarded header, or an empty list if none specified.
      • acceptLanguage

        java.util.List<ParameterizedHeaderWithValue> acceptLanguage()

        Gets the Accept-Language header value.

        For example, if a client sends en-US,en;q=0.5 then this would return a list of two values: en-US and en where en has a q value of 0.5

        Returns:
        Returns a parsed Accept-Language header, or an empty list if none specified.
      • cacheControl

        ParameterizedHeader cacheControl()
        Gets the Cache-Control header value.
        Returns:
        A map of cache control directives to their optional values. If no cache-control is in the header, then the resulting map will be empty.
      • contentType

        javax.ws.rs.core.MediaType contentType()
        Gets the parsed Content-Type header value.
        Returns:
        The media type of the content specified by the headers, or null if not set.
      • toString

        java.lang.String toString()
        Returns a string representation of the headers. Note: The following headers will have their actual values replaced with the string (hidden) in order to protect potentially sensitive information: authorization, cookie and set-cookie.

        If you wish to print all values or customize the header values that are hidden, use toString(Collection)

        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of these headers
      • toString

        java.lang.String toString​(java.util.Collection<java.lang.String> toSuppress)
        Returns a string representation of the headers with selected header values replaced with the string (hidden).

        This may be useful where headers are logged for diagnostic purposes while not revealing values that are held in potentially sensitive headers.

        Parameters:
        toSuppress - A collection of case-insensitive header names which will not have their values printed. Pass an empty collection to print all header values. A null value will hide the header values as defined on toString().
        Returns:
        a string representation of these headers
      • http1Headers

        static Headers http1Headers()
        Creates new headers for HTTP1 requests
        Returns:
        An empty headers object.
      • http2Headers

        static Headers http2Headers()
        Creates new headers for HTTP2 requests
        Returns:
        An empty headers object.