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.- Since:
- 9.0.0
- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_QUOTE_IF_NECESSARYDefault quote if necessary: false (since v10)static StringSEPARATOR_KEY_VALUEThe separator between key and value
-
Constructor Summary
Constructors Constructor Description HttpHeaderMap()Default constructor.HttpHeaderMap(HttpHeaderMap aOther)Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAllHeaders(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.voidaddHeader(String sName, String sValue)Add 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)booleanequals(Object o)voidforEachHeaderLine(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.ICommonsList<String>getAllHeaderLines(boolean bUnifyValue)Get all header lines as a list of strings.ICommonsList<String>getAllHeaderLines(boolean bUnifyValue, boolean bQuoteIfNecessary)Get all header lines as a list of strings.ICommonsOrderedSet<String>getAllHeaderNames()ICommonsOrderedMap<String,ICommonsList<String>>getAllHeaders()ICommonsList<String>getAllHeaderValues(String sName)Get all header values of a certain header name.Map<String,List<String>>getAsMapStringToListString()HttpHeaderMapgetClone()static StringgetDateTimeAsString(LocalDateTime aLDT)static StringgetDateTimeAsString(ZonedDateTime aDT)StringgetFirstHeaderValue(String sName)Get the first header value of a certain header name.StringgetHeaderCombined(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<Map.Entry<String,ICommonsList<String>>>iterator()EChangeremoveAll()Remove all contained headers.EChangeremoveHeader(String sName, String sValue)EChangeremoveHeaders(String sName)Remove all header values with the provided nameEChangeremoveHeadersIf(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.voidsetHeader(String sName, String sValue)Set 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()StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods 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, getCount
-
Methods inherited from interface com.helger.commons.lang.IHasSize
isNotEmpty
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
SEPARATOR_KEY_VALUE
public static final String SEPARATOR_KEY_VALUE
The separator between key and value- See Also:
- Constant Field Values
-
DEFAULT_QUOTE_IF_NECESSARY
public static final boolean DEFAULT_QUOTE_IF_NECESSARY
Default quote if necessary: false (since v10)- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HttpHeaderMap
public HttpHeaderMap()
Default constructor.
-
HttpHeaderMap
public HttpHeaderMap(@Nonnull HttpHeaderMap aOther)
Copy constructor.- Parameters:
aOther- Map to copy from. May not benull.
-
-
Method Detail
-
getUnifiedValue
@Nonnull public static String getUnifiedValue(@Nullable String sValue)
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
@Nonnull public static String getUnifiedValue(@Nullable String sValue, boolean bQuoteIfNecessary)
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
@Nonnull public EChange removeAll()
Remove all contained headers.- Specified by:
removeAllin interfaceIClearable- Returns:
EChange.
-
setHeader
public void setHeader(@Nonnull @Nonempty String sName, @Nullable String sValue)
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
public void addHeader(@Nonnull @Nonempty String sName, @Nullable String sValue)
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
@Nonnull public static String getDateTimeAsString(@Nonnull ZonedDateTime aDT)
-
getDateTimeAsString
@Nonnull public static String getDateTimeAsString(@Nonnull LocalDateTime aLDT)
-
setDateHeader
public void setDateHeader(@Nonnull @Nonempty String sName, long nMillis)
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
public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDate aLD)
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
public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDateTime aLDT)
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
public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull ZonedDateTime aDT)
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
public void addDateHeader(@Nonnull @Nonempty String sName, long nMillis)
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
public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDate aLD)
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
public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDateTime aLDT)
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
public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull ZonedDateTime aDT)
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
public void setIntHeader(@Nonnull @Nonempty String sName, int nValue)
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
public void addIntHeader(@Nonnull @Nonempty String sName, int nValue)
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
public void setLongHeader(@Nonnull @Nonempty String sName, long nValue)
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
public void addLongHeader(@Nonnull @Nonempty String sName, long nValue)
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
public void setAllHeaders(@Nonnull HttpHeaderMap aOther)
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
public void addAllHeaders(@Nonnull HttpHeaderMap aOther)
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
@Nonnull @ReturnsMutableCopy public ICommonsOrderedMap<String,ICommonsList<String>> getAllHeaders()
-
getAllHeaderNames
@Nonnull @ReturnsMutableCopy public ICommonsOrderedSet<String> getAllHeaderNames()
- Returns:
- A copy of all contained header names. Never
null.
-
getAllHeaderValues
@Nonnull @ReturnsMutableCopy public 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
@Nullable public String getFirstHeaderValue(@Nullable String sName)
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
@Nullable public String getHeaderCombined(@Nullable String sName, @Nonnull String sDelimiter)
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.
-
removeHeadersIf
@Nonnull public 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
@Nonnull public EChange removeHeaders(@Nullable String sName)
Remove all header values with the provided name- Parameters:
sName- The name to be removed. May benull.- Returns:
EChange
-
size
@Nonnegative public int size()
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceIHasSize- Returns:
trueif no items are present,falseif at least a single item is present.- See Also:
IHasSize.size(),IHasSize.isNotEmpty()
-
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:
getUnifiedValue(String,boolean)
-
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:
getUnifiedValue(String, boolean)
-
forEachHeaderLine
public void forEachHeaderLine(@Nonnull Consumer<? super String> aConsumer, boolean bUnifyValue)
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:
getUnifiedValue(String,boolean)
-
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:
getUnifiedValue(String,boolean)
-
getAllHeaderLines
@Nonnull @ReturnsMutableCopy public 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
@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)
-
getClone
@Nonnull @ReturnsMutableCopy public HttpHeaderMap getClone()
- Specified by:
getClonein interfaceICloneable<HttpHeaderMap>- Returns:
- A 100% deep-copy of the implementing class.
-
getAsMapStringToListString
@Nonnull public Map<String,List<String>> getAsMapStringToListString()
- Returns:
- The HTTP header map in a different representation. Never
nullbut maybe empty. - Since:
- 10.0
-
-