Class HTTPResponse

java.lang.Object
io.fusionauth.http.server.HTTPResponse

public class HTTPResponse extends Object
An HTTP response that the server sends back to a client. The handler that processes the HTTP request can fill out this object and the HTTP server will send it back to the client.
Author:
Brian Pontarelli
  • Constructor Details

    • HTTPResponse

      public HTTPResponse()
  • Method Details

    • addCookie

      public void addCookie(Cookie cookie)
    • addHeader

      public void addHeader(String name, String value)
      Add a response header. Calling this method multiple times with the same name will result in multiple headers being written to the HTTP response.

      Optionally call setHeader(String, String) if you wish to only write a single header by name, overwriting any previously written headers.

      If either parameter are null, the header will not be added to the response.

      Parameters:
      name - the header name
      value - the header value
    • clearHeaders

      public void clearHeaders()
    • close

      public void close() throws IOException
      Closes the HTTP response to ensure that the client is notified that the server is finished responding. This closes the Writer or the OutputStream if they are available. The Writer is preferred if it exists so that it is properly flushed.
      Throws:
      IOException
    • containsHeader

      public boolean containsHeader(String name)
    • failure

      public boolean failure()
    • flush

      public void flush() throws IOException
      Flushes any buffered response (including the preamble) to the client. This is a force flush operation.
      Throws:
      IOException - If the socket throws.
    • getCharset

      public Charset getCharset()
      Determines the character set by parsing the Content-Type header (if it exists) to pull out the charset parameter.
      Returns:
      The Charset or UTF-8 if it wasn't specified in the Content-Type header.
    • getContentLength

      public Long getContentLength()
    • reset

      public void reset()
      Hard resets this response if it hasn't been committed yet. If the response has been committed back to the client, this throws up.
    • setContentLength

      public void setContentLength(long length)
    • getContentType

      public String getContentType()
    • setContentType

      public void setContentType(String contentType)
    • getCookies

      public List<Cookie> getCookies()
    • getException

      public Throwable getException()
    • setException

      public void setException(Throwable exception)
    • getHeader

      public String getHeader(String name)
    • getHeaders

      public List<String> getHeaders(String key)
    • getHeadersMap

      public Map<String,List<String>> getHeadersMap()
    • getOutputStream

      public OutputStream getOutputStream()
    • setOutputStream

      public void setOutputStream(HTTPOutputStream outputStream)
    • getRedirect

      public String getRedirect()
    • getStatus

      public int getStatus()
    • setStatus

      public void setStatus(int status)
    • getStatusMessage

      public String getStatusMessage()
    • setStatusMessage

      public void setStatusMessage(String statusMessage)
    • getWriter

      public Writer getWriter()
    • isCommitted

      public boolean isCommitted()
      Returns:
      True if the response has been committed, meaning at least one byte was written back to the client. False otherwise.
    • isCompress

      public boolean isCompress()
      Returns:
      true if compression will be utilized when writing the HTTP OutputStream.
    • setCompress

      public void setCompress(boolean compress)
      Provides runtime configuration for HTTP response compression. This can be called as many times as you wish prior to the first byte being written to the HTTP OutputStream.

      An IllegalStateException will be thrown if you call this method after writing to the OutputStream.

      Parameters:
      compress - true to enable the response to be written back compressed.
    • isKeepAlive

      public boolean isKeepAlive()
      Returns:
      If the connection should be kept open (keep-alive) or not. The default is to return the Connection: keep-alive header, which this method does.
    • removeCookie

      public void removeCookie(String name)
    • removeHeader

      public void removeHeader(String name)
      Remove a header by name.
      Parameters:
      name - the header name to remove
    • sendRedirect

      public void sendRedirect(String uri)
    • setDateHeader

      public void setDateHeader(String name, ZonedDateTime value)
    • setHeader

      public void setHeader(String name, String value)
      Set the header, replacing any existing header values. If you wish to add to existing response headers, use the addHeader(String, String) instead.

      If either parameter are null, the header will not be added to the response.

      Parameters:
      name - the header name
      value - the header value
    • willCompress

      public boolean willCompress()
      Returns:
      true if compression has been requested and as far as we know, we will.