Class RequestHandler
- Direct Known Subclasses:
MethodeBasedHandler, RouteBasedHandler, SSEHandler
This class provides a framework for implementing custom request handlers that process
HTTP requests and generate appropriate responses. Subclasses must implement the
handle(HTTPRequest) method to define their specific request handling logic.
The class provides two variants of the handle method:
handle(HTTPRequest)- Standard request handling without socket accesshandle(HTTPRequest, Socket)- Extended handling with direct socket access for cases requiring persistent connections (e.g., WebSockets, SSE)
By default, the socket-aware method delegates to the standard method. Handlers that require socket access (such as streaming or bidirectional communication) should override the socket-aware variant.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract HTTPResponsehandle(HTTPRequest request) Processes an HTTP request and generates a response.handle(HTTPRequest request, Socket socket) Processes an HTTP request with access to the underlying socket connection.
-
Constructor Details
-
RequestHandler
public RequestHandler()
-
-
Method Details
-
handle
Processes an HTTP request and generates a response.This is the primary method that subclasses must implement to define their request handling behavior. Implementations should:
- Parse and validate the request
- Execute the necessary business logic
- Construct and return an appropriate HTTPResponse
- Parameters:
request- The HTTP request to process.- Returns:
- An HTTPResponse object containing the response to be sent to the client.
- Throws:
Exception- if an error occurs during request processing. The exception will be handled by the calling framework.
-
handle
Processes an HTTP request with access to the underlying socket connection.This method provides an extension point for handlers that need direct access to the client socket, such as:
- Server-Sent Events (SSE) requiring persistent connections
- WebSocket upgrades
- Custom streaming protocols
- Long-polling implementations
The default implementation delegates to
handle(HTTPRequest), making socket access optional. Subclasses requiring socket access should override this method instead of (or in addition to) the standard handle method.- Parameters:
request- The HTTP request to process.socket- The client socket connection, providing direct I/O access.- Returns:
- An HTTPResponse object containing the response to be sent to the client.
- Throws:
Exception- if an error occurs during request processing. The exception will be handled by the calling framework.
-