Class HttpHeaders
- java.lang.Object
-
- org.jboss.netty.handler.codec.http.HttpHeaders
-
- Direct Known Subclasses:
DefaultHttpHeaders
public abstract class HttpHeaders extends Object implements Iterable<Map.Entry<String,String>>
Provides the constants for the standard HTTP header names and values and commonly used utility methods that accesses anHttpMessage
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpHeaders.Names
Standard HTTP header names.static class
HttpHeaders.Values
Standard HTTP header values.
-
Field Summary
Fields Modifier and Type Field Description static HttpHeaders
EMPTY_HEADERS
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpHeaders()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract HttpHeaders
add(String name, Iterable<?> values)
Adds a new header with the specified name and values.abstract HttpHeaders
add(String name, Object value)
Adds a new header with the specified name and value.HttpHeaders
add(HttpHeaders headers)
Adds all header entries of the specifiedheaders
.static void
addDateHeader(HttpMessage message, String name, Date value)
Adds a new date header with the specified name and value.static void
addHeader(HttpMessage message, String name, Object value)
Adds a new header with the specified name and value.static void
addIntHeader(HttpMessage message, String name, int value)
Adds a new integer header with the specified name and value.abstract HttpHeaders
clear()
Removes all headers from thisHttpMessage
.static void
clearHeaders(HttpMessage message)
Removes all headers from the specified message.abstract boolean
contains(String name)
Checks to see if there is a header with the specified nameboolean
contains(String name, String value, boolean ignoreCaseValue)
Returnstrue
if a header with the name and value exists.abstract List<Map.Entry<String,String>>
entries()
Returns a newList
that contains all headers in this object.abstract String
get(String name)
Returns the value of a header with the specified name.abstract List<String>
getAll(String name)
Returns the values of headers with the specified namestatic long
getContentLength(HttpMessage message)
Returns the length of the content.static long
getContentLength(HttpMessage message, long defaultValue)
Returns the length of the content.static Date
getDate(HttpMessage message)
Returns the value of the"Date"
header.static Date
getDate(HttpMessage message, Date defaultValue)
Returns the value of the"Date"
header.static Date
getDateHeader(HttpMessage message, String name)
Returns the date header value with the specified header name.static Date
getDateHeader(HttpMessage message, String name, Date defaultValue)
Returns the date header value with the specified header name.static String
getHeader(HttpMessage message, String name)
Returns the header value with the specified header name.static String
getHeader(HttpMessage message, String name, String defaultValue)
Returns the header value with the specified header name.static String
getHost(HttpMessage message)
Returns the value of the"Host"
header.static String
getHost(HttpMessage message, String defaultValue)
Returns the value of the"Host"
header.static int
getIntHeader(HttpMessage message, String name)
Returns the integer header value with the specified header name.static int
getIntHeader(HttpMessage message, String name, int defaultValue)
Returns the integer header value with the specified header name.static boolean
is100ContinueExpected(HttpMessage message)
Returnstrue
if and only if the specified message contains the"Expect: 100-continue"
header.static boolean
isContentLengthSet(HttpMessage m)
abstract boolean
isEmpty()
Checks if no header exists.static boolean
isKeepAlive(HttpMessage message)
Returnstrue
if and only if the connection can remain open and thus 'kept alive'.static boolean
isTransferEncodingChunked(HttpMessage message)
Checks to see if the transfer encoding in a specifiedHttpMessage
is chunkedabstract Set<String>
names()
Returns a newSet
that contains the names of all headers in this object.abstract HttpHeaders
remove(String name)
Removes the header with the specified name.static void
removeHeader(HttpMessage message, String name)
Removes the header with the specified name.static void
removeTransferEncodingChunked(HttpMessage m)
abstract HttpHeaders
set(String name, Iterable<?> values)
Sets a header with the specified name and values.abstract HttpHeaders
set(String name, Object value)
Sets a header with the specified name and value.HttpHeaders
set(HttpHeaders headers)
Cleans the current header entries and copies all header entries of the specifiedheaders
.static void
set100ContinueExpected(HttpMessage message)
Sets the"Expect: 100-continue"
header to the specified message.static void
set100ContinueExpected(HttpMessage message, boolean set)
Sets or removes the"Expect: 100-continue"
header to / from the specified message.static void
setContentLength(HttpMessage message, long length)
Sets the"Content-Length"
header.static void
setDate(HttpMessage message, Date value)
Sets the"Date"
header.static void
setDateHeader(HttpMessage message, String name, Iterable<Date> values)
Sets a new date header with the specified name and values.static void
setDateHeader(HttpMessage message, String name, Date value)
Sets a new date header with the specified name and value.static void
setHeader(HttpMessage message, String name, Iterable<?> values)
Sets a new header with the specified name and values.static void
setHeader(HttpMessage message, String name, Object value)
Sets a new header with the specified name and value.static void
setHost(HttpMessage message, String value)
Sets the"Host"
header.static void
setIntHeader(HttpMessage message, String name, int value)
Sets a new integer header with the specified name and value.static void
setIntHeader(HttpMessage message, String name, Iterable<Integer> values)
Sets a new integer header with the specified name and values.static void
setKeepAlive(HttpMessage message, boolean keepAlive)
Sets the value of the"Connection"
header depending on the protocol version of the specified message.static void
setTransferEncodingChunked(HttpMessage m)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
Field Detail
-
EMPTY_HEADERS
public static final HttpHeaders EMPTY_HEADERS
-
-
Method Detail
-
isKeepAlive
public static boolean isKeepAlive(HttpMessage message)
Returnstrue
if and only if the connection can remain open and thus 'kept alive'. This methods respects the value of the"Connection"
header first and then the return value ofHttpVersion.isKeepAliveDefault()
.
-
setKeepAlive
public static void setKeepAlive(HttpMessage message, boolean keepAlive)
Sets the value of the"Connection"
header depending on the protocol version of the specified message. This method sets or removes the"Connection"
header depending on what the default keep alive mode of the message's protocol version is, as specified byHttpVersion.isKeepAliveDefault()
.- If the connection is kept alive by default:
- set to
"close"
ifkeepAlive
isfalse
. - remove otherwise.
- set to
- If the connection is closed by default:
- set to
"keep-alive"
ifkeepAlive
istrue
. - remove otherwise.
- set to
- If the connection is kept alive by default:
-
getHeader
public static String getHeader(HttpMessage message, String name)
Returns the header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value or
null
if there is no such header
-
getHeader
public static String getHeader(HttpMessage message, String name, String defaultValue)
Returns the header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value or the
defaultValue
if there is no such header
-
setHeader
public static void setHeader(HttpMessage message, String name, Object value)
Sets a new header with the specified name and value. If there is an existing header with the same name, the existing header is removed. If the specified value is not aString
, it is converted into aString
byObject.toString()
, except forDate
andCalendar
which are formatted to the date format defined in RFC2616.
-
setHeader
public static void setHeader(HttpMessage message, String name, Iterable<?> values)
Sets a new header with the specified name and values. If there is an existing header with the same name, the existing header is removed. This getMethod can be represented approximately as the following code:removeHeader(message, name); for (Object v: values) { if (v == null) { break; } addHeader(message, name, v); }
-
addHeader
public static void addHeader(HttpMessage message, String name, Object value)
-
removeHeader
public static void removeHeader(HttpMessage message, String name)
Removes the header with the specified name.
-
clearHeaders
public static void clearHeaders(HttpMessage message)
Removes all headers from the specified message.
-
getIntHeader
public static int getIntHeader(HttpMessage message, String name)
Returns the integer header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value
- Throws:
NumberFormatException
- if there is no such header or the header value is not a number
-
getIntHeader
public static int getIntHeader(HttpMessage message, String name, int defaultValue)
Returns the integer header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value or the
defaultValue
if there is no such header or the header value is not a number
-
setIntHeader
public static void setIntHeader(HttpMessage message, String name, int value)
Sets a new integer header with the specified name and value. If there is an existing header with the same name, the existing header is removed.
-
setIntHeader
public static void setIntHeader(HttpMessage message, String name, Iterable<Integer> values)
Sets a new integer header with the specified name and values. If there is an existing header with the same name, the existing header is removed.
-
addIntHeader
public static void addIntHeader(HttpMessage message, String name, int value)
Adds a new integer header with the specified name and value.
-
getDateHeader
public static Date getDateHeader(HttpMessage message, String name) throws ParseException
Returns the date header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value
- Throws:
ParseException
- if there is no such header or the header value is not a formatted date
-
getDateHeader
public static Date getDateHeader(HttpMessage message, String name, Date defaultValue)
Returns the date header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.- Returns:
- the header value or the
defaultValue
if there is no such header or the header value is not a formatted date
-
setDateHeader
public static void setDateHeader(HttpMessage message, String name, Date value)
Sets a new date header with the specified name and value. If there is an existing header with the same name, the existing header is removed. The specified value is formatted as defined in RFC2616
-
setDateHeader
public static void setDateHeader(HttpMessage message, String name, Iterable<Date> values)
Sets a new date header with the specified name and values. If there is an existing header with the same name, the existing header is removed. The specified values are formatted as defined in RFC2616
-
addDateHeader
public static void addDateHeader(HttpMessage message, String name, Date value)
Adds a new date header with the specified name and value. The specified value is formatted as defined in RFC2616
-
getContentLength
public static long getContentLength(HttpMessage message)
Returns the length of the content. Please note that this value is not retrieved fromHttpMessage.getContent()
but from the"Content-Length"
header, and thus they are independent from each other.- Returns:
- the content length
- Throws:
NumberFormatException
- if the message does not have the"Content-Length"
header or its value is not a number
-
getContentLength
public static long getContentLength(HttpMessage message, long defaultValue)
Returns the length of the content. Please note that this value is not retrieved fromHttpMessage.getContent()
but from the"Content-Length"
header, and thus they are independent from each other.- Returns:
- the content length or
defaultValue
if this message does not have the"Content-Length"
header or its value is not a number
-
setContentLength
public static void setContentLength(HttpMessage message, long length)
Sets the"Content-Length"
header.
-
getHost
public static String getHost(HttpMessage message)
Returns the value of the"Host"
header.
-
getHost
public static String getHost(HttpMessage message, String defaultValue)
Returns the value of the"Host"
header. If there is no such header, thedefaultValue
is returned.
-
setHost
public static void setHost(HttpMessage message, String value)
Sets the"Host"
header.
-
getDate
public static Date getDate(HttpMessage message) throws ParseException
Returns the value of the"Date"
header.- Throws:
ParseException
- if there is no such header or the header value is not a formatted date
-
getDate
public static Date getDate(HttpMessage message, Date defaultValue)
Returns the value of the"Date"
header. If there is no such header or the header is not a formatted date, thedefaultValue
is returned.
-
setDate
public static void setDate(HttpMessage message, Date value)
Sets the"Date"
header.
-
is100ContinueExpected
public static boolean is100ContinueExpected(HttpMessage message)
Returnstrue
if and only if the specified message contains the"Expect: 100-continue"
header.
-
set100ContinueExpected
public static void set100ContinueExpected(HttpMessage message)
Sets the"Expect: 100-continue"
header to the specified message. If there is any existing"Expect"
header, they are replaced with the new one.
-
set100ContinueExpected
public static void set100ContinueExpected(HttpMessage message, boolean set)
Sets or removes the"Expect: 100-continue"
header to / from the specified message. If the specifiedvalue
istrue
, the"Expect: 100-continue"
header is set and all other previous"Expect"
headers are removed. Otherwise, all"Expect"
headers are removed completely.
-
isTransferEncodingChunked
public static boolean isTransferEncodingChunked(HttpMessage message)
Checks to see if the transfer encoding in a specifiedHttpMessage
is chunked- Parameters:
message
- The message to check- Returns:
- True if transfer encoding is chunked, otherwise false
-
removeTransferEncodingChunked
public static void removeTransferEncodingChunked(HttpMessage m)
-
setTransferEncodingChunked
public static void setTransferEncodingChunked(HttpMessage m)
-
isContentLengthSet
public static boolean isContentLengthSet(HttpMessage m)
-
get
public abstract String get(String name)
Returns the value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.- Parameters:
name
- The name of the header to search- Returns:
- The first header value or
null
if there is no such header
-
getAll
public abstract List<String> getAll(String name)
Returns the values of headers with the specified name- Parameters:
name
- The name of the headers to search- Returns:
- A
List
of header values which will be empty if no values are found
-
entries
public abstract List<Map.Entry<String,String>> entries()
Returns a newList
that contains all headers in this object. Note that modifying the returnedList
will not affect the state of this object. If you intend to enumerate over the header entries only, useIterable.iterator()
instead, which has much less overhead.
-
contains
public abstract boolean contains(String name)
Checks to see if there is a header with the specified name- Parameters:
name
- The name of the header to search for- Returns:
- True if at least one header is found
-
isEmpty
public abstract boolean isEmpty()
Checks if no header exists.
-
names
public abstract Set<String> names()
Returns a newSet
that contains the names of all headers in this object. Note that modifying the returnedSet
will not affect the state of this object. If you intend to enumerate over the header entries only, useIterable.iterator()
instead, which has much less overhead.
-
add
public abstract HttpHeaders add(String name, Object value)
Adds a new header with the specified name and value. If the specified value is not aString
, it is converted into aString
byObject.toString()
, except in the cases ofDate
andCalendar
, which are formatted to the date format defined in RFC2616.- Parameters:
name
- The name of the header being addedvalue
- The value of the header being added- Returns:
this
-
add
public abstract HttpHeaders add(String name, Iterable<?> values)
Adds a new header with the specified name and values. This getMethod can be represented approximately as the following code:for (Object v: values) { if (v == null) { break; } headers.add(name, v); }
- Parameters:
name
- The name of the headers being setvalues
- The values of the headers being set- Returns:
this
-
add
public HttpHeaders add(HttpHeaders headers)
Adds all header entries of the specifiedheaders
.- Returns:
this
-
set
public abstract HttpHeaders set(String name, Object value)
Sets a header with the specified name and value. If there is an existing header with the same name, it is removed. If the specified value is not aString
, it is converted into aString
byObject.toString()
, except forDate
andCalendar
, which are formatted to the date format defined in RFC2616.- Parameters:
name
- The name of the header being setvalue
- The value of the header being set- Returns:
this
-
set
public abstract HttpHeaders set(String name, Iterable<?> values)
Sets a header with the specified name and values. If there is an existing header with the same name, it is removed. This getMethod can be represented approximately as the following code:headers.remove(name); for (Object v: values) { if (v == null) { break; } headers.add(name, v); }
- Parameters:
name
- The name of the headers being setvalues
- The values of the headers being set- Returns:
this
-
set
public HttpHeaders set(HttpHeaders headers)
Cleans the current header entries and copies all header entries of the specifiedheaders
.- Returns:
this
-
remove
public abstract HttpHeaders remove(String name)
Removes the header with the specified name.- Parameters:
name
- The name of the header to remove- Returns:
this
-
clear
public abstract HttpHeaders clear()
Removes all headers from thisHttpMessage
.- Returns:
this
-
contains
public boolean contains(String name, String value, boolean ignoreCaseValue)
Returnstrue
if a header with the name and value exists.- Parameters:
name
- the headernamevalue
- the valueignoreCaseValue
-true
if case should be ignored- Returns:
- contains
true
if it contains itfalse
otherwise
-
-