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