Interface ContainerResponseContext


public interface ContainerResponseContext
Container response filter context. A mutable class that provides response-specific information for the filter, such as message headers, message entity or request-scoped properties. The exposed setters allow modification of the exposed response-specific information.
Since:
2.0
Author:
Marek Potociar
  • Method Details Link icon

    • getStatus Link icon

      int getStatus()
      Get the status code associated with the response.
      Returns:
      the response status code or -1 if the status was not set.
    • setStatus Link icon

      void setStatus(int code)
      Set a new response status code.
      Parameters:
      code - new status code.
    • getStatusInfo Link icon

      Response.StatusType getStatusInfo()
      Get the complete status information associated with the response.
      Returns:
      the response status information or null if the status was not set.
    • setStatusInfo Link icon

      void setStatusInfo(Response.StatusType statusInfo)
      Set the complete status information (status code and reason phrase) associated with the response.
      Parameters:
      statusInfo - the response status information.
    • getHeaders Link icon

      Get the mutable response headers multivalued map.
      Returns:
      mutable multivalued map of response headers.
      See Also:
    • getStringHeaders Link icon

      MultivaluedMap<String,String> getStringHeaders()
      Get a string view of header values associated with the message. Changes in the underlying headers map are reflected in this view.

      The method converts the non-string header values to strings using a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate.createHeaderDelegate(java.lang.Class) for the class of the value or using the values toString method if a header delegate is not available.

      Returns:
      response headers as a string view of header values.
      See Also:
    • getHeaderString Link icon

      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.
      See Also:
    • containsHeaderString Link icon

      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 - Regular expression that 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.
      Since:
      4.0
      See Also:
    • containsHeaderString Link icon

      default 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.
      Since:
      4.0
      See Also:
    • getAllowedMethods Link icon

      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 Link icon

      Date getDate()
      Get message date.
      Returns:
      the message date, otherwise null if not present.
    • getLanguage Link icon

      Locale getLanguage()
      Get the language of the entity.
      Returns:
      the language of the entity or null if not specified
    • getLength Link icon

      int getLength()
      Get Content-Length value.
      Returns:
      Content-Length as integer if present and valid number. In other cases returns -1.
    • getMediaType Link icon

      MediaType getMediaType()
      Get the media type of the entity.
      Returns:
      the media type or null if not specified (e.g. there's no response entity).
    • getCookies Link icon

      Map<String,NewCookie> getCookies()
      Get any new cookies set on the response message.
      Returns:
      a read-only map of cookie name (String) to a new cookie.
    • getEntityTag Link icon

      EntityTag getEntityTag()
      Get the entity tag.
      Returns:
      the entity tag, otherwise null if not present.
    • getLastModified Link icon

      Date getLastModified()
      Get the last modified date.
      Returns:
      the last modified date, otherwise null if not present.
    • getLocation Link icon

      URI getLocation()
      Get the location.
      Returns:
      the location URI, otherwise null if not present.
    • getLinks Link icon

      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.
    • hasLink Link icon

      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.
    • getLink Link icon

      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 Link icon

      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.
    • hasEntity Link icon

      boolean hasEntity()
      Check if there is an entity available in the response. The method returns true if the entity is present, returns false otherwise.
      Returns:
      true if there is an entity present in the message, false otherwise.
    • getEntity Link icon

      Object getEntity()
      Get the message entity Java instance. Returns null if the message does not contain an entity.
      Returns:
      the message entity or null if message does not contain an entity body.
    • getEntityClass Link icon

      Class<?> getEntityClass()
      Get the raw entity type information.
      Returns:
      raw entity type.
    • getEntityType Link icon

      Type getEntityType()
      Get the generic entity type information.
      Returns:
      declared generic entity type.
    • setEntity Link icon

      void setEntity(Object entity)
      Set a new message entity. The existing entity annotations and media type are preserved.

      It is the callers responsibility to wrap the actual entity with GenericEntity if preservation of its generic type is required.

      Parameters:
      entity - entity object.
      See Also:
    • setEntity Link icon

      void setEntity(Object entity, Annotation[] annotations, MediaType mediaType)
      Set a new message entity, including the attached annotations and the media type.

      It is the callers responsibility to wrap the actual entity with GenericEntity if preservation of its generic type is required.

      Parameters:
      entity - entity object.
      annotations - annotations attached to the entity instance.
      mediaType - entity media type.
      See Also:
    • getEntityAnnotations Link icon

      Annotation[] getEntityAnnotations()
      Get the annotations attached to the entity instance.

      Note that the returned annotations array contains only those annotations explicitly attached to entity instance (such as the ones attached using Response.ResponseBuilder.entity(Object, java.lang.annotation.Annotation[]) method as well as the ones attached to the resource method that has returned the response). The entity instance annotations array does not include annotations declared on the entity implementation class or its ancestors.

      Note that container response filters invoked earlier in the filter chain may modify the entity annotations value, in which case this getter method would return the last annotations value set by a container response filter invoked earlier in the filter chain.

      For example:

       @Path("my-resource")
       public class MyResource {
         private final Annotations[] extras = ... ;
      
         @GET
         @Custom
         public String getAnnotatedMe() {
           return Response.ok().entity("Annotated me", extras).build();
         }
         ...
       }
       

      The container response context for a response returned from the getMe() method above would contain all the annotations declared on the getAnnotatedMe() method (@GET, @Custom) as well as all the annotations from the extras field, provided this value has not been replaced by any container response filter invoked earlier.

      Similarly:

       @Custom
       public class AnnotatedMe { ... }
      
       @Path("my-resource")
       public class MyResource {
         private final Annotations[] extras = ... ;
      
         @GET
         public AnnotatedMe getMe() {
           return Response.ok().entity(new AnnotatedMe(), extras).build();
         }
         ...
       }
       

      Provided that the value has not been replaced by any container response filter invoked earlier, the container response context for a response returned from the getMe() method above would contain all the annotations on the getMe() method (@GET) as well as all the annotations from the extras field. It would however not contain any annotations declared on the AnnotatedMe class.

      Returns:
      annotations attached to the entity instance.
    • getEntityStream Link icon

      OutputStream getEntityStream()
      Get the entity output stream. The JAX-RS runtime is responsible for closing the output stream.
      Returns:
      entity output stream.
    • setEntityStream Link icon

      void setEntityStream(OutputStream outputStream)
      Set a new entity output stream. The JAX-RS runtime is responsible for closing the output stream.
      Parameters:
      outputStream - new entity output stream.