Class RestApiRouter

java.lang.Object
com.loomcache.server.rest.RestApiRouter

public class RestApiRouter extends Object
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.

  • Constructor Details

    • RestApiRouter

      public RestApiRouter(RestApiServer apiServer)
      Create a new REST API router.
      Parameters:
      apiServer - the REST API server instance (non-null)
      Throws:
      NullPointerException - if apiServer is null
  • Method Details

    • setManagementHandler

      public void setManagementHandler(@Nullable ManagementApiHandler handler)
      Set the management API handler for dashboard endpoints.
      Parameters:
      handler - the management handler (may be null to disable)
    • route

      public void route(HttpExchange exchange)
      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

      public static Map<String,String> parseQueryParams(@Nullable String query)
      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

      public static String readRequestBody(HttpExchange exchange) throws IOException
      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 occurs
      NullPointerException - 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 occurs
      NullPointerException - 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 occurs
      NullPointerException - if any parameter is null