use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class LocationChangeEventTest method setup.
@Before
public void setup() {
UI ui = new RouterTestUI();
event = new LocationChangeEvent(ui.getRouterInterface().get(), ui, NavigationTrigger.PROGRAMMATIC, new Location(""), Arrays.asList(new AnotherTestView(), new AnotherParentView()), Collections.emptyMap());
event.getSource().reconfigure(c -> c.setParentView(TestView.class, ParentView.class));
navigationEvent = new NavigationEvent(event.getSource(), event.getLocation(), event.getUI(), event.getTrigger());
}
use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class RouterConfigurationTest method normalViewStatusCode.
@Test
public void normalViewStatusCode() {
Router router = new Router();
router.reconfigure(c -> c.setRoute("*", ParentView.class));
int statusCode = router.getConfiguration().resolveRoute(new Location("")).get().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
Assert.assertEquals(HttpServletResponse.SC_OK, statusCode);
}
use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class RouterConfigurationTest method customErrorViewStatusCode.
@Test
public void customErrorViewStatusCode() {
Router router = new Router();
router.reconfigure(c -> {
c.setErrorView(ParentView.class);
});
int statusCode = router.getConfiguration().getErrorHandler().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, statusCode);
}
use of com.vaadin.flow.router.NavigationEvent 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);
}
use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.
the class AbstractNavigationStateRenderer method forward.
private int forward(NavigationEvent event, BeforeEvent beforeNavigation) {
NavigationHandler handler = beforeNavigation.getForwardTarget();
NavigationEvent newNavigationEvent = getNavigationEvent(event, beforeNavigation);
newNavigationEvent.getUI().getPage().getHistory().replaceState(null, newNavigationEvent.getLocation());
return handler.handle(newNavigationEvent);
}
Aggregations