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 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 Details

    • Router

      public Router()
  • Method Details

    • notFound

      public T notFound()
      Returns the fallback target for use when there's no match at route(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 at route(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 with notFound as the target if it is set, otherwise returns null.
    • 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 *, use allAllowedMethods() 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. For OPTIONS *.
    • toString

      public String toString()
      Returns visualized routing rules.
      Overrides:
      toString in class Object
    • addConnect

      public Router<T> addConnect(String path, T target)
    • addDelete

      public Router<T> addDelete(String path, T target)
    • addGet

      public Router<T> addGet(String path, T target)
    • addHead

      public Router<T> addHead(String path, T target)
    • addOptions

      public Router<T> addOptions(String path, T target)
    • addPatch

      public Router<T> addPatch(String path, T target)
    • addPost

      public Router<T> addPost(String path, T target)
    • addPut

      public Router<T> addPut(String path, T target)
    • addTrace

      public Router<T> addTrace(String path, T target)
    • addAny

      public Router<T> addAny(String path, T target)