Interface ServerResponse

    • Method Detail

      • webServer

        WebServer webServer()
        Returns actual WebServer instance.
        Returns:
        an actual WebServer instance
      • status

        Http.ResponseStatus status()
        Returns actual response status code.

        Default value for handlers is 200 and for failure handlers 500. Value can be redefined using status(int) method before headers are send.

        Returns:
        an HTTP status code
      • headers

        ResponseHeaders headers()
        Returns response headers. It can be modified before headers are sent to the client.
        Returns:
        a response headers
      • writerContext

        MessageBodyWriterContext writerContext()
        Get the writer context used to marshall data.
        Returns:
        MessageBodyWriterContext
      • send

        <T> Single<ServerResponse> send​(T content)
        Send a message and close the response.

        Marshalling

        Data are marshaled using default or registered writer to the format of ByteBuffer Publisher. The last registered compatible writer is used.

        Default writers supports:

        Blocking

        The method blocks only during marshalling. It means until registered writer produce a Publisher and subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returned Single to monitor and react on finished sending process.
        Type Parameters:
        T - a type of the content
        Parameters:
        content - a response content to send
        Returns:
        a completion stage of the response - completed when response is transferred
        Throws:
        IllegalArgumentException - if there is no registered writer for a given type
        IllegalStateException - if any send(...) method was already called
      • send

        <T> Single<ServerResponse> send​(Flow.Publisher<T> content,
                                        Class<T> clazz)
        Send a message with the given entity stream as content and close the response.
        Type Parameters:
        T - entity type
        Parameters:
        content - entity stream
        clazz - class representing the entity type
        Returns:
        a completion stage of the response - completed when response is transferred
      • send

        Single<ServerResponse> send​(Flow.Publisher<DataChunk> content)
        Send a message as is without any other marshalling. The response is completed when publisher send Flow.Subscriber.onComplete() method to its subscriber.

        A single Subscriber subscribes to the provided Publisher during the method execution.

        Blocking

        The method blocks only during marshalling. It means until registered writer produce a Publisher and subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher implementations. Use returned Single to monitor and react on finished sending process.
        Parameters:
        content - a response content publisher
        Returns:
        a completion stage of the response - completed when response is transferred
        Throws:
        IllegalStateException - if any send(...) method was already called
      • send

        Single<ServerResponse> send()
        Sends an empty response. Do nothing if response was already send.
        Returns:
        a completion stage of the response - completed when response is transferred
      • registerWriter

        @Deprecated
        <T> ServerResponse registerWriter​(Class<T> type,
                                          MediaType contentType,
                                          Function<? extends T,​Flow.Publisher<DataChunk>> function)
        Deprecated.
        Registers a content writer for a given type and media type.

        Registered writer is used to marshal response content of given type to the Publisher of response chunks. It is used only if Content-Type header is compatible with a given content type or if it is null. If Content-Type is null and it is still possible to modify headers (headers were not send yet), the provided content type will be set.

        Type Parameters:
        T - a type of the content
        Parameters:
        type - a type of the content. If null then accepts any type.
        contentType - a Content-Type of the entity
        function - a writer function
        Returns:
        this instance of ServerResponse
        Throws:
        NullPointerException - if function parameter is null
      • registerWriter

        @Deprecated
        <T> ServerResponse registerWriter​(Predicate<?> accept,
                                          MediaType contentType,
                                          Function<T,​Flow.Publisher<DataChunk>> function)
        Deprecated.
        Registers a content writer for all accepted contents.

        Registered writer is used to marshal response content of given type to the Publisher of response chunks. It is used only if Content-Type header is compatible with a given content type or if it is null. If Content-Type is null and it is still possible to modify headers (headers were not send yet), the provided content type will be set.

        Type Parameters:
        T - a type of the content
        Parameters:
        accept - a predicate to test if content is marshallable by the writer. If null then accepts any type.
        contentType - a Content-Type of the entity
        function - a writer function
        Returns:
        this instance of ServerResponse
        Throws:
        NullPointerException - if function parameter is null
      • registerFilter

        @Deprecated
        ServerResponse registerFilter​(Function<Flow.Publisher<DataChunk>,​Flow.Publisher<DataChunk>> function)
        Deprecated.
        Registers a provider of the new response content publisher - typically a filter.

        All response content is always represented by a single Publisher of response chunks. This method can be used to filter or completely replace original publisher by a new one with different contract. For example data coding, logging, filtering, caching, etc.

        New publisher is created at the moment of content write by any send(...) method including the empty one.

        All registered filters are used as a chain from original content Publisher, first registered to the last registered.

        Parameters:
        function - a function to map previously registered or original Publisher to the new one. If returns null then the result will be ignored.
        Returns:
        this instance of ServerResponse
        Throws:
        NullPointerException - if parameter function is null
      • registerFilter

        ServerResponse registerFilter​(MessageBodyFilter filter)
        Description copied from interface: MessageBodyFilters
        Registers a message body filter.

        The registered filters are applied to form a chain from the first registered to the last registered. The first evaluation of the function transforms the original publisher to a new publisher. Any subsequent evaluation receives the publisher transformed by the last previously registered filter.

        Specified by:
        registerFilter in interface MessageBodyFilters
        Parameters:
        filter - a function to map previously registered or original Publisher to the new one. If returns null then the result will be ignored.
        Returns:
        this instance of MessageBodyFilters
        See Also:
        MessageBodyContext.applyFilters(Flow.Publisher)
      • whenSent

        Single<ServerResponse> whenSent()
        Completion stage is completed when response is completed.

        It can be used to react on the response completion without invocation of a closing event.

        Returns:
        a completion stage of the response
      • requestId

        long requestId()
        A unique correlation ID that is associated with this response and its associated request.
        Returns:
        a unique correlation ID associated with this response and its request