Search in sources :

Example 1 with Route

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

the class AbstractRouteRegistryInitializer method validateRouteAnnotation.

/* Route validator methods for bootstrap annotations */
private void validateRouteAnnotation(Class<?> route, Class<? extends Annotation> annotation) {
    Route routeAnnotation = route.getAnnotation(Route.class);
    if (!UI.class.equals(routeAnnotation.layout())) {
        if (route.isAnnotationPresent(annotation)) {
            throw new InvalidRouteLayoutConfigurationException(String.format("%s annotation needs to be on the top parent layout '%s' not on '%s'", annotation.getSimpleName(), RouterUtil.getTopParentLayout(route, routeAnnotation.value()).getName(), route.getName()));
        }
        List<Class<? extends RouterLayout>> parentLayouts = RouterUtil.getParentLayouts(route, routeAnnotation.value());
        Class<? extends RouterLayout> topParentLayout = RouterUtil.getTopParentLayout(route, routeAnnotation.value());
        validateParentAnnotation(parentLayouts, topParentLayout, annotation);
    }
}
Also used : RouterLayout(com.vaadin.flow.router.RouterLayout) UI(com.vaadin.flow.component.UI) InvalidRouteLayoutConfigurationException(com.vaadin.flow.server.InvalidRouteLayoutConfigurationException) Route(com.vaadin.flow.router.Route)

Example 2 with Route

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

the class RouteRegistry method getNavigationRoute.

/**
 * Collect the whole route for the navigation target.
 * <p>
 * The whole route is composed of the Route annotation and any
 * ParentLayout:@RoutePrefix that may be in the navigation chain.
 *
 * @param navigationTarget
 *            navigation target to get chain route for
 * @return full navigation route
 */
private String getNavigationRoute(Class<?> navigationTarget, Collection<String> aliases) {
    Route annotation = navigationTarget.getAnnotation(Route.class);
    aliases.addAll(getRouteAliases(navigationTarget));
    return RouterUtil.getRoutePath(navigationTarget, annotation);
}
Also used : Route(com.vaadin.flow.router.Route)

Example 3 with Route

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

the class RequestUtilTest method addRoute.

private void addRoute(SpringServlet servlet, Class<? extends Component> view) {
    Optional<Route> route = AnnotationReader.getAnnotationFor(view, Route.class);
    if (!route.isPresent()) {
        throw new IllegalArgumentException("Unable find a @Route annotation");
    }
    String path = RouteUtil.getRoutePath(view, route.get());
    RouteRegistry routeRegistry = servlet.getService().getRouter().getRegistry();
    RouteTarget publicRouteTarget = Mockito.mock(RouteTarget.class);
    NavigationRouteTarget navigationTarget = Mockito.mock(NavigationRouteTarget.class);
    Mockito.when(routeRegistry.getNavigationRouteTarget(path)).thenReturn(navigationTarget);
    Mockito.when(navigationTarget.getRouteTarget()).thenReturn(publicRouteTarget);
    Mockito.when(publicRouteTarget.getTarget()).thenReturn((Class) view);
}
Also used : RouteRegistry(com.vaadin.flow.server.RouteRegistry) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) RouteTarget(com.vaadin.flow.router.internal.RouteTarget) Route(com.vaadin.flow.router.Route)

Example 4 with Route

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

the class RouteUtil method updateRouteRegistry.

/**
 * Updates route registry as necessary when classes have been added /
 * modified / deleted.
 *
 * @param registry
 *            route registry
 * @param addedClasses
 *            added classes
 * @param modifiedClasses
 *            modified classes
 * @param deletedClasses
 *            deleted classes
 */
public static void updateRouteRegistry(RouteRegistry registry, Set<Class<?>> addedClasses, Set<Class<?>> modifiedClasses, Set<Class<?>> deletedClasses) {
    RouteConfiguration routeConf = RouteConfiguration.forRegistry(registry);
    Logger logger = LoggerFactory.getLogger(RouteUtil.class);
    registry.update(() -> {
        // remove deleted classes and classes that lost the annotation from
        // registry
        Stream.concat(deletedClasses.stream(), modifiedClasses.stream().filter(clazz -> !clazz.isAnnotationPresent(Route.class))).filter(Component.class::isAssignableFrom).forEach(clazz -> {
            Class<? extends Component> componentClass = (Class<? extends Component>) clazz;
            logger.debug("Removing route to {}", componentClass);
            routeConf.removeRoute(componentClass);
        });
        // add new routes to registry
        Stream.concat(addedClasses.stream(), modifiedClasses.stream()).distinct().filter(Component.class::isAssignableFrom).filter(clazz -> clazz.isAnnotationPresent(Route.class)).forEach(clazz -> {
            Class<? extends Component> componentClass = (Class<? extends Component>) clazz;
            logger.debug("Updating route {} to {}", componentClass.getAnnotation(Route.class).value(), clazz);
            routeConf.removeRoute(componentClass);
            routeConf.setAnnotatedRoute(componentClass);
        });
    });
}
Also used : Logger(org.slf4j.Logger) RouterLayout(com.vaadin.flow.router.RouterLayout) Component(com.vaadin.flow.component.Component) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) RoutePathProvider(com.vaadin.flow.router.RoutePathProvider) LoggerFactory(org.slf4j.LoggerFactory) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Set(java.util.Set) RoutePrefix(com.vaadin.flow.router.RoutePrefix) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) List(java.util.List) VaadinContext(com.vaadin.flow.server.VaadinContext) Stream(java.util.stream.Stream) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) Optional(java.util.Optional) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) Collections(java.util.Collections) ParentLayout(com.vaadin.flow.router.ParentLayout) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) Logger(org.slf4j.Logger) Component(com.vaadin.flow.component.Component) Route(com.vaadin.flow.router.Route)

Example 5 with Route

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

the class AbstractRouteRegistryInitializer method validateRouteParentLayout.

private void validateRouteParentLayout(Class<?> route) {
    Route annotation = route.getAnnotation(Route.class);
    ParentLayout parentLayout = route.getAnnotation(ParentLayout.class);
    if (annotation == null || parentLayout == null) {
        return;
    }
    if (!RouterLayout.class.isAssignableFrom(route)) {
        throw new InvalidRouteLayoutConfigurationException(String.format("The class '%s' should either be a '%s' or only a navigation target using" + " '%s.layout' to set the parent layout", route.getSimpleName(), RouterLayout.class.getSimpleName(), Route.class.getSimpleName()));
    }
}
Also used : RouterLayout(com.vaadin.flow.router.RouterLayout) InvalidRouteLayoutConfigurationException(com.vaadin.flow.server.InvalidRouteLayoutConfigurationException) ParentLayout(com.vaadin.flow.router.ParentLayout) Route(com.vaadin.flow.router.Route)

Aggregations

Route (com.vaadin.flow.router.Route)17 RouterLayout (com.vaadin.flow.router.RouterLayout)8 Component (com.vaadin.flow.component.Component)6 InvalidRouteLayoutConfigurationException (com.vaadin.flow.server.InvalidRouteLayoutConfigurationException)5 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)5 Test (org.junit.Test)5 UI (com.vaadin.flow.component.UI)4 RouteAlias (com.vaadin.flow.router.RouteAlias)3 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 ServletContext (javax.servlet.ServletContext)3 Before (org.junit.Before)3 HtmlContainer (com.vaadin.flow.component.HtmlContainer)2 Tag (com.vaadin.flow.component.Tag)2 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)2 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)2 BeforeEvent (com.vaadin.flow.router.BeforeEvent)2