use of com.vaadin.flow.router.internal.RouteTarget 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);
}
use of com.vaadin.flow.router.internal.RouteTarget 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;
}
Aggregations