Class Response

java.lang.Object
dev.relism.flash.models.Response

public class Response extends Object
HTTP response. All mutating methods return this for fluent chaining.

 // fixed body
 return new Response(200, "ok", ContentType.TEXT_PLAIN);

 // known-length stream → Content-Length header
 return new Response(200, ContentType.BINARY).stream(Files.newInputStream(p), Files.size(p));

 // unknown-length stream → Transfer-Encoding: chunked
 return new Response(200, ContentType.TEXT_PLAIN).chunked(source);
 
The connection driver (e.g. Http1Connection) owns one Response instance per connection, reset before every handler call rather than reallocated — the same treatment Request gets (see its Javadoc for the full pooling/dev-mode-guard rationale, which applies identically here). A handler that returns a different Response instance (e.g. return new Response(404, "Not Found", ContentType.TEXT_PLAIN);) is fully supported — that instance is a normal, unpooled, freshly-constructed object like any public-constructor Response always was; only the connection driver's own default instance is pooled and poisoned after use.
  • Constructor Details

    • Response

      public Response(int statusCode, ContentType contentType)
    • Response

      public Response(int statusCode, byte[] body, ContentType contentType)
    • Response

      public Response(int statusCode, String text, ContentType contentType)
  • Method Details

    • reset

      public Response reset(int statusCode, ContentType contentType)
      Repositions this instance for a new request/response cycle — clears the body, stream, status, content type, and every header recorded by the previous cycle. Public because the connection driver that owns the pooled instance lives in a different package (matching RequestLine.reset(dev.relism.flash.http.HttpMethod, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.flash.models.HeaderView)'s precedent); user code never calls this.
    • recycle

      public void recycle()
      Marks this instance unsafe for further use — see Request.recycle() for the full rationale, identical here. public for the same cross-package reason.
    • status

      public Response status(int code)
      Sets the status code. The phrase is looked up from HttpStatus on the write path.
    • setStatusCode

      public void setStatusCode(int code)
      Lombok-style setter kept for API compatibility — equivalent to status(int) without the fluent return.
    • status

      public Response status(HttpStatus status)
      Sets the status from an HttpStatus constant. The pre-encoded bytes are used directly on the write path — zero lookup, zero allocation.
    • type

      public Response type(ContentType ct)
    • type

      public Response type(String ct)
    • body

      public Response body(byte[] bytes)
    • body

      public Response body(String text)
    • stream

      public Response stream(InputStream is, long length)
      Streaming response with known length; written with Content-Length.
    • chunked

      public Response chunked(InputStream is)
      Streaming response with unknown length; written with Transfer-Encoding: chunked.
    • streaming

      public Response streaming(Consumer<ResponseStream> producer)
      Push-style streaming response with bounded blocking backpressure.
    • trailer

      public Response trailer(String name, String value)
      Adds a trailer rendered after the response body on both HTTP versions.
    • trailer

      public Response trailer(PreEncodedHeader trailer)
      Adds a pre-encoded structured trailer.
    • redirect

      public Response redirect(String url)
      302 Found redirect. Clears the body, sets status and Location header. Encoded once at call time; zero-alloc on the write path.
      
       return res.redirect("/login");
       
    • redirect

      public Response redirect(HttpStatus status, String url)
      Redirect with an explicit 3xx status. Use HttpStatus.MOVED_PERMANENTLY, HttpStatus.TEMPORARY_REDIRECT (307), or HttpStatus.PERMANENT_REDIRECT (308) when semantics matter.
      
       return res.redirect(HttpStatus.MOVED_PERMANENTLY, "/new-path");
       
    • header

      public Response header(String name, String value)
      reused byte region (via ByteWriter.writeAscii(java.lang.String)) instead of building an intermediate String and re-encoding it — zero allocation once the region has grown to this connection's high-water mark.
    • header

      public Response header(PreEncodedHeader preEncoded)
      Adds a header from a PreEncodedHeader built once (typically at boot). Copies its precomputed name/value bytes into this response's region — a memcpy, not a re-encode. Preserving the field structure makes it usable by both HTTP versions.
    • header

      public Response header(byte[] preEncoded)
      Adds a pre-encoded, fully-rendered header line (e.g. a static "X-RateLimit-Limit: 100\r\n" byte array pre-built at boot time). Zero-alloc on both the call path and the h1 write path.

      h1-only: a rendered "Name: Value\r\n" line carries no structured name/value data an HPACK encoder could use, so this header is not representable on a HTTP/2 response path — prefer header(PreEncodedHeader) for anything that must render correctly on both protocols. Kept for existing HTTP/1-only callers.

    • isStreaming

      public boolean isStreaming()
    • isChunked

      public boolean isChunked()
    • isPushStreaming

      public boolean isPushStreaming()
      Internal distinction between producer-driven and InputStream-driven response bodies.
    • getStatusCode

      public int getStatusCode()
    • getStatusBytes

      public byte[] getStatusBytes()
    • getBody

      public byte[] getBody()
    • getContentType

      public byte[] getContentType()
    • getStream

      public InputStream getStream()
    • getStreamLength

      public long getStreamLength()
    • hasTrailers

      public boolean hasTrailers()
    • writeTrailers

      public void writeTrailers(OutputStream output) throws IOException
      Throws:
      IOException
    • setBody

      public Response setBody(Object body)
      Sets the body from a handler return value. Accepted types: byte[], String, CharSequence. Any other non-null type throws IllegalArgumentException — return a Response directly, or serialize to String/byte[] before returning.
    • getHeaders

      public List<byte[]> getHeaders()
      Returns custom headers as fully-rendered "Name: Value\r\n" lines, or an empty list if none were added. Introspection/debugging accessor — reconstructs each line from the internal region on every call, so it is not on the zero-alloc write path; writeHeaders(java.io.OutputStream) and ResponseSerializer read the internal representation directly instead of going through this method.
    • writeHeadersInto

      public void writeHeadersInto(ByteWriter head)
      Writes every custom header directly into head (a scratch ByteWriter — This is what Http1ResponseWriter uses; writeHeaders(OutputStream) below (the OutputStream equivalent) exists for the streaming-body write paths that cannot fold their whole write into one scratch buffer.
    • writeHeaders

      public void writeHeaders(OutputStream out) throws IOException
      Writes every custom header directly to out, in call order. Zero-alloc when no headers are set or on a warm region.
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object