Search in sources :

Example 1 with NavigationState

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;
    }
}
Also used : NavigationState(com.vaadin.flow.router.NavigationState) Location(com.vaadin.flow.router.Location)

Example 2 with NavigationState

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);
}
Also used : NavigationState(com.vaadin.flow.router.NavigationState) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) NotFoundException(com.vaadin.flow.router.NotFoundException) ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer) ErrorParameter(com.vaadin.flow.router.ErrorParameter)

Example 3 with NavigationState

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);
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationState(com.vaadin.flow.router.NavigationState) JsonObject(elemental.json.JsonObject) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with NavigationState

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);
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationState(com.vaadin.flow.router.NavigationState) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) ErrorParameter(com.vaadin.flow.router.ErrorParameter) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 5 with NavigationState

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());
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) ErrorParameter(com.vaadin.flow.router.ErrorParameter) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationState(com.vaadin.flow.router.NavigationState) HasElement(com.vaadin.flow.component.HasElement) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

NavigationState (com.vaadin.flow.router.NavigationState)6 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)4 Location (com.vaadin.flow.router.Location)4 UI (com.vaadin.flow.component.UI)3 ErrorParameter (com.vaadin.flow.router.ErrorParameter)3 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)3 MockUI (com.vaadin.tests.util.MockUI)3 Test (org.junit.Test)3 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)2 NotFoundException (com.vaadin.flow.router.NotFoundException)2 HasElement (com.vaadin.flow.component.HasElement)1 HasUrlParameter (com.vaadin.flow.router.HasUrlParameter)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 ErrorStateRenderer (com.vaadin.flow.router.internal.ErrorStateRenderer)1 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)1 JsonObject (elemental.json.JsonObject)1