Class RestApiRouter
java.lang.Object
com.loomcache.server.rest.RestApiRouter
HTTP request router for the REST API.
Responsibilities: - Route parsing: extract path segments and HTTP method - Query parameter parsing and URL decoding - HTTP method dispatch (GET, PUT, POST, DELETE) - Request body reading and JSON parsing - Error handling with proper HTTP status codes - Response serialization (JSON)
Thread-safe: Can handle concurrent requests safely.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classTyped client-input failure for request bodies that exceed the REST router limit. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionparseQueryParams(@Nullable String query) Parse query parameters from the query string.static StringreadRequestBody(HttpExchange exchange) Read the request body as a UTF-8 string.voidroute(HttpExchange exchange) Route an HTTP request to the appropriate handler based on path and method.static voidsendError(HttpExchange exchange, int statusCode, String message, String detail) Send an error response in standard JSON error format.static voidsendJson(HttpExchange exchange, int statusCode, @Nullable Object body) Send a JSON response.voidsetManagementHandler(@Nullable ManagementApiHandler handler) Set the management API handler for dashboard endpoints.
-
Constructor Details
-
RestApiRouter
Create a new REST API router.- Parameters:
apiServer- the REST API server instance (non-null)- Throws:
NullPointerException- if apiServer is null
-
-
Method Details
-
setManagementHandler
Set the management API handler for dashboard endpoints.- Parameters:
handler- the management handler (may be null to disable)
-
route
Route an HTTP request to the appropriate handler based on path and method.- Parameters:
exchange- the HTTP exchange (non-null)- Throws:
NullPointerException- if exchange is null
-
parseQueryParams
Parse query parameters from the query string.Decodes URL-encoded parameters and returns an unmodifiable map. Handles missing values gracefully (default to empty string) and rejects malformed percent-encoding by propagating
IllegalArgumentException.- Parameters:
query- the query string (may be null or empty)- Returns:
- unmodifiable map of query parameters (empty if query is null/empty)
-
readRequestBody
Read the request body as a UTF-8 string.- Parameters:
exchange- the HTTP exchange (non-null)- Returns:
- request body string (may be empty but non-null)
- Throws:
IOException- if an I/O error occursNullPointerException- if exchange is null
-
sendJson
public static void sendJson(HttpExchange exchange, int statusCode, @Nullable Object body) throws IOException Send a JSON response.Sets Content-Type to application/json, encodes body as JSON, and sends response.
- Parameters:
exchange- the HTTP exchange (non-null)statusCode- the HTTP status code (2xx, 4xx, 5xx, etc.)body- the response body to serialize as JSON (may be null)- Throws:
IOException- if an I/O error occursNullPointerException- if exchange is null
-
sendError
public static void sendError(HttpExchange exchange, int statusCode, String message, String detail) throws IOException Send an error response in standard JSON error format.Response structure: { "error": "message", "message": "detail", "timestamp": milliseconds }
- Parameters:
exchange- the HTTP exchange (non-null)statusCode- the HTTP status code (non-2xx)message- the error title/classification (non-null, non-empty)detail- additional error details (non-null)- Throws:
IOException- if an I/O error occursNullPointerException- if any parameter is null
-