Class MessageBodyWriterContext

java.lang.Object
io.helidon.media.common.MessageBodyContext
io.helidon.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, Parameters headers, List<MediaType> 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, Parameters headers, List<MediaType> 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(Parameters 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, Parameters 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
    • registerWriter

      @Deprecated public <T> MessageBodyWriterContext registerWriter(Class<T> type, Function<T,Flow.Publisher<DataChunk>> function)
      Deprecated.
      since 2.0.0, use registerWriter(MessageBodyWriter) instead
      Registers a writer function with a given type.
      Type Parameters:
      T - entity type
      Parameters:
      type - class representing the type supported by this writer
      function - writer function
      Returns:
      this MessageBodyWriteableContent instance
    • registerWriter

      @Deprecated public <T> MessageBodyWriterContext registerWriter(Class<T> type, MediaType contentType, Function<? extends T,Flow.Publisher<DataChunk>> function)
      Deprecated.
      since 2.0.0, use registerWriter(MessageBodyWriter) instead
      Registers a writer function with a given type and media type.
      Type Parameters:
      T - entity type
      Parameters:
      type - class representing the type supported by this writer
      contentType - the media type
      function - writer function
      Returns:
      this MessageBodyWriteableContent instance
    • registerWriter

      @Deprecated public <T> MessageBodyWriterContext registerWriter(Predicate<?> accept, Function<T,Flow.Publisher<DataChunk>> function)
      Deprecated.
      since 2.0.0 use registerWriter(MessageBodyWriter) instead
      Registers a writer function with a given predicate.
      Type Parameters:
      T - entity type
      Parameters:
      accept - the object predicate
      function - writer function
      Returns:
      this MessageBodyWriteableContent instance
    • registerWriter

      @Deprecated public <T> MessageBodyWriterContext registerWriter(Predicate<?> accept, MediaType contentType, Function<T,Flow.Publisher<DataChunk>> function)
      Deprecated.
      since 2.0.0, use registerWriter(MessageBodyWriter) instead
      Registers a writer function with a given predicate and media type.
      Type Parameters:
      T - entity type
      Parameters:
      accept - the object predicate
      contentType - the media type
      function - writer function
      Returns:
      this MessageBodyWriteableContent instance
    • 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 Parameters headers()
      Get the underlying headers.
      Returns:
      Parameters, never null
    • contentType

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

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

      public void contentType(MediaType 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
    • 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 MediaType findAccepted(Predicate<MediaType> predicate, MediaType 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 MediaType findAccepted(MediaType 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