Class Router<T>
- java.lang.Object
-
- org.apache.flink.runtime.rest.handler.router.Router<T>
-
public class Router<T> extends Object
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 restoresRouterHandler
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.javaRouter 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 Constructor Description Router()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Router<T>
addAny(String path, T target)
Router<T>
addConnect(String path, T target)
Router<T>
addDelete(String path, T target)
Router<T>
addGet(String path, T target)
Router<T>
addHead(String path, T target)
Router<T>
addOptions(String path, T target)
Router<T>
addPatch(String path, T target)
Router<T>
addPost(String path, T target)
Router<T>
addPut(String path, T target)
Router<T>
addRoute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String pathPattern, T target)
Add route.Router<T>
addTrace(String path, T target)
Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod>
allAllowedMethods()
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.T
notFound()
Returns the fallback target for use when there's no match atroute(HttpMethod, String)
.Router<T>
notFound(T target)
Sets the fallback target for use when there's no match atroute(HttpMethod, String)
.void
removePathPattern(String pathPattern)
Removes the route specified by the path pattern.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 withnotFound
as the target if it is set, otherwise returnsnull
.RouteResult<T>
route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path, Map<String,List<String>> queryParameters)
int
size()
Returns the number of routes in this router.String
toString()
Returns visualized routing rules.
-
-
-
Method Detail
-
notFound
public T 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
public Router<T> notFound(T target)
Sets the fallback target for use when there's no match atroute(HttpMethod, String)
.
-
removePathPattern
public void removePathPattern(String pathPattern)
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 withnotFound
as the target if it is set, otherwise returnsnull
.
-
route
public RouteResult<T> route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path, Map<String,List<String>> queryParameters)
-
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
public String toString()
Returns visualized routing rules.
-
-