com.vaadin.flow.router.
Class RouteConfiguration
- java.lang.Object
-
- com.vaadin.flow.router.RouteConfiguration
-
All Implemented Interfaces:
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 Summary
All Methods Modifier and Type Method Description Registration
addRoutesChangeListener(RoutesChangedListener listener)
Add a listener that is notified when routes change for the registry.
static RouteConfiguration
forApplicationScope()
Get a
RouteConfiguration
that edits the application scope routes.static RouteConfiguration
forRegistry(RouteRegistry registry)
Get a
RouteConfiguration
for editing the given RouteRegistry implementation.static RouteConfiguration
forSessionScope()
Get a
RouteConfiguration
that edits the session scope routes.List<RouteData>
getAvailableRoutes()
Get the
RouteData
for all registered navigation targets.RouteRegistry
getHandledRegistry()
Get the registry that this configuration is working with.
Optional<Class<? extends Component>>
getRoute(String path)
Gets the registered route class for a given path.
Optional<Class<? extends Component>>
getRoute(String path, List<String> segments)
Gets the optional navigation target class for a given Location matching with path segments.
Optional<String>
getTemplate(Class<? extends Component> navigationTarget)
Gets the route template for the given target.
<T,C extends Component & HasUrlParameter<T>>
StringgetUrl(Class<? extends C> navigationTarget, List<T> parameters)
Get the url string for given navigation target with the parameters in the url.
<T,C extends Component & HasUrlParameter<T>>
StringgetUrl(Class<? extends C> navigationTarget, T parameter)
Get the url string for given navigation target with the parameter in the url.
String
getUrl(Class<? extends Component> navigationTarget)
Get the registered url string for given navigation target.
String
getUrl(Class<? extends Component> navigationTarget, RouteParameters parameters)
Gets the url which navigates to given navigationTarget using given parameters.
Optional<String>
getUrlBase(Class<? extends Component> navigationTarget)
Return the url base without any route parameters.
boolean
isPathAvailable(String path)
Check if the given path is available.
boolean
isPathRegistered(String path)
Deprecated.
boolean
isRouteRegistered(Class<? extends Component> route)
Check if the route is available as a registered target.
void
removeRoute(Class<? extends Component> navigationTarget)
Remove the given navigation target route registration.
void
removeRoute(String path)
Remove all registrations for given path.
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.
void
setAnnotatedRoute(Class<? extends Component> navigationTarget)
Giving a navigation target here will handle the
Route
annotation to get the path and also register anyRouteAlias
that may be on the class.void
setParentAnnotatedRoute(String path, Class<? extends Component> navigationTarget)
Register a navigation target on the specified path.
void
setRoute(String path, Class<? extends Component> navigationTarget)
Register a navigation target with specified path and with no parent layouts.
void
setRoute(String path, Class<? extends Component> navigationTarget, Class<? extends RouterLayout>... parentChain)
Register a navigation target with specified path and given parent layout chain.
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.
void
update(Command command)
Block updates to the registry configuration from other threads until update command has completed.
-
-
-
Method Detail
-
forSessionScope
public static RouteConfiguration forSessionScope()
Get a
RouteConfiguration
that edits the session scope routes. This requires thatVaadinSession.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 thatVaadinServlet.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 requiredCurrentInstance
is not yet populated.Parameters:
registry
- registry to edit through the controllerReturns:
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
-
isPathRegistered
@Deprecated public boolean isPathRegistered(String path)
Deprecated.Check if there is a registered target for the given path.
Parameters:
path
- path to check for route registrationReturns:
true if there exists a route for the given path
-
isPathAvailable
public boolean isPathAvailable(String path)
Check if the given path is available.
Parameters:
path
- path to check for availabilityReturns:
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 registrationReturns:
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 forReturns:
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, notnull
segments
- segments given for pathReturns:
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 addReturns:
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 anyRouteAlias
that may be on the class.Parameters:
navigationTarget
- navigation target to registerThrows:
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, butRoute
andRouteAlias
will not be taken into consideration.Parameters:
path
- path to register navigation target tonavigationTarget
- navigation target to registerThrows:
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
orRouteAlias
will be ignored in route handling.Parameters:
path
- path to register navigation target tonavigationTarget
- navigation target to registerThrows:
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
orRouteAlias
will be ignored in route handling.Parameters:
path
- path to register navigation target tonavigationTarget
- navigation target to registerparentChain
- chain of parent layouts that should be used with this targetThrows:
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
orRouteAlias
will be ignored in route handling.Parameters:
path
- path to register navigation target tonavigationTarget
- navigation target to registerparentChain
- chain of parent layouts that should be used with this targetThrows:
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, usingremoveRoute(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 registrynavigationTarget
- 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 forReturns:
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 forReturns:
optional url base without route parameters or empty if there is no registered route for
navigationTarget
, notnull
-
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 aString
will fail.Type Parameters:
T
- url parameter typeC
- navigation target typeParameters:
navigationTarget
- navigation target to get url forparameter
- parameter to embed into the generated urlReturns:
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 aString
will fail.Type Parameters:
T
- url parameter typeC
- navigation target typeParameters:
navigationTarget
- navigation target to get url forparameters
- parameters to embed into the generated url, not nullReturns:
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.
-
-