Search in sources :

Example 1 with NavigationRouteTarget

use of com.vaadin.flow.router.internal.NavigationRouteTarget 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 NavigationRouteTarget

use of com.vaadin.flow.router.internal.NavigationRouteTarget 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)

Aggregations

NavigationRouteTarget (com.vaadin.flow.router.internal.NavigationRouteTarget)2 RouteTarget (com.vaadin.flow.router.internal.RouteTarget)2 RouteRegistry (com.vaadin.flow.server.RouteRegistry)2 Route (com.vaadin.flow.router.Route)1 Router (com.vaadin.flow.router.Router)1 VaadinService (com.vaadin.flow.server.VaadinService)1 SpringServlet (com.vaadin.flow.spring.SpringServlet)1