public class Router
extends java.lang.Object
The main advantage is that each router can easily be used with each of our servers:
All you need to do is create a Router, add some routes to this Router, and then link your Router to each intended server.
There exists 2 types of routes:
For each route, you can then easily define the response to send back to the client.
Note: a default "hello"
route is added to each router.
You can then easily test your installation by sending an "hello" request to your server(s).
GeneralExample
Modifier and Type | Class and Description |
---|---|
static interface |
Router.Response
Java 8 interface used to represent a response to a request without parameters.
|
static interface |
Router.ResponseWithParams
Interface used to represent a response to a request with parameters.
|
Constructor and Description |
---|
Router()
Constructor for a basic router.
|
Modifier and Type | Method and Description |
---|---|
void |
addRoute(java.lang.String route,
Router.Response response)
Adds a new route WITHOUT parameters to the router.
|
void |
addRouteWithParams(java.lang.String route,
int nbParams,
Router.ResponseWithParams response)
Adds a new route WITH parameters to the router.
|
java.lang.String |
getResponse(java.lang.String route,
java.lang.String[] params)
Checks the validity of the request, and then evaluate the response to send
back to the client.
|
java.util.Map<java.lang.String,Router.Response> |
getRoutesWithoutParams()
Get the list of requests without parameters contained in this router.
|
java.util.Map<java.lang.String,com.raspoid.network.Pair<java.lang.Integer,Router.ResponseWithParams>> |
getRoutesWithParams()
Get the list of requests with parameters contained in this router.
|
boolean |
routeIsValid(java.lang.String route,
java.lang.String[] params)
Checks if the route is valid and contains a correspondance in the router.
|
public Router()
A default "hello" route is added to each router. You can then easily test your installation by sending an "hello" request to your server(s).
public void addRoute(java.lang.String route, Router.Response response)
route
- the command corresponding to this route.response
- the response to execute when the request will be triggered.public void addRouteWithParams(java.lang.String route, int nbParams, Router.ResponseWithParams response)
route
- the command corresponding to this route.nbParams
- the number of parameters that form the request.response
- the response to execute when the request will be triggered.public boolean routeIsValid(java.lang.String route, java.lang.String[] params)
route
- the route to analyze.params
- the input parameters of the route to analyze. null if no parameters.public java.lang.String getResponse(java.lang.String route, java.lang.String[] params)
This method always returns a comprehensive response String, even if the request is invalid.
route
- the route to analyze.params
- the input parameters to use with the route. null if route without parameters.public java.util.Map<java.lang.String,Router.Response> getRoutesWithoutParams()
public java.util.Map<java.lang.String,com.raspoid.network.Pair<java.lang.Integer,Router.ResponseWithParams>> getRoutesWithParams()