Search in sources :

Example 1 with InternalRedirectHandler

use of com.vaadin.flow.router.internal.InternalRedirectHandler in project flow by vaadin.

the class Router method navigate.

/**
 * Navigates the given UI to the given location.
 *
 * @param ui
 *            the UI to update, not <code>null</code>
 * @param location
 *            the location to navigate to, not <code>null</code>
 * @param trigger
 *            the type of user action that triggered this navigation, not
 *            <code>null</code>
 * @return the HTTP status code resulting from the navigation
 */
@Override
public int navigate(UI ui, Location location, NavigationTrigger trigger) {
    assert ui != null;
    assert location != null;
    assert trigger != null;
    // Read volatile field only once per navigation
    ImmutableRouterConfiguration currentConfig = configuration;
    assert currentConfig.isConfigured();
    NavigationEvent navigationEvent = new NavigationEvent(this, location, ui, trigger);
    Optional<NavigationHandler> handler = currentConfig.getResolver().resolve(navigationEvent);
    if (!handler.isPresent()) {
        handler = currentConfig.resolveRoute(location);
    }
    // location but there is a mapping for the other
    if (!handler.isPresent() && !location.getPath().isEmpty()) {
        Location toggledLocation = location.toggleTrailingSlash();
        Optional<NavigationHandler> toggledHandler = currentConfig.resolveRoute(toggledLocation);
        if (toggledHandler.isPresent()) {
            handler = Optional.of(new InternalRedirectHandler(toggledLocation));
        }
    }
    if (!handler.isPresent()) {
        NavigationHandler errorHandler = currentConfig.getErrorHandler();
        handler = Optional.of(errorHandler);
    }
    return handler.get().handle(navigationEvent);
}
Also used : InternalRedirectHandler(com.vaadin.flow.router.internal.InternalRedirectHandler) NavigationEvent(com.vaadin.flow.router.NavigationEvent) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location)

Example 2 with InternalRedirectHandler

use of com.vaadin.flow.router.internal.InternalRedirectHandler in project flow by vaadin.

the class Router method handleNavigation.

private int handleNavigation(UI ui, Location location, NavigationTrigger trigger) {
    NavigationState newState = getRouteResolver().resolve(new ResolveRequest(this, location));
    if (newState != null) {
        NavigationEvent navigationEvent = new NavigationEvent(this, location, ui, trigger);
        NavigationHandler handler = new NavigationStateRenderer(newState);
        return handler.handle(navigationEvent);
    } else if (!location.getPath().isEmpty()) {
        Location slashToggledLocation = location.toggleTrailingSlash();
        NavigationState slashToggledState = getRouteResolver().resolve(new ResolveRequest(this, slashToggledLocation));
        if (slashToggledState != null) {
            NavigationEvent navigationEvent = new NavigationEvent(this, slashToggledLocation, ui, trigger);
            NavigationHandler handler = new InternalRedirectHandler(slashToggledLocation);
            return handler.handle(navigationEvent);
        }
    }
    throw new NotFoundException("Couldn't find route for '" + location.getPath() + "'");
}
Also used : InternalRedirectHandler(com.vaadin.flow.router.internal.InternalRedirectHandler) ResolveRequest(com.vaadin.flow.router.internal.ResolveRequest) NavigationStateRenderer(com.vaadin.flow.router.internal.NavigationStateRenderer)

Aggregations

InternalRedirectHandler (com.vaadin.flow.router.internal.InternalRedirectHandler)2 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NavigationHandler (com.vaadin.flow.router.NavigationHandler)1 NavigationStateRenderer (com.vaadin.flow.router.internal.NavigationStateRenderer)1 ResolveRequest (com.vaadin.flow.router.internal.ResolveRequest)1