Package ej.hoka.http

Class HttpResponse


  • public class HttpResponse
    extends java.lang.Object
    Represents a HTTP Response.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addCookie​(Cookie cookie)
      Adds cookie to the response.
      void addCookie​(java.lang.String name, java.lang.String value)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addCookie​(java.lang.String name, java.lang.String value, int maxAge)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addCookie​(java.lang.String name, java.lang.String value, int maxAge, boolean secured, boolean httpOnly)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addCookie​(java.lang.String name, java.lang.String value, java.lang.String domain, java.lang.String path, int maxAge, boolean secured, boolean httpOnly)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addCookie​(java.lang.String name, java.lang.String value, java.lang.String domain, java.lang.String path, int maxAge, java.util.Date expires, boolean secured, boolean httpOnly)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addCookie​(java.lang.String name, java.lang.String value, java.lang.String domain, java.lang.String path, int maxAge, java.util.Date expires, boolean secured, boolean httpOnly, Cookie.SameSite sameSite)
      Deprecated.
      Please use addCookie(Cookie) and Cookie.builder() instead of this method.
      void addHeader​(java.lang.String name, java.lang.String value)
      Adds a response header field.
      void addHeaders​(java.util.Map<java.lang.String,​java.lang.String> map)
      Adds a map of header fields.
      java.lang.String getHeader​(java.lang.String key)
      Returns the header field value associated to the given header field key.
      protected long getLength()
      Returns the length (in bytes) of the response data or -1 if the length is unknown.
      java.lang.String getMimeType()
      Returns the MIME-TYPE of the response.
      java.lang.String getStatus()
      Returns the response status.
      Cookie removeCookie​(java.lang.String name)
      Remove the cookie mapped by name.
      void setData​(byte[] data)
      Set the data contained by this response.
      void setData​(java.io.InputStream dataStream)
      Sets the InputStream from which the response data can be read.
      void setData​(java.io.InputStream dataStream, long length)
      Sets the InputStream from which the response data can be read.
      void setData​(java.lang.String data)
      Set the data contained by this response.
      void setData​(java.lang.String data, java.lang.String encoding)
      Set HttpResponse body using the String data as response data and the encoding.
      void setMimeType​(java.lang.String mimeType)
      Set the response MIME-TYPE.
      void setStatus​(java.lang.String status)
      Set the response status.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpResponse

        public HttpResponse()
        Creates an empty HttpResponse.
    • Method Detail

      • getHeader

        @Nullable
        public java.lang.String getHeader​(@Nullable
                                          java.lang.String key)
        Returns the header field value associated to the given header field key.
        Parameters:
        key - a header field name (if null, null is returned).
        Returns:
        the replied header field value, null if the header field is not found.
      • getLength

        protected long getLength()
        Returns the length (in bytes) of the response data or -1 if the length is unknown.
        Returns:
        the length (in bytes) of the response data.
      • getMimeType

        @Nullable
        public java.lang.String getMimeType()
        Returns the MIME-TYPE of the response.
        Returns:
        the response MIME-TYPE.
      • getStatus

        public java.lang.String getStatus()
        Returns the response status.
        Returns:
        the response status.
      • setData

        public void setData​(@Nullable
                            java.lang.String data)
        Set the data contained by this response.
        Parameters:
        data - the response data as a byte array (set to empty if null is given)
      • setData

        public void setData​(@Nullable
                            java.lang.String data,
                            java.lang.String encoding)
                     throws java.io.UnsupportedEncodingException
        Set HttpResponse body using the String data as response data and the encoding.
        Parameters:
        data - the String to be used as response body.
        encoding - the encoding used to transform the String data to bytes. The following encodings can be used:
        • ISO-8859-1 ISO-8859-1 encoding, always supported by the platform
        • UTF-8 UTF-8 encoding, only supported if the "Embed UTF-8 encoding" option is enabled in the Run Configurations. If this option is not set, an UnsupportedEncodingException is thrown.
        • US-ASCII US-ASCII encoding
        Throws:
        java.io.UnsupportedEncodingException - when the specified encoding is not supported.
      • setData

        public void setData​(@Nullable
                            byte[] data)
        Set the data contained by this response.
        Parameters:
        data - the response data as a byte array (set to empty if null is given)
      • setData

        public void setData​(java.io.InputStream dataStream)
        Sets the InputStream from which the response data can be read.

        This method should be used only if response data length is not known in advance. If the length is known by advance the setData(InputStream, long) should be used instead of this one. When response data is specified with this method, the response must be sent using the chunked transfer-coding which increase the response message size.

        Parameters:
        dataStream - the InputStream from which the response data can be read, the stream will be closed automatically when the response is sent.
      • setData

        public void setData​(java.io.InputStream dataStream,
                            long length)
        Sets the InputStream from which the response data can be read.

        This method should be used when response data length is known in advance. It allows to transfer response body without using the chunked transfer-coding. This reduces response message size.

        Parameters:
        dataStream - the InputStream from which the response data can be read, the stream will be closed automatically when the response is sent.
        length - the number of byte to be read from the InputStream.
      • setMimeType

        public final void setMimeType​(java.lang.String mimeType)
        Set the response MIME-TYPE.
        Parameters:
        mimeType - the response MIME-TYPE to set.
      • setStatus

        public final void setStatus​(java.lang.String status)
        Set the response status.
        Parameters:
        status - the response status to set. Should be one of the HTTP_STATUS_* constants defined in HttpConstants
      • addCookie

        public void addCookie​(Cookie cookie)
        Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.
        Parameters:
        cookie - the cookie to add.
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value,
                              int maxAge)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
        maxAge - max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value,
                              int maxAge,
                              boolean secured,
                              boolean httpOnly)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
        maxAge - max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
        secured - if true : cookie will be secured
        httpOnly - if true: cookie will be marked as http only
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value,
                              @Nullable
                              java.lang.String domain,
                              @Nullable
                              java.lang.String path,
                              int maxAge,
                              boolean secured,
                              boolean httpOnly)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
        domain - domain of the cookie
        path - path of the cookie
        maxAge - max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
        secured - if true : cookie will be secured
        httpOnly - if true: cookie will be marked as http only
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value,
                              @Nullable
                              java.lang.String domain,
                              @Nullable
                              java.lang.String path,
                              int maxAge,
                              @Nullable
                              java.util.Date expires,
                              boolean secured,
                              boolean httpOnly)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
        domain - domain of the cookie
        path - path of the cookie
        maxAge - max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
        expires - The maximum lifetime of the cookie as an HTTP-date timestamp.
        secured - if true : cookie will be secured
        httpOnly - if true: cookie will be marked as http only
      • addCookie

        @Deprecated
        public void addCookie​(java.lang.String name,
                              java.lang.String value,
                              @Nullable
                              java.lang.String domain,
                              @Nullable
                              java.lang.String path,
                              int maxAge,
                              @Nullable
                              java.util.Date expires,
                              boolean secured,
                              boolean httpOnly,
                              @Nullable
                              Cookie.SameSite sameSite)
        Deprecated.
        Please use addCookie(Cookie) and Cookie.builder() instead of this method.
        Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
        Parameters:
        name - name of the cookie
        value - value of the cookie
        domain - domain of the cookie
        path - path of the cookie
        maxAge - max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
        expires - The maximum lifetime of the cookie as an HTTP-date timestamp.
        secured - if true : cookie will be secured
        httpOnly - if true: cookie will be marked as http only
        sameSite - same site value of the cookie Cookie.SameSite
      • removeCookie

        @Nullable
        public Cookie removeCookie​(java.lang.String name)
        Remove the cookie mapped by name.
        Parameters:
        name - of the cookie
        Returns:
        the previous value associated with name, or null if there was no mapping for name.