use of com.vaadin.flow.router.NavigationState in project flow by vaadin.
the class JavaScriptBootstrapUI method navigate.
/*
* Navigate to a path triggered by a server view.
*/
@Override
public void navigate(String pathname, QueryParameters queryParameters) {
Location location = new Location(pathname, queryParameters);
if (Boolean.TRUE.equals(getSession().getAttribute(SERVER_ROUTING))) {
// server-side routing
renderViewForRoute(location, NavigationTrigger.UI_NAVIGATE);
return;
}
// prevent looping
if (navigationInProgress || getInternals().hasLastHandledLocation() && sameLocation(getInternals().getLastHandledLocation(), location)) {
return;
}
navigationInProgress = true;
try {
Optional<NavigationState> navigationState = getInternals().getRouter().resolveNavigationTarget(location);
if (navigationState.isPresent()) {
// Navigation can be done in server side without extra
// round-trip
handleNavigation(location, navigationState.get(), NavigationTrigger.UI_NAVIGATE);
if (getForwardToClientUrl() != null) {
// Server is forwarding to a client route from a
// BeforeEnter.
navigateToClient(getForwardToClientUrl());
}
} else {
// Server cannot resolve navigation, let client-side to
// handle it.
navigateToClient(location.getPathWithQueryParameters());
}
} finally {
navigationInProgress = false;
}
}
use of com.vaadin.flow.router.NavigationState in project flow by vaadin.
the class JavaScriptBootstrapUI method handleErrorNavigation.
private void handleErrorNavigation(Location location) {
NavigationState errorNavigationState = getInternals().getRouter().resolveRouteNotFoundNavigationTarget().orElse(getDefaultNavigationError());
ErrorStateRenderer errorStateRenderer = new ErrorStateRenderer(errorNavigationState);
NotFoundException notFoundException = new NotFoundException("Couldn't find route for '" + location.getPath() + "'");
ErrorParameter<NotFoundException> errorParameter = new ErrorParameter<>(NotFoundException.class, notFoundException);
ErrorNavigationEvent errorNavigationEvent = new ErrorNavigationEvent(getInternals().getRouter(), location, this, NavigationTrigger.CLIENT_SIDE, errorParameter);
errorStateRenderer.handle(errorNavigationEvent);
}
use of com.vaadin.flow.router.NavigationState in project flow by vaadin.
the class ErrorStateRendererTest method handle_openNPEView_infiniteReroute_noStackOverflow_throws.
@Test(expected = ExceptionsTrace.class)
public void handle_openNPEView_infiniteReroute_noStackOverflow_throws() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(InfiniteLoopNPEView.class).build();
NavigationStateRenderer renderer = new NavigationStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(InfiniteLoopNPEView.class);
((ApplicationRouteRegistry) ui.getInternals().getRouter().getRegistry()).setErrorNavigationTargets(Collections.singleton(InfiniteLoopErrorTarget.class));
NavigationEvent event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.CLIENT_SIDE);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
JsonObject routerLinkState = Json.createObject();
routerLinkState.put("href", "router_link");
routerLinkState.put("scrollPositionX", 0d);
routerLinkState.put("scrollPositionY", 0d);
event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.ROUTER_LINK, routerLinkState, false);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
}
use of com.vaadin.flow.router.NavigationState in project flow by vaadin.
the class ErrorStateRendererTest method handle_openNPEErrorTarget_infiniteReroute_noStackOverflow_throws.
@Test(expected = ExceptionsTrace.class)
public void handle_openNPEErrorTarget_infiniteReroute_noStackOverflow_throws() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(InfiniteLoopErrorTarget.class).build();
ErrorStateRenderer renderer = new ErrorStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(InfiniteLoopNPEView.class);
ErrorParameter<Exception> parameter = new ErrorParameter<>(Exception.class, new NullPointerException());
ErrorNavigationEvent event = new ErrorNavigationEvent(ui.getInternals().getRouter(), new Location("error"), ui, NavigationTrigger.CLIENT_SIDE, parameter);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
}
use of com.vaadin.flow.router.NavigationState in project flow by vaadin.
the class ErrorStateRendererTest method handle_errorViewLayoutForwardsToAView_viewIsNavigated.
@Test
public void handle_errorViewLayoutForwardsToAView_viewIsNavigated() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(HappyPathErrorTarget.class).build();
ErrorStateRenderer renderer = new ErrorStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(HappyPathViewView.class);
ErrorParameter<Exception> parameter = new ErrorParameter<>(Exception.class, new NullPointerException());
ErrorNavigationEvent event = new ErrorNavigationEvent(ui.getInternals().getRouter(), new Location("error"), ui, NavigationTrigger.CLIENT_SIDE, parameter);
Assert.assertEquals(200, renderer.handle(event));
List<HasElement> chain = ui.getInternals().getActiveRouterTargetsChain();
Assert.assertEquals(1, chain.size());
Assert.assertEquals(HappyPathViewView.class, chain.get(0).getClass());
}
Aggregations