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 Details

    • runtimeDelegateDecorator

      protected RuntimeDelegate runtimeDelegateDecorator
  • Constructor Details

    • MessageHeaderMethods

      protected MessageHeaderMethods(Configuration configuration)
    • MessageHeaderMethods

      protected MessageHeaderMethods(MessageHeaderMethods other)
  • Method Details

    • 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 a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate#createHeaderDelegate(java.lang.Class) for the header value class or using its toString method 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 null is 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()
      Return HeaderValueException.Context type of the message context.
      Returns:
      HeaderValueException.Context type of the message context.
    • getLinks

      public abstract Set<Link> getLinks()
      Get the links attached to the message as header.
      Returns:
      links, may return empty Set if no links are present. Never returns null.
    • 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 a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate#createHeaderDelegate(java.lang.Class) for the header value class or using its toString method if a header delegate is not available.

      For example: containsHeaderString("cache-control", ",", "no-store"::equalsIgnoreCase) will return true if a Cache-Control header exists that has the value no-store, the value No-Store or the value Max-Age, NO-STORE, no-transform, but false when it has the value no-store;no-transform (missing comma), or the value no - store (whitespace within value).

      Parameters:
      name - the message header.
      valueSeparatorRegex - Separates the header value into single values. null does not split.
      valuePredicate - value must fulfil this predicate.
      Returns:
      true if 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 a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate#createHeaderDelegate(java.lang.Class) for the header value class or using its toString method if a header delegate is not available.

      For example: containsHeaderString("cache-control", "no-store"::equalsIgnoreCase) will return true if a Cache-Control header exists that has the value no-store, the value No-Store or the value Max-Age, NO-STORE, no-transform, but false when it has the value no-store;no-transform (missing comma), or the value no - store (whitespace within value).

      Parameters:
      name - the message header.
      valuePredicate - value must fulfil this predicate.
      Returns:
      true if 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 null if not present.
    • getEntityTag

      public EntityTag getEntityTag()
      Get the entity tag.
      Returns:
      the entity tag, otherwise null if not present.
    • getLanguage

      public Locale getLanguage()
      Get the language of the entity.
      Returns:
      the language of the entity or null if not specified
    • getLastModified

      public Date getLastModified()
      Get the last modified date.
      Returns:
      the last modified date, otherwise null if not present.
    • getLength

      public int getLength()
      Get Content-Length value.

      Note: getLengthLong() should be preferred over this method, since it returns a long instead and is therefore more portable.

      Returns:
      Content-Length as a postive integer if present and valid number, -1 if negative number.
      Throws:
      ProcessingException - when Integer.parseInt(String) (String)} throws NumberFormatException.
    • getLengthLong

      public long getLengthLong()
      Get Content-Length value.
      Returns:
      Content-Length as a positive long if present and valid number, -1 if negative number.
      Throws:
      ProcessingException - when Long.parseLong(String) throws NumberFormatException.
    • getLink

      public Link getLink(String relation)
      Get the link for the relation.
      Parameters:
      relation - link relation.
      Returns:
      the link for the relation, otherwise null if not present.
    • getLinkBuilder

      public Link.Builder getLinkBuilder(String relation)
      Convenience method that returns a Link.Builder for the relation.
      Parameters:
      relation - link relation.
      Returns:
      the link builder for the relation, otherwise null if not present.
    • getLocation

      public URI getLocation()
      Get the location.
      Returns:
      the location URI, otherwise null if 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:
      true if the for the relation link exists, false otherwise.
    • 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 throw ProcessingException if conversion fails.
      convertNull - if true this method calls the provided converter even for null. Otherwise this method returns the null without calling the converter.
      Returns:
      value of the header, or (possibly converted) null if 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 throw ProcessingException if conversion fails.
      convertNull - if true this method calls the provided converter even for null. Otherwise this method returns the null without calling the converter.
      Returns:
      value of the header, or (possibly converted) null if not present.
    • exception

      protected HeaderValueException exception(String headerName, Object headerValue, Exception e)