Package nl.colorize.util.http
Class Headers
java.lang.Object
nl.colorize.util.http.Headers
Immutable representation of the request/response headers sent as part of an
HTTP request. Used in conjunction with
URLLoader
, which supports a
variety of HTTP clients depending on the platform.
HTTP headers are sometimes represented using a Map<String, String>
,
but this does not allow for the following:
- Headers have an explicitly defined order.
- The same header can occur multiple times.
- Header names are case-insensitive.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
forEach
(BiConsumer<String, String> callback) static Headers
Creates aHeaders
instance from a map containing the name/value pairs of the HTTP headers.Returns the value of the header with the specified name.Returns all values for headers with the specified name.Iterates over header names.iterator()
static Headers
none()
Returns an emptyHeaders
instance that indicates the HTTP request or response does not contain any headers.static Headers
Creates aHeaders
instance from the specified HTTP header names and values.toString()
static void
validateHeader
(String name, String value) Helper method that validates if the specified name and value should be allowed as a HTTP headers.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Headers
Creates a newHeaders
instance from the specified list of HTTP header names/values. This class also defines a number of static factory methods for creating aHeaders
instance from other sources.- Throws:
IllegalArgumentException
- if any of the header names and/or values are invalid.validateHeader(String, String)
is used to perform this validation.
-
-
Method Details
-
getHeaderNames
Iterates over header names. Note that header names can appear multiple times in the list, if there are multiple occurrences of that header. -
get
Returns the value of the header with the specified name. If the header occurs multiple times, the first occurrence is returned. -
getAll
Returns all values for headers with the specified name. If the header is not present, this will return an empty list. -
iterator
-
forEach
-
toString
-
of
Creates aHeaders
instance from the specified HTTP header names and values. Therest
parameter expects values in the ordername1, value1, name2, value2, ...
.- Throws:
IllegalArgumentException
- When an odd number of arguments is passed to the method.
-
fromMap
Creates aHeaders
instance from a map containing the name/value pairs of the HTTP headers. The order of the headers is based on the map's iteration order. -
none
Returns an emptyHeaders
instance that indicates the HTTP request or response does not contain any headers. -
validateHeader
Helper method that validates if the specified name and value should be allowed as a HTTP headers.- Throws:
IllegalArgumentException
- if the validation fails for the header name and/or value.
-