Class OutboundMessageContext

  • Direct Known Subclasses:
    ClientRequest

    public class OutboundMessageContext
    extends java.lang.Object
    Base outbound message context implementation.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  OutboundMessageContext.StreamProvider
      The callback interface which is used to get the terminal output stream into which the entity should be written and to inform the implementation about the entity size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the context.
      void commitStream()
      Commits the entity stream if it wasn't already committed.
      void enableBuffering​(javax.ws.rs.core.Configuration configuration)
      Enable a buffering of serialized entity.
      private static HeaderValueException exception​(java.lang.String headerName, java.lang.Object headerValue, java.lang.Exception e)  
      java.util.List<java.util.Locale> getAcceptableLanguages()
      Get a list of languages that are acceptable for the message.
      java.util.List<javax.ws.rs.core.MediaType> getAcceptableMediaTypes()
      Get a list of media types that are acceptable for the message.
      java.util.Set<java.lang.String> getAllowedMethods()
      Get the allowed HTTP methods from the Allow HTTP header.
      java.util.Date getDate()
      Get message date.
      java.lang.Object getEntity()
      Get the message entity Java instance.
      java.lang.annotation.Annotation[] getEntityAnnotations()
      Get the annotations attached to the entity.
      java.lang.Class<?> getEntityClass()
      Get the raw message entity type information.
      java.io.OutputStream getEntityStream()
      Get the entity output stream.
      javax.ws.rs.core.EntityTag getEntityTag()
      Get the entity tag.
      java.lang.reflect.Type getEntityType()
      Get the message entity type information.
      javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> getHeaders()
      Get the mutable message headers multivalued map.
      java.lang.String getHeaderString​(java.lang.String name)
      Get a message header as a single string value.
      java.util.Locale getLanguage()
      Get the language of the entity.
      java.util.Date getLastModified()
      Get the last modified date.
      int getLength()
      Get Content-Length value.
      long getLengthLong()
      Get Content-Length value.
      javax.ws.rs.core.Link getLink​(java.lang.String relation)
      Get the link for the relation.
      javax.ws.rs.core.Link.Builder getLinkBuilder​(java.lang.String relation)
      Convenience method that returns a Link.Builder for the relation.
      java.util.Set<javax.ws.rs.core.Link> getLinks()
      Get the links attached to the message as header.
      java.net.URI getLocation()
      Get the location.
      javax.ws.rs.core.MediaType getMediaType()
      Get the media type of the entity.
      java.util.Map<java.lang.String,​javax.ws.rs.core.Cookie> getRequestCookies()
      Get any cookies that accompanied the message.
      java.util.Map<java.lang.String,​javax.ws.rs.core.NewCookie> getResponseCookies()
      Get any new cookies set on the message message.
      javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> getStringHeaders()
      Get a multi-valued map representing outbound message headers with their values converted to strings.
      boolean hasEntity()
      Check if there is an entity available in the message.
      boolean hasLink​(java.lang.String relation)
      Check if link for relation exists.
      boolean isCommitted()
      Returns true if the entity stream has been committed.
      void replaceHeaders​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
      Replace all headers.
      void setEntity​(java.lang.Object entity)
      Set a new message message entity.
      void setEntity​(java.lang.Object entity, java.lang.annotation.Annotation[] annotations)
      Set a new message message entity.
      void setEntity​(java.lang.Object entity, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
      Set a new message message entity.
      void setEntity​(java.lang.Object entity, java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations)
      Set a new message message entity.
      private void setEntity​(java.lang.Object entity, javax.ws.rs.core.GenericType<?> type)
      Set a new message message entity.
      void setEntityAnnotations​(java.lang.annotation.Annotation[] annotations)
      Set the annotations attached to the entity.
      void setEntityStream​(java.io.OutputStream outputStream)
      Set a new entity output stream.
      void setEntityType​(java.lang.reflect.Type type)
      Set the message entity type information.
      void setMediaType​(javax.ws.rs.core.MediaType mediaType)
      Set the message content media type.
      void setStreamProvider​(OutboundMessageContext.StreamProvider streamProvider)
      Set a stream provider callback.
      private <T> T singleHeader​(java.lang.String name, java.lang.Class<T> valueType, java.util.function.Function<java.lang.String,​T> converter, boolean convertNull)
      Get a single typed header value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_ANNOTATIONS

        private static final java.lang.annotation.Annotation[] EMPTY_ANNOTATIONS
      • WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST

        private static final java.util.List<javax.ws.rs.core.MediaType> WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST
      • headers

        private final javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers
      • entity

        private java.lang.Object entity
      • entityType

        private javax.ws.rs.core.GenericType<?> entityType
      • entityAnnotations

        private java.lang.annotation.Annotation[] entityAnnotations
      • entityStream

        private java.io.OutputStream entityStream
    • Constructor Detail

      • OutboundMessageContext

        public OutboundMessageContext()
        Create new outbound message context.
      • OutboundMessageContext

        public OutboundMessageContext​(OutboundMessageContext original)
        Create new outbound message context copying the content of another context.
        Parameters:
        original - the original outbound message context.
    • Method Detail

      • replaceHeaders

        public void replaceHeaders​(javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> headers)
        Replace all headers.
        Parameters:
        headers - new headers.
      • getStringHeaders

        public javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> getStringHeaders()
        Get a multi-valued map representing outbound message headers with their values converted to strings.
        Returns:
        multi-valued map of outbound message header names to their string-converted values.
      • getHeaderString

        public java.lang.String getHeaderString​(java.lang.String name)
        Get a message header as a single string value.

        Each single 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.
      • singleHeader

        private <T> T singleHeader​(java.lang.String name,
                                   java.lang.Class<T> valueType,
                                   java.util.function.Function<java.lang.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.
      • exception

        private static HeaderValueException exception​(java.lang.String headerName,
                                                      java.lang.Object headerValue,
                                                      java.lang.Exception e)
      • getHeaders

        public javax.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.Object> getHeaders()
        Get the mutable message headers multivalued map.
        Returns:
        mutable multivalued map of message headers.
      • getDate

        public java.util.Date getDate()
        Get message date.
        Returns:
        the message date, otherwise null if not present.
      • getLanguage

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

        public javax.ws.rs.core.MediaType getMediaType()
        Get the media type of the entity.
        Returns:
        the media type or null if not specified (e.g. there's no message entity).
      • getAcceptableMediaTypes

        public java.util.List<javax.ws.rs.core.MediaType> getAcceptableMediaTypes()
        Get a list of media types that are acceptable for the message.
        Returns:
        a read-only list of requested message media types sorted according to their q-value, with highest preference first.
      • getAcceptableLanguages

        public java.util.List<java.util.Locale> getAcceptableLanguages()
        Get a list of languages that are acceptable for the message.
        Returns:
        a read-only list of acceptable languages sorted according to their q-value, with highest preference first.
      • getRequestCookies

        public java.util.Map<java.lang.String,​javax.ws.rs.core.Cookie> getRequestCookies()
        Get any cookies that accompanied the message.
        Returns:
        a read-only map of cookie name (String) to Cookie.
      • getAllowedMethods

        public java.util.Set<java.lang.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.
      • 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:
        javax.ws.rs.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:
        javax.ws.rs.ProcessingException - when Long.parseLong(String) throws NumberFormatException.
      • getResponseCookies

        public java.util.Map<java.lang.String,​javax.ws.rs.core.NewCookie> getResponseCookies()
        Get any new cookies set on the message message.
        Returns:
        a read-only map of cookie name (String) to a new cookie.
      • getEntityTag

        public javax.ws.rs.core.EntityTag getEntityTag()
        Get the entity tag.
        Returns:
        the entity tag, otherwise null if not present.
      • getLastModified

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

        public java.net.URI getLocation()
        Get the location.
        Returns:
        the location URI, otherwise null if not present.
      • getLinks

        public java.util.Set<javax.ws.rs.core.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

        public boolean hasLink​(java.lang.String relation)
        Check if link for relation exists.
        Parameters:
        relation - link relation.
        Returns:
        true if the for the relation link exists, false otherwise.
      • getLink

        public javax.ws.rs.core.Link getLink​(java.lang.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 javax.ws.rs.core.Link.Builder getLinkBuilder​(java.lang.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

        public boolean hasEntity()
        Check if there is an entity available in the message.

        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

        public java.lang.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.
      • setEntity

        public void setEntity​(java.lang.Object entity)
        Set a new message message entity.
        Parameters:
        entity - entity object.
        See Also:
        MessageBodyWriter
      • setEntity

        public void setEntity​(java.lang.Object entity,
                              java.lang.annotation.Annotation[] annotations)
        Set a new message message entity.
        Parameters:
        entity - entity object.
        annotations - annotations attached to the entity.
        See Also:
        MessageBodyWriter
      • setEntity

        private void setEntity​(java.lang.Object entity,
                               javax.ws.rs.core.GenericType<?> type)
        Set a new message message entity.
        Parameters:
        entity - entity object.
        type - entity generic type information.
        See Also:
        MessageBodyWriter
      • setEntity

        public void setEntity​(java.lang.Object entity,
                              java.lang.reflect.Type type,
                              java.lang.annotation.Annotation[] annotations)
        Set a new message message entity.
        Parameters:
        entity - entity object.
        type - declared entity class.
        annotations - annotations attached to the entity.
        See Also:
        MessageBodyWriter
      • setEntity

        public void setEntity​(java.lang.Object entity,
                              java.lang.annotation.Annotation[] annotations,
                              javax.ws.rs.core.MediaType mediaType)
        Set a new message message entity.
        Parameters:
        entity - entity object.
        annotations - annotations attached to the entity.
        mediaType - entity media type.
        See Also:
        MessageBodyWriter
      • setMediaType

        public void setMediaType​(javax.ws.rs.core.MediaType mediaType)
        Set the message content media type.
        Parameters:
        mediaType - message content media type.
      • getEntityClass

        public java.lang.Class<?> getEntityClass()
        Get the raw message entity type information.
        Returns:
        raw message entity type information.
      • getEntityType

        public java.lang.reflect.Type getEntityType()
        Get the message entity type information.
        Returns:
        message entity type.
      • setEntityType

        public void setEntityType​(java.lang.reflect.Type type)
        Set the message entity type information.

        This method overrides any computed or previously set entity type information.

        Parameters:
        type - overriding message entity type.
      • getEntityAnnotations

        public java.lang.annotation.Annotation[] getEntityAnnotations()
        Get the annotations attached to the entity.
        Returns:
        entity annotations.
      • setEntityAnnotations

        public void setEntityAnnotations​(java.lang.annotation.Annotation[] annotations)
        Set the annotations attached to the entity.
        Parameters:
        annotations - entity annotations.
      • getEntityStream

        public java.io.OutputStream getEntityStream()
        Get the entity output stream.
        Returns:
        entity output stream.
      • setEntityStream

        public void setEntityStream​(java.io.OutputStream outputStream)
        Set a new entity output stream.
        Parameters:
        outputStream - new entity output stream.
      • enableBuffering

        public void enableBuffering​(javax.ws.rs.core.Configuration configuration)
        Enable a buffering of serialized entity. The buffering will be configured from configuration. The property determining the size of the buffer is CommonProperties.OUTBOUND_CONTENT_LENGTH_BUFFER.

        The buffering functionality is by default disabled and could be enabled by calling this method. In this case this method must be called before first bytes are written to the entity stream.
        Parameters:
        configuration - runtime configuration.
      • setStreamProvider

        public void setStreamProvider​(OutboundMessageContext.StreamProvider streamProvider)
        Set a stream provider callback.

        This method must be called before first bytes are written to the entity stream.

        Parameters:
        streamProvider - non-null output stream provider.
      • commitStream

        public void commitStream()
                          throws java.io.IOException
        Commits the entity stream if it wasn't already committed.
        Throws:
        java.io.IOException - in case of the IO error.
      • isCommitted

        public boolean isCommitted()
        Returns true if the entity stream has been committed.
        Returns:
        true if the entity stream has been committed. Otherwise returns false.
      • close

        public void close()
        Closes the context. Flushes and closes the entity stream.