Package com.storedobject.core
Class CORS
java.lang.Object
com.storedobject.core.CORS
The CORS class manages Cross-Origin Resource Sharing (CORS) validation and handling
for HTTP requests. It ensures that the origin of a request is allowed based on
predefined rules and provides options to respond appropriately for valid and invalid origins.
This class supports setting custom reject handling, processing allowed origins dynamically,
and responding to CORS preflight and simple requests.
The CORS class is tightly coupled with the Device interface, which represents the
origin of the request context and utilizes configurations for dynamic CORS policies.
- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
clear()
Clear the allowed origins list so that it will be re-populated on the next request.static void
reject
(jakarta.servlet.http.HttpServletResponse response) Reject the request with a default error message.static boolean
rejected
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles a cross-origin resource sharing (CORS) request and determines whether it should be rejected.static boolean
rejected
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Consumer<jakarta.servlet.http.HttpServletResponse> reject) Handles a cross-origin resource sharing (CORS) request and determines whether it should be rejected.
-
Method Details
-
reject
Reject the request with a default error message.- Parameters:
response
- Response instance.- Throws:
IOException
- If an error occurs.
-
rejected
public static boolean rejected(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException Handles a cross-origin resource sharing (CORS) request and determines whether it should be rejected. Updates the response headers to allow or forbid origin-based access depending on the origin and request method.- Parameters:
request
- The HTTP request object containing the CORS request details.response
- The HTTP response object to be configured based on CORS policies.- Returns:
- true if the request is rejected; false if the request is accepted.
- Throws:
IOException
- If an error occurs while writing to the response.
-
rejected
public static boolean rejected(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Consumer<jakarta.servlet.http.HttpServletResponse> reject) throws IOException Handles a cross-origin resource sharing (CORS) request and determines whether it should be rejected. Updates the response headers to allow or forbid origin-based access depending on the origin and request method.- Parameters:
request
- The HTTP request object containing the CORS request details.response
- The HTTP response object to be configured based on CORS policies.reject
- A custom handler to be invoked if the request is rejected.- Returns:
- true if the request is rejected; false if the request is accepted.
- Throws:
IOException
- If an error occurs while writing to the response.
-
clear
public static void clear()Clear the allowed origins list so that it will be re-populated on the next request.
-