Class Request

java.lang.Object
de.craftsblock.craftsnet.api.http.Request
All Implemented Interfaces:
RequireAble, AutoCloseable

public class Request extends Object implements AutoCloseable, RequireAble
The Request class represents an incoming HTTP request received by the web server. It encapsulates information related to the request, such as headers, query parameters, cookies, and request body.

The class is responsible for parsing and providing access to various elements of the request, making it easier for request handlers to process and respond to the incoming requests.

Since:
1.0.0-SNAPSHOT
See Also:
  • Constructor Details

    • Request

      public Request(CraftsNet craftsNet, HttpExchange httpExchange, Headers headers, String url, String ip, String domain, HttpMethod httpMethod)
      Constructs a new Request object.
      Parameters:
      craftsNet - The CraftsNet instance to which the request was made.
      httpExchange - The HttpExchange object representing the incoming HTTP request.
      headers - The Headers object representing the headers of the incoming http request.
      url - The query string extracted from the request URI.
      ip - The ip address of the client sending the request.
      domain - The domain used to make the http request.
      httpMethod - The HttpMethod used to access the route.
  • Method Details

    • close

      public void close() throws Exception
      Closes the Request object, releasing associated resources such as the HttpExchange and request headers. If request bodies are present, it is also closed all of them to free any related resources.
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - If an error occurs while closing the Request object or its associated resources.
    • isClosed

      public boolean isClosed()
      Gets if the request has been closed.
      Returns:
      true if the request was closed, false otherwise.
    • setExchange

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

      public Exchange getExchange()
      Gets the Exchange managing this request.
      Returns:
      The Exchange managing this request.
    • getCraftsNet

      public CraftsNet getCraftsNet()
      Gets the instance of CraftsNet to which the request was made.
      Returns:
      The CraftsNet instance.
    • getDomain

      public String getDomain()
      Gets the domain through which the route was accessed.
      Returns:
      The domain which is used.
    • getHttpMethod

      public HttpMethod getHttpMethod()
      Gets the http method which is used to access this route.
      Returns:
      The http method which is used.
    • getRawUrl

      public String getRawUrl()
      Gets the raw url which is not trimmed and therefore contains anything except the domain
      Returns:
      The raw url of the request.
    • getUrl

      public String getUrl()
      Gets the trimmed url which only includes the location of the resource.
      Returns:
      The trimmed url of the request.
    • getQueryParams

      public Map<String,String> getQueryParams()
      Gets all query parameters stored in a map of the request.
      Returns:
      The mapped query parameters.
    • hasParam

      public boolean hasParam(String key)
      Checks if the request contains the specified query parameter.
      Parameters:
      key - The key of the query parameter to check.
      Returns:
      True if the request contains the specified query parameter, false otherwise.
    • retrieveParam

      @Nullable public @Nullable String retrieveParam(@NotNull @NotNull String key)
      Retrieves the value of the specified query parameter from the request.
      Parameters:
      key - The key of the query parameter to retrieve.
      Returns:
      The value of the specified query parameter, or null if the parameter is not found.
    • retrieveParam

      @NotNull public @NotNull String retrieveParam(@NotNull @NotNull String key, @NotNull @NotNull String fallback)
      Retrieves the value of the specified query parameter from the request or return a fallback value if it is not present on this request.
      Parameters:
      key - The key of the query parameter to retrieve.
      fallback - The fallback value, if the desired query parameter is not present.
      Returns:
      The value of the specified query parameter, or null if the parameter is not found.
    • getCookies

      public Map<String,Cookie> getCookies()
      Gets all cookies stored in a map of the request.
      Returns:
      The mapped view of all cookies.
    • hasCookie

      public boolean hasCookie(String key)
      Checks if the request contains the specified cookie.
      Parameters:
      key - The name of the cookie to check.
      Returns:
      True if the request contains the specified cookie, false otherwise.
    • retrieveCookie

      @Nullable public @Nullable Cookie retrieveCookie(@NotNull @NotNull String key)
      Retrieves the specified cookie from the request.
      Parameters:
      key - The name of the cookie to retrieve.
      Returns:
      The specified cookie, or null if the cookie is not found.
    • retrieveCookie

      @NotNull public @NotNull Cookie retrieveCookie(@NotNull @NotNull String key, @NotNull @NotNull Cookie fallback)
      Retrieves the specified cookie from the request or return a fallback cookie if it is not present on this request.
      Parameters:
      key - The name of the cookie to retrieve.
      fallback - The fallback value, if the desired cookie is not present.
      Returns:
      The specified cookie, or null if the cookie is not found.
    • getContentType

      public String getContentType()
      Retrieves the content type form the http request.
      Returns:
      The content type.
    • getRoutes

      @Nullable public @Nullable Collection<RouteRegistry.EndpointMapping> getRoutes()
      Retrieves the matched routes mapping for the request.
      Returns:
      The RouteMapping objects representing the matched route, or null if no route is matched.
    • hasBody

      public boolean hasBody()
      Checks if the HTTP request has a request body.
      Returns:
      true if a request body exists, otherwise false.
    • getBody

      public Body getBody()
      Retrieves the HTTP request body, if it exists.
      Returns:
      The HTTP request body as a Body object, or null if no body exists or an error occurs.
    • getRawBody

      public InputStream getRawBody() throws FileNotFoundException
      Retrieves a new instance of an input stream containing the request body.
      Returns:
      The input stream containing the body.
      Throws:
      FileNotFoundException - If the save file which contains the bytes of the body was not found.
    • getHeaders

      public Headers getHeaders()
      Gets all the headers which are present on the request.
      Returns:
      The headers object including the headers.
    • hasHeader

      public boolean hasHeader(String name)
      Checks if the request contains the specified header.
      Parameters:
      name - The name of the header to check.
      Returns:
      True if the request contains the specified header, false otherwise.
    • getHeader

      @Nullable public @Nullable String getHeader(String name)
      Retrieves the value of the specified header from the request.
      Parameters:
      name - The name of the header to retrieve.
      Returns:
      The value of the specified header, or null if the header is not found.
    • getIp

      public String getIp()
      Retrieves the IP address of the client sending the request.
      Returns:
      The IP address of the client.
    • getRequestMethod

      public HttpMethod getRequestMethod()
      Retrieves the RequestMethod of the request (e.g., GET, POST, PUT, etc.).
      Returns:
      The RequestMethod enum representing the request method.
    • getStreamEncoder

      public StreamEncoder getStreamEncoder()
      Retrieves the StreamEncoder that was determined during the request body loading.
      Returns:
      The StreamEncoder that was determined. Can be null if there was no request body or the encoding of the body is unsupported.
      Since:
      3.3.3-SNAPSHOT
    • unsafe

      public HttpExchange unsafe()
      Retrieves the HttpExchange object representing the incoming HTTP request.
      Returns:
      The HttpExchange object.
    • setRoutes

      protected void setRoutes(Collection<RouteRegistry.EndpointMapping> routes)
      Sets the matched routes mapping for the request.
      Parameters:
      routes - The RouteMapping objects representing the matched route.
    • parseCookies

      protected static ConcurrentLinkedQueue<Cookie> parseCookies(Headers headers)
      Parses the "Cookie" header from the given HTTP headers and returns a collection of Cookie objects.

      This method extracts cookie information from the "Cookie" header and constructs Cookie objects for each cookie found. It handles attributes such as Path, Domain, Expires, SameSite, Secure, and HttpOnly.

      Parameters:
      headers - The HTTP headers from which to parse cookies
      Returns:
      A ConcurrentLinkedQueue containing all parsed cookies