Class ReadableEntityBase

java.lang.Object
io.helidon.http.media.ReadableEntityBase
All Implemented Interfaces:
ReadableEntity
Direct Known Subclasses:
ClientResponseEntity, Http2ServerRequestEntity, ServerRequestEntity

public abstract class ReadableEntityBase extends Object implements ReadableEntity
Base for readable entities.
  • Constructor Details

    • ReadableEntityBase

      protected ReadableEntityBase(Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize)
      Create a new base.
      Parameters:
      readEntityFunction - accepts estimate of needed bytes, returns buffer data (the length of buffer data may differ from the estimate)
      entityProcessedRunnable - runnable to run when entity is fully read
      maxBufferedEntitySize - maximum size of a buffered entity
    • ReadableEntityBase

      protected ReadableEntityBase(Consumer<Boolean> entityRequestedCallback, Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize)
      Create a new base.
      Parameters:
      entityRequestedCallback - callback invoked when entity is requested
      readEntityFunction - accepts estimate of needed bytes, returns buffer data (the length of buffer data may differ from the estimate)
      entityProcessedRunnable - runnable to run when entity is fully read
      maxBufferedEntitySize - maximum length of a buffered entity
  • Method Details

    • empty

      public static ReadableEntity empty()
      Create a new empty readable entity (when there is no entity).
      Returns:
      empty readable entity
    • inputStream

      public InputStream inputStream()
      Description copied from interface: ReadableEntity
      Input stream to read bytes of the entity. Cannot be combined with methods ReadableEntity.as(Class) or ReadableEntity.as(io.helidon.common.GenericType) If there is no entity, returns input stream on empty byte array. This stream is not limited by max-buffered-entity-size.
      Specified by:
      inputStream in interface ReadableEntity
      Returns:
      input stream to entity bytes
    • buffer

      public void buffer()
      Description copied from interface: ReadableEntity
      If called before ReadableEntity.inputStream(), it will buffer the entity and support multiple calls to ReadableEntity.inputStream() after that. Normally, ReadableEntity.inputStream() can only be called at most once. On a server request, this buffers decoded bytes and applies both the protocol's max-buffered-entity-size and the configured max-payload-size before allowing the entity to be read again.
      Specified by:
      buffer in interface ReadableEntity
    • as

      public <T> T as(Class<T> type)
      Description copied from interface: ReadableEntity
      Get the entity as a specific class. The entity will use MediaContext to find correct media mapper.
      Specified by:
      as in interface ReadableEntity
      Type Parameters:
      T - type of the entity
      Parameters:
      type - class of the entity
      Returns:
      entity correctly typed
    • asOptional

      public <T> Optional<T> asOptional(Class<T> type)
      Description copied from interface: ReadableEntity
      Get the entity as a specific class (optional). The entity will use MediaContext to find correct media mapper.
      Specified by:
      asOptional in interface ReadableEntity
      Type Parameters:
      T - type of the entity
      Parameters:
      type - class of the entity
      Returns:
      entity correctly typed, or an empty optional if entity is not present
    • as

      public final <T> T as(GenericType<T> type)
      Description copied from interface: ReadableEntity
      Get the entity as a specific type. The entity will use MediaContext to find correct media mapper.
      Specified by:
      as in interface ReadableEntity
      Type Parameters:
      T - type of the entity
      Parameters:
      type - generic type of the entity
      Returns:
      entity correctly typed
    • asOptional

      public <T> Optional<T> asOptional(GenericType<T> type)
      Description copied from interface: ReadableEntity
      Get the entity as a specific type (optional). The entity will use MediaContext to find correct media mapper.
      Specified by:
      asOptional in interface ReadableEntity
      Type Parameters:
      T - type of the entity
      Parameters:
      type - generic type of the entity
      Returns:
      entity correctly typed, or an empty optional if entity is not present
    • consumed

      public boolean consumed()
      Description copied from interface: ReadableEntity
      Whether this entity has been consumed already.
      Specified by:
      consumed in interface ReadableEntity
      Returns:
      true if the entity is already consumed; a consumed entity cannot be consumed again
    • consume

      public void consume()
      Description copied from interface: ReadableEntity
      Consume the entity if not yet consumed. Reads all bytes from the entity and discards them. If entity is larger than allowed, appropriate HTTP exception is thrown. If entity is already consumed, this is a no-op.
      Specified by:
      consume in interface ReadableEntity
    • hasEntity

      public boolean hasEntity()
      Description copied from interface: ReadableEntity
      Whether an entity actually exists.
      Specified by:
      hasEntity in interface ReadableEntity
      Returns:
      true if an entity exists and can be read
    • entityAs

      protected abstract <T> T entityAs(GenericType<T> type)
      Read entity as a specific generic type.
      Type Parameters:
      T - type of the entity
      Parameters:
      type - type the entity should be coerced into
      Returns:
      entity value
    • readAllBytes

      protected byte[] readAllBytes()
      Read all bytes of the entity into memory.
      Returns:
      all bytes of the entity
    • readEntityFunction

      protected Function<Integer, BufferData> readEntityFunction()
      Function to request entity bytes.
      Returns:
      function that accepts an integer with a suggestion of how many bytes are requested, and returns a buffer
    • entityProcessedRunnable

      protected Runnable entityProcessedRunnable()
      Runnable to run once the entity is fully consumed.
      Returns:
      runnable to run on consumed
    • maxBufferedEntitySize

      protected long maxBufferedEntitySize()
      Maximum entity length for materializing reads and explicit buffering.
      Returns:
      maximum number of input bytes to materialize
    • bufferedEntityAvailable

      protected boolean bufferedEntityAvailable()
      Whether an entity has been buffered and can be read again. An entity implementation that decodes its input stream must not decode this buffer again.
      Returns:
      whether a buffered entity is available
    • bufferedEntityLimitExceeded

      protected RuntimeException bufferedEntityLimitExceeded()
      Exception to throw when explicit buffering exceeds its configured size.
      Returns:
      exception for a buffered entity that exceeds the limit
    • materializedEntityLimitExceeded

      protected RuntimeException materializedEntityLimitExceeded()
      Exception to throw when a materializing reader exceeds its configured size limit. Server request implementations override this to produce an HTTP error response.
      Returns:
      exception for a materialized entity that exceeds the limit
    • readEntity

      protected <T> T readEntity(EntityReader<T> reader, GenericType<T> type, Headers headers)
      Read a server request with the selected media reader, enforcing the materialization limit. If a reader wraps or suppresses a materialization-limit failure, the original limit failure is thrown. Streaming readers retain responsibility for stream ownership as described by EntityReader.streaming().
      Type Parameters:
      T - type of the entity
      Parameters:
      reader - the selected media reader
      type - type of the entity
      headers - request headers
      Returns:
      entity value
    • readEntity

      protected <T> T readEntity(EntityReader<T> reader, GenericType<T> type, Headers requestHeaders, Headers responseHeaders)
      Read a client response with the selected media reader, enforcing the materialization limit. If a reader wraps or suppresses a materialization-limit failure, the original limit failure is thrown. Streaming readers retain responsibility for stream ownership as described by EntityReader.streaming().
      Type Parameters:
      T - type of the entity
      Parameters:
      reader - the selected media reader
      type - type of the entity
      requestHeaders - request headers
      responseHeaders - response headers
      Returns:
      entity value