Class RequestHandler

java.lang.Object
com.ns.tcpframework.reqeustHandlers.RequestHandler
Direct Known Subclasses:
MethodeBasedHandler, RouteBasedHandler, SSEHandler

public abstract class RequestHandler extends Object
Abstract base class for handling HTTP requests.

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:

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 Details

    • RequestHandler

      public RequestHandler()
  • Method Details

    • handle

      public abstract HTTPResponse handle(HTTPRequest request) throws Exception
      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

      public HTTPResponse handle(HTTPRequest request, Socket socket) throws Exception
      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.