Class MessageHeaderMethods
- java.lang.Object
-
- org.glassfish.jersey.message.internal.MessageHeaderMethods
-
- Direct Known Subclasses:
InboundMessageContext,OutboundMessageContext
public abstract class MessageHeaderMethods extends Object
Common header methods for outbound and inbound messages.
-
-
Field Summary
Fields Modifier and Type Field Description protected RuntimeDelegateruntimeDelegateDecorator
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageHeaderMethods(Configuration configuration)protectedMessageHeaderMethods(MessageHeaderMethods other)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleancontainsHeaderString(String name, String valueSeparatorRegex, Predicate<String> valuePredicate)Checks whether a header with a specific name and value (or item of the token-separated value list) exists.booleancontainsHeaderString(String name, Predicate<String> valuePredicate)Checks whether a header with a specific name and value (or item of the comma-separated value list) exists.protected HeaderValueExceptionexception(String headerName, Object headerValue, Exception e)Set<String>getAllowedMethods()Get the allowed HTTP methods from the Allow HTTP header.DategetDate()Get message date.EntityTaggetEntityTag()Get the entity tag.abstract MultivaluedMap<String,?>getHeaders()Get the mutable message headers multivalued map.abstract StringgetHeaderString(String name)Get a message header as a single string value.protected abstract HeaderValueException.ContextgetHeaderValueExceptionContext()ReturnHeaderValueException.Contexttype of the message context.LocalegetLanguage()Get the language of the entity.DategetLastModified()Get the last modified date.intgetLength()Get Content-Length value.longgetLengthLong()Get Content-Length value.LinkgetLink(String relation)Get the link for the relation.Link.BuildergetLinkBuilder(String relation)Convenience method that returns aLink.Builderfor the relation.abstract Set<Link>getLinks()Get the links attached to the message as header.URIgetLocation()Get the location.Map<String,Cookie>getRequestCookies()Get any cookies that accompanied the message.Map<String,NewCookie>getResponseCookies()Get any new cookies set on the message.booleanhasLink(String relation)Check if link for relation exists.protected <T> TsingleHeader(String name, Class<T> valueType, Function<String,T> converter, boolean convertNull)Get a single typed header value.protected <T> TsingleHeader(String name, Function<String,T> converter, boolean convertNull)Get a single typed header value for Inbound messages
-
-
-
Field Detail
-
runtimeDelegateDecorator
protected RuntimeDelegate runtimeDelegateDecorator
-
-
Constructor Detail
-
MessageHeaderMethods
protected MessageHeaderMethods(Configuration configuration)
-
MessageHeaderMethods
protected MessageHeaderMethods(MessageHeaderMethods other)
-
-
Method Detail
-
getHeaderString
public abstract String getHeaderString(String name)
Get a message header as a single string value. Each single non-string header value is converted to String using aRuntimeDelegate.HeaderDelegateif one is available viaRuntimeDelegate#createHeaderDelegate(java.lang.Class)for the header value class or using itstoStringmethod if a header delegate is not available.- Parameters:
name- the message header.- Returns:
- the message header value. If the message header is not present then
nullis returned. If the message header is present but has no value then the empty string is returned. If the message header is present more than once then the values of joined together and separated by a ',' character.
-
getHeaders
public abstract MultivaluedMap<String,?> getHeaders()
Get the mutable message headers multivalued map.- Returns:
- mutable multivalued map of message headers.
-
getHeaderValueExceptionContext
protected abstract HeaderValueException.Context getHeaderValueExceptionContext()
ReturnHeaderValueException.Contexttype of the message context.- Returns:
HeaderValueException.Contexttype of the message context.
-
getLinks
public abstract Set<Link> getLinks()
Get the links attached to the message as header.- Returns:
- links, may return empty
Setif no links are present. Never returnsnull.
-
containsHeaderString
public boolean containsHeaderString(String name, String valueSeparatorRegex, Predicate<String> valuePredicate)
Checks whether a header with a specific name and value (or item of the token-separated value list) exists. Each single non-string header value is converted to String using aRuntimeDelegate.HeaderDelegateif one is available viaRuntimeDelegate#createHeaderDelegate(java.lang.Class)for the header value class or using itstoStringmethod if a header delegate is not available.For example:
containsHeaderString("cache-control", ",", "no-store"::equalsIgnoreCase)will returntrueif aCache-Controlheader exists that has the valueno-store, the valueNo-Storeor the valueMax-Age, NO-STORE, no-transform, butfalsewhen it has the valueno-store;no-transform(missing comma), or the valueno - store(whitespace within value).- Parameters:
name- the message header.valueSeparatorRegex- Separates the header value into single values.nulldoes not split.valuePredicate- value must fulfil this predicate.- Returns:
trueif and only if a header with the given name exists, having either a whitespace-trimmed value matching the predicate, or having at least one whitespace-trimmed single value in a token-separated list of single values.
-
containsHeaderString
public boolean containsHeaderString(String name, Predicate<String> valuePredicate)
Checks whether a header with a specific name and value (or item of the comma-separated value list) exists. Each single non-string header value is converted to String using aRuntimeDelegate.HeaderDelegateif one is available viaRuntimeDelegate#createHeaderDelegate(java.lang.Class)for the header value class or using itstoStringmethod if a header delegate is not available.For example:
containsHeaderString("cache-control", "no-store"::equalsIgnoreCase)will returntrueif aCache-Controlheader exists that has the valueno-store, the valueNo-Storeor the valueMax-Age, NO-STORE, no-transform, butfalsewhen it has the valueno-store;no-transform(missing comma), or the valueno - store(whitespace within value).- Parameters:
name- the message header.valuePredicate- value must fulfil this predicate.- Returns:
trueif and only if a header with the given name exists, having either a whitespace-trimmed value matching the predicate, or having at least one whitespace-trimmed single value in a comma-separated list of single values.
-
getAllowedMethods
public Set<String> getAllowedMethods()
Get the allowed HTTP methods from the Allow HTTP header.- Returns:
- the allowed HTTP methods, all methods will returned as upper case strings.
-
getDate
public Date getDate()
Get message date.- Returns:
- the message date, otherwise
nullif not present.
-
getEntityTag
public EntityTag getEntityTag()
Get the entity tag.- Returns:
- the entity tag, otherwise
nullif not present.
-
getLanguage
public Locale getLanguage()
Get the language of the entity.- Returns:
- the language of the entity or
nullif not specified
-
getLastModified
public Date getLastModified()
Get the last modified date.- Returns:
- the last modified date, otherwise
nullif not present.
-
getLength
public int getLength()
Get Content-Length value.Note:
getLengthLong()should be preferred over this method, since it returns alonginstead and is therefore more portable.- Returns:
- Content-Length as a postive integer if present and valid number,
-1if negative number. - Throws:
ProcessingException- whenInteger.parseInt(String)(String)} throwsNumberFormatException.
-
getLengthLong
public long getLengthLong()
Get Content-Length value.- Returns:
- Content-Length as a positive long if present and valid number,
-1if negative number. - Throws:
ProcessingException- whenLong.parseLong(String)throwsNumberFormatException.
-
getLink
public Link getLink(String relation)
Get the link for the relation.- Parameters:
relation- link relation.- Returns:
- the link for the relation, otherwise
nullif not present.
-
getLinkBuilder
public Link.Builder getLinkBuilder(String relation)
Convenience method that returns aLink.Builderfor the relation.- Parameters:
relation- link relation.- Returns:
- the link builder for the relation, otherwise
nullif not present.
-
getLocation
public URI getLocation()
Get the location.- Returns:
- the location URI, otherwise
nullif not present.
-
getRequestCookies
public Map<String,Cookie> getRequestCookies()
Get any cookies that accompanied the message.- Returns:
- a read-only map of cookie name (String) to
Cookie.
-
getResponseCookies
public Map<String,NewCookie> getResponseCookies()
Get any new cookies set on the message.- Returns:
- a read-only map of cookie name (String) to a
new cookie.
-
hasLink
public boolean hasLink(String relation)
Check if link for relation exists.- Parameters:
relation- link relation.- Returns:
trueif the for the relation link exists,falseotherwise.
-
singleHeader
protected <T> T singleHeader(String name, Class<T> valueType, Function<String,T> converter, boolean convertNull)
Get a single typed header value.- Type Parameters:
T- header value type.- Parameters:
name- header name.valueType- header value class.converter- from string conversion function. Is expected to throwProcessingExceptionif conversion fails.convertNull- iftruethis method calls the provided converter even fornull. Otherwise this method returns thenullwithout calling the converter.- Returns:
- value of the header, or (possibly converted)
nullif not present.
-
singleHeader
protected <T> T singleHeader(String name, Function<String,T> converter, boolean convertNull)
Get a single typed header value for Inbound messages- Type Parameters:
T- header value type.- Parameters:
name- header name.converter- from string conversion function. Is expected to throwProcessingExceptionif conversion fails.convertNull- iftruethis method calls the provided converter even fornull. Otherwise this method returns thenullwithout calling the converter.- Returns:
- value of the header, or (possibly converted)
nullif not present.
-
exception
protected HeaderValueException exception(String headerName, Object headerValue, Exception e)
-
-