Class MessageBodyWriterContext

java.lang.Object
io.helidon.reactive.media.common.MessageBodyContext
io.helidon.reactive.media.common.MessageBodyWriterContext
All Implemented Interfaces:
MessageBodyFilters, MessageBodyWriters

public final class MessageBodyWriterContext extends MessageBodyContext implements MessageBodyWriters, MessageBodyFilters
Implementation of MessageBodyWriters.
  • Method Details

    • create

      public static MessageBodyWriterContext create(MediaContext mediaContext, MessageBodyContext.EventListener eventListener, WritableHeaders<?> headers, List<HttpMediaType> acceptedTypes)
      Create a new writer context.
      Parameters:
      mediaContext - media support used to derive the parent context, may be null
      eventListener - message body subscription event listener, may be null
      headers - backing headers, must not be null
      acceptedTypes - accepted types, may be null
      Returns:
      MessageBodyWriterContext
    • create

      public static MessageBodyWriterContext create(MessageBodyWriterContext parent, MessageBodyContext.EventListener eventListener, WritableHeaders<?> headers, List<HttpMediaType> acceptedTypes)
      Create a new writer context.
      Parameters:
      parent - parent context, may be null
      eventListener - message body subscription event listener, may be null
      headers - backing headers, must not be null
      acceptedTypes - accepted types, may be null
      Returns:
      MessageBodyWriterContext
    • create

      public static MessageBodyWriterContext create(WritableHeaders<?> headers)
      Create a new empty writer context backed by the specified headers.
      Parameters:
      headers - headers
      Returns:
      MessageBodyWriterContext
    • create

      public static MessageBodyWriterContext create(MessageBodyWriterContext parent)
      Create a new parented writer context.
      Parameters:
      parent - parent writer context
      Returns:
      MessageBodyWriterContext
    • create

      public static MessageBodyWriterContext create(MessageBodyWriterContext parent, WritableHeaders<?> headers)
      Create a new parented writer context backed by the specified headers.
      Parameters:
      parent - parent writer context
      headers - headers
      Returns:
      MessageBodyWriterContext
    • create

      public static MessageBodyWriterContext create()
      Create a new empty writer context backed by empty read-only headers. Such writer context is typically the parent context that is used to hold application wide writers and outbound filters.
      Returns:
      MessageBodyWriterContext
    • registerWriter

      public MessageBodyWriterContext registerWriter(MessageBodyWriter<?> writer)
      Description copied from interface: MessageBodyWriters
      Register a writer.
      Specified by:
      registerWriter in interface MessageBodyWriters
      Parameters:
      writer - writer to register
      Returns:
      Writers
    • registerWriter

      public MessageBodyWriterContext registerWriter(MessageBodyStreamWriter<?> writer)
      Description copied from interface: MessageBodyWriters
      Register a stream writer.
      Specified by:
      registerWriter in interface MessageBodyWriters
      Parameters:
      writer - writer to register
      Returns:
      Writers
    • marshall

      public <T> Flow.Publisher<DataChunk> marshall(Single<T> content, GenericType<T> type)
      Convert a given input publisher into HTTP payload by selecting a writer that accepts the specified type and current context.
      Type Parameters:
      T - entity type parameter
      Parameters:
      content - input publisher
      type - actual representation of the entity type
      Returns:
      publisher, never null
    • marshall

      public <T> Flow.Publisher<DataChunk> marshall(Single<T> content, MessageBodyWriter<T> writer, GenericType<T> type)
      Convert a given input publisher into HTTP payload by selecting a writer with the specified class.
      Type Parameters:
      T - entity type parameter
      Parameters:
      content - input publisher
      writer - specific writer
      type - actual representation of the entity type
      Returns:
      publisher, never null
    • marshallStream

      public <T> Flow.Publisher<DataChunk> marshallStream(Flow.Publisher<T> content, GenericType<T> type)
      Convert a given input publisher into HTTP payload by selecting a stream writer that accepts the specified type and current context.
      Type Parameters:
      T - entity type parameter
      Parameters:
      content - input publisher
      type - actual representation of the entity type
      Returns:
      publisher, never null
    • marshallStream

      public <T> Flow.Publisher<DataChunk> marshallStream(Flow.Publisher<T> content, MessageBodyStreamWriter<T> writer, GenericType<T> type)
      Convert a given input publisher into HTTP payload by selecting a stream writer with the specified class.
      Type Parameters:
      T - entity type parameter
      Parameters:
      content - input publisher
      writer - specific writer
      type - actual representation of the entity type
      Returns:
      publisher, never null
    • headers

      public WritableHeaders<?> headers()
      Get the underlying headers.
      Returns:
      Parameters, never null
    • contentType

      public Optional<HttpMediaType> contentType()
      Get the Content-Type header.
      Returns:
      Optional, never null
    • acceptedTypes

      public List<HttpMediaType> acceptedTypes()
      Get the inbound Accept header.
      Returns:
      List never null
    • contentType

      public void contentType(HttpMediaType contentType)
      Set the Content-Type header value in the underlying headers if not present.
      Parameters:
      contentType - Content-Type value to set, must not be null
    • contentType

      public void contentType(MediaType mediaType)
      Set the Content-Type header value in the underlying headers if not present.
      Parameters:
      mediaType - Content-Type value to set, must not be null
    • contentLength

      public void contentLength(long contentLength)
      Set the Content-Length header value in the underlying headers if not present.
      Parameters:
      contentLength - Content-Length value to set, must be a positive value
    • findAccepted

      public HttpMediaType findAccepted(Predicate<HttpMediaType> predicate, HttpMediaType defaultType) throws IllegalStateException
      Find an media type in the inbound Accept header with the given predicate and default value.
      • The default value is returned if the predicate matches a media type with a wildcard subtype.
      • The default value if the current Content-Type header is not set and the inbound Accept header is empty or missing.
      • When the Content-Type header is set, if the predicate matches the Content-Type header value is returned.
      Parameters:
      predicate - a predicate to match against the inbound Accept header
      defaultType - a default media type
      Returns:
      MediaType, never null
      Throws:
      IllegalStateException - if no media type can be returned
    • findAccepted

      public HttpMediaType findAccepted(HttpMediaType mediaType) throws IllegalStateException
      Find the given media type in the inbound Accept header.
      Parameters:
      mediaType - media type to search for
      Returns:
      MediaType, never null
      Throws:
      IllegalStateException - if the media type is not found
    • charset

      public Charset charset() throws IllegalStateException
      Description copied from class: MessageBodyContext
      Derive the charset to use from the Content-Type header value or using a default charset as fallback.
      Specified by:
      charset in class MessageBodyContext
      Returns:
      Charset, never null
      Throws:
      IllegalStateException - if an error occurs loading the charset specified by the Content-Type header value