Package io.muserver

Interface MuResponse


  • public interface MuResponse

    A response sent to a client.

    The status(int) and headers() methods are used to set the response code and response headers.

    The addCookie(Cookie) method can be used to add a cookie to the response.

    There are several ways to send data back to the client:

    Note: only one of the above methods can be used per response, and aside from sendChunk it is not allowed to call the same method more than once..

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void addCookie​(Cookie cookie)
      Sends a cookie to the client.
      void contentType​(java.lang.CharSequence contentType)
      Sets the Content-Type response header.
      boolean hasStartedSendingData()
      Specifies whether or not any response data has already been sent to the client.
      Headers headers()
      Gets the response headers map which can be used to specify response headers.
      java.io.OutputStream outputStream()
      Gets a buffered output stream that can send data to the client.
      java.io.OutputStream outputStream​(int bufferSize)
      Gets a buffered output stream that can send data to the client.
      void redirect​(java.lang.String url)
      Redirects to the given URL.
      void redirect​(java.net.URI uri)
      Redirects to the given URL.
      ResponseState responseState()  
      void sendChunk​(java.lang.String text)
      Immediately sends the given text to the client as a chunk.
      int status()  
      void status​(int value)
      Sets the response code for this request.
      void write​(java.lang.String text)
      Writes the given text as the response body for this request.
      java.io.PrintWriter writer()
      A print writer that can be used to send text to the client.
    • Method Detail

      • status

        int status()
        Returns:
        The HTTP status of this request.
      • status

        void status​(int value)
        Sets the response code for this request. Defaults to 200
        Parameters:
        value - The response code to send to the client.
      • write

        void write​(java.lang.String text)

        Writes the given text as the response body for this request. This can only be called once.

        If you want to send multiple chunks of text, see sendChunk(String)

        Parameters:
        text - The full response body to send to the client.
        Throws:
        java.lang.IllegalStateException - Thrown if this is called twice, or this is called after any other body-writing methods.
      • sendChunk

        void sendChunk​(java.lang.String text)
        Immediately sends the given text to the client as a chunk.
        Parameters:
        text - Text to send to the client as an HTTP chunk.
        Throws:
        java.lang.IllegalStateException - Thrown if write(String) or outputStream() or writer() was already called.
      • redirect

        void redirect​(java.lang.String url)

        Redirects to the given URL. If relative, it will be converted to an absolute URL.

        The response code will be 302 unless the status is already set to 300, 301, or 303.

        Parameters:
        url - The full or relative URL to redirect to.
      • redirect

        void redirect​(java.net.URI uri)

        Redirects to the given URL. If relative, it will be converted to an absolute URL.

        The response code will be 302 unless the status is already set to 300, 301, or 303.

        Parameters:
        uri - The full or relative URI to redirect to.
      • headers

        Headers headers()
        Gets the response headers map which can be used to specify response headers. Example: response.headers().set("access-control-allow-origin", "*");
        Returns:
        The response headers map that can be used to set headers.
      • contentType

        void contentType​(java.lang.CharSequence contentType)
        Sets the Content-Type response header.
        Parameters:
        contentType - The content type of the response, for example application/json
        See Also:
        ContentTypes
      • addCookie

        void addCookie​(Cookie cookie)

        Sends a cookie to the client.

        Example: response.addCookie(new Cookie("user", user));

        If using HTTPS, it's recommended to use response.addCookie(Cookie.secureCookie("user", user));

        Parameters:
        cookie - A cookie to store on the client.
      • outputStream

        java.io.OutputStream outputStream()

        Gets a buffered output stream that can send data to the client.

        To set the size of the buffer, use @{link outputStream(int)} instead.

        If you are writing text, you may prefer the writer() or sendChunk(String) methods.

        Returns:
        An output stream to send data to the client.
      • outputStream

        java.io.OutputStream outputStream​(int bufferSize)

        Gets a buffered output stream that can send data to the client.

        If you are writing text, you may prefer the writer() or sendChunk(String) methods.

        Parameters:
        bufferSize - The size of the output buffer, e.g. 8192, or 0 if you do not want to buffer the response bytes.
        Returns:
        An output stream to send data to the client.
      • writer

        java.io.PrintWriter writer()

        A print writer that can be used to send text to the client. It is a convenience method, wrapping outputStream() in a PrintWriter.

        You may prefer using sendChunk(String) or write(String) to send text.

        Returns:
        A print writer that can be used to send text to the client.
      • hasStartedSendingData

        boolean hasStartedSendingData()
        Specifies whether or not any response data has already been sent to the client. Note that once any data is sent to the client then status(int) and headers() can no longer be changed.
        Returns:
        Returns true if any data has been sent to the client; otherwise false.
      • responseState

        ResponseState responseState()
        Returns:
        The current state of this response