Interface for classes managing HTTP headers.
This is modeled after the class
Headers
and is provided so that the module jdk.httpserver will not be needed.
This is useful if a class is to be used with Java's internal
HTTP server and also with a servlet.-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
Add a value to a multivalued header.default String
Returns the first value (or only) value associated with a specified key.static HeaderOps
Create a new instance of this interface with a default implementation.Parse a multi-valued HTTP header.parseFirst
(String name, boolean acceptCommas) Parse a single-valued HTTP header.default void
Set the first (or only) value for a header with the specified key.Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Method Details
-
getFirst
Returns the first value (or only) value associated with a specified key.- Parameters:
key
- the key- Returns:
- the value; null if there is none
-
set
Set the first (or only) value for a header with the specified key. Existing values will be replaced.- Parameters:
key
- the keyvalue
- the value
-
add
Add a value to a multivalued header. If the header does not exist, a new entry will be created. This method can be used after callingset(String,String)
.- Parameters:
key
- the keyvalue
- the value to add
-
newInstance
Create a new instance of this interface with a default implementation.- Returns:
- the new instance of
HeaderOps
-
parseFirst
default Map<String,String> parseFirst(String name, boolean acceptCommas) throws IllegalStateException Parse a single-valued HTTP header. The result is map whose keys are the parameter names and whose values are the parameter values. As a special case, the value of the header, excluding its parameters, has the header's name as the header's key, unless it contains "=", in which case the corresponding name and value becomes the first entry in the map that is returned. Double quotes and escapes will be removed. Each key in the map is a lower-case string.For example, the header
would return a map with two entries: the key "content-type" with "text/plain" as its value, and the key "charset" with "utf-8" as its value. While generally parameters are name-value pairs, in a few cases, parameters may consist of a name without a value, in which case the value will be null. One should use the containsKey method to determine if such entries exist.content-type: text/plain; charset=utf-8
Typically a comma (unless quoted or in a comment) is treated as a delimiter separating multiple values. A few headers (e.g. cookies) use commas as part of a value.
- Parameters:
name
- the name of the headeracceptCommas
- true if commas are valid in a value or a parameter; false if commas are delimiters separating multiple values- Returns:
- a map providing the value and parameters for the header; null if there is no header with the specified name
- Throws:
IllegalStateException
- comments were not nested correctly
-
parseAll
default List<Map<String,String>> parseAll(String name, boolean acceptCommas) throws IllegalStateException Parse a multi-valued HTTP header. The result is a list of maps, where each value in the header is represented by a map whose keys are the parameter names and whose values are the parameter values. As a special case, the value of the header, excluding its parameters, has the header's name as the header's key, unless it contains "=", in which case the corresponding name and value becomes the first entry in the map that is returned. Double quotes and escapes will be removed. All keys for these maps are in lower case.For example, the header
would return a list containing two maps, each with two entries: the key "accept" with "text/plain" as its value, and the key "charset" with "utf-8" as its value. While generally parameters are name-value pairs, in a few cases, parameters may consist of a name without a value, in which case the value will be null. One should use the containsKey method to determine if such entries exist.accept: text/plain; charset=utf-8, text/html; charset=utf-8
Typically a comma (unless quoted or in a comment) is treated as a delimiter separating multiple values. A few headers (e.g. cookies) use commas as part of a value, in which case there may be multiple separate headers, all with the same name.
- Parameters:
name
- the name of the headeracceptCommas
- true if commas are valid in a value or a parameter; false if headers are delimiters separating multiple values- Returns:
- a list of maps providing the value and parameters for each of the headers multiple values; null if there is no header with the specified name
- Throws:
IllegalStateException
- comments were not nested correctly
-