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);
 
  • 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

    • status

      public Response status(int code)
      Sets the status code. The phrase is looked up from HttpStatus on the write path.
    • 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.
    • 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)
      Adds a response header. Encoded once at call time; zero-alloc on the write path.
    • header

      public Response header(byte[] preEncoded)
      Adds a pre-encoded header (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 write path.
    • isStreaming

      public boolean isStreaming()
    • 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, or an empty list if none were added.
    • writeHeaders

      public void writeHeaders(OutputStream out) throws IOException
      Writes pre-encoded custom headers directly to out. Zero-alloc when no headers are set.
      Throws:
      IOException