public class HttpHeaders extends Object implements MultiValueMap<String,String>, Serializable
In addition to the normal methods defined by Map
, this class offers the following
convenience methods:
getFirst(String)
returns the first value associated with a given header nameadd(String, String)
adds a header value to the list of values for a header nameset(String, String)
sets the header value to a single string valueInspired by Headers
.
Constructor and Description |
---|
HttpHeaders()
Constructs a new, empty instance of the
HttpHeaders object. |
Modifier and Type | Method and Description |
---|---|
void |
add(String headerName,
String headerValue)
Add the given, single header value under the given name.
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<String,List<String>>> |
entrySet() |
boolean |
equals(Object other) |
List<String> |
get(Object key) |
List<MediaType> |
getAccept()
Return the list of acceptable media types,
as specified by the
Accept header. |
List<Charset> |
getAcceptCharset()
Return the list of acceptable charsets,
as specified by the
Accept-Charset header. |
Set<HttpMethod> |
getAllow()
Return the set of allowed
HTTP methods ,
as specified by the Allow header. |
String |
getCacheControl()
Returns the value of the
Cache-Control header. |
List<String> |
getConnection()
Returns the value of the
Connection header. |
long |
getContentLength()
Return the length of the body in bytes, as specified by the
Content-Length header. |
MediaType |
getContentType()
Return the media type of the body, as specified
by the
Content-Type header. |
long |
getDate()
Return the date and time at which the message was created, as specified
by the
Date header. |
String |
getETag()
Return the entity tag of the body, as specified by the
ETag header. |
long |
getExpires()
Return the date and time at which the message is no longer valid,
as specified by the
Expires header. |
String |
getFirst(String headerName)
Return the first header value for the given header name, if any.
|
long |
getFirstDate(String headerName)
Parse the first header value for the given header name as a date,
return -1 if there is no value, or raise
IllegalArgumentException
if the value cannot be parsed as a date. |
protected List<String> |
getFirstValueAsList(String header) |
long |
getIfModifiedSince()
Return the value of the
If-Modified-Since header. |
List<String> |
getIfNoneMatch()
Return the value of the
If-None-Match header. |
long |
getIfNotModifiedSince()
Deprecated.
|
long |
getLastModified()
Return the time the resource was last changed, as specified by the
Last-Modified header. |
URI |
getLocation()
Return the (new) location of a resource
as specified by the
Location header. |
String |
getOrigin()
Return the value of the
Origin header. |
String |
getPragma()
Return the value of the
Pragma header. |
String |
getUpgrade()
Returns the value of the
Upgrade header. |
int |
hashCode() |
boolean |
isEmpty() |
Set<String> |
keySet() |
List<String> |
put(String key,
List<String> value) |
void |
putAll(Map<? extends String,? extends List<String>> map) |
static HttpHeaders |
readOnlyHttpHeaders(HttpHeaders headers)
Return a
HttpHeaders object that can only be read, not written to. |
List<String> |
remove(Object key) |
void |
set(String headerName,
String headerValue)
Set the given, single header value under the given name.
|
void |
setAccept(List<MediaType> acceptableMediaTypes)
Set the list of acceptable media types,
as specified by the
Accept header. |
void |
setAcceptCharset(List<Charset> acceptableCharsets)
Set the list of acceptable charsets,
as specified by the
Accept-Charset header. |
void |
setAll(Map<String,String> values)
Set the given values under.
|
void |
setAllow(Set<HttpMethod> allowedMethods)
Set the set of allowed
HTTP methods ,
as specified by the Allow header. |
void |
setCacheControl(String cacheControl)
Set the (new) value of the
Cache-Control header. |
void |
setConnection(List<String> connection)
Set the (new) value of the
Connection header. |
void |
setConnection(String connection)
Set the (new) value of the
Connection header. |
void |
setContentDispositionFormData(String name,
String filename)
Set the (new) value of the
Content-Disposition header
for form-data . |
void |
setContentLength(long contentLength)
Set the length of the body in bytes, as specified by the
Content-Length header. |
void |
setContentType(MediaType mediaType)
Set the media type of the body,
as specified by the
Content-Type header. |
void |
setDate(long date)
Set the date and time at which the message was created, as specified
by the
Date header. |
void |
setDate(String headerName,
long date)
Set the given date under the given header name after formatting it as a string
using the pattern
"EEE, dd MMM yyyy HH:mm:ss zzz" . |
void |
setETag(String eTag)
Set the (new) entity tag of the body, as specified by the
ETag header. |
void |
setExpires(long expires)
Set the date and time at which the message is no longer valid,
as specified by the
Expires header. |
void |
setIfModifiedSince(long ifModifiedSince)
Set the (new) value of the
If-Modified-Since header. |
void |
setIfNoneMatch(List<String> ifNoneMatchList)
Set the (new) values of the
If-None-Match header. |
void |
setIfNoneMatch(String ifNoneMatch)
Set the (new) value of the
If-None-Match header. |
void |
setLastModified(long lastModified)
Set the time the resource was last changed, as specified by the
Last-Modified header. |
void |
setLocation(URI location)
Set the (new) location of a resource,
as specified by the
Location header. |
void |
setOrigin(String origin)
Set the (new) value of the
Origin header. |
void |
setPragma(String pragma)
Set the (new) value of the
Pragma header. |
void |
setUpgrade(String upgrade)
Set the (new) value of the
Upgrade header. |
int |
size() |
protected String |
toCommaDelimitedString(List<String> list) |
Map<String,String> |
toSingleValueMap()
Returns the first values contained in this
MultiValueMap . |
String |
toString() |
Collection<List<String>> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public HttpHeaders()
HttpHeaders
object.public void setAccept(List<MediaType> acceptableMediaTypes)
Accept
header.public List<MediaType> getAccept()
Accept
header.
Returns an empty list when the acceptable media types are unspecified.
public void setAcceptCharset(List<Charset> acceptableCharsets)
Accept-Charset
header.public List<Charset> getAcceptCharset()
Accept-Charset
header.public void setAllow(Set<HttpMethod> allowedMethods)
HTTP methods
,
as specified by the Allow
header.public Set<HttpMethod> getAllow()
HTTP methods
,
as specified by the Allow
header.
Returns an empty set when the allowed methods are unspecified.
public void setCacheControl(String cacheControl)
Cache-Control
header.public String getCacheControl()
Cache-Control
header.public void setConnection(String connection)
Connection
header.public void setConnection(List<String> connection)
Connection
header.public void setContentDispositionFormData(String name, String filename)
Content-Disposition
header
for form-data
.name
- the control namefilename
- the filename (may be null
)public void setContentLength(long contentLength)
Content-Length
header.public long getContentLength()
Content-Length
header.
Returns -1 when the content-length is unknown.
public void setContentType(MediaType mediaType)
Content-Type
header.public MediaType getContentType()
Content-Type
header.
Returns null
when the content-type is unknown.
public void setDate(long date)
Date
header.
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
public long getDate()
Date
header.
The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
IllegalArgumentException
- if the value can't be converted to a datepublic void setETag(String eTag)
ETag
header.public String getETag()
ETag
header.public void setExpires(long expires)
Expires
header.
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
public long getExpires()
Expires
header.
The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
public void setIfModifiedSince(long ifModifiedSince)
If-Modified-Since
header.
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
@Deprecated public long getIfNotModifiedSince()
getIfModifiedSince()
IfModifiedSince
header.
The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
public long getIfModifiedSince()
If-Modified-Since
header.
The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
public void setIfNoneMatch(String ifNoneMatch)
If-None-Match
header.public void setIfNoneMatch(List<String> ifNoneMatchList)
If-None-Match
header.public void setLastModified(long lastModified)
Last-Modified
header.
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
public long getLastModified()
Last-Modified
header.
The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown.
public void setLocation(URI location)
Location
header.public URI getLocation()
Location
header.
Returns null
when the location is unknown.
public void setOrigin(String origin)
Origin
header.public String getOrigin()
Origin
header.public void setPragma(String pragma)
Pragma
header.public String getPragma()
Pragma
header.public void setUpgrade(String upgrade)
Upgrade
header.public String getUpgrade()
Upgrade
header.public long getFirstDate(String headerName)
IllegalArgumentException
if the value cannot be parsed as a date.public void setDate(String headerName, long date)
"EEE, dd MMM yyyy HH:mm:ss zzz"
. The equivalent of
set(String, String)
but for date headers.public String getFirst(String headerName)
getFirst
in interface MultiValueMap<String,String>
headerName
- the header namenull
if nonepublic void add(String headerName, String headerValue)
add
in interface MultiValueMap<String,String>
headerName
- the header nameheaderValue
- the header valueUnsupportedOperationException
- if adding headers is not supportedput(String, List)
,
set(String, String)
public void set(String headerName, String headerValue)
set
in interface MultiValueMap<String,String>
headerName
- the header nameheaderValue
- the header valueUnsupportedOperationException
- if adding headers is not supportedput(String, List)
,
add(String, String)
public void setAll(Map<String,String> values)
MultiValueMap
setAll
in interface MultiValueMap<String,String>
values
- the values.public Map<String,String> toSingleValueMap()
MultiValueMap
MultiValueMap
.toSingleValueMap
in interface MultiValueMap<String,String>
public boolean containsKey(Object key)
containsKey
in interface Map<String,List<String>>
public boolean containsValue(Object value)
containsValue
in interface Map<String,List<String>>
public Collection<List<String>> values()
public boolean equals(Object other)
public int hashCode()
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers)
HttpHeaders
object that can only be read, not written to.