Search in sources :

Example 6 with RouteParameters

use of com.vaadin.flow.router.RouteParameters in project flow by vaadin.

the class RouteSegment method matchSegmentTemplatesWithParameters.

void matchSegmentTemplatesWithParameters(String template, RouteParameters parameters, Consumer<RouteSegmentValue> segmentProcessor, Consumer<RouteSegment> targetSegmentProcessor) {
    final List<String> segmentTemplates = PathUtil.getSegmentsList(template);
    if (segmentTemplates.isEmpty() && parameters.getParameterNames().isEmpty()) {
        return;
    }
    final Set<String> parameterNames = new HashSet<>(parameters.getParameterNames());
    RouteParameters finalParameters = parameters;
    matchSegmentTemplates(template, segmentTemplates, routeSegment -> {
        final Optional<String> segmentValue = getSegmentValue(routeSegment, finalParameters);
        if (routeSegment.isParameter()) {
            parameterNames.remove(routeSegment.getName());
        }
        if (segmentProcessor != null) {
            segmentProcessor.accept(new RouteSegmentValue(routeSegment, segmentValue));
        }
    }, routeSegment -> {
        // All parameter must be used.
        if (!parameterNames.isEmpty()) {
            throw new IllegalArgumentException("All provided RouteParameters must be used to process the template. Provide the exact required RouteParameters or a template that will use all RouteParameters");
        }
        if (targetSegmentProcessor != null) {
            targetSegmentProcessor.accept(routeSegment);
        }
    });
}
Also used : RouteParameters(com.vaadin.flow.router.RouteParameters) HashSet(java.util.HashSet)

Example 7 with RouteParameters

use of com.vaadin.flow.router.RouteParameters in project flow by vaadin.

the class UITest method navigate_throws_illegal_argument_exception.

@Test
public void navigate_throws_illegal_argument_exception() {
    UI ui = new UI();
    initUI(ui, "", null);
    try {
        ui.navigate(Parameterized.class);
        Assert.fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(e.getMessage().endsWith("requires a parameter."));
    }
    try {
        ui.navigate(Parameterized.class, (String) null);
        Assert.fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(e.getMessage().endsWith("requires a parameter."));
    }
    try {
        ui.navigate(Parameterized.class, RouteParameters.empty());
        Assert.fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(e.getMessage().endsWith("requires a parameter."));
    }
    try {
        ui.navigate(Parameterized.class, new RouteParameters("some", "value"));
        Assert.fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(e.getMessage().endsWith("requires a parameter."));
    }
}
Also used : MockUI(com.vaadin.tests.util.MockUI) RouteParameters(com.vaadin.flow.router.RouteParameters) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 8 with RouteParameters

use of com.vaadin.flow.router.RouteParameters in project flow by vaadin.

the class AbstractNavigationStateRenderer method getNavigationEvent.

private NavigationEvent getNavigationEvent(NavigationEvent event, BeforeEvent beforeNavigation) {
    if (beforeNavigation.hasErrorParameter()) {
        ErrorParameter<?> errorParameter = beforeNavigation.getErrorParameter();
        return new ErrorNavigationEvent(event.getSource(), event.getLocation(), event.getUI(), NavigationTrigger.PROGRAMMATIC, errorParameter);
    }
    String url;
    final boolean isForward = beforeNavigation.hasForwardTarget();
    if (isForward) {
        url = beforeNavigation.getForwardUrl();
    } else {
        url = beforeNavigation.getRerouteUrl();
    }
    if (url == null) {
        final String redirectType;
        final Class<? extends Component> redirectTarget;
        final RouteParameters redirectParameters;
        if (isForward) {
            redirectType = "forward";
            redirectTarget = beforeNavigation.getForwardTargetType();
            redirectParameters = beforeNavigation.getForwardTargetRouteParameters();
        } else {
            redirectType = "reroute";
            redirectTarget = beforeNavigation.getRerouteTargetType();
            redirectParameters = beforeNavigation.getRerouteTargetRouteParameters();
        }
        throw new IllegalStateException(String.format("Attempting to %s to unresolved location target %s with route parameters %s", redirectType, redirectTarget, redirectParameters));
    }
    Location location = new Location(url, event.getLocation().getQueryParameters());
    return new NavigationEvent(event.getSource(), location, event.getUI(), NavigationTrigger.PROGRAMMATIC, null, true);
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouteParameters(com.vaadin.flow.router.RouteParameters) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) Location(com.vaadin.flow.router.Location)

Aggregations

RouteParameters (com.vaadin.flow.router.RouteParameters)8 Test (org.junit.Test)5 BootstrapHandlerTest (com.vaadin.flow.server.BootstrapHandlerTest)3 MockUI (com.vaadin.tests.util.MockUI)3 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 Component (com.vaadin.flow.component.Component)1 HasElement (com.vaadin.flow.component.HasElement)1 UI (com.vaadin.flow.component.UI)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)1 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NotFoundException (com.vaadin.flow.router.NotFoundException)1 RouteParam (com.vaadin.flow.router.RouteParam)1 RouterLayout (com.vaadin.flow.router.RouterLayout)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1