Class Response
java.lang.Object
de.craftsblock.craftsnet.api.http.Response
- All Implemented Interfaces:
AutoCloseable
The Response class represents an HTTP response sent by the web server to the client.
It provides methods to set response headers, status code, and send response content.
This class implements the AutoCloseable interface to ensure proper handling and closing of the response stream.
It is used internally by the WebServer to construct and send HTTP responses to client requests.
Developers should not create instances of this class directly but use the provided methods in the WebServer class.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedResponse(CraftsNet craftsNet, StreamEncoder streamEncoder, HttpExchange httpExchange, HttpMethod httpMethod) Constructor for creating a new Response object. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a response header with the specified key and value.voidclose()Closes the responsedeleteCookie(String name) Marks a cookie for deletion by invoking the mark deleted method on the specific cookie.Returns a collection of all cookies currently set for the response.Returns the cors policy added to the header of the response.Gets the instance of CraftsNet which will send the response.Gets theExchangemanaging this response.Gets the value of the response header with the specified key.Allows raw access to the underlyingHeadersobject, which stores the response headers.getHeaders(String key) Get all the values from the response headers for the specified header name.Retrieves theStreamEncoderthat is used to encode the response body.booleanChecks if a response header with the specified key exists.booleanReturns whether the response headers have already been sent or not.booleanReturns whether the response can have a response body.voidprint(byte[] bytes) Sends the provided bytes as the response body.voidprint(de.craftsblock.craftscore.json.Json json, boolean pretty) Sends the string representation of the provided json object as the response body while setting the pretty printing flag.voidSends the provided file as the response body.voidprint(InputStream stream) Sends the content of the provided input stream as the response body.voidSends the string representation of the provided object as the response body.voidSends the provided text as the response body in UTF-8 encoding.voidSends the provided file behind thePathas the response body.voidSends the provided text as the response body followed by a new line in UTF-8 encoding.booleanReturns whether the response was used to send a file to the client.voidsetCode(int code) Sets the HTTP status code for the response.voidsetContentType(@Nullable String contentType) Sets the content type for the response.voidsetContentType(@Nullable String contentType, @Nullable String fallback) Sets the content type for the response or use the fallback value if the contentType is null.Sets a cookie with the specified name and no value.Sets a cookie with the specified name and value.Adds a cookie to the response, overriding any existing cookie with the same name.voidsetCorsPolicy(CorsPolicy corsPolicy) Updates the cors policy added to the header of the response.protected voidsetExchange(Exchange exchange) Sets theExchangemanaging this response.voidSets a response header with the specified key and value.voidsetStreamEncoder(@NotNull StreamEncoder streamEncoder) Sets theStreamEncoderthat is used to encode the response body.voidsetStreamEncoder(String encodingName) Sets theStreamEncoderby its name that is used to encode the response body.unsafe()Retrieves the underlying HttpExchange object associated with this Response.
-
Constructor Details
-
Response
protected Response(CraftsNet craftsNet, StreamEncoder streamEncoder, HttpExchange httpExchange, HttpMethod httpMethod) Constructor for creating a new Response object.- Parameters:
craftsNet- TheCraftsNetinstance which instantiates thisstreamEncoder- TheStreamEncoderthat should be used to encode the response body.httpExchange- TheHttpExchangeobject representing the HTTP request-response exchange.httpMethod- TheHttpMethodused to access the route.
-
-
Method Details
-
print
Sends the string representation of the provided object as the response body.- Parameters:
object- The object to be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs.
-
print
public void print(de.craftsblock.craftscore.json.Json json, boolean pretty) Sends the string representation of the provided json object as the response body while setting the pretty printing flag.- Parameters:
json- The json object to be sent as the response body.pretty- Whether the json should be printed pretty.- Throws:
RuntimeException- if an I/O error occurs
-
println
Sends the provided text as the response body followed by a new line in UTF-8 encoding.- Parameters:
text- The text to be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs
-
print
Sends the provided text as the response body in UTF-8 encoding.- Parameters:
text- The text to be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs
-
print
Sends the provided file as the response body.- Parameters:
file- The file to be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs
-
print
Sends the provided file behind thePathas the response body.- Parameters:
path- ThePathof the file to be sent.- Throws:
RuntimeException- if an I/O error occurs- Since:
- 3.3.5-SNAPSHOT
-
print
public void print(byte[] bytes) Sends the provided bytes as the response body.- Parameters:
bytes- The bytes to be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs
-
print
Sends the content of the provided input stream as the response body.- Parameters:
stream- The input stream which content should be sent as the response body.- Throws:
RuntimeException- if an I/O error occurs
-
close
Closes the response- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if an I/O error occurs.
-
setExchange
Sets theExchangemanaging this response.- Parameters:
exchange- TheExchangemanaging the response
-
getExchange
Gets theExchangemanaging this response.- Returns:
- The
Exchangemanaging this response.
-
setStreamEncoder
Sets theStreamEncoderby its name that is used to encode the response body.- Parameters:
encodingName- The name of theStreamEncoderthat should be used.- Throws:
IllegalStateException- If the response headers have already been sent.- Since:
- 3.3.5-SNAPSHOT
-
setStreamEncoder
Sets theStreamEncoderthat is used to encode the response body.- Parameters:
streamEncoder- TheStreamEncoderthat should be used.- Throws:
IllegalStateException- If the response headers have already been sent.- Since:
- 3.3.5-SNAPSHOT
-
getStreamEncoder
Retrieves theStreamEncoderthat is used to encode the response body.- Returns:
- The
StreamEncoderis used. - Since:
- 3.3.3-SNAPSHOT
-
setContentType
Sets the content type for the response.- Parameters:
contentType- The content type to be set in the response header.
-
setContentType
public void setContentType(@Nullable @Nullable String contentType, @Nullable @Nullable String fallback) Sets the content type for the response or use the fallback value if the contentType is null. If both, the preferred content type and the fallback content type are null, then no further action will be made.- Parameters:
contentType- The content type to be set in the response header.fallback- The fallback content type to be set in the response header if the content type was null.
-
setCode
public void setCode(int code) Sets the HTTP status code for the response.- Parameters:
code- The HTTP status code to be set in the response.- Throws:
IllegalStateException- if the response headers have already been sent.
-
hasHeader
Checks if a response header with the specified key exists.- Parameters:
key- The key of the header to check.- Returns:
- true if a header with the specified key exists, false otherwise.
-
getHeader
Gets the value of the response header with the specified key.- Parameters:
key- The key of the header to get the value for.- Returns:
- The value of the header with the specified key, or null if the header is not found.
-
addHeader
Adds a response header with the specified key and value.- Parameters:
key- The key of the header to be added.value- The value of the header to be added.- Throws:
IllegalStateException- if the response headers have already been sent.
-
setHeader
Sets a response header with the specified key and value. If a header with the same key already exists, it will be replaced.- Parameters:
key- The key of the header to be set.value- The value of the header to be set.- Throws:
IllegalStateException- if the response headers have already been sent.
-
getHeaders
Get all the values from the response headers for the specified header name.- Parameters:
key- The "name" of the header used to find the header- Returns:
- A list of alle the values
- Since:
- 3.4.0-SNAPSHOT
-
getHeaders
Allows raw access to the underlyingHeadersobject, which stores the response headers.- Returns:
- The
Headersobject storing the response headers. - Since:
- 3.4.0-SNAPSHOT
-
setCorsPolicy
Updates the cors policy added to the header of the response.- Parameters:
corsPolicy- The cors policy which should be sent.
-
getCorsPolicy
Returns the cors policy added to the header of the response.- Returns:
- The modifiable cors policy.
-
setCookie
Sets a cookie with the specified name and no value.- Parameters:
name- The name of the cookie, cannot be null- Returns:
- The created Cookie object
-
setCookie
Sets a cookie with the specified name and value.- Parameters:
name- The name of the cookie, cannot be nullvalue- The value of the cookie, can be null- Returns:
- The created Cookie object
-
setCookie
Adds a cookie to the response, overriding any existing cookie with the same name.- Parameters:
cookie- The Cookie object to add, cannot be null- Returns:
- The added Cookie object
-
deleteCookie
Marks a cookie for deletion by invoking the mark deleted method on the specific cookie.- Parameters:
name- The name of the cookie to delete- Returns:
- The deleted Cookie object
-
getCookies
Returns a collection of all cookies currently set for the response.- Returns:
- A ConcurrentLinkedQueue containing all cookies
-
headersSent
public boolean headersSent()Returns whether the response headers have already been sent or not.- Returns:
trueif the response headers have been sent,falseotherwise.
-
sendingFile
public boolean sendingFile()Returns whether the response was used to send a file to the client.- Returns:
trueif a file was sent,falseotherwise.- Since:
- 3.3.3-SNAPSHOT
-
isBodyAble
public boolean isBodyAble()Returns whether the response can have a response body.- Returns:
trueif the response can have a body,falseotherwise.
-
unsafe
Retrieves the underlying HttpExchange object associated with this Response.- Returns:
- The HttpExchange object representing the HTTP request-response exchange.
-
getCraftsNet
Gets the instance of CraftsNet which will send the response.- Returns:
- The CraftsNet instance.
-