Class HTTPHeaderContainer
- All Implemented Interfaces:
Serializable,org.omegazero.common.util.AttachmentContainer
- Direct Known Subclasses:
HTTPMessage,HTTPMessageTrailers
This class is not thread-safe.
- Since:
- 1.2.1
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionContains the header data.Fields inherited from class org.omegazero.common.util.SimpleAttachmentContainer
attachments -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newHTTPHeaderContainerwith an empty set of headers.HTTPHeaderContainer(Map<String, String[]> headers) Creates a newHTTPHeaderContainerwith the given headers.HTTPHeaderContainer(HTTPHeaderContainer headers) Copies the givenHTTPHeaderContainer. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new header with the given key (name) and value to the end of the header list.voidAdds a new header with the given key (name) and value at the given index.voidappendHeader(String key, String value) Appends the given value to an existing header with the given key (name), separated by", ", or sets a header with the given value if no such header exists.voidappendHeader(String key, String value, String separator) Appends the given value to an existing header with the given key (name), separated by separator, or sets a header with the given value if no such header exists.voidappendHeader(String key, String value, String separator, int index) Appends the given value to an existing header with the given key (name), separated by separator, or sets a header with the given value if no such header exists.protected voidMay be implemented by subclasses to prevent changing header data in certain situations.voiddeleteHeader(String key) Deletes all headers with the given key (name).deleteHeader(String key, int index) Deletes the header with the given key (name) and index.editHeader(String key, String value, int index) Edits an existing header with the given key (name) by setting its value to the given value.extractHeader(String key) Deletes all headers with the given key (name) and returns the value of the first deleted header.String[]extractHeaders(String key) Deletes all headers with the given key (name) and returns the previous header values.static HTTPHeaderContainerfromLegacy(Map<String, String> headerData) Creates aHTTPHeaderContainerfrom the given single-valued header list.Returns the value of a header with the given key (name).Returns the value of a header with the given key (name).Returns the value of a header with the given key (name), or def if it does not exist.Returns the value of a header with the given key (name), or def if no header with the given name exists.intgetHeaderCount(String key) Returns the number of header fields with the given key (name).booleanheaderExists(String key) Checks whether there is at least one header with the given key (name).Returns an iterator iterating over each header key-value pair.intReturns the number of unique header names.Returns an unmodifiable set of all header names stored in thisHTTPHeaderContainer.headers()Returns anIterablefor use in a for-each loop.Returns an unmodifiable set of all headers stored in thisHTTPHeaderContainer.voidSets the value of the header with the given key (name) to the given value.toString()Returns a string representation of thisHTTPHeaderContainer, containing all header key-value pairs.Methods inherited from class org.omegazero.common.util.SimpleAttachmentContainer
getAttachment, hasAttachment, removeAttachment, setAttachmentMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.omegazero.common.util.AttachmentContainer
requireAttachment
-
Field Details
-
headerFields
Contains the header data. The key is the name of a header field and the value is an array of the values of all header fields with this name. The value array always contains at least one element for any existing header field name.
-
-
Constructor Details
-
HTTPHeaderContainer
public HTTPHeaderContainer()Creates a newHTTPHeaderContainerwith an empty set of headers. -
HTTPHeaderContainer
Creates a newHTTPHeaderContainerwith the given headers.- Parameters:
headers- The headers, ornullto create an empty set of headers
-
HTTPHeaderContainer
Copies the givenHTTPHeaderContainer.- Parameters:
headers- TheHTTPHeaderContainerto copy from
-
-
Method Details
-
getHeader
Returns the value of a header with the given key (name).- If there is no header with the given key,
nullis returned - If there is a single header with the given key, its value is returned
- If there are multiple headers with the given key, the value of the first header is returned
getHeader(key, 0)- Parameters:
key- The name of the HTTP header field to get- Returns:
- The value of the first header with the given name, or
nullif none exists - See Also:
- If there is no header with the given key,
-
getHeader
Returns the value of a header with the given key (name).If multiple header fields with the given name exist, the 0-based index specifies which header to retrieve. This value may be negative, in which case it specifies the offset from the end of the header value list (for example, a value of
-1indicates the last header with the given name). This value is ignored if there are no headers with the given name.- Parameters:
key- The name of the HTTP header field to getindex- The index of the header field to get- Returns:
- The value of the header with the given name and index, or
nullif it does not exist - See Also:
-
getHeader
Returns the value of a header with the given key (name), or def if no header with the given name exists. SeegetHeader(String)for more information.- Parameters:
key- The name of the HTTP header field to getdef- A value to return if a header field with the specified name does not exist- Returns:
- The value of the first header with the given name, or def if none exists
- See Also:
-
getHeader
Returns the value of a header with the given key (name), or def if it does not exist. SeegetHeader(String, int)for more information.- Parameters:
key- The name of the HTTP header field to getindex- The index of the header field to getdef- A value to return if a header field with the specified name and index does not exist- Returns:
- The value of the header with the given name and index, or def if it does not exist
- See Also:
-
extractHeader
Deletes all headers with the given key (name) and returns the value of the first deleted header.- Parameters:
key- The name of the HTTP header field(s) to delete- Returns:
- The previous value of the first header with the given name, or
nullif it did not exist
-
extractHeaders
Deletes all headers with the given key (name) and returns the previous header values. The returned array may have length0if no headers with the given name existed.- Parameters:
key- The name of the HTTP header field(s) to delete- Returns:
- The previous value(s) of the header(s) with the given name
-
setHeader
Sets the value of the header with the given key (name) to the given value. If present, all previous headers with this name are replaced by a single header with the given value, or deleted, if the given value isnull.- Parameters:
key- The name of the HTTP header field to setvalue- The value of this header field, replacing any previous headers. Ifnull, the header will be deleted
-
editHeader
Edits an existing header with the given key (name) by setting its value to the given value. The value may benull, in which case the specified header is deleted.If multiple header fields with the given name exist, the 0-based index specifies which header to edit. This value may be negative, in which case it specifies the offset from the end of the header value list (for example, a value of
-1indicates the last header with the given name).- Parameters:
key- The name of the HTTP header field to editvalue- The new value of the HTTP header fieldindex- The index of the header field to edit- Returns:
- The previous value of the header
- Throws:
ArrayIndexOutOfBoundsException- If no header with the given name and index exists- See Also:
-
addHeader
Adds a new header with the given key (name) and value to the end of the header list. This, in contrast tosetHeader(String, String), preserves any existing headers with the given name.- Parameters:
key- The name of the HTTP header fieldvalue- The new value of the HTTP header field- See Also:
-
addHeader
Adds a new header with the given key (name) and value at the given index.Any existing header at this index and subsequent headers are moved to the next higher index and other headers are preserved. Unlike other methods in this class, the only negative value the index parameter may have is
-1, in which case the new header is added at the end of the list.- Parameters:
key- The name of the HTTP header fieldvalue- The new value of the HTTP header fieldindex- The index at which to insert the new header field- Throws:
ArrayIndexOutOfBoundsException- If the index is out of range- See Also:
-
appendHeader
Appends the given value to an existing header with the given key (name), separated by", ", or sets a header with the given value if no such header exists.If multiple headers with the given name exist, only the last one is edited.
A call to this method is equivalent to a call to
appendHeader(key, value, ", ", -1)- Parameters:
key- The name of the HTTP header field to editvalue- The value to append to this header field, or the value of the header if it did not exist- See Also:
-
appendHeader
Appends the given value to an existing header with the given key (name), separated by separator, or sets a header with the given value if no such header exists.If multiple headers with the given name exist, only the last one is edited.
A call to this method is equivalent to a call to
appendHeader(key, value, separator, -1)- Parameters:
key- The name of the HTTP header field to editvalue- The value to append to this header field, or the value of the header if it did not existseparator- The separator between the existing value and the new value- See Also:
-
appendHeader
Appends the given value to an existing header with the given key (name), separated by separator, or sets a header with the given value if no such header exists.If multiple header fields with the given name exist, the 0-based index specifies which header to edit. This value may be negative, in which case it specifies the offset from the end of the header value list (for example, a value of
-1indicates the last header with the given name). This value is ignored if there are no headers with the given name.- Parameters:
key- The name of the HTTP header field to editvalue- The value to append to this header field, or the value of the header if it did not existseparator- The separator between the existing value and the new valueindex- The index of the header field to edit- Throws:
ArrayIndexOutOfBoundsException- If there exists at least one header with the given name and index is out of range- See Also:
-
getHeaderCount
Returns the number of header fields with the given key (name).- Parameters:
key- The name of the HTTP header field to search for- Returns:
- The number of header fields with the given name
-
headerExists
Checks whether there is at least one header with the given key (name).- Parameters:
key- The name of the HTTP header field to search for- Returns:
trueif a header with the given name exists
-
deleteHeader
Deletes all headers with the given key (name).A call to this method is equivalent to a call to
setHeader(key, null)- Parameters:
key- The name of the HTTP header field to delete
-
deleteHeader
Deletes the header with the given key (name) and index.A call to this method is equivalent to a call to
editHeader(key, null, index)- Parameters:
key- The name of the HTTP header field to deleteindex- The index of the header field to delete- Returns:
- The previous value of the header
-
headerNameSet
Returns an unmodifiable set of all header names stored in thisHTTPHeaderContainer.- Returns:
- An unmodifiable set of all header names
-
headerNameCount
public int headerNameCount()Returns the number of unique header names. This may not be the actual number of headers if there are multiple headers with the same name.- Returns:
- The number of unique header names
-
headerSet
Returns an unmodifiable set of all headers stored in thisHTTPHeaderContainer.- Returns:
- An unmodifiable set of all headers
-
headerIterator
Returns an iterator iterating over each header key-value pair. The same keys may occur multiple times if multiple headers with the same name exist.- Returns:
- The iterator
- See Also:
-
headers
Returns anIterablefor use in a for-each loop. The underlying iterator iterates over each header key-value pair. The same keys may occur multiple times if multiple headers with the same name exist.- Returns:
- The
Iterable - See Also:
-
checkLocked
protected void checkLocked()May be implemented by subclasses to prevent changing header data in certain situations. This method is called each time an action that would change any header data is about to be performed. To deny this action, this method should throw anIllegalStateException. -
toString
Returns a string representation of thisHTTPHeaderContainer, containing all header key-value pairs. -
fromLegacy
Creates aHTTPHeaderContainerfrom the given single-valued header list.- Parameters:
headerData- The single-valued header list- Returns:
- The header data
-