Class ServerResponseBase<T extends ServerResponseBase<T>>

java.lang.Object
io.helidon.webserver.http.ServerResponseBase<T>
Type Parameters:
T - type of the response extending this class to allow fluent API
All Implemented Interfaces:
RoutingResponse, ServerResponse

public abstract class ServerResponseBase<T extends ServerResponseBase<T>> extends Object implements RoutingResponse
Base class for common server response tasks that can be shared across HTTP versions.
  • Field Details

    • STREAM_RESULT_NAME

      protected static final HeaderName STREAM_RESULT_NAME
      Stream result trailer name.
    • STREAM_RESULT_OK

      protected static final Header STREAM_RESULT_OK
      Stream result OK.
    • STREAM_TRAILERS

      protected static final Header STREAM_TRAILERS
      Stream status trailers.
  • Constructor Details

    • ServerResponseBase

      protected ServerResponseBase(ConnectionContext ctx, ServerRequest request)
      Create server response.
      Parameters:
      ctx - context
      request - server request
  • Method Details

    • status

      public T status(Status status)
      Description copied from interface: ServerResponse
      Status of the response.

      The status is mutable response metadata. Once configured, it remains on this response unless later application code or error handling changes it. Calling ServerResponse.next(), calling ServerResponse.reroute(String), or throwing an exception does not clear the configured status automatically.

      Configure the status before calling ServerResponse.send() or ServerResponse.outputStream() when it should apply to the response emitted by this route.

      Specified by:
      status in interface ServerResponse
      Parameters:
      status - HTTP status
      Returns:
      this instance
    • status

      public Status status()
      Description copied from interface: ServerResponse
      Configured HTTP status, if not configured, returns Status.OK_200.
      Specified by:
      status in interface ServerResponse
      Returns:
      status
    • send

      public void send()
      Description copied from interface: ServerResponse
      Send a response with no entity. Use this method for a HEAD response after configuring the representation metadata in the response headers.
      Specified by:
      send in interface ServerResponse
    • send

      public void send(Object entity)
      Description copied from interface: ServerResponse
      Send an entity, a MediaContext will be used to serialize the entity.
      Specified by:
      send in interface ServerResponse
      Parameters:
      entity - entity object
    • beforeSend

      public ServerResponse beforeSend(Runnable listener)
      Description copied from interface: ServerResponse
      Executed right before the first byte is written to the socket (including response status and headers). Response can be modified (i.e. headers, status) at this point, though modifying the entity may not be done, as this method is most likely called from within one of the ServerResponse.send() methods. Changing the response status is supported only if the new status has the same response entity semantics. For example, changing between an entity-bearing status and a status without an entity may leave response framing inconsistent and break the response. The listener remains registered if error handling replaces the unsent response entity.

      Note: this method is implemented as a default method that does nothing, for backward compatibility.

      Specified by:
      beforeSend in interface ServerResponse
      Parameters:
      listener - lister to add to the list of listeners that will be triggered before the response is sent
      Returns:
      this instance
    • entityBeforeSend

      public void entityBeforeSend(Runnable listener)
      Description copied from interface: RoutingResponse
      Register an infrastructure listener associated with the current response entity.

      Implementations which distinguish entity-scoped listeners remove the listener if error handling replaces the unsent entity. Application listeners should use ServerResponse.beforeSend(Runnable) so they also apply to a replacement error response. The default fallback registers the listener through the public method and therefore retains it.

      Specified by:
      entityBeforeSend in interface RoutingResponse
      Parameters:
      listener - listener invoked before the response is sent
    • automaticContentEncoding

      public T automaticContentEncoding(boolean enabled)
      Description copied from interface: RoutingResponse
      Configure whether the WebServer response layer may automatically encode the response entity using the listener content encoding context. This does not remove or rewrite any explicitly configured Content-Encoding header. Repeated calls replace the previous setting until response content encoding is selected.
      Specified by:
      automaticContentEncoding in interface RoutingResponse
      Specified by:
      automaticContentEncoding in interface ServerResponse
      Parameters:
      enabled - whether automatic response content encoding is enabled
      Returns:
      this instance
    • contentEncoder

      public T contentEncoder(ContentEncoder encoder)
      Description copied from interface: RoutingResponse
      Configure an explicit response content encoder. Repeated calls replace the previous encoder until response content encoding is selected. The selected encoder's headers are applied once during selection.
      Specified by:
      contentEncoder in interface RoutingResponse
      Specified by:
      contentEncoder in interface ServerResponse
      Parameters:
      encoder - content encoder
      Returns:
      this instance
    • whenSent

      public T whenSent(Runnable listener)
      Description copied from interface: ServerResponse
      Completed when last byte is buffered for socket write.
      Specified by:
      whenSent in interface ServerResponse
      Parameters:
      listener - listener to add to the list of listeners that will be triggered once the response is sent
      Returns:
      this instance
    • reroute

      public T reroute(String newPath)
      Description copied from interface: ServerResponse
      Re-route using a different path. Configured response status and headers remain on this response unless later handling changes them.
      Specified by:
      reroute in interface ServerResponse
      Parameters:
      newPath - new path to use
      Returns:
      this instance
    • reroute

      public T reroute(String path, UriQuery query)
      Description copied from interface: ServerResponse
      Re-route using a different path and query. Configured response status and headers remain on this response unless later handling changes them.
      Specified by:
      reroute in interface ServerResponse
      Parameters:
      path - new path
      query - new query
      Returns:
      this instance
      See Also:
    • next

      public T next()
      Description copied from interface: ServerResponse
      Continue processing with the next route (and if none found, return a Status.NOT_FOUND_404). If any entity method was called, this method will throw an exception. Configured response status and headers remain on this response unless later handling changes them.
      Specified by:
      next in interface ServerResponse
      Returns:
      this instance
    • resetRouting

      public void resetRouting()
      Description copied from interface: RoutingResponse
      Reset routing information (nexted, rerouted etc.).
      Specified by:
      resetRouting in interface RoutingResponse
    • shouldReroute

      public boolean shouldReroute()
      Description copied from interface: RoutingResponse
      Should we reroute this exchange.
      Specified by:
      shouldReroute in interface RoutingResponse
      Returns:
      whether rerouting was requested
    • reroutePrologue

      public HttpPrologue reroutePrologue(HttpPrologue prologue)
      Description copied from interface: RoutingResponse
      A new, rerouted prologue.
      Specified by:
      reroutePrologue in interface RoutingResponse
      Parameters:
      prologue - current prologue
      Returns:
      prologue to use when rerouting
    • isNexted

      public boolean isNexted()
      Description copied from interface: RoutingResponse
      Whether this request is nexted (ServerResponse.next() was called).
      Specified by:
      isNexted in interface RoutingResponse
      Returns:
      if nexted
    • isResponseHandled

      public boolean isResponseHandled()
      Description copied from interface: ServerResponse
      Whether this response has already been handled by the application.

      This method is intended for generated or framework code that may send a default response only when user code has not already sent an entity, obtained an output stream, or selected another routing action. Header and status changes alone do not handle the response.

      The default implementation returns ServerResponse.isSent().

      Specified by:
      isResponseHandled in interface ServerResponse
      Returns:
      whether response handling was already selected
    • resetEntity

      public boolean resetEntity()
      Description copied from interface: RoutingResponse
      Reset the response entity so an unsent response can be replaced while preserving response metadata unrelated to the entity, such as CORS, cookies, cache controls, and Vary headers.

      This method is intended for Helidon infrastructure that replaces a failed response entity.

      Implementations that track entity metadata must override this method to reset entity buffers and remove framing, representation, validator, range, and trailer headers. Implementations with separate trailer state must reset that state as well. This method does not change the response status. An error handler that replaces the entity is responsible for configuring or deliberately retaining an appropriate status before sending the replacement response.

      For compatibility with existing RoutingResponse implementations, the default implementation calls RoutingResponse.resetStream().

      Specified by:
      resetEntity in interface RoutingResponse
      Returns:
      true if reset was successful and a new entity can be created instead of the existing one, false if reset failed and status and headers (and maybe entity bytes) were already sent
    • streamFilter

      public void streamFilter(UnaryOperator<OutputStream> filterFunction)
      Description copied from interface: ServerResponse
      Configure a custom output stream to wrap the output stream of the response. The filter remains registered if error handling replaces the unsent response entity. A filter that changes representation metadata, such as Content-Encoding, must use ServerResponse.beforeSend(Runnable) to configure the matching headers for each entity that is sent.
      Specified by:
      streamFilter in interface ServerResponse
      Parameters:
      filterFunction - the function to replace output stream of this response with a user provided one
    • entityStreamFilter

      public void entityStreamFilter(UnaryOperator<OutputStream> filterFunction)
      Description copied from interface: RoutingResponse
      Configure an infrastructure output stream filter associated with the current response entity.

      Implementations which distinguish entity-scoped filters remove the filter if error handling replaces the unsent entity. Application filters should use ServerResponse.streamFilter(UnaryOperator) so they also apply to a replacement error response. The default fallback registers the filter through the public method and therefore retains it.

      Specified by:
      entityStreamFilter in interface RoutingResponse
      Parameters:
      filterFunction - function that wraps the response output stream
    • beforeTrailers

      public ServerResponse beforeTrailers(Consumer<ServerResponseTrailers> beforeTrailers)
      Description copied from interface: ServerResponse
      Callback to update any last minute trailers before they are written to the output stream. The callback is associated with the current response entity and is cleared if error handling replaces that entity before it is sent. A response-scoped policy should use ServerResponse.beforeSend(Runnable) to register a fresh callback for each entity.
      Specified by:
      beforeTrailers in interface ServerResponse
      Parameters:
      beforeTrailers - consumer of mutable trailers
      Returns:
      this instance
    • beforeTrailers

      protected Consumer<ServerResponseTrailers> beforeTrailers()
      Gets consumer for server response trailers if registered on this response.
      Returns:
      consumer if registered or null otherwise
    • findSinkProvider

      protected final SinkProvider<?> findSinkProvider(GenericType<? extends Sink<?>> sinkType, ServerRequest request)
      Find a sink provider for the requested sink type.
      Parameters:
      sinkType - sink type
      request - server request
      Returns:
      matching sink provider
    • createSink

      protected final <X extends Sink<?>> X createSink(SinkProvider<?> provider, ServerRequest request, ConnectionContext connectionContext, Function<Runnable, Optional<OutputStream>> entityOutputStreamProvider, Runnable closeRunnable, Runnable flushHeadersRunnable)
      Create a sink using the shared provider context and protocol-specific callbacks.
      Type Parameters:
      X - sink type
      Parameters:
      provider - sink provider
      request - server request
      connectionContext - connection context
      entityOutputStreamProvider - protocol entity stream provider
      closeRunnable - protocol close callback
      flushHeadersRunnable - protocol header flush callback
      Returns:
      created sink
    • hasStreamFilter

      protected final boolean hasStreamFilter()
      Whether this response has any output stream filters.
      Returns:
      whether an output stream filter is configured
    • prepareFilteredHeadResponse

      protected final void prepareFilteredHeadResponse()
      Discard entity stream filters and suppress an implicit content length for a HEAD response.
    • suppressImplicitContentLength

      protected final boolean suppressImplicitContentLength(int length)
      Whether the protocol implementation should suppress an implicit content length.
      Parameters:
      length - response entity length
      Returns:
      whether to suppress the implicit content length
    • applyStreamFilters

      protected final OutputStream applyStreamFilters(OutputStream outputStream)
      Apply configured output stream filters.
      Parameters:
      outputStream - output stream to wrap
      Returns:
      filtered output stream
    • mediaContext

      protected MediaContext mediaContext()
      Gets media context for this response.
      Returns:
      the media context
    • entityBytes

      protected byte[] entityBytes(byte[] configuredEntity)
      Entity bytes encoded using content encoding. Automatic encoding is skipped for an empty entity. An explicitly configured encoder is applied unless the response status does not allow an entity.
      Parameters:
      configuredEntity - plain bytes
      Returns:
      encoded bytes or same entity array if encoding is disabled
    • entityBytes

      protected byte[] entityBytes(byte[] configuredEntity, int position, int length)
      Entity bytes encoded using content encoding. Automatic encoding is skipped for an empty entity. An explicitly configured encoder is applied unless the response status does not allow an entity.
      Parameters:
      configuredEntity - plain bytes
      position - starting position
      length - number of bytes
      Returns:
      encoded bytes or same entity array if encoding is disabled
    • contentEncode

      protected OutputStream contentEncode(OutputStream outputStream)
      Encode content using requested/default content encoder.
      Parameters:
      outputStream - output stream to write encoded data to
      Returns:
      output stream to write plain data to
    • contentEncode

      protected OutputStream contentEncode(OutputStream outputStream, boolean allowAutomaticEncoding)
      Encode content using an explicitly configured encoder, or an automatic encoder when allowed.
      Parameters:
      outputStream - output stream to write encoded data to
      allowAutomaticEncoding - whether automatic encoding may be selected
      Returns:
      output stream to write plain data to
    • resetAutomaticContentEncoding

      protected void resetAutomaticContentEncoding()
      Reset response-layer automatic content encoding to its default behavior.
    • resetContentEncoding

      protected void resetContentEncoding()
      Reset all response-layer content encoding state to its defaults.
    • beforeSend

      protected void beforeSend()
      Execute before send runnables.
    • afterSend

      protected void afterSend()
      Execute after send runnables.