org.vertx.java.core.http

## Class HttpServerResponse

- java.lang.Object
- - org.vertx.java.core.http.HttpServerResponse

- All Implemented Interfaces:
    WriteStream

      public abstract class HttpServerResponse
      extends java.lang.Object
      implements WriteStream

  Represents a server-side HTTP response.

  An instance of this class is created and associated to every instance of `HttpServerRequest` that is created.

  It allows the developer to control the HTTP response that is sent back to the client for a partcularHTTP request. It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response.

  It also allows files to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.

  It implements `WriteStream` so it can be used with `Pump` to pump data with flow control.

  Instances of this class are not thread-safe

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `int` | `statusCode` The HTTP status code of the response.
    `java.lang.String` | `statusMessage` The HTTP status message of the response.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `HttpServerResponse()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `abstract void` | `close()` Close the underlying TCP connection
    `abstract void` | `closeHandler(Handler<java.lang.Void> handler)` Set a close handler for the response.
    `abstract void` | `end()` Ends the response.
    `abstract void` | `end(Buffer chunk)` Same as `end()` but writes some data to the response body before ending.
    `abstract void` | `end(java.lang.String chunk)` Same as `end(Buffer)` but writes a String with the default encoding before ending the response.
    `abstract void` | `end(java.lang.String chunk, java.lang.String enc)` Same as `end(Buffer)` but writes a String with the specified encoding before ending the response.
    `abstract java.util.Map<java.lang.String,java.lang.Object>` | `headers()`
    `abstract HttpServerResponse` | `putHeader(java.lang.String name, java.lang.Object value)` Put an HTTP header - fluent API
    `abstract HttpServerResponse` | `putTrailer(java.lang.String name, java.lang.Object value)` Put an HTTP trailer - fluent API
    `abstract HttpServerResponse` | `sendFile(java.lang.String filename)` Tell the kernel to stream a file as specified by `filename` directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system.
    `abstract HttpServerResponse` | `setChunked(boolean chunked)` If `chunked` is `true`, this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire.
    `abstract java.util.Map<java.lang.String,java.lang.Object>` | `trailers()`
    `abstract HttpServerResponse` | `write(Buffer chunk)` Write a `Buffer` to the response body.
    `abstract HttpServerResponse` | `write(Buffer chunk, Handler<java.lang.Void> doneHandler)` Write a `Buffer` to the response body.
    `abstract HttpServerResponse` | `write(java.lang.String chunk)` Write a `String` to the response body, encoded in UTF-8.
    `abstract HttpServerResponse` | `write(java.lang.String chunk, Handler<java.lang.Void> doneHandler)` Write a `String` to the response body, encoded in UTF-8.
    `abstract HttpServerResponse` | `write(java.lang.String chunk, java.lang.String enc)` Write a `String` to the response body, encoded using the encoding `enc`.
    `abstract HttpServerResponse` | `write(java.lang.String chunk, java.lang.String enc, Handler<java.lang.Void> doneHandler)` Write a `String` to the response body, encoded with encoding `enc`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.vertx.java.core.streams.WriteStream

      `drainHandler, exceptionHandler, setWriteQueueMaxSize, writeBuffer, writeQueueFull`

- - ### Field Detail

    - #### statusCode

          public int statusCode

      The HTTP status code of the response. The default is `200` representing `OK`.

    - #### statusMessage

          public java.lang.String statusMessage

      The HTTP status message of the response. If this is not specified a default value will be used depending on what `statusCode` has been set to.

  - ### Constructor Detail

    - #### HttpServerResponse

          protected HttpServerResponse()

  - ### Method Detail

    - #### setChunked

          public abstract HttpServerResponse setChunked(boolean chunked)

      If `chunked` is `true`, this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire.

      If chunked encoding is used the HTTP header `Transfer-Encoding` with a value of `Chunked` will be automatically inserted in the response.

      If `chunked` is `false`, this response will not use HTTP chunked encoding, and therefore if any data is written the body of the response, the total size of that data must be set in the `Content-Length` header before any data is written to the response body.

      An HTTP chunked response is typically used when you do not know the total size of the request body up front.

      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### headers

          public abstract java.util.Map<java.lang.String,java.lang.Object> headers()

      Returns:
        The HTTP headers

    - #### putHeader

          public abstract HttpServerResponse putHeader(java.lang.String name,
                                     java.lang.Object value)

      Put an HTTP header - fluent API
      Parameters:
        `name` - The header name
        `value` - The header value
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### trailers

          public abstract java.util.Map<java.lang.String,java.lang.Object> trailers()

      Returns:
        The HTTP trailers

    - #### putTrailer

          public abstract HttpServerResponse putTrailer(java.lang.String name,
                                      java.lang.Object value)

      Put an HTTP trailer - fluent API
      Parameters:
        `name` - The trailer name
        `value` - The trailer value
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### closeHandler

          public abstract void closeHandler(Handler<java.lang.Void> handler)

      Set a close handler for the response. This will be called if the underlying connection closes before the response is complete.
      Parameters:
        `handler` -

    - #### write

          public abstract HttpServerResponse write(Buffer chunk)

      Write a `Buffer` to the response body.
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### write

          public abstract HttpServerResponse write(java.lang.String chunk,
                                 java.lang.String enc)

      Write a `String` to the response body, encoded using the encoding `enc`.
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### write

          public abstract HttpServerResponse write(java.lang.String chunk)

      Write a `String` to the response body, encoded in UTF-8.
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### write

          public abstract HttpServerResponse write(Buffer chunk,
                                 Handler<java.lang.Void> doneHandler)

      Write a `Buffer` to the response body. The `doneHandler` is called after the buffer is actually written to the wire.

      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### write

          public abstract HttpServerResponse write(java.lang.String chunk,
                                 java.lang.String enc,
                                 Handler<java.lang.Void> doneHandler)

      Write a `String` to the response body, encoded with encoding `enc`. The `doneHandler` is called after the buffer is actually written to the wire.
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### write

          public abstract HttpServerResponse write(java.lang.String chunk,
                                 Handler<java.lang.Void> doneHandler)

      Write a `String` to the response body, encoded in UTF-8. The `doneHandler` is called after the buffer is actually written to the wire.
      Returns:
        A reference to this, so multiple method calls can be chained.

    - #### end

          public abstract void end(java.lang.String chunk)

      Same as `end(Buffer)` but writes a String with the default encoding before ending the response.

    - #### end

          public abstract void end(java.lang.String chunk,
                 java.lang.String enc)

      Same as `end(Buffer)` but writes a String with the specified encoding before ending the response.

    - #### end

          public abstract void end(Buffer chunk)

      Same as `end()` but writes some data to the response body before ending. If the response is not chunked and no other data has been written then the Content-Length header will be automatically set

    - #### end

          public abstract void end()

      Ends the response. If no data has been written to the response body, the actual response won't get written until this method gets called.

      Once the response has ended, it cannot be used any more.

    - #### sendFile

          public abstract HttpServerResponse sendFile(java.lang.String filename)

      Tell the kernel to stream a file as specified by `filename` directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to serve files.

    - #### close

          public abstract void close()

      Close the underlying TCP connection