Class HttpServer.Response

java.lang.Object
com.codename1.backend.HttpServer.Response
Enclosing class:
HttpServer

public static final class HttpServer.Response extends Object
What a handler returns.
  • 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

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