Class Request
java.lang.Object
de.craftsblock.craftsnet.api.http.Request
- All Implemented Interfaces:
RequireAble,AutoCloseable
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 Summary
ConstructorsConstructorDescriptionRequest(CraftsNet craftsNet, HttpExchange httpExchange, Headers headers, String url, String ip, String domain, HttpMethod httpMethod) Constructs a new Request object. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the Request object, releasing associated resources such as the HttpExchange and request headers.getBody()Retrieves the HTTP request body, if it exists.Retrieves the content type form the http request.Gets all cookies stored in a map of the request.Gets the instance of CraftsNet to which the request was made.Gets the domain through which the route was accessed.Gets theExchangemanaging this request.@Nullable StringRetrieves the value of the specified header from the request.Gets all the headers which are present on the request.Gets the http method which is used to access this route.getIp()Retrieves the IP address of the client sending the request.Gets all query parameters stored in a map of the request.Retrieves a new instance of an input stream containing the request body.Gets the raw url which is not trimmed and therefore contains anything except the domainRetrieves the RequestMethod of the request (e.g., GET, POST, PUT, etc.).@Nullable Collection<RouteRegistry.EndpointMapping>Retrieves the matched routes mapping for the request.Retrieves theStreamEncoderthat was determined during the request body loading.getUrl()Gets the trimmed url which only includes the location of the resource.booleanhasBody()Checks if the HTTP request has a request body.booleanChecks if the request contains the specified cookie.booleanChecks if the request contains the specified header.booleanChecks if the request contains the specified query parameter.booleanisClosed()Gets if the request has been closed.protected static ConcurrentLinkedQueue<Cookie>parseCookies(Headers headers) Parses the "Cookie" header from the given HTTP headers and returns a collection of Cookie objects.@Nullable CookieretrieveCookie(@NotNull String key) Retrieves the specified cookie from the request.@NotNull CookieretrieveCookie(@NotNull String key, @NotNull Cookie fallback) Retrieves the specified cookie from the request or return a fallback cookie if it is not present on this request.@Nullable StringretrieveParam(@NotNull String key) Retrieves the value of the specified query parameter from the request.@NotNull StringretrieveParam(@NotNull String key, @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.protected voidsetExchange(Exchange exchange) Sets theExchangemanaging this request.protected voidSets the matched routes mapping for the request.unsafe()Retrieves the HttpExchange object representing the incoming HTTP request.
-
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- TheCraftsNetinstance to which the request was made.httpExchange- TheHttpExchangeobject representing the incoming HTTP request.headers- TheHeadersobject 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- TheHttpMethodused to access the route.
-
-
Method Details
-
close
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:
closein interfaceAutoCloseable- 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
Sets theExchangemanaging this request.- Parameters:
exchange- TheExchangemanaging the request
-
getExchange
Gets theExchangemanaging this request.- Returns:
- The
Exchangemanaging this request.
-
getCraftsNet
Gets the instance of CraftsNet to which the request was made.- Returns:
- The CraftsNet instance.
-
getDomain
Gets the domain through which the route was accessed.- Returns:
- The domain which is used.
-
getHttpMethod
Gets the http method which is used to access this route.- Returns:
- The http method which is used.
-
getRawUrl
Gets the raw url which is not trimmed and therefore contains anything except the domain- Returns:
- The raw url of the request.
-
getUrl
Gets the trimmed url which only includes the location of the resource.- Returns:
- The trimmed url of the request.
-
getQueryParams
Gets all query parameters stored in a map of the request.- Returns:
- The mapped query parameters.
-
hasParam
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
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
Gets all cookies stored in a map of the request.- Returns:
- The mapped view of all cookies.
-
hasCookie
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
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
Retrieves the content type form the http request.- Returns:
- The content type.
-
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:
trueif a request body exists, otherwisefalse.
-
getBody
Retrieves the HTTP request body, if it exists.- Returns:
- The HTTP request body as a
Bodyobject, ornullif no body exists or an error occurs.
-
getRawBody
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
Gets all the headers which are present on the request.- Returns:
- The headers object including the headers.
-
hasHeader
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
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
Retrieves the IP address of the client sending the request.- Returns:
- The IP address of the client.
-
getRequestMethod
Retrieves the RequestMethod of the request (e.g., GET, POST, PUT, etc.).- Returns:
- The RequestMethod enum representing the request method.
-
getStreamEncoder
Retrieves theStreamEncoderthat was determined during the request body loading.- Returns:
- The
StreamEncoderthat was determined. Can benullif there was no request body or the encoding of the body is unsupported. - Since:
- 3.3.3-SNAPSHOT
-
unsafe
Retrieves the HttpExchange object representing the incoming HTTP request.- Returns:
- The HttpExchange object.
-
setRoutes
Sets the matched routes mapping for the request.- Parameters:
routes- The RouteMapping objects representing the matched route.
-
parseCookies
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
-