Package io.github.jirkasa.servletrouter
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract String
getRequestPath
(Request request) This method is called by implementation of handle method to get request path.final boolean
Handles request.final boolean
Determines whether handler should be called when full request path matches or not.final void
Registers handler(s) for all paths.final <T extends Controller<Request,
Response>>
voidRegisters array of handlers and controller(s) for all paths.final <T extends Controller<Request,
Response>>
voidregister
(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.final <T extends Controller<Request,
Response>>
voidregister
(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.final <T extends Controller<Request,
Response>>
voidregister
(Handler<Request, Response> handler1, Handler<Request, Response> handler2, Handler<Request, Response> handler3, Class<T>... controllers) Registers three handlers and controller(s) for all paths.final <T extends Controller<Request,
Response>>
voidregister
(Handler<Request, Response> handler1, Handler<Request, Response> handler2, Class<T>... controllers) Registers two handlers and controller(s) for all paths.final <T extends Controller<Request,
Response>>
voidRegisters handler and controller(s) for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters controller(s) for all paths.final void
Registers handler(s) for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters array of handlers and controller(s) for the specified path.final <T extends Controller<Request,
Response>>
voidregister
(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.final <T extends Controller<Request,
Response>>
voidregister
(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.final <T extends Controller<Request,
Response>>
voidregister
(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.final <T extends Controller<Request,
Response>>
voidregister
(String path, Handler<Request, Response> handler1, Handler<Request, Response> handler2, Class<T>... controllers) Registers two handlers and controller(s) for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters handler and controller(s) for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters controller(s) for the specified path.final void
Registers list of handlers for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters list of handlers and controller(s) for the specified path.final <T extends Controller<Request,
Response>>
voidRegisters list of handlers and list of controllers for the specified path.final void
Registers list of handlers for all paths.final <T extends Controller<Request,
Response>>
voidRegisters list of handlers and controller(s) for all paths.final <T extends Controller<Request,
Response>>
voidRegisters list of handlers and list of controllers for all paths.final <T extends ErrorController<Request,
Response>>
voidregisterErrorController
(Class<T> errorController) By default when error occurs in handle method of router, exception is thrown.final void
setPathParams
(Map<String, String> pathParams) Sets path parameters.
-
Constructor Details
-
Router
public Router()
-
-
Method Details
-
register
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
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
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
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
Description copied from interface:Handler
Sets path parameters.- Specified by:
setPathParams
in interfaceHandler<Request extends javax.servlet.ServletRequest,
Response extends javax.servlet.ServletResponse> - Parameters:
pathParams
- Map of path parameters.
-
handle
Description copied from interface:Handler
Handles request. -
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 interfaceHandler<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
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/").
-