Class AbstractRouter<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>,F extends RouteExtractor<A,C>>

java.lang.Object
io.inverno.mod.http.base.router.AbstractRouter<A,B,C,D,E,F>
Type Parameters:
A - the resource type
B - the input type
C - the route type
D - the route manager type
E - the router type
F - the route extractor type
All Implemented Interfaces:
Router<A,B,C,D,E>

public abstract class AbstractRouter<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>,F extends RouteExtractor<A,C>> extends Object implements Router<A,B,C,D,E>

Base Router implementation.

It is based on a routing chain, implementors must also provide consistent implementations for: AbstractRoute, AbstractRouteManager and AbstractRouteExtractor.

Assuming resource resolution is based on criteria A, B and C extracted from a SampleInput, implementors would provides RoutingLink implementations: ARoutingLink, BRoutingLink and CRoutingLink, each of them being responsible for resolving the next routing link from their respective criteria. A sample router could then be implemented as follows:


 public class SampleRouter extends AbstractRouter<SampleResource, SampleInput, SampleRoute, SampleRouteManager, SampleRouter, SampleRouteExtractor> {

     public TestRouter() {
         super(RoutingLink
             .link(ARoutingLink::new)
             .link(BRoutingLink::new)
             .link(ign -> new CRoutingLink())
         );
     }

     @Override
     protected SampleRoute createRoute(String resource, boolean disabled) {
         return new SampleRoute(this, resource, disabled);
     }

     @Override
     protected SampleRouteExtractor createRouteExtractor() {
         return new SampleRouteExtractor(this);
     }

     @Override
     protected SampleRouteManager createRouteManager() {
         return new SampleRouteManager(this);
     }
 }
 
Since:
1.12
Author:
Jeremy Kuhn
See Also:
  • Constructor Details

    • AbstractRouter

      protected AbstractRouter(RoutingLink.ChainBuilder<A,B,C,F> chainBuilder)

      Creates a router.

      Parameters:
      chainBuilder - the routing chain builder
    • AbstractRouter

      protected AbstractRouter(RoutingLink<A,B,C,F> firstLink)

      Creates a router.

      Parameters:
      firstLink - the first link in the routing chain
  • Method Details

    • createRoute

      protected abstract C createRoute(A resource, boolean disabled)

      Creates a route.

      Parameters:
      resource - the resource targeted by the route
      disabled - true to create a disabled route, false otherwise
      Returns:
      a route
    • createRouteManager

      protected abstract D createRouteManager()

      Creates a route manager.

      Returns:
      a route manager
    • createRouteExtractor

      protected abstract F createRouteExtractor()

      Creates a route extractor.

      Returns:
      a route extractor
    • setRoute

      protected void setRoute(C route)

      Sets the specified route in the router.

      Parameters:
      route - the route to set
    • removeRoute

      protected void removeRoute(C route)

      Removes the specified route from the router.

      Parameters:
      route - the route to remove
    • disableRoute

      protected void disableRoute(C route)

      Disables the specified route in the router.

      Parameters:
      route - the route to disable
    • enableRoute

      protected void enableRoute(C route)

      Enables the specified route in the router.

      Parameters:
      route - the route to enable
    • route

      public D route()
      Description copied from interface: Router

      Returns a new route manager to define a route in the router.

      Specified by:
      route in interface Router<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>>
      Returns:
      a new route manager
    • route

      public E route(Consumer<D> configurer)
      Description copied from interface: Router

      Defines a route in the router using a configurer.

      Specified by:
      route in interface Router<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>>
      Parameters:
      configurer - a route configurer
      Returns:
      the router
    • getRoutes

      public final Set<C> getRoutes()
      Description copied from interface: Router

      Returns the routes defined in the router.

      Specified by:
      getRoutes in interface Router<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>>
      Returns:
      a set of routes
    • resolve

      public A resolve(B input)
      Description copied from interface: Router

      Resolves the resource best matching the specified input.

      Specified by:
      resolve in interface Router<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>>
      Parameters:
      input - an input
      Returns:
      the best matching resource or null if the input is not matching any route
    • resolveAll

      public Collection<A> resolveAll(B input)
      Description copied from interface: Router

      Resolves all resources matching the specified input ordered from the best matching to the least matching.

      Specified by:
      resolveAll in interface Router<A,B,C extends Route<A>,D extends RouteManager<A,B,C,D,E>,E extends Router<A,B,C,D,E>>
      Parameters:
      input - an input
      Returns:
      a list of resources ordered from the best matching to the least matching or an empty if no route is matching the input