Package dev.relism.flash.models
Class Response
java.lang.Object
dev.relism.flash.models.Response
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 Summary
ConstructorsConstructorDescriptionResponse(int statusCode, byte[] body, ContentType contentType) Response(int statusCode, ContentType contentType) Response(int statusCode, String text, ContentType contentType) -
Method Summary
Modifier and TypeMethodDescriptionbody(byte[] bytes) chunked(InputStream is) Streaming response with unknown length; written withTransfer-Encoding: chunked.List<byte[]> Returns custom headers, or an empty list if none were added.header(byte[] preEncoded) Adds a pre-encoded header (e.g.Adds a response header.booleanredirect(HttpStatus status, String url) Redirect with an explicit 3xx status.302 Found redirect.Sets the body from a handler return value.status(int code) Sets the status code.status(HttpStatus status) Sets the status from anHttpStatusconstant.stream(InputStream is, long length) Streaming response with known length; written withContent-Length.type(ContentType ct) voidwriteHeaders(OutputStream out) Writes pre-encoded custom headers directly toout.
-
Constructor Details
-
Response
-
Response
-
Response
-
-
Method Details
-
status
Sets the status code. The phrase is looked up fromHttpStatuson the write path. -
status
Sets the status from anHttpStatusconstant. The pre-encoded bytes are used directly on the write path — zero lookup, zero allocation. -
type
-
type
-
body
-
body
-
stream
Streaming response with known length; written withContent-Length. -
chunked
Streaming response with unknown length; written withTransfer-Encoding: chunked. -
redirect
302 Found redirect. Clears the body, sets status andLocationheader. Encoded once at call time; zero-alloc on the write path.return res.redirect("/login"); -
redirect
Redirect with an explicit 3xx status. UseHttpStatus.MOVED_PERMANENTLY,HttpStatus.TEMPORARY_REDIRECT(307), orHttpStatus.PERMANENT_REDIRECT(308) when semantics matter.return res.redirect(HttpStatus.MOVED_PERMANENTLY, "/new-path"); -
header
Adds a response header. Encoded once at call time; zero-alloc on the write path. -
header
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
Sets the body from a handler return value. Accepted types:byte[],String,CharSequence. Any other non-null type throwsIllegalArgumentException— return aResponsedirectly, or serialize toString/byte[]before returning. -
getHeaders
Returns custom headers, or an empty list if none were added. -
writeHeaders
Writes pre-encoded custom headers directly toout. Zero-alloc when no headers are set.- Throws:
IOException
-