Search in sources :

Example 1 with RouteData

use of com.vaadin.flow.router.RouteData in project flow by vaadin.

the class RouteRegistry method getRegisteredRoutes.

/**
 * Get the {@link RouteData} for all registered navigation targets.
 *
 * @return list of routes available for this registry
 */
public List<RouteData> getRegisteredRoutes() {
    // Build and collect only on first request
    if (routeData.get() == null) {
        List<RouteData> registeredRoutes = new ArrayList<>();
        targetRoutes.get().forEach((target, url) -> {
            List<Class<?>> parameters = getRouteParameters(target);
            RouteData route = new RouteData(getParentLayout(target), url, parameters, target);
            registeredRoutes.add(route);
        });
        Collections.sort(registeredRoutes);
        routeData.compareAndSet(null, Collections.unmodifiableList(registeredRoutes));
    }
    return routeData.get();
}
Also used : ArrayList(java.util.ArrayList) RouteData(com.vaadin.flow.router.RouteData)

Example 2 with RouteData

use of com.vaadin.flow.router.RouteData in project flow by vaadin.

the class AbstractRouteRegistry method populateRegisteredRoutes.

private void populateRegisteredRoutes(ConfiguredRoutes configuration, List<RouteData> registeredRoutes, Class<? extends Component> target, String template) {
    List<RouteAliasData> routeAliases = new ArrayList<>();
    configuration.getRoutePaths(target).stream().filter(routePathTemplate -> !routePathTemplate.equals(template)).forEach(aliasRoutePathTemplate -> routeAliases.add(new RouteAliasData(getParentLayouts(configuration, aliasRoutePathTemplate), aliasRoutePathTemplate, configuration.getParameters(aliasRoutePathTemplate), target)));
    List<Class<? extends RouterLayout>> parentLayouts = getParentLayouts(configuration, template);
    RouteData route = new RouteData(parentLayouts, template, configuration.getParameters(template), target, routeAliases);
    registeredRoutes.add(route);
}
Also used : RouteAliasData(com.vaadin.flow.router.RouteAliasData) Component(com.vaadin.flow.component.Component) SerializableBiConsumer(com.vaadin.flow.function.SerializableBiConsumer) Registration(com.vaadin.flow.shared.Registration) ArrayList(java.util.ArrayList) Map(java.util.Map) Command(com.vaadin.flow.server.Command) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) NotFoundException(com.vaadin.flow.router.NotFoundException) RouterLayout(com.vaadin.flow.router.RouterLayout) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RouteData(com.vaadin.flow.router.RouteData) RouteRegistry(com.vaadin.flow.server.RouteRegistry) ReflectTools(com.vaadin.flow.internal.ReflectTools) RoutesChangedListener(com.vaadin.flow.router.RoutesChangedListener) Serializable(java.io.Serializable) InvalidRouteConfigurationException(com.vaadin.flow.server.InvalidRouteConfigurationException) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) RouteBaseData(com.vaadin.flow.router.RouteBaseData) Collections(java.util.Collections) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteParameters(com.vaadin.flow.router.RouteParameters) RouterLayout(com.vaadin.flow.router.RouterLayout) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteData(com.vaadin.flow.router.RouteData) RouteAliasData(com.vaadin.flow.router.RouteAliasData)

Example 3 with RouteData

use of com.vaadin.flow.router.RouteData in project flow by vaadin.

the class SessionRouteRegistry method addRoutesChangeListener.

/**
 * Adds the given route change listener to the registry.
 * <p>
 * For the session scoped registry also changes to the application scoped
 * registry will be delegated to the listener if the added or removed route
 * was not masked by a registration in the session scope.
 *
 * @param listener
 *            listener to add
 * @return registration to remove the listener
 */
@Override
public Registration addRoutesChangeListener(RoutesChangedListener listener) {
    final Registration parentRegistration = getParentRegistry().addRoutesChangeListener(event -> {
        ConfiguredRoutes configuration = getConfiguration();
        List<RouteBaseData<?>> addedVisible = event.getAddedRoutes().stream().filter(routeData -> !configuration.hasTemplate(routeData.getTemplate())).collect(Collectors.toList());
        List<RouteBaseData<?>> removedVisible = event.getRemovedRoutes().stream().filter(routeData -> !configuration.hasTemplate(routeData.getTemplate())).collect(Collectors.toList());
        // Only fire an event if we have visible changes.
        if (!(addedVisible.isEmpty() && removedVisible.isEmpty())) {
            fireEvent(new RoutesChangedEvent(event.getSource(), addedVisible, removedVisible));
        }
    });
    final Registration registration = super.addRoutesChangeListener(listener);
    return () -> {
        registration.remove();
        parentRegistration.remove();
    };
}
Also used : RouterLayout(com.vaadin.flow.router.RouterLayout) Component(com.vaadin.flow.component.Component) RouteData(com.vaadin.flow.router.RouteData) AbstractRouteRegistry(com.vaadin.flow.router.internal.AbstractRouteRegistry) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) RoutesChangedListener(com.vaadin.flow.router.RoutesChangedListener) Collectors(java.util.stream.Collectors) ConfiguredRoutes(com.vaadin.flow.router.internal.ConfiguredRoutes) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) Optional(java.util.Optional) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) PathUtil(com.vaadin.flow.router.internal.PathUtil) RouteBaseData(com.vaadin.flow.router.RouteBaseData) RouteTarget(com.vaadin.flow.router.internal.RouteTarget) RouteParameters(com.vaadin.flow.router.RouteParameters) Registration(com.vaadin.flow.shared.Registration) RouteBaseData(com.vaadin.flow.router.RouteBaseData) ConfiguredRoutes(com.vaadin.flow.router.internal.ConfiguredRoutes) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent)

Example 4 with RouteData

use of com.vaadin.flow.router.RouteData in project flow by vaadin.

the class VaadinService method init.

/**
 * Initializes this service. The service should be initialized before it is
 * used.
 *
 * @throws ServiceException
 *             if a problem occurs when creating the service
 */
public void init() throws ServiceException {
    doSetClassLoader();
    instantiator = createInstantiator();
    // init the router now so that registry will be available for
    // modifications
    router = new Router(getRouteRegistry());
    List<RequestHandler> handlers = createRequestHandlers();
    ServiceInitEvent event = new ServiceInitEvent(this);
    // allow service init listeners and DI to use thread local access to
    // e.g. application scoped route registry
    runWithServiceContext(() -> {
        instantiator.getServiceInitListeners().forEach(listener -> listener.serviceInit(event));
        event.getAddedRequestHandlers().forEach(handlers::add);
        Collections.reverse(handlers);
        requestHandlers = Collections.unmodifiableCollection(handlers);
        dependencyFilters = Collections.unmodifiableCollection(instantiator.getDependencyFilters(event.getAddedDependencyFilters()).collect(Collectors.toList()));
        bootstrapListeners = instantiator.getBootstrapListeners(event.getAddedBootstrapListeners()).collect(Collectors.toList());
        indexHtmlRequestListeners = instantiator.getIndexHtmlRequestListeners(event.getAddedIndexHtmlRequestListeners()).collect(Collectors.toList());
    });
    DeploymentConfiguration configuration = getDeploymentConfiguration();
    if (!configuration.isProductionMode()) {
        Logger logger = getLogger();
        logger.debug("The application has the following routes: ");
        List<RouteData> routeDataList = getRouteRegistry().getRegisteredRoutes();
        if (!routeDataList.isEmpty()) {
            addRouterUsageStatistics();
        }
        routeDataList.stream().map(Object::toString).forEach(logger::debug);
    }
    if (getDeploymentConfiguration().isPnpmEnabled()) {
        UsageStatistics.markAsUsed("flow/pnpm", null);
    }
    initialized = true;
}
Also used : StreamRequestHandler(com.vaadin.flow.server.communication.StreamRequestHandler) UidlRequestHandler(com.vaadin.flow.server.communication.UidlRequestHandler) SessionRequestHandler(com.vaadin.flow.server.communication.SessionRequestHandler) Router(com.vaadin.flow.router.Router) RouteData(com.vaadin.flow.router.RouteData) Logger(org.slf4j.Logger) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 5 with RouteData

use of com.vaadin.flow.router.RouteData in project flow by vaadin.

the class AbstractRouteRegistry method flattenRoutes.

/**
 * Flatten route data so that all route aliases are also as their own
 * entries in the list. Removes any route aliases as the route is the same
 * even if aliases change.
 *
 * @param routeData
 *            route data to flatten.
 * @return flattened list of routes and aliases
 */
private List<RouteBaseData<?>> flattenRoutes(List<RouteData> routeData) {
    List<RouteBaseData<?>> flatRoutes = new ArrayList<>();
    for (RouteData route : routeData) {
        RouteData nonAliasCollection = new RouteData(route.getParentLayouts(), route.getTemplate(), route.getRouteParameters(), route.getNavigationTarget(), Collections.emptyList());
        flatRoutes.add(nonAliasCollection);
        route.getRouteAliases().forEach(flatRoutes::add);
    }
    return flatRoutes;
}
Also used : RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteData(com.vaadin.flow.router.RouteData)

Aggregations

RouteData (com.vaadin.flow.router.RouteData)7 ArrayList (java.util.ArrayList)4 RouteAliasData (com.vaadin.flow.router.RouteAliasData)3 RouteBaseData (com.vaadin.flow.router.RouteBaseData)3 Component (com.vaadin.flow.component.Component)2 RouteParameters (com.vaadin.flow.router.RouteParameters)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 RoutesChangedEvent (com.vaadin.flow.router.RoutesChangedEvent)2 RoutesChangedListener (com.vaadin.flow.router.RoutesChangedListener)2 Registration (com.vaadin.flow.shared.Registration)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Lookup (com.vaadin.flow.di.Lookup)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 SerializableBiConsumer (com.vaadin.flow.function.SerializableBiConsumer)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)1 NotFoundException (com.vaadin.flow.router.NotFoundException)1