Package io.fusionauth.http.server
Class HTTPResponse
java.lang.Object
io.fusionauth.http.server.HTTPResponse
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 Summary
ConstructorsConstructorDescriptionHTTPResponse(OutputStream outputStream, HTTPRequest request) HTTPResponse(OutputStream outputStream, HTTPRequest request, boolean compressByDefault) -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidAdd a response header.voidvoidclose()Closes the HTTP response to ensure that the client is notified that the server is finished responding.booleancontainsHeader(String name) booleanfailure()Determines the character set by parsing theContent-Typeheader (if it exists) to pull out thecharsetparameter.getHeaders(String key) intbooleanbooleanbooleanvoidremoveCookie(String name) voidremoveHeader(String name) Remove a header by name.voidsendRedirect(String uri) voidsetCommitted(boolean committed) Sets the committed status of the response.voidsetCompress(boolean compress) Provides runtime configuration for HTTP response compression.voidsetContentLength(long length) voidsetContentType(String contentType) voidsetDateHeader(String name, ZonedDateTime value) voidsetException(Throwable exception) voidSet the header, replacing any existing header values.voidsetStatus(int status) voidsetStatusMessage(String statusMessage) boolean
-
Constructor Details
-
HTTPResponse
-
HTTPResponse
-
-
Method Details
-
addCookie
-
addHeader
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 namevalue- the header value
-
clearHeaders
public void clearHeaders() -
close
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
-
failure
public boolean failure() -
getCharset
Determines the character set by parsing theContent-Typeheader (if it exists) to pull out thecharsetparameter.- Returns:
- The Charset or UTF-8 if it wasn't specified in the
Content-Typeheader.
-
getContentLength
-
setContentLength
public void setContentLength(long length) -
getContentType
-
setContentType
-
getCookies
-
getException
-
setException
-
getHeader
-
getHeaders
-
getHeadersMap
-
getOutputStream
-
getRedirect
-
getStatus
public int getStatus() -
setStatus
public void setStatus(int status) -
getStatusMessage
-
setStatusMessage
-
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.
-
setCommitted
public void setCommitted(boolean committed) Sets the committed status of the response.- Parameters:
committed- The status.
-
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
IllegalStateExceptionwill 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
-
removeHeader
Remove a header by name.- Parameters:
name- the header name to remove
-
sendRedirect
-
setDateHeader
-
setHeader
Set the header, replacing any existing header values. If you wish to add to existing response headers, use theaddHeader(String, String)instead.If either parameter are null, the header will not be added to the response.
- Parameters:
name- the header namevalue- the header value
-
willCompress
public boolean willCompress()- Returns:
- true if compression has been requested and as far as we know, we will.
-