Class RouteConfiguration

java.lang.Object
com.vaadin.flow.router.RouteConfiguration
All Implemented Interfaces:
Serializable

public class RouteConfiguration extends Object implements Serializable
Route configuration helper class for adding, removing and reading routes from the different registries.
Since:
1.3
See Also:
  • Method Details

    • forSessionScope

      public static RouteConfiguration forSessionScope()
      Get a RouteConfiguration that edits the session scope routes. This requires that VaadinSession.getCurrent() is populated.

      Note! Session scoped registry sees also the application scope routes.

      Returns:
      configurator for session scope routes
    • forApplicationScope

      public static RouteConfiguration forApplicationScope()
      Get a RouteConfiguration that edits the application scope routes. This requires that VaadinServlet.getCurrent() is populated.
      Returns:
      configurator for application scope routes
    • forRegistry

      public static RouteConfiguration forRegistry(RouteRegistry registry)
      Get a RouteConfiguration for editing the given RouteRegistry implementation. This enables editing of registry when the required CurrentInstance is not yet populated.
      Parameters:
      registry - registry to edit through the controller
      Returns:
      configurator for editing given registry
    • getAvailableRoutes

      public List<RouteData> getAvailableRoutes()
      Get the RouteData for all registered navigation targets.

      Note! This would be best to request for session scope registry as it will then contain the actual currently visible routes from both the session and application scopes.

      Note! Size of the list is only main routes as RouteData will contain a list of alias route registrations.

      Returns:
      list of all routes available
    • isPathAvailable

      public boolean isPathAvailable(String path)
      Check if the given path is available.
      Parameters:
      path - path to check for availability
      Returns:
      true if there exists a route for the given path
    • isRouteRegistered

      public boolean isRouteRegistered(Class<? extends Component> route)
      Check if the route is available as a registered target.
      Parameters:
      route - target class to check for registration
      Returns:
      true if class is registered
    • getRoute

      public Optional<Class<? extends Component>> getRoute(String path)
      Gets the registered route class for a given path. Returns an empty optional if no navigation target corresponds to the given path.
      Parameters:
      path - path to get route for
      Returns:
      optional containing the path component or empty if not found
    • getRoute

      public Optional<Class<? extends Component>> getRoute(String path, List<String> segments)
      Gets the optional navigation target class for a given Location matching with path segments.
      Parameters:
      path - path to get navigation target for, not null
      segments - segments given for path
      Returns:
      optional navigation target corresponding to the given path and segments
    • addRoutesChangeListener

      public Registration addRoutesChangeListener(RoutesChangedListener listener)
      Add a listener that is notified when routes change for the registry.
      Parameters:
      listener - listener to add
      Returns:
      registration for removing the listener
    • update

      public void update(Command command)
      Block updates to the registry configuration from other threads until update command has completed.

      Using this method makes the registry changes made inside the command atomic for the registry as no one else can change the state during the duration of the command.

      Any other configuration thread for the same registry will be blocked until all the update locks have been released.

      Note! During an update other threads will get the pre-update state of the registry until the update has fully completed.

      Parameters:
      command - command to execute for the update
    • setAnnotatedRoute

      public void setAnnotatedRoute(Class<? extends Component> navigationTarget)
      Giving a navigation target here will handle the Route annotation to get the path and also register any RouteAlias that may be on the class.
      Parameters:
      navigationTarget - navigation target to register
      Throws:
      InvalidRouteConfigurationException - thrown if exact route already defined in this scope
    • setParentAnnotatedRoute

      public void setParentAnnotatedRoute(String path, Class<? extends Component> navigationTarget)
      Register a navigation target on the specified path. Any ParentLayout annotation on class will be used to populate layout chain, but Route and RouteAlias will not be taken into consideration.
      Parameters:
      path - path to register navigation target to
      navigationTarget - navigation target to register
      Throws:
      InvalidRouteConfigurationException - thrown if exact route already defined in this scope
    • setRoute

      public void setRoute(String path, Class<? extends Component> navigationTarget)
      Register a navigation target with specified path and with no parent layouts.

      Note! Any ParentLayout, Route or RouteAlias will be ignored in route handling.

      Parameters:
      path - path to register navigation target to
      navigationTarget - navigation target to register
      Throws:
      InvalidRouteConfigurationException - thrown if exact route already defined in this scope
    • setRoute

      public void setRoute(String path, Class<? extends Component> navigationTarget, List<Class<? extends RouterLayout>> parentChain)
      Register a navigation target with specified path and given parent layout chain.

      Note! Any ParentLayout, Route or RouteAlias will be ignored in route handling.

      Parameters:
      path - path to register navigation target to
      navigationTarget - navigation target to register
      parentChain - chain of parent layouts that should be used with this target
      Throws:
      InvalidRouteConfigurationException - thrown if exact route already defined in this scope
    • setRoute

      public void setRoute(String path, Class<? extends Component> navigationTarget, Class<? extends RouterLayout>... parentChain)
      Register a navigation target with specified path and given parent layout chain.

      Note! Any ParentLayout, Route or RouteAlias will be ignored in route handling.

      Parameters:
      path - path to register navigation target to
      navigationTarget - navigation target to register
      parentChain - chain of parent layouts that should be used with this target
      Throws:
      InvalidRouteConfigurationException - thrown if exact route already defined in this scope
    • removeRoute

      public void removeRoute(Class<? extends Component> navigationTarget)
      Remove the given navigation target route registration. Path where the navigation target was may still be usable, e.g. we remove target with url param and there is left a non param target, but will not return the removed target.

      Note! this will remove target route and if possible any RouteAlias route that can be found for the class.

      Parameters:
      navigationTarget - navigation target class to remove
    • removeRoute

      public void removeRoute(String path)
      Remove all registrations for given path. This means that any navigation target registered on the given path will be removed. But if a removed navigationTarget for the path exists it is then stored with a new main path so it can still get a resolved url.

      E.g. path "home" contains HomeView and DetailsView[String path param] both will be removed.

      Note! The restored path will be the first found match for all paths that are registered.

      In case navigationTarget is a HasUrlParameter, path argument needs to include the parameter placeholder which is added automatically. Otherwise, using removeRoute(String, Class) is preferred in such a case.

      Parameters:
      path - path for which to remove all navigation targets
    • removeRoute

      public void removeRoute(String path, Class<? extends Component> navigationTarget)
      Remove only the specified navigationTarget from the path and not other targets if they exist for the same path.

      Note! If another path exists for the removed navigation target it will get a new main path so it can still get a resolved url. The restored path will be the first found match for all paths that are registered.

      Parameters:
      path - path to remove from registry
      navigationTarget - path navigation target to remove
    • getHandledRegistry

      public RouteRegistry getHandledRegistry()
      Get the registry that this configuration is working with.
      Returns:
      handled RouteRegistry
    • getUrl

      public String getUrl(Class<? extends Component> navigationTarget)
      Get the registered url string for given navigation target.

      Note! If the navigation target has a url parameter that is required then this method will throw an IllegalArgumentException.

      Parameters:
      navigationTarget - navigation target to get url for
      Returns:
      url for the navigation target
      Throws:
      IllegalArgumentException - if the navigation target requires a parameter
    • getUrlBase

      public Optional<String> getUrlBase(Class<? extends Component> navigationTarget)
      Return the url base without any route parameters.
      Parameters:
      navigationTarget - navigation target to get url for
      Returns:
      optional url base without route parameters or empty if there is no registered route for navigationTarget, not null
    • getTemplate

      public Optional<String> getTemplate(Class<? extends Component> navigationTarget)
      Gets the route template for the given target.
      Parameters:
      navigationTarget - target class.
      Returns:
      main template for the given target.
    • getUrl

      public <T, C extends Component & HasUrlParameter<T>> String getUrl(Class<? extends C> navigationTarget, T parameter)
      Get the url string for given navigation target with the parameter in the url.

      Note! Given parameter is checked for correct class type. This means that if the navigation target defined parameter is of type Boolean then calling getUrl with a String will fail.

      Type Parameters:
      T - url parameter type
      C - navigation target type
      Parameters:
      navigationTarget - navigation target to get url for
      parameter - parameter to embed into the generated url
      Returns:
      url for the navigation target with parameter
    • getUrl

      public <T, C extends Component & HasUrlParameter<T>> String getUrl(Class<? extends C> navigationTarget, List<T> parameters)
      Get the url string for given navigation target with the parameters in the url.

      Note! Given parameters are checked for correct class type. This means that if the navigation target defined parameter is of type Boolean then calling getUrl with a String will fail.

      Type Parameters:
      T - url parameter type
      C - navigation target type
      Parameters:
      navigationTarget - navigation target to get url for
      parameters - parameters to embed into the generated url, not null
      Returns:
      url for the navigation target with parameter
    • getUrl

      public String getUrl(Class<? extends Component> navigationTarget, RouteParameters parameters)
      Gets the url which navigates to given navigationTarget using given parameters.
      Parameters:
      navigationTarget - navigation target.
      parameters - route parameters.
      Returns:
      the url which navigates to given navigationTarget using given parameters.
      Throws:
      NotFoundException - in case the navigationTarget is not registered with a url template matching the given parameters.