Class MethodeBasedHandler

java.lang.Object
com.ns.tcpframework.reqeustHandlers.RequestHandler
com.ns.tcpframework.reqeustHandlers.MethodeBasedHandler
Direct Known Subclasses:
HelloWorldHandler, StaticFileHandler

public class MethodeBasedHandler extends RequestHandler
Abstract base class for handling HTTP requests. This class uses the Template Method design pattern to define the structure of request handling. Subclasses can override specific HTTP method handlers (GET, POST, PUT, DELETE) as needed.
  • Constructor Details

    • MethodeBasedHandler

      public MethodeBasedHandler()
  • Method Details

    • handle

      public HTTPResponse handle(HTTPRequest request) throws Exception
      Handles an HTTP request by routing it to the appropriate method based on the HTTP method.
      Specified by:
      handle in class RequestHandler
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the response to be sent to the client.
      Throws:
      Exception - If an error occurs during request handling.
    • handleGetRequest

      protected HTTPResponse handleGetRequest(HTTPRequest request) throws Exception
      Handles HTTP GET requests. Subclasses should override this method to provide specific GET request handling logic.
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the status code, headers, and body to send to the client.
      Throws:
      Exception - If the method is not implemented or an error occurs.
    • handlePostRequest

      protected HTTPResponse handlePostRequest(HTTPRequest request) throws Exception
      Handles HTTP POST requests. Subclasses should override this method to provide specific POST request handling logic.
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the status code, headers, and body to send to the client.
      Throws:
      Exception - If the method is not implemented or an error occurs.
    • handlePutRequest

      protected HTTPResponse handlePutRequest(HTTPRequest request) throws Exception
      Handles HTTP PUT requests. Subclasses should override this method to provide specific PUT request handling logic.
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the status code, headers, and body to send to the client.
      Throws:
      Exception - If the method is not implemented or an error occurs.
    • handleDeleteRequest

      protected HTTPResponse handleDeleteRequest(HTTPRequest request) throws Exception
      Handles HTTP DELETE requests. Subclasses should override this method to provide specific DELETE request handling logic.
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the status code, headers, and body to send to the client.
      Throws:
      Exception - If the method is not implemented or an error occurs.
    • handleHeadRequest

      protected HTTPResponse handleHeadRequest(HTTPRequest request) throws Exception
      Handles HTTP HEAD requests. Subclasses should override this method to provide specific HEAD request handling logic.
      Parameters:
      request - The HTTP request to handle.
      Returns:
      An HTTPResponse object containing the status code and headers to send to the client (no body).
      Throws:
      Exception - If the method is not implemented or an error occurs.