Class Router<T>
java.lang.Object
org.apache.flink.runtime.rest.handler.router.Router<T>
This is adopted and simplified code from tv.cntt:netty-router library. Compared to original
version this one defines and guarantees an order of pattern matching routes, drops reverse
routing feature and restores
RouterHandler which was dropped in tv.cntt:netty-router
2.X.X. Original code:
https://github.com/sinetja/netty-router/blob/2.2.0/src/main/java/io/netty/handler/codec/http/router/Router.java
Router that contains information about both route matching orders and HTTP request methods.
Routes are guaranteed to be matched in order of their addition.
Route targets can be any type. In the below example, targets are classes:
Router<Class> router = new Router<Class>()
.GET ("/articles", IndexHandler.class)
.GET ("/articles/:id", ShowHandler.class)
.POST ("/articles", CreateHandler.class)
.GET ("/download/:*", DownloadHandler.class) // ":*" must be the last token
.GET_FIRST("/articles/new", NewHandler.class); // This will be matched first
Slashes at both ends are ignored. These are the same:
router.GET("articles", IndexHandler.class);
router.GET("/articles", IndexHandler.class);
router.GET("/articles/", IndexHandler.class);
You can remove routes by target or by path pattern:
router.removePathPattern("/articles");
To match requests use route(HttpMethod, String).
From the RouteResult you can extract params embedded in the path and from the query
part of the request URI.
notFound(Object) will be used as the target when there's no match.
router.notFound(My404Handler.class);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddConnect(String path, T target) addOptions(String path, T target) addRoute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String pathPattern, T target) Add route.Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod>Returns all methods that this router handles.Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod>allowedMethods(String uri) Returns allowed methods for a specific URI.notFound()Returns the fallback target for use when there's no match atroute(HttpMethod, String).Sets the fallback target for use when there's no match atroute(HttpMethod, String).voidremovePathPattern(String pathPattern) Removes the route specified by the path pattern.If there's no match, returns the result withnotFoundas the target if it is set, otherwise returnsnull.route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path, Map<String, List<String>> queryParameters) intsize()Returns the number of routes in this router.toString()Returns visualized routing rules.
-
Constructor Details
-
Router
public Router()
-
-
Method Details
-
notFound
Returns the fallback target for use when there's no match atroute(HttpMethod, String). -
size
public int size()Returns the number of routes in this router. -
addRoute
public Router<T> addRoute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String pathPattern, T target) Add route.A path pattern can only point to one target. This method does nothing if the pattern has already been added.
-
notFound
Sets the fallback target for use when there's no match atroute(HttpMethod, String). -
removePathPattern
Removes the route specified by the path pattern. -
route
public RouteResult<T> route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path) If there's no match, returns the result withnotFoundas the target if it is set, otherwise returnsnull. -
route
-
allowedMethods
public Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> allowedMethods(String uri) Returns allowed methods for a specific URI.For
OPTIONS *, useallAllowedMethods()instead of this method. -
allAllowedMethods
public Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> allAllowedMethods()Returns all methods that this router handles. ForOPTIONS *. -
toString
Returns visualized routing rules. -
addConnect
-
addDelete
-
addGet
-
addHead
-
addOptions
-
addPatch
-
addPost
-
addPut
-
addTrace
-
addAny
-