Class HTTPResponse
java.lang.Object
com.pushtechnology.diffusion.api.webserver.HTTPResponse
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGenerate a HTTP Response from the details provided and populate a newly created Byte Buffer.Returns a HashMap of the existing headers.boolean
Indicates whether compression is enabled.void
populateResponseBuffer
(ByteBuffer buffer) Generate a HTTP Response from the details provided and populate the ByteBuffer.removeHeader
(String header) Removes a header from the response.void
setBody
(byte[] bytes) Set the body of the HTTP request.void
setBody
(ByteBuffer bb) Set the body of the HTTP request.void
Sets transfer encoding header to chunked.void
setCompressionEnabled
(boolean enabled) Toggles the response to be compressed or not.void
Sets the connection header to close.void
Sets the Keep-Alive header.void
setContentLength
(int length) Sets the content length header.void
Sets the session cookie header.void
setCorsResponseOrigin
(String origin) Sets the cors response origin header.void
Sets the Etag header.void
Adds a header to the response.void
If this method is called, then the resulting response will be a HEAD response only.void
setLastModifiedDate
(long date) Sets the Last Modified header to the date supplied.void
setMimeType
(String type) Change the default mime type.void
Sets cache control to no-store, no-cache.void
setResponseCode
(int responseCode) Set the response code for the HTTP response, default is 200.
-
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
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
Adds a header to the response.- Parameters:
header
- the headervalue
- the header value- Since:
- 4.1
-
removeHeader
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
Sets the Etag header.- Parameters:
etag
- the value- Since:
- 4.1
-
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
Sets the session cookie header.- Parameters:
token
- the token- Since:
- 4.1
-
setCorsResponseOrigin
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
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
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
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
-