Package com.helger.commons.http
Class HttpHeaderMap
java.lang.Object
com.helger.commons.http.HttpHeaderMap
- All Implemented Interfaces:
ICommonsIterable<Map.Entry<String,,ICommonsList<String>>> ICloneable<HttpHeaderMap>,IHasSize,IClearable,Iterable<Map.Entry<String,ICommonsList<String>>>
@NotThreadSafe
public class HttpHeaderMap
extends Object
implements IHasSize, ICommonsIterable<Map.Entry<String,ICommonsList<String>>>, ICloneable<HttpHeaderMap>, IClearable
Abstracts HTTP header interface for external usage.
Note: since version 9.1.8 (issue #11) the internal scheme changed and the original case is stored. Older versions always stored the lower case header names. The implications are that the casing of the first header is sustained. So if the first API call uses name "Foo" and the second is "foo" they both refer to the same header name and "Foo" will be the name that is retrieved.
Note: since version 9.1.8 (issue #11) the internal scheme changed and the original case is stored. Older versions always stored the lower case header names. The implications are that the casing of the first header is sustained. So if the first API call uses name "Foo" and the second is "foo" they both refer to the same header name and "Foo" will be the name that is retrieved.
- Since:
- 9.0.0
- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanDefault quote if necessary: false (since v10)static final StringThe separator between key and value -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllHeaders(HttpHeaderMap aOther) Add all headers from the passed map.voidaddDateHeader(String sName, long nMillis) Add the passed header as a date header.voidaddDateHeader(String sName, LocalDate aLD) Add the passed header as a date header.voidaddDateHeader(String sName, LocalDateTime aLDT) Add the passed header as a date header.voidaddDateHeader(String sName, ZonedDateTime aDT) Add the passed header as a date header.voidAdd the passed header as is.voidaddIntHeader(String sName, int nValue) Add the passed header as a number.voidaddLongHeader(String sName, long nValue) Add the passed header as a number.booleancontainsHeaders(String sName) booleanvoidforEachHeaderLine(Consumer<? super String> aConsumer, boolean bUnifyValue) Invoke the provided consumer for every header line.voidforEachHeaderLine(Consumer<? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every header line.voidforEachSingleHeader(BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue) Invoke the provided consumer for every name/value pair.voidforEachSingleHeader(BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every name/value pair.getAllHeaderLines(boolean bUnifyValue) Get all header lines as a list of strings.getAllHeaderLines(boolean bUnifyValue, boolean bQuoteIfNecessary) Get all header lines as a list of strings.getAllHeaderValues(String sName) Get all header values of a certain header name.getClone()static Stringstatic StringgetFirstHeaderValue(String sName) Get the first header value of a certain header name.getHeaderCombined(String sName, String sDelimiter) Get the header value as a combination of all contained values.static StringgetUnifiedValue(String sValue) Avoid having header values spanning multiple lines.static StringgetUnifiedValue(String sValue, boolean bQuoteIfNecessary) Avoid having header values spanning multiple lines.inthashCode()booleanisEmpty()iterator()Remove all contained headers.removeHeader(String sName, String sValue) removeHeaders(String sName) Remove all header values with the provided nameremoveHeadersIf(Predicate<? super String> aNameFilter) Remove all header values where the name matches the provided filter.voidsetAllHeaders(HttpHeaderMap aOther) Set all headers from the passed map.voidsetContentLength(long nLength) voidsetContentType(String sContentType) voidsetDateHeader(String sName, long nMillis) Set the passed header as a date header.voidsetDateHeader(String sName, LocalDate aLD) Set the passed header as a date header.voidsetDateHeader(String sName, LocalDateTime aLDT) Set the passed header as a date header.voidsetDateHeader(String sName, ZonedDateTime aDT) Set the passed header as a date header.voidSet the passed header as is.voidsetIntHeader(String sName, int nValue) Set the passed header as a number.voidsetLongHeader(String sName, long nValue) Set the passed header as a number.intsize()toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.commons.collection.impl.ICommonsIterable
containsAny, containsNone, containsOnly, findAll, findAllInstanceOf, findAllMapped, findAllMapped, findAllMapped, findFirst, findFirst, findFirstIndex, findFirstMapped, findFirstMapped, findLastIndex, forEachBreakable, forEachByIndex, forEachThrowing, getCount, getCountMethods inherited from interface com.helger.commons.lang.IHasSize
isNotEmptyMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
SEPARATOR_KEY_VALUE
The separator between key and value- See Also:
-
DEFAULT_QUOTE_IF_NECESSARY
public static final boolean DEFAULT_QUOTE_IF_NECESSARYDefault quote if necessary: false (since v10)- See Also:
-
-
Constructor Details
-
HttpHeaderMap
public HttpHeaderMap()Default constructor. -
HttpHeaderMap
Copy constructor.- Parameters:
aOther- Map to copy from. May not benull.
-
-
Method Details
-
getUnifiedValue
Avoid having header values spanning multiple lines. This has been deprecated by RFC 7230 and Jetty 9.3 refuses to parse these requests with HTTP 400 by default.
Since v9.3.6 this method also takes care of quoting header values correctly. If the value does not correspond to a token according to RFC 2616 chapter 2.2, the value is enclosed in double quotes.- Parameters:
sValue- The source header value. May benull.- Returns:
- The unified header value without \r, \n and \t. Never
null.
-
getUnifiedValue
Avoid having header values spanning multiple lines. This has been deprecated by RFC 7230 and Jetty 9.3 refuses to parse these requests with HTTP 400 by default.- Parameters:
sValue- The source header value. May benull.bQuoteIfNecessary-trueif automatic quoting according to RFC 2616, chapter 2.2 should be used if necessary.- Returns:
- The unified header value without \r, \n and \t. Never
null. - Since:
- 9.3.6
-
removeAll
Remove all contained headers.- Specified by:
removeAllin interfaceIClearable- Returns:
EChange.
-
setHeader
Set the passed header as is.- Parameters:
sName- Header name. May neither benullnor empty.sValue- The value to be set. May benullin which case nothing happens.
-
addHeader
Add the passed header as is.- Parameters:
sName- Header name. May neither benullnor empty.sValue- The value to be set. May benullin which case nothing happens.
-
getDateTimeAsString
-
getDateTimeAsString
-
setDateHeader
Set the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.nMillis- The milliseconds to set as a date.
-
setDateHeader
Set the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aLD- The LocalDate to set as a date. The time is set to start of day. May not benull.
-
setDateHeader
Set the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aLDT- The LocalDateTime to set as a date. May not benull.
-
setDateHeader
Set the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aDT- The DateTime to set as a date. May not benull.
-
addDateHeader
Add the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.nMillis- The milliseconds to set as a date.
-
addDateHeader
Add the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aLD- The LocalDate to set as a date. The time is set to start of day. May not benull.
-
addDateHeader
Add the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aLDT- The LocalDateTime to set as a date. May not benull.
-
addDateHeader
Add the passed header as a date header.- Parameters:
sName- Header name. May neither benullnor empty.aDT- The DateTime to set as a date. May not benull.
-
setIntHeader
Set the passed header as a number.- Parameters:
sName- Header name. May neither benullnor empty.nValue- The value to be set. May not benull.
-
addIntHeader
Add the passed header as a number.- Parameters:
sName- Header name. May neither benullnor empty.nValue- The value to be set. May not benull.
-
setLongHeader
Set the passed header as a number.- Parameters:
sName- Header name. May neither benullnor empty.nValue- The value to be set. May not benull.
-
addLongHeader
Add the passed header as a number.- Parameters:
sName- Header name. May neither benullnor empty.nValue- The value to be set. May not benull.
-
setAllHeaders
Set all headers from the passed map. Existing headers with the same name are overwritten. Existing headers are not changed!- Parameters:
aOther- The header map to add. May not benull.
-
addAllHeaders
Add all headers from the passed map. Existing headers with the same name are extended.- Parameters:
aOther- The header map to add. May not benull.
-
getAllHeaders
-
getAllHeaderNames
- Returns:
- A copy of all contained header names. Never
null.
-
getAllHeaderValues
Get all header values of a certain header name.- Parameters:
sName- The name to be searched.- Returns:
- The list with all matching values. Never
nullbut maybe empty.
-
getFirstHeaderValue
Get the first header value of a certain header name. The matching of the name happens case insensitive.- Parameters:
sName- The name to be searched. May benull.- Returns:
- The first matching value or
null.
-
getHeaderCombined
Get the header value as a combination of all contained values. The matching of the name happens case insensitive.- Parameters:
sName- The header name to retrieve. May benull.sDelimiter- The delimiter to be used. May not benull.- Returns:
nullif no such header is contained.
-
containsHeaders
-
removeHeadersIf
Remove all header values where the name matches the provided filter.- Parameters:
aNameFilter- The name filter to be applied. May not benull.- Returns:
EChange
-
removeHeaders
Remove all header values with the provided name- Parameters:
sName- The name to be removed. May benull.- Returns:
EChange
-
removeHeader
-
iterator
-
size
-
isEmpty
public boolean isEmpty() -
forEachSingleHeader
public void forEachSingleHeader(@Nonnull BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue) Invoke the provided consumer for every name/value pair.- Parameters:
aConsumer- Consumer with key and unified value to be invoked. May not benull.bUnifyValue-trueto unify the values,falseif not- Since:
- 9.3.6
- See Also:
-
forEachSingleHeader
public void forEachSingleHeader(@Nonnull BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every name/value pair.- Parameters:
aConsumer- Consumer with key and unified value to be invoked. May not benull.bUnifyValue-trueto unify the values,falseif notbQuoteIfNecessary-trueto automatically quote values if it is necessary,falseto not do it. This is only used, if "unify values" istrue.- Since:
- 9.3.7
- See Also:
-
forEachHeaderLine
Invoke the provided consumer for every header line.- Parameters:
aConsumer- Consumer with the assembled line to be invoked. May not benull.bUnifyValue-trueto unify the values,falseif not- Since:
- 9.3.6
- See Also:
-
forEachHeaderLine
public void forEachHeaderLine(@Nonnull Consumer<? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every header line.- Parameters:
aConsumer- Consumer with the assembled line to be invoked. May not benull.bUnifyValue-trueto unify the values,falseif not.bQuoteIfNecessary-trueto automatically quote values if it is necessary,falseto not do it. This is only used, if "unify values" istrue.- Since:
- 9.3.7
- See Also:
-
getAllHeaderLines
Get all header lines as a list of strings.- Parameters:
bUnifyValue-trueto unify the values,falseif not- Returns:
- Never
nullbut maybe an empty list.
-
getAllHeaderLines
@Nonnull @ReturnsMutableCopy public ICommonsList<String> getAllHeaderLines(boolean bUnifyValue, boolean bQuoteIfNecessary) Get all header lines as a list of strings.- Parameters:
bUnifyValue-trueto unify the values,falseif notbQuoteIfNecessary-trueto automatically quote values if it is necessary,falseto not do it. This is only used, if "unify values" istrue.- Returns:
- Never
nullbut maybe an empty list. - Since:
- 9.3.7
-
setContentLength
public void setContentLength(long nLength) -
setContentType
-
getClone
- Specified by:
getClonein interfaceICloneable<HttpHeaderMap>- Returns:
- A 100% deep-copy of the implementing class.
-
getAsMapStringToListString
- Returns:
- The HTTP header map in a different representation. Never
nullbut maybe empty. - Since:
- 10.0
-
equals
-
hashCode
public int hashCode() -
toString
-