Class HTTPResponse

java.lang.Object
com.pushtechnology.diffusion.api.webserver.HTTPResponse

public class HTTPResponse extends Object
HTTPResponse

If you need to generate a HTTP Response, this class can help. Headers and mime types can be set, as well as attributes like compression and minification.

Since:
4.1
  • Constructor Details

    • HTTPResponse

      public HTTPResponse()
      Creates an HTTP Response object.
  • Method Details

    • isCompressionEnabled

      public boolean isCompressionEnabled()
      Indicates whether compression is enabled.
      Returns:
      true if compression is enabled, else false
    • setMimeType

      public void setMimeType(String type)
      Change the default mime type.
      Parameters:
      type - the mime type
    • setResponseCode

      public void setResponseCode(int responseCode)
      Set the response code for the HTTP response, default is 200.
      Parameters:
      responseCode - the response code
      Since:
      4.1
    • setHeadResponse

      public void setHeadResponse()
      If this method is called, then the resulting response will be a HEAD response only.
      Since:
      4.1
    • setCompressionEnabled

      public void setCompressionEnabled(boolean enabled)
      Toggles the response to be compressed or not.
      Parameters:
      enabled - true to set compression enabled.
      Since:
      4.1
    • setLastModifiedDate

      public void setLastModifiedDate(long date)
      Sets the Last Modified header to the date supplied.
      Parameters:
      date - the last modified data to set
      Since:
      4.1
    • setHeader

      public void setHeader(String header, String value)
      Adds a header to the response.
      Parameters:
      header - the header
      value - the header value
      Since:
      4.1
    • removeHeader

      public String removeHeader(String header)
      Removes a header from the response.
      Parameters:
      header - the header to remove
      Returns:
      the previous value or null if not found
      Since:
      4.1
    • setETag

      public void setETag(String etag)
      Sets the Etag header.
      Parameters:
      etag - the value
      Since:
      4.1
    • getHeaders

      public HashMap<String,String> getHeaders()
      Returns a HashMap of the existing headers.
      Returns:
      the headers
      Since:
      4.1
    • setConnectionKeepAlive

      public void setConnectionKeepAlive()
      Sets the Keep-Alive header.
      Since:
      4.1
    • setConnectionClose

      public void setConnectionClose()
      Sets the connection header to close.
      Since:
      4.1
    • setNoCache

      public void setNoCache()
      Sets cache control to no-store, no-cache.
      Since:
      4.1
    • setChunkedEncoding

      public void setChunkedEncoding()
      Sets transfer encoding header to chunked.
      Since:
      4.1
    • setCookie

      public void setCookie(String token)
      Sets the session cookie header.
      Parameters:
      token - the token
      Since:
      4.1
    • setCorsResponseOrigin

      public void setCorsResponseOrigin(String origin)
      Sets the cors response origin header.
      Parameters:
      origin - the origin header value
      Since:
      4.1
    • setContentLength

      public void setContentLength(int length)
      Sets the content length header.
      Parameters:
      length - the content length
      Since:
      4.1
    • populateResponseBuffer

      public void populateResponseBuffer(ByteBuffer buffer) throws IOException
      Generate a HTTP Response from the details provided and populate the ByteBuffer. The buffer will not be flipped for output.
      Parameters:
      buffer - the buffer to populate
      Throws:
      IOException - if population of buffer fails
      Since:
      4.1
    • generateResponseBuffer

      public ByteBuffer generateResponseBuffer() throws IOException
      Generate a HTTP Response from the details provided and populate a newly created Byte Buffer.
      Returns:
      a ByteBuffer which has been flipped ready for output
      Throws:
      IOException - if generation of response buffer fails
      Since:
      4.1
    • setBody

      public void setBody(ByteBuffer bb)
      Set the body of the HTTP request.
      Since:
      4.1
    • setBody

      public void setBody(byte[] bytes)
      Set the body of the HTTP request.
      Since:
      6.5