Class Response

java.lang.Object
de.craftsblock.craftsnet.api.http.Response
All Implemented Interfaces:
AutoCloseable

public class Response extends Object implements 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.
Since:
1.0.0-SNAPSHOT
See Also:
  • Constructor Details

    • Response

      protected Response(CraftsNet craftsNet, StreamEncoder streamEncoder, HttpExchange httpExchange, HttpMethod httpMethod)
      Constructor for creating a new Response object.
      Parameters:
      craftsNet - The CraftsNet instance which instantiates this
      streamEncoder - The StreamEncoder that should be used to encode the response body.
      httpExchange - The HttpExchange object representing the HTTP request-response exchange.
      httpMethod - The HttpMethod used to access the route.
  • Method Details

    • print

      public void print(Object object)
      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

      public void println(String text)
      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

      public void print(String text)
      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

      public void print(File file)
      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

      public void print(Path path)
      Sends the provided file behind the Path as the response body.
      Parameters:
      path - The Path of 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

      public void print(InputStream stream)
      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

      public void close() throws IOException
      Closes the response
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - if an I/O error occurs.
    • setExchange

      protected void setExchange(Exchange exchange)
      Sets the Exchange managing this response.
      Parameters:
      exchange - The Exchange managing the response
    • getExchange

      public Exchange getExchange()
      Gets the Exchange managing this response.
      Returns:
      The Exchange managing this response.
    • setStreamEncoder

      public void setStreamEncoder(String encodingName)
      Sets the StreamEncoder by its name that is used to encode the response body.
      Parameters:
      encodingName - The name of the StreamEncoder that should be used.
      Throws:
      IllegalStateException - If the response headers have already been sent.
      Since:
      3.3.5-SNAPSHOT
    • setStreamEncoder

      public void setStreamEncoder(@NotNull @NotNull StreamEncoder streamEncoder)
      Sets the StreamEncoder that is used to encode the response body.
      Parameters:
      streamEncoder - The StreamEncoder that should be used.
      Throws:
      IllegalStateException - If the response headers have already been sent.
      Since:
      3.3.5-SNAPSHOT
    • getStreamEncoder

      public StreamEncoder getStreamEncoder()
      Retrieves the StreamEncoder that is used to encode the response body.
      Returns:
      The StreamEncoder is used.
      Since:
      3.3.3-SNAPSHOT
    • setContentType

      public void setContentType(@Nullable @Nullable String contentType)
      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

      public boolean hasHeader(String key)
      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

      public String getHeader(@NotNull @NotNull String key)
      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

      public void addHeader(String key, String value)
      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

      public void setHeader(String key, String value)
      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

      public List<String> getHeaders(String key)
      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

      public Headers getHeaders()
      Allows raw access to the underlying Headers object, which stores the response headers.
      Returns:
      The Headers object storing the response headers.
      Since:
      3.4.0-SNAPSHOT
    • setCorsPolicy

      public void setCorsPolicy(CorsPolicy corsPolicy)
      Updates the cors policy added to the header of the response.
      Parameters:
      corsPolicy - The cors policy which should be sent.
    • getCorsPolicy

      public CorsPolicy getCorsPolicy()
      Returns the cors policy added to the header of the response.
      Returns:
      The modifiable cors policy.
    • setCookie

      public Cookie setCookie(@NotNull @NotNull String name)
      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

      public Cookie setCookie(@NotNull @NotNull String name, @Nullable @Nullable Object value)
      Sets a cookie with the specified name and value.
      Parameters:
      name - The name of the cookie, cannot be null
      value - The value of the cookie, can be null
      Returns:
      The created Cookie object
    • setCookie

      public Cookie setCookie(Cookie cookie)
      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

      public Cookie deleteCookie(String name)
      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

      public ConcurrentLinkedQueue<Cookie> 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:
      true if the response headers have been sent, false otherwise.
    • sendingFile

      public boolean sendingFile()
      Returns whether the response was used to send a file to the client.
      Returns:
      true if a file was sent, false otherwise.
      Since:
      3.3.3-SNAPSHOT
    • isBodyAble

      public boolean isBodyAble()
      Returns whether the response can have a response body.
      Returns:
      true if the response can have a body, false otherwise.
    • unsafe

      public HttpExchange unsafe()
      Retrieves the underlying HttpExchange object associated with this Response.
      Returns:
      The HttpExchange object representing the HTTP request-response exchange.
    • getCraftsNet

      public CraftsNet getCraftsNet()
      Gets the instance of CraftsNet which will send the response.
      Returns:
      The CraftsNet instance.