Class HttpEncoder<T extends HttpMessageHeader,D extends HttpMessageHeader>

java.lang.Object
org.jdrupes.httpcodec.protocols.http.HttpCodec<T>
org.jdrupes.httpcodec.protocols.http.HttpEncoder<T,D>
Type Parameters:
T - the type of the message header to be encoded
D - the type of the message header decoded by the peer decoder
All Implemented Interfaces:
Codec, Encoder<T,D>
Direct Known Subclasses:
HttpRequestEncoder, HttpResponseEncoder

public abstract class HttpEncoder<T extends HttpMessageHeader,D extends HttpMessageHeader> extends HttpCodec<T> implements Encoder<T,D>
Implements an encoder for HTTP.

The class can be used as base class for both a request and a response encoder.

  • Field Details

  • Constructor Details

    • HttpEncoder

      public HttpEncoder()
      Creates a new encoder.
  • Method Details

    • setPeerDecoder

      public Encoder<T,D> setPeerDecoder(Decoder<D,T> decoder)
      Description copied from interface: Encoder
      Sets the peer decoder.

      Some decoder implementations need to know the state of the decoder or the last decoded message.

      Specified by:
      setPeerDecoder in interface Encoder<T extends HttpMessageHeader,D extends HttpMessageHeader>
      Parameters:
      decoder - the decoder
      Returns:
      the encoder
    • resultFactory

      Returns the result factory for this codec.
      Returns:
      the factory
    • pendingLimit

      public int pendingLimit()
      Returns the limit for pending body bytes.

      If the protocol is HTTP/1.0 and the message has a body but no “Content-Length” header, the only (unreliable) way to indicate the end of the body is to close the connection after all body bytes have been sent.

      The encoder tries to calculate the content length by buffering the body data up to the "pending" limit. If the body is smaller than the limit, the message is set with the calculated content length header, else the data is sent without such a header and the connection is closed.

      If the response protocol is HTTP/1.1 and there is no "Content-Length" header, chunked transfer encoding is used.

      Returns:
      the limit
    • setPendingLimit

      public void setPendingLimit(int pendingLimit)
      Sets the limit for the pending body bytes.
      Parameters:
      pendingLimit - the limit to set
    • isClosed

      public boolean isClosed()
      Returns true if the encoder does not accept further input because the processed data indicated that the connection has been or is to be closed.
      Returns:
      the result
    • startMessage

      protected abstract void startMessage(T messageHeader, Writer writer) throws IOException
      Writes the first line of the message (including the terminating CRLF).

      Must be provided by the derived class because the first line depends on whether a request or response is encoded.

      Parameters:
      messageHeader - the message header to encode (see encode(HttpMessageHeader)
      writer - the Writer to use for writing
      Throws:
      IOException - if an I/O error occurs
    • forceCloseAfterBody

      protected boolean forceCloseAfterBody()
      Force close after body.

      Used to override the header driven behavior for HTTP/1.0 responses.

      Returns:
      true, if forced
    • encode

      public void encode(T messageHeader)
      Set a new HTTP message that is to be encoded.
      Specified by:
      encode in interface Encoder<T extends HttpMessageHeader,D extends HttpMessageHeader>
      Parameters:
      messageHeader - the response
    • encode

      public Codec.Result encode(Buffer in, ByteBuffer out, boolean endOfInput)
      Encodes a HTTP message.
      Specified by:
      encode in interface Encoder<T extends HttpMessageHeader,D extends HttpMessageHeader>
      Parameters:
      in - the body data
      out - the buffer to which data is written
      endOfInput - true if there is no input left beyond the data currently in the in buffer (indicates end of body or no body at all)
      Returns:
      the result
    • header

      public Optional<T> header()
      Description copied from interface: Encoder
      Returns the last message (header) encoded.
      Specified by:
      header in interface Encoder<T extends HttpMessageHeader,D extends HttpMessageHeader>
      Returns:
      the result