Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public static final class HttpServer.Response

  1. Object
  2. HttpServer.Response
What a handler returns.

Constructors

public Response(int status, String contentType, byte[] body)
public Response(int status, String contentType, byte[] body, Map extraHeaders)A body AND application headers, which nothing public could express.

Methods

public static HttpServer.Response stream(int status, String contentType, HttpServer.BodyWriter writer)A response generated as it is downloaded.
public static HttpServer.Response file(int status, String contentType, int fd, long offset, long length, Map extraHeaders)A response whose body is a file.
public static HttpServer.Response text(int status, String body)
public static HttpServer.Response json(int status, String body)
public static HttpServer.Response jsonValue(int status, Object value)A JSON response serialised straight into the connection’s write buffer.
public static HttpServer.Response empty(int status, String contentType, Map extraHeaders)A response with headers but no body, for 304 and for HEAD.
public int getStatus()
public HttpServer.Response header(String name, String value)Adds a response header and returns this Response.

Inherited methods

Constructor details

Response

public Response(int status, String contentType, byte[] body)

Response

public Response(int status, String contentType, byte[] body, Map extraHeaders)

A body AND application headers, which nothing public could express.

The constructor above always passed null for them, empty() takes headers but discards the body, and file() wants a descriptor – so a handler returning JSON with a Set-Cookie, a CORS header or a cache directive had no supported way to say so, even though both protocol writers send extra headers. Server-owned names are still refused at write time.

Method details

stream

public static HttpServer.Response stream(int status, String contentType, HttpServer.BodyWriter writer)
A response generated as it is downloaded. Its length is unknown: HTTP/1.1 uses chunked framing, HTTP/1.0 closes after the body, and HTTP/2 uses DATA frames with flow control. A generation failure aborts the response; headers have already been sent, so it cannot be replaced with a successful error body.

file

public static HttpServer.Response file(int status, String contentType, int fd, long offset, long length, Map extraHeaders)
A response whose body is a file. The server sends it with sendfile where the platform has it, so the bytes never enter user space, and CLOSES the descriptor when it is done – a handler that returned one must not.

text

public static HttpServer.Response text(int status, String body)

json

public static HttpServer.Response json(int status, String body)

jsonValue

public static HttpServer.Response jsonValue(int status, Object value)

A JSON response serialised straight into the connection’s write buffer.

Prefer this to json(status, Json.write(value)): that builds a StringBuilder, grows its char[], copies it into a String and encodes that to bytes, for a document about to go to a socket and be discarded. Deliberately a DIFFERENT NAME rather than an overload taking Object – an overload would bind a String-typed variable to this method and double-encode it, which is exactly the kind of thing that is found in production rather than in review.

empty

public static HttpServer.Response empty(int status, String contentType, Map extraHeaders)
A response with headers but no body, for 304 and for HEAD.

getStatus

public int getStatus()

header

public HttpServer.Response header(String name, String value)
Adds a response header and returns this Response. A copy is made rather than writing into the map the Response was built with, which may be a caller’s constant; a header the server owns – Date, Content-Length – is refused when the response is written.