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);
}
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;
}
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();
}
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);
});
});
}
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);
}
Aggregations