Class HttpServerResponse

  • All Implemented Interfaces:
    StreamBase, WriteStream<Buffer>

    public class HttpServerResponse
    extends Object
    implements WriteStream<Buffer>
    Represents a server-side HTTP response.

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

    It allows the developer to control the HTTP response that is sent back to the client for a particular HTTP 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.

    NOTE: This class has been automatically generated from the original non Mutiny-ified interface using Vert.x codegen.

    • Constructor Detail

      • HttpServerResponse

        public HttpServerResponse​(io.vertx.core.http.HttpServerResponse delegate)
    • Method Detail

      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • end

        public io.smallrye.mutiny.Uni<Void> end()
        Same as end() but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Specified by:
        end in interface WriteStream<Buffer>
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • endAndAwait

        public Void endAndAwait()
        Blocking variant of WriteStream.end().

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Specified by:
        endAndAwait in interface WriteStream<Buffer>
        Returns:
        the Void instance produced by the operation
      • write

        public io.smallrye.mutiny.Uni<Void> write​(Buffer data)
        Same as write(io.vertx.mutiny.core.buffer.Buffer) but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Specified by:
        write in interface WriteStream<Buffer>
        Parameters:
        data -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • writeAndAwait

        public Void writeAndAwait​(Buffer data)
        Blocking variant of write(io.vertx.mutiny.core.buffer.Buffer).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Specified by:
        writeAndAwait in interface WriteStream<Buffer>
        Parameters:
        data -
        Returns:
        the Void instance produced by the operation
      • setWriteQueueMaxSize

        public HttpServerResponse setWriteQueueMaxSize​(int maxSize)
        Specified by:
        setWriteQueueMaxSize in interface WriteStream<Buffer>
        Parameters:
        maxSize - the max size of the write stream
        Returns:
        a reference to this, so the API can be used fluently
      • getStatusCode

        public int getStatusCode()
        Returns:
        the HTTP status code of the response. The default is 200 representing OK.
      • setStatusCode

        public HttpServerResponse setStatusCode​(int statusCode)
        Parameters:
        statusCode -
        Returns:
        a reference to this, so the API can be used fluently
      • getStatusMessage

        public String getStatusMessage()
        Returns:
        the HTTP status message of the response. If this is not specified a default value will be used depending on what setStatusCode(int) has been set to.
      • setStatusMessage

        public HttpServerResponse setStatusMessage​(String statusMessage)
        Parameters:
        statusMessage -
        Returns:
        a reference to this, so the API can be used fluently
      • setChunked

        public HttpServerResponse setChunked​(boolean chunked)
        Parameters:
        chunked -
        Returns:
        a reference to this, so the API can be used fluently
      • isChunked

        public boolean isChunked()
        Returns:
        is the response chunked?
      • headers

        public MultiMap headers()
        Returns:
        The HTTP headers
      • putHeader

        public HttpServerResponse putHeader​(String name,
                                            String value)
        Parameters:
        name - the header name
        value - the header value.
        Returns:
        a reference to this, so the API can be used fluently
      • trailers

        public MultiMap trailers()
        Returns:
        The HTTP trailers
      • putTrailer

        public HttpServerResponse putTrailer​(String name,
                                             String value)
        Parameters:
        name - the trailer name
        value - the trailer value
        Returns:
        a reference to this, so the API can be used fluently
      • closeHandler

        public HttpServerResponse closeHandler​(Consumer<Void> handler)
        Parameters:
        handler - the handler
        Returns:
        a reference to this, so the API can be used fluently
      • endHandler

        public HttpServerResponse endHandler​(Consumer<Void> handler)
        Parameters:
        handler - the handler
        Returns:
        a reference to this, so the API can be used fluently
      • write

        public io.smallrye.mutiny.Uni<Void> write​(String chunk,
                                                  String enc)
        Same as write(io.vertx.mutiny.core.buffer.Buffer) but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        chunk -
        enc -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • writeAndAwait

        public Void writeAndAwait​(String chunk,
                                  String enc)
        Blocking variant of write(String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        chunk -
        enc -
        Returns:
        the Void instance produced by the operation
      • write

        public io.smallrye.mutiny.Uni<Void> write​(String chunk)
        Same as write(io.vertx.mutiny.core.buffer.Buffer) but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        chunk -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • writeAndAwait

        public Void writeAndAwait​(String chunk)
        Blocking variant of write(String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        chunk -
        Returns:
        the Void instance produced by the operation
      • writeContinue

        public HttpServerResponse writeContinue()
        Returns:
        a reference to this, so the API can be used fluently
      • end

        public io.smallrye.mutiny.Uni<Void> end​(String chunk)
        Same as end() but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        chunk -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • endAndAwait

        public Void endAndAwait​(String chunk)
        Blocking variant of end(String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        chunk -
        Returns:
        the Void instance produced by the operation
      • end

        public io.smallrye.mutiny.Uni<Void> end​(String chunk,
                                                String enc)
        Same as end() but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        chunk -
        enc -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • endAndAwait

        public Void endAndAwait​(String chunk,
                                String enc)
        Blocking variant of end(String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        chunk -
        enc -
        Returns:
        the Void instance produced by the operation
      • end

        public io.smallrye.mutiny.Uni<Void> end​(Buffer chunk)
        Same as end() but with an handler called when the operation completes

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Specified by:
        end in interface WriteStream<Buffer>
        Parameters:
        chunk -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • endAndAwait

        public Void endAndAwait​(Buffer chunk)
        Blocking variant of end(io.vertx.mutiny.core.buffer.Buffer).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Specified by:
        endAndAwait in interface WriteStream<Buffer>
        Parameters:
        chunk -
        Returns:
        the Void instance produced by the operation
      • sendFile

        public io.smallrye.mutiny.Uni<Void> sendFile​(String filename)
        Like sendFile(java.lang.String) but providing a handler which will be notified once the file has been completely written to the wire.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        filename - path to the file to serve
        Returns:
        a reference to this, so the API can be used fluently
      • sendFileAndAwait

        public Void sendFileAndAwait​(String filename)
        Blocking variant of sendFile(String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        filename - path to the file to serve
        Returns:
        a reference to this, so the API can be used fluently
      • sendFile

        public io.smallrye.mutiny.Uni<Void> sendFile​(String filename,
                                                     long offset)
        Like sendFile(java.lang.String) but providing a handler which will be notified once the file has been completely written to the wire.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        filename - path to the file to serve
        offset - the offset to serve from
        Returns:
        a reference to this, so the API can be used fluently
      • sendFileAndAwait

        public Void sendFileAndAwait​(String filename,
                                     long offset)
        Blocking variant of sendFile(String,long).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        filename - path to the file to serve
        offset - the offset to serve from
        Returns:
        a reference to this, so the API can be used fluently
      • sendFile

        public io.smallrye.mutiny.Uni<Void> sendFile​(String filename,
                                                     long offset,
                                                     long length)
        Like sendFile(java.lang.String) but providing a handler which will be notified once the file has been completely written to the wire.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        filename - path to the file to serve
        offset - the offset to serve from
        length - the length to serve to
        Returns:
        a reference to this, so the API can be used fluently
      • sendFileAndAwait

        public Void sendFileAndAwait​(String filename,
                                     long offset,
                                     long length)
        Blocking variant of sendFile(String,long,long).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        filename - path to the file to serve
        offset - the offset to serve from
        length - the length to serve to
        Returns:
        a reference to this, so the API can be used fluently
      • close

        public void close()
      • ended

        public boolean ended()
        Returns:
        has the response already ended?
      • closed

        public boolean closed()
        Returns:
        has the underlying TCP connection corresponding to the request already been closed?
      • headWritten

        public boolean headWritten()
        Returns:
        have the headers for the response already been written?
      • headersEndHandler

        public HttpServerResponse headersEndHandler​(Consumer<Void> handler)
        Parameters:
        handler - the handler
        Returns:
        a reference to this, so the API can be used fluently
      • bodyEndHandler

        public HttpServerResponse bodyEndHandler​(Consumer<Void> handler)
        Parameters:
        handler - the handler
        Returns:
        a reference to this, so the API can be used fluently
      • bytesWritten

        public long bytesWritten()
        Returns:
        the total number of bytes written for the body of the response.
      • streamId

        public int streamId()
        Returns:
        the id of the stream of this response, for HTTP/1.x
      • pushAndAwait

        public HttpServerResponse pushAndAwait​(io.vertx.core.http.HttpMethod method,
                                               String host,
                                               String path)
        Blocking variant of push(HttpMethod,String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method -
        host -
        path -
        Returns:
        the HttpServerResponse instance produced by the operation
      • push

        public io.smallrye.mutiny.Uni<HttpServerResponse> push​(io.vertx.core.http.HttpMethod method,
                                                               String path,
                                                               MultiMap headers)
        Like push(io.vertx.core.http.HttpMethod, java.lang.String, java.lang.String) with the host copied from the current request.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method -
        path -
        headers -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • pushAndAwait

        public HttpServerResponse pushAndAwait​(io.vertx.core.http.HttpMethod method,
                                               String path,
                                               MultiMap headers)
        Blocking variant of push(HttpMethod,String,io.vertx.mutiny.core.MultiMap).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method -
        path -
        headers -
        Returns:
        the HttpServerResponse instance produced by the operation
      • push

        public io.smallrye.mutiny.Uni<HttpServerResponse> push​(io.vertx.core.http.HttpMethod method,
                                                               String path)
        Like push(io.vertx.core.http.HttpMethod, java.lang.String, java.lang.String) with the host copied from the current request.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method -
        path -
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • pushAndAwait

        public HttpServerResponse pushAndAwait​(io.vertx.core.http.HttpMethod method,
                                               String path)
        Blocking variant of push(HttpMethod,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method -
        path -
        Returns:
        the HttpServerResponse instance produced by the operation
      • push

        public io.smallrye.mutiny.Uni<HttpServerResponse> push​(io.vertx.core.http.HttpMethod method,
                                                               String host,
                                                               String path,
                                                               MultiMap headers)
        Push a response to the client.

        The handler will be notified with a success when the push can be sent and with a failure when the client has disabled push or reset the push before it has been sent.

        The handler may be queued if the client has reduced the maximum number of streams the server can push concurrently.

        Push can be sent only for peer initiated streams and if the response is not ended.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method - the method of the promised request
        host - the host of the promised request
        path - the path of the promised request
        headers - the headers of the promised request
        Returns:
        a reference to this, so the API can be used fluently
      • pushAndAwait

        public HttpServerResponse pushAndAwait​(io.vertx.core.http.HttpMethod method,
                                               String host,
                                               String path,
                                               MultiMap headers)
        Blocking variant of push(HttpMethod,String,String,io.vertx.mutiny.core.MultiMap).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method - the method of the promised request
        host - the host of the promised request
        path - the path of the promised request
        headers - the headers of the promised request
        Returns:
        a reference to this, so the API can be used fluently
      • reset

        public void reset()
      • reset

        public void reset​(long code)
        Parameters:
        code - the error code
      • writeCustomFrame

        public HttpServerResponse writeCustomFrame​(int type,
                                                   int flags,
                                                   Buffer payload)
        Parameters:
        type - the 8-bit frame type
        flags - the 8-bit frame flags
        payload - the frame payload
        Returns:
        a reference to this, so the API can be used fluently
      • setStreamPriority

        public HttpServerResponse setStreamPriority​(io.vertx.core.http.StreamPriority streamPriority)
        Parameters:
        streamPriority - the priority for this request's stream
        Returns:
      • addCookie

        public HttpServerResponse addCookie​(Cookie cookie)
        Parameters:
        cookie - the cookie
        Returns:
        a reference to this, so the API can be used fluently
      • removeCookie

        public Cookie removeCookie​(String name)
        Parameters:
        name - the name of the cookie
        Returns:
        the cookie, if it existed, or null
      • removeCookie

        public Cookie removeCookie​(String name,
                                   boolean invalidate)
        Parameters:
        name - the name of the cookie
        invalidate -
        Returns:
        the cookie, if it existed, or null
      • writeAndForget

        public HttpServerResponse writeAndForget​(String chunk,
                                                 String enc)
        Variant of write(String,String) that ignores the result of the operation.

        This method subscribes on the result of write(String,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from write(String,String) but you don't need to compose it with other operations.

        Parameters:
        chunk - the string to write
        enc - the encoding to use
        Returns:
        a reference to this, so the API can be used fluently
      • writeAndForget

        public HttpServerResponse writeAndForget​(String chunk)
        Variant of write(String) that ignores the result of the operation.

        This method subscribes on the result of write(String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from write(String) but you don't need to compose it with other operations.

        Parameters:
        chunk - the string to write
        Returns:
        a reference to this, so the API can be used fluently
      • endAndForget

        public void endAndForget​(String chunk)
        Variant of end(String) that ignores the result of the operation.

        This method subscribes on the result of end(String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from end(String) but you don't need to compose it with other operations.

        Parameters:
        chunk - the string to write before ending the response
      • endAndForget

        public void endAndForget​(String chunk,
                                 String enc)
        Variant of end(String,String) that ignores the result of the operation.

        This method subscribes on the result of end(String,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from end(String,String) but you don't need to compose it with other operations.

        Parameters:
        chunk - the string to write before ending the response
        enc - the encoding to use
      • endAndForget

        public void endAndForget()
        Variant of end() that ignores the result of the operation.

        This method subscribes on the result of end(), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from end() but you don't need to compose it with other operations.

      • sendFileAndForget

        public HttpServerResponse sendFileAndForget​(String filename)
        Variant of sendFile(String) that ignores the result of the operation.

        This method subscribes on the result of sendFile(String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from sendFile(String) but you don't need to compose it with other operations.

        Parameters:
        filename - path to the file to serve
        Returns:
        a reference to this, so the API can be used fluently
      • sendFileAndForget

        public HttpServerResponse sendFileAndForget​(String filename,
                                                    long offset)
        Variant of sendFile(String,long) that ignores the result of the operation.

        This method subscribes on the result of sendFile(String,long), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from sendFile(String,long) but you don't need to compose it with other operations.

        Parameters:
        filename - path to the file to serve
        offset - offset to start serving from
        Returns:
        a reference to this, so the API can be used fluently
      • sendFileAndForget

        public HttpServerResponse sendFileAndForget​(String filename,
                                                    long offset,
                                                    long length)
        Variant of sendFile(String,long,long) that ignores the result of the operation.

        This method subscribes on the result of sendFile(String,long,long), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from sendFile(String,long,long) but you don't need to compose it with other operations.

        Parameters:
        filename - path to the file to serve
        offset - offset to start serving from
        length - the number of bytes to send
        Returns:
        a reference to this, so the API can be used fluently
      • newInstance

        public static HttpServerResponse newInstance​(io.vertx.core.http.HttpServerResponse arg)