Search in sources :

Example 1 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry 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 2 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class RequestUtil method isAnonymousRoute.

/**
 * Checks whether the request targets a Flow route that is public, i.e.
 * marked as @{@link AnonymousAllowed}.
 *
 * @param request
 *            the servlet request
 * @return {@code true} if the request is targeting an anonymous route,
 *         {@code false} otherwise
 */
public boolean isAnonymousRoute(HttpServletRequest request) {
    String vaadinMapping = configurationProperties.getUrlMapping();
    String requestedPath = HandlerHelper.getRequestPathInsideContext(request);
    Optional<String> maybePath = HandlerHelper.getPathIfInsideServlet(vaadinMapping, requestedPath);
    if (!maybePath.isPresent()) {
        return false;
    }
    String path = maybePath.get();
    if (path.startsWith("/")) {
        // Requested path includes a beginning "/" but route mapping is done
        // without one
        path = path.substring(1);
    }
    SpringServlet servlet = springServletRegistration.getServlet();
    VaadinService service = servlet.getService();
    Router router = service.getRouter();
    RouteRegistry routeRegistry = router.getRegistry();
    NavigationRouteTarget target = routeRegistry.getNavigationRouteTarget(path);
    if (target == null) {
        return false;
    }
    RouteTarget routeTarget = target.getRouteTarget();
    if (routeTarget == null) {
        return false;
    }
    Class<? extends com.vaadin.flow.component.Component> targetView = routeTarget.getTarget();
    if (targetView == null) {
        return false;
    }
    // Check if a not authenticated user can access the view
    boolean result = accessAnnotationChecker.hasAccess(targetView, null, role -> false);
    if (result) {
        getLogger().debug(path + " refers to a public view");
    }
    return result;
}
Also used : RouteRegistry(com.vaadin.flow.server.RouteRegistry) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) VaadinService(com.vaadin.flow.server.VaadinService) Router(com.vaadin.flow.router.Router) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) RouteTarget(com.vaadin.flow.router.internal.RouteTarget) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Example 3 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class DefaultRouteResolver method resolve.

@Override
public NavigationState resolve(ResolveRequest request) {
    RouteRegistry registry = request.getRouter().getRegistry();
    final String path = request.getLocation().getPath();
    NavigationRouteTarget navigationResult = registry.getNavigationRouteTarget(path);
    if (!navigationResult.hasTarget()) {
        return null;
    }
    NavigationStateBuilder builder = new NavigationStateBuilder(request.getRouter());
    try {
        builder.withTarget(navigationResult.getRouteTarget(), navigationResult.getRouteParameters());
        builder.withPath(navigationResult.getPath());
    } catch (NotFoundException nfe) {
        String message = "Exception while navigation to path " + path;
        LoggerFactory.getLogger(this.getClass().getName()).warn(message, nfe);
        throw nfe;
    }
    return builder.build();
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) RouteRegistry(com.vaadin.flow.server.RouteRegistry) NotFoundException(com.vaadin.flow.router.NotFoundException)

Example 4 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry 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 RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class NavigationStateRendererTest method init.

@Before
public void init() {
    RouteRegistry registry = ApplicationRouteRegistry.getInstance(new MockVaadinContext());
    router = new Router(registry);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Router(com.vaadin.flow.router.Router) Before(org.junit.Before)

Aggregations

RouteRegistry (com.vaadin.flow.server.RouteRegistry)13 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)6 SessionRouteRegistry (com.vaadin.flow.server.SessionRouteRegistry)5 Test (org.junit.Test)5 Router (com.vaadin.flow.router.Router)4 UI (com.vaadin.flow.component.UI)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 NotFoundException (com.vaadin.flow.router.NotFoundException)2 Route (com.vaadin.flow.router.Route)2 NavigationRouteTarget (com.vaadin.flow.router.internal.NavigationRouteTarget)2 RouteTarget (com.vaadin.flow.router.internal.RouteTarget)2 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)2 VaadinContext (com.vaadin.flow.server.VaadinContext)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 SpringServlet (com.vaadin.flow.spring.SpringServlet)2 Component (com.vaadin.flow.component.Component)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 Page (com.vaadin.flow.component.page.Page)1 Lookup (com.vaadin.flow.di.Lookup)1 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)1