Class Router<Request extends javax.servlet.ServletRequest,Response extends javax.servlet.ServletResponse>

java.lang.Object
io.github.jirkasa.servletrouter.Router<Request,Response>
Type Parameters:
Request - Type of ServletRequest.
Response - Type of ServletResponse.
All Implemented Interfaces:
Handler<Request,Response>
Direct Known Subclasses:
HttpRouter

public abstract class Router<Request extends javax.servlet.ServletRequest,Response extends javax.servlet.ServletResponse> extends Object implements Handler<Request,Response>
Registers handlers and controllers to specific paths and performs routing for incoming requests.
  • Constructor Details

    • Router

      public Router()
  • Method Details

    • register

      @SafeVarargs public final void register(String path, Handler<Request,Response>... handlers)
      Registers handler(s) for the specified path.
      Parameters:
      path - Path for which the handler(s) is to be called.
      handlers - Handler(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Class<T>... controllers)
      Registers controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the controller(s) is to be called.
      controllers - Controller(s).
    • register

      public final void register(String path, List<Handler<Request,Response>> handlers)
      Registers list of handlers for the specified path.
      Parameters:
      path - Path for which the handlers are to be called.
      handlers - List of handlers.
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, List<Handler<Request,Response>> handlers, Class<T>... controllers)
      Registers list of handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controller(s) are to be called.
      handlers - List of handlers.
      controllers - Controller(s).
    • register

      public final <T extends Controller<Request, Response>> void register(String path, List<Handler<Request,Response>> handlers, List<Class<T>> controllers)
      Registers list of handlers and list of controllers for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controllers are to be called.
      handlers - List of handlers.
      controllers - List of controllers.
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response>[] handlers, Class<T>... controllers)
      Registers array of handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controller(s) are to be called.
      handlers - Array of handlers.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response> handler, Class<T>... controllers)
      Registers handler and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handler and controller(s) are to be called.
      handler - Handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response> handler1, Handler<Request,Response> handler2, Class<T>... controllers)
      Registers two handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handler and controller(s) are to be called.
      handler1 - First handler.
      handler2 - Second handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Class<T>... controllers)
      Registers three handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controller(s) are to be called.
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Handler<Request,Response> handler4, Class<T>... controllers)
      Registers four handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controller(s) are to be called.
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      handler4 - Fourth handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(String path, Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Handler<Request,Response> handler4, Handler<Request,Response> handler5, Class<T>... controllers)
      Registers five handlers and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      path - Path for which the handlers and controller(s) are to be called.
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      handler4 - Fourth handler.
      handler5 - Fifth handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final void register(Handler<Request,Response>... handlers)
      Registers handler(s) for all paths.
      Parameters:
      handlers - Handler(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Class<T>... controllers)
      Registers controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      controllers - Controller(s).
    • register

      public final void register(List<Handler<Request,Response>> handlers)
      Registers list of handlers for all paths.
      Parameters:
      handlers - List of handlers.
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(List<Handler<Request,Response>> handlers, Class<T>... controllers)
      Registers list of handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handlers - List of handlers.
      controllers - Controller(s).
    • register

      public final <T extends Controller<Request, Response>> void register(List<Handler<Request,Response>> handlers, List<Class<T>> controllers)
      Registers list of handlers and list of controllers for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handlers - List of handlers.
      controllers - List of controllers.
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response>[] handlers, Class<T>... controllers)
      Registers array of handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handlers - Array of handlers.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response> handler, Class<T>... controllers)
      Registers handler and controller(s) for the specified path.
      Type Parameters:
      T - Controller class.
      Parameters:
      handler - Handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response> handler1, Handler<Request,Response> handler2, Class<T>... controllers)
      Registers two handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handler1 - First handler.
      handler2 - Second handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Class<T>... controllers)
      Registers three handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Handler<Request,Response> handler4, Class<T>... controllers)
      Registers four handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      handler4 - Fourth handler.
      controllers - Controller(s).
    • register

      @SafeVarargs public final <T extends Controller<Request, Response>> void register(Handler<Request,Response> handler1, Handler<Request,Response> handler2, Handler<Request,Response> handler3, Handler<Request,Response> handler4, Handler<Request,Response> handler5, Class<T>... controllers)
      Registers five handlers and controller(s) for all paths.
      Type Parameters:
      T - Controller class.
      Parameters:
      handler1 - First handler.
      handler2 - Second handler.
      handler3 - Third handler.
      handler4 - Fourth handler.
      handler5 - Fifth handler.
      controllers - Controller(s).
    • registerErrorController

      public final <T extends ErrorController<Request, Response>> void registerErrorController(Class<T> errorController)
      By default when error occurs in handle method of router, exception is thrown. This method registers error controller that is called instead of that.
      Type Parameters:
      T - Error controller class.
      Parameters:
      errorController - Error controller to be called when error occurs in handle method.
    • setPathParams

      public final void setPathParams(Map<String,String> pathParams)
      Description copied from interface: Handler
      Sets path parameters.
      Specified by:
      setPathParams in interface Handler<Request extends javax.servlet.ServletRequest,Response extends javax.servlet.ServletResponse>
      Parameters:
      pathParams - Map of path parameters.
    • handle

      public final boolean handle(Request request, Response response) throws Exception
      Description copied from interface: Handler
      Handles request.
      Specified by:
      handle in interface Handler<Request extends javax.servlet.ServletRequest,Response extends javax.servlet.ServletResponse>
      Parameters:
      request - Request to be handled.
      response - Response to be handled.
      Returns:
      Determines whether handlers chain should continue or not.
      Throws:
      Exception
    • matchesFullPath

      public final boolean matchesFullPath()
      Description copied from interface: Handler
      Determines whether handler should be called when full request path matches or not.
      Specified by:
      matchesFullPath in interface Handler<Request extends javax.servlet.ServletRequest,Response extends javax.servlet.ServletResponse>
      Returns:
      True indicates that full request path should match path of handler. False indicates that just start of request path should match path of handler.
    • getRequestPath

      protected abstract String getRequestPath(Request request)
      This method is called by implementation of handle method to get request path.
      Parameters:
      request - Request based on which should be determined request path.
      Returns:
      Request path without leading and trailing slashes. For example "info/about" (not "/info/about/").