Package com.helger.http.header
Class HttpHeaderMap
java.lang.Object
com.helger.http.header.HttpHeaderMap
- All Implemented Interfaces:
com.helger.base.clone.ICloneable<HttpHeaderMap>,com.helger.base.iface.IHasSize,com.helger.base.state.IClearable,com.helger.collection.commons.ICommonsIterable<Map.Entry<String,,com.helger.collection.commons.ICommonsList<String>>> Iterable<Map.Entry<String,com.helger.collection.commons.ICommonsList<String>>>
@NotThreadSafe
public class HttpHeaderMap
extends Object
implements com.helger.collection.commons.ICommonsIterable<Map.Entry<String,com.helger.collection.commons.ICommonsList<String>>>, com.helger.base.clone.ICloneable<HttpHeaderMap>, com.helger.base.state.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
ConstructorsConstructorDescriptionDefault constructor.HttpHeaderMap(@NonNull HttpHeaderMap aOther) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllHeaders(@NonNull HttpHeaderMap aOther) Add all headers from the passed map.voidaddDateHeader(@NonNull @Nonempty String sName, long nMillis) Add the passed header as a date header.voidaddDateHeader(@NonNull @Nonempty String sName, @NonNull LocalDate aLD) Add the passed header as a date header.voidaddDateHeader(@NonNull @Nonempty String sName, @NonNull LocalDateTime aLDT) Add the passed header as a date header.voidaddDateHeader(@NonNull @Nonempty String sName, @NonNull ZonedDateTime aDT) Add the passed header as a date header.voidAdd the passed header as is.voidaddIntHeader(@NonNull @Nonempty String sName, int nValue) Add the passed header as a number.voidaddLongHeader(@NonNull @Nonempty String sName, long nValue) Add the passed header as a number.booleancontainsHeaders(@Nullable String sName) Check if headers with the specified name exist.booleanvoidforEachHeaderLine(@NonNull Consumer<? super String> aConsumer, boolean bUnifyValue) Invoke the provided consumer for every header line.voidforEachHeaderLine(@NonNull Consumer<? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every header line.voidforEachSingleHeader(@NonNull BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue) Invoke the provided consumer for every name/value pair.voidforEachSingleHeader(@NonNull BiConsumer<? super String, ? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary) Invoke the provided consumer for every name/value pair.@NonNull com.helger.collection.commons.ICommonsList<String> getAllHeaderLines(boolean bUnifyValue) Get all header lines as a list of strings.@NonNull com.helger.collection.commons.ICommonsList<String> getAllHeaderLines(boolean bUnifyValue, boolean bQuoteIfNecessary) Get all header lines as a list of strings.@NonNull com.helger.collection.commons.ICommonsOrderedSet<String> @NonNull com.helger.collection.commons.ICommonsOrderedMap<String, com.helger.collection.commons.ICommonsList<String>> @NonNull com.helger.collection.commons.ICommonsList<String> getAllHeaderValues(@Nullable String sName) Get all header values of a certain header name.@NonNull HttpHeaderMapgetClone()static @NonNull StringgetDateTimeAsString(@NonNull LocalDateTime aLDT) Convert aLocalDateTimeto a string suitable for use in HTTP date headers according to RFC 822.static @NonNull StringgetDateTimeAsString(@NonNull ZonedDateTime aDT) Convert aZonedDateTimeto a string suitable for use in HTTP date headers according to RFC 822.@Nullable StringgetFirstHeaderValue(@Nullable String sName) Get the first header value of a certain header name.@Nullable StringgetHeaderCombined(@Nullable String sName, @NonNull String sDelimiter) Get the header value as a combination of all contained values.static @NonNull StringgetUnifiedValue(@Nullable String sValue) Avoid having header values spanning multiple lines.static @NonNull StringgetUnifiedValue(@Nullable String sValue, boolean bQuoteIfNecessary) Avoid having header values spanning multiple lines.inthashCode()booleanisEmpty()iterator()@NonNull com.helger.base.state.EChangeRemove all contained headers.@NonNull com.helger.base.state.EChangeremoveHeader(@Nullable String sName, @Nullable String sValue) Remove a specific header value from the header with the specified name.@NonNull com.helger.base.state.EChangeremoveHeaders(@Nullable String sName) Remove all header values with the provided name@NonNull com.helger.base.state.EChangeremoveHeadersIf(@NonNull Predicate<? super String> aNameFilter) Remove all header values where the name matches the provided filter.voidsetAllHeaders(@NonNull HttpHeaderMap aOther) Set all headers from the passed map.voidsetContentLength(long nLength) Set the "Content-Length" HTTP header.voidsetContentType(@NonNull String sContentType) Set the "Content-Type" HTTP header.voidsetDateHeader(@NonNull @Nonempty String sName, long nMillis) Set the passed header as a date header.voidsetDateHeader(@NonNull @Nonempty String sName, @NonNull LocalDate aLD) Set the passed header as a date header.voidsetDateHeader(@NonNull @Nonempty String sName, @NonNull LocalDateTime aLDT) Set the passed header as a date header.voidsetDateHeader(@NonNull @Nonempty String sName, @NonNull ZonedDateTime aDT) Set the passed header as a date header.voidSet the passed header as is.voidsetIntHeader(@NonNull @Nonempty String sName, int nValue) Set the passed header as a number.voidsetLongHeader(@NonNull @Nonempty 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.collection.commons.ICommonsIterable
containsAny, containsNone, containsOnly, findAll, findAllInstanceOf, findAllMapped, findAllMapped, findAllMapped, findFirst, findFirst, findFirstIndex, findFirstMapped, findFirstMapped, findLastIndex, forEachBreakable, forEachByIndex, forEachThrowing, getCountMethods inherited from interface com.helger.base.iface.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
public @NonNull com.helger.base.state.EChange removeAll()Remove all contained headers.- Specified by:
removeAllin interfacecom.helger.base.state.IClearable- 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
Convert aZonedDateTimeto a string suitable for use in HTTP date headers according to RFC 822.- Parameters:
aDT- The date time to convert. May not benull.- Returns:
- The formatted date string. Never
null.
-
getDateTimeAsString
Convert aLocalDateTimeto a string suitable for use in HTTP date headers according to RFC 822.- Parameters:
aLDT- The local date time to convert. May not benull.- Returns:
- The formatted date string. Never
null.
-
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
@ReturnsMutableCopy public @NonNull com.helger.collection.commons.ICommonsOrderedMap<String,com.helger.collection.commons.ICommonsList<String>> getAllHeaders()- Returns:
- A copy of all contained headers as an ordered map. Never
null.
-
getAllHeaderNames
@ReturnsMutableCopy public @NonNull com.helger.collection.commons.ICommonsOrderedSet<String> getAllHeaderNames()- Returns:
- A copy of all contained header names. Never
null.
-
getAllHeaderValues
@ReturnsMutableCopy public @NonNull com.helger.collection.commons.ICommonsList<String> getAllHeaderValues(@Nullable String sName) 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
Check if headers with the specified name exist. The matching happens case insensitive.- Parameters:
sName- The header name to check. May benull.- Returns:
trueif at least one header with the specified name exists.
-
removeHeadersIf
public @NonNull com.helger.base.state.EChange removeHeadersIf(@NonNull Predicate<? super String> aNameFilter) 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
public @NonNull com.helger.base.state.EChange removeHeader(@Nullable String sName, @Nullable String sValue) Remove a specific header value from the header with the specified name. If the last value of a header is removed, the entire header entry is removed.- Parameters:
sName- The header name. May benull.sValue- The header value to remove. May benull.- Returns:
EChange
-
iterator
-
size
@Nonnegative public int 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
@ReturnsMutableCopy public @NonNull com.helger.collection.commons.ICommonsList<String> getAllHeaderLines(boolean bUnifyValue) 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
@ReturnsMutableCopy public @NonNull com.helger.collection.commons.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) Set the "Content-Length" HTTP header.- Parameters:
nLength- The content length to set.
-
setContentType
Set the "Content-Type" HTTP header.- Parameters:
sContentType- The content type to set. May not benull.
-
getClone
- Specified by:
getClonein interfacecom.helger.base.clone.ICloneable<HttpHeaderMap>- Returns:
- A deep copy of this header map. Never
null.
-
getAsMapStringToListString
- Returns:
- The HTTP header map in a different representation. Never
nullbut maybe empty. - Since:
- 10.0
-
equals
-
hashCode
public int hashCode() -
toString
-