Class 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 Detail

      • Router

        public Router()
    • Method Detail

      • 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.

      • 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.
      • 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)
      • addOptions

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