Class RequestHandler

java.lang.Object
dev.relism.flash.models.RequestHandler
Direct Known Subclasses:
BaseViewHandler, SimpleHandler

public abstract class RequestHandler extends Object
Base class for class-based route handlers.

Declare the route with @Route or a shorthand such as @GET("/path"), then register via FlashRegistrar.scan(java.lang.String). For ad-hoc routes use the fluent API (app.get(path, handler) or with middleware).

Lifecycle

  1. Instantiation — no-arg constructor (for scan) or manual new Handler(...)
  2. bind(dev.relism.flash.extension.FlashContext) — called once by the framework at start(), injects the FlashContext and invokes onInit()
  3. handle(dev.relism.flash.models.Request, dev.relism.flash.models.Response) — called on every matching request (hot-path, zero-alloc)

Service access

Override onInit() to cache services from the FlashContext into private fields. This keeps the hot-path (handle) free of map lookups.

 @GET("/users")
 public class UserHandler extends RequestHandler {
     private UserService users;

     @Override protected void onInit() {
         users = require(UserService.class);
     }

     @Override public Object handle(Request req, Response res) {
         return users.findAll();
     }
 }
 
  • Constructor Details

    • RequestHandler

      public RequestHandler()
  • Method Details

    • bind

      public final void bind(FlashContext ctx)
      Called once by the framework after instantiation, before the first request. Injects the FlashContext and triggers onInit().

      Infrastructure method — do not call from user code. Use FlashRegistrar.scan(java.lang.String) instead.

    • onInit

      protected void onInit()
      Override to cache services at boot time. Called once after bind(dev.relism.flash.extension.FlashContext), before any request reaches this handler.

      Use require(java.lang.Class<T>) and find(java.lang.Class<T>) to retrieve services from the FlashContext. Cache them in private fields so the hot-path (handle(dev.relism.flash.models.Request, dev.relism.flash.models.Response)) has zero lookup overhead.

      Important: if your class extends another handler base (e.g. JacksonHandler), call super.onInit() first so the parent can initialise its own services.

      
       @Override protected void onInit() {
           super.onInit();
           myService = require(MyService.class);
       }
       
    • require

      protected <T> T require(Class<T> type)
      Retrieves a required service from the FlashContext. Throws IllegalStateException if the service is not registered or this handler has not been bound yet.

      Typically called inside onInit() to cache the result.

      Type Parameters:
      T - the service type
      Parameters:
      type - the service class
      Returns:
      the service instance, never null
    • find

      protected <T> Optional<T> find(Class<T> type)
      Looks up an optional service from the FlashContext.
      Type Parameters:
      T - the service type
      Parameters:
      type - the service class
      Returns:
      the service, or empty if not registered
    • optional

      protected <T> Optional<T> optional(Class<T> type)
      Looks up an optional service from the FlashContext. Identical to find(java.lang.Class<T>) — prefer this name for expressive call sites (optional(MyService.class).ifPresent(...)).
      Type Parameters:
      T - the service type
      Parameters:
      type - the service class
      Returns:
      the service, or empty if not registered
    • handle

      public abstract Object handle(Request request, Response response) throws Exception
      Handles an incoming request. The return value determines the response body: return a Response to replace the whole response, any other non-null value to set it as the body, or null to leave the response as-is.
      Throws:
      Exception