Search in sources :

Example 1 with RouteConfiguration

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

the class UidlWriterTest method initializeUIForDependenciesTest.

private UI initializeUIForDependenciesTest(UI ui) throws Exception {
    mocks = new MockServletServiceSessionSetup();
    VaadinServletContext context = (VaadinServletContext) mocks.getService().getContext();
    Lookup lookup = context.getAttribute(Lookup.class);
    Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(new RoutePathProviderImpl());
    VaadinSession session = mocks.getSession();
    session.lock();
    ui.getInternals().setSession(session);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        routeConfiguration.setAnnotatedRoute(BaseClass.class);
    });
    for (String type : new String[] { "html", "js", "css" }) {
        mocks.getServlet().addServletContextResource("inline." + type, "inline." + type);
    }
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    ui.doInit(vaadinRequestMock, 1);
    ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(vaadinRequestMock));
    return ui;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinSession(com.vaadin.flow.server.VaadinSession) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) RoutePathProviderImpl(com.vaadin.flow.server.MockVaadinContext.RoutePathProviderImpl) Lookup(com.vaadin.flow.di.Lookup) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup)

Example 2 with RouteConfiguration

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

the class RouteUtil method updateRouteRegistry.

/**
 * Updates route registry as necessary when classes have been added /
 * modified / deleted.
 *
 * @param registry
 *            route registry
 * @param addedClasses
 *            added classes
 * @param modifiedClasses
 *            modified classes
 * @param deletedClasses
 *            deleted classes
 */
public static void updateRouteRegistry(RouteRegistry registry, Set<Class<?>> addedClasses, Set<Class<?>> modifiedClasses, Set<Class<?>> deletedClasses) {
    RouteConfiguration routeConf = RouteConfiguration.forRegistry(registry);
    Logger logger = LoggerFactory.getLogger(RouteUtil.class);
    registry.update(() -> {
        // remove deleted classes and classes that lost the annotation from
        // registry
        Stream.concat(deletedClasses.stream(), modifiedClasses.stream().filter(clazz -> !clazz.isAnnotationPresent(Route.class))).filter(Component.class::isAssignableFrom).forEach(clazz -> {
            Class<? extends Component> componentClass = (Class<? extends Component>) clazz;
            logger.debug("Removing route to {}", componentClass);
            routeConf.removeRoute(componentClass);
        });
        // add new routes to registry
        Stream.concat(addedClasses.stream(), modifiedClasses.stream()).distinct().filter(Component.class::isAssignableFrom).filter(clazz -> clazz.isAnnotationPresent(Route.class)).forEach(clazz -> {
            Class<? extends Component> componentClass = (Class<? extends Component>) clazz;
            logger.debug("Updating route {} to {}", componentClass.getAnnotation(Route.class).value(), clazz);
            routeConf.removeRoute(componentClass);
            routeConf.setAnnotatedRoute(componentClass);
        });
    });
}
Also used : Logger(org.slf4j.Logger) RouterLayout(com.vaadin.flow.router.RouterLayout) Component(com.vaadin.flow.component.Component) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) RoutePathProvider(com.vaadin.flow.router.RoutePathProvider) LoggerFactory(org.slf4j.LoggerFactory) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Set(java.util.Set) RoutePrefix(com.vaadin.flow.router.RoutePrefix) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) List(java.util.List) VaadinContext(com.vaadin.flow.server.VaadinContext) Stream(java.util.stream.Stream) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) Optional(java.util.Optional) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) Collections(java.util.Collections) ParentLayout(com.vaadin.flow.router.ParentLayout) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) Logger(org.slf4j.Logger) Component(com.vaadin.flow.component.Component) Route(com.vaadin.flow.router.Route)

Example 3 with RouteConfiguration

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

the class CustomInitListener method serviceInit.

@Override
public void serviceInit(ServiceInitEvent event) {
    final CustomRouteRegistry registry = CustomRouteRegistry.getInstance(event.getSource().getContext());
    RouteConfiguration configuration = RouteConfiguration.forRegistry(registry);
    configuration.setRoute("", CustomRoute.class);
}
Also used : RouteConfiguration(com.vaadin.flow.router.RouteConfiguration)

Example 4 with RouteConfiguration

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

the class TestingServiceInitListener method serviceInit.

@Override
public void serviceInit(ServiceInitEvent event) {
    event.getSource().addUIInitListener(this::handleUIInit);
    initCount.incrementAndGet();
    RouteConfiguration configuration = RouteConfiguration.forApplicationScope();
    // lock registry from any other updates to get registrations correctly.
    configuration.getHandledRegistry().update(() -> {
        if (!configuration.isPathRegistered(DYNAMICALLY_REGISTERED_ROUTE)) {
            configuration.setRoute(DYNAMICALLY_REGISTERED_ROUTE, DynamicallyRegisteredRoute.class);
        }
    });
    event.addRequestHandler((session, request, response) -> {
        requestCount.incrementAndGet();
        return false;
    });
}
Also used : RouteConfiguration(com.vaadin.flow.router.RouteConfiguration)

Example 5 with RouteConfiguration

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

the class UI method navigate.

/**
 * Updates this UI to show the view corresponding to the given navigation
 * target with the specified parameters. The parameters needs to comply with
 * the ones defined in one of the {@link com.vaadin.flow.router.Route} or
 * {@link com.vaadin.flow.router.RouteAlias} annotating the navigationTarget
 * and with any {@link com.vaadin.flow.router.RoutePrefix} annotating the
 * parent layouts of the navigationTarget.
 * <p>
 * Besides the navigation to the {@code location} this method also updates
 * the browser location (and page history).
 *
 * @param navigationTarget
 *            navigation target to navigate to.
 * @param parameters
 *            parameters to pass to view.
 * @throws IllegalArgumentException
 *             if navigationTarget is a {@link HasUrlParameter} with a
 *             mandatory parameter, but parameters argument doesn't provide
 *             {@link HasUrlParameterFormat#PARAMETER_NAME} parameter.
 * @throws NotFoundException
 *             in case there is no route defined for the given
 *             navigationTarget matching the parameters.
 */
public void navigate(Class<? extends Component> navigationTarget, RouteParameters parameters) {
    RouteConfiguration configuration = RouteConfiguration.forRegistry(getInternals().getRouter().getRegistry());
    navigate(configuration.getUrl(navigationTarget, parameters));
}
Also used : RouteConfiguration(com.vaadin.flow.router.RouteConfiguration)

Aggregations

RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)12 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)3 Lookup (com.vaadin.flow.di.Lookup)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)2 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)2 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)2 VaadinResponse (com.vaadin.flow.server.VaadinResponse)2 VaadinService (com.vaadin.flow.server.VaadinService)2 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)2 Test (org.junit.Test)2 Component (com.vaadin.flow.component.Component)1 PushConfiguration (com.vaadin.flow.component.PushConfiguration)1 UI (com.vaadin.flow.component.UI)1 Push (com.vaadin.flow.component.page.Push)1 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)1 ParentLayout (com.vaadin.flow.router.ParentLayout)1 Route (com.vaadin.flow.router.Route)1 RouteAlias (com.vaadin.flow.router.RouteAlias)1 RoutePathProvider (com.vaadin.flow.router.RoutePathProvider)1