Search in sources :

Example 31 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class VaadinAppShellInitializer method init.

/**
 * Initializes the {@link AppShellRegistry} for the application.
 *
 * @param classes
 *            a set of classes that matches the {@link HandlesTypes} set in
 *            this class.
 * @param context
 *            the {@link VaadinContext}.
 */
@SuppressWarnings("unchecked")
public static void init(Set<Class<?>> classes, VaadinContext context) {
    ApplicationConfiguration config = ApplicationConfiguration.get(context);
    if (config.useV14Bootstrap()) {
        return;
    }
    boolean disregardOffendingAnnotations = config.getBooleanProperty(Constants.ALLOW_APPSHELL_ANNOTATIONS, false);
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    registry.reset();
    if (classes == null || classes.isEmpty()) {
        return;
    }
    List<String> offendingAnnotations = new ArrayList<>();
    classes.stream().sorted((a, b) -> registry.isShell(a) ? -1 : registry.isShell(b) ? 1 : 0).forEach(clz -> {
        if (registry.isShell(clz)) {
            registry.setShell((Class<? extends AppShellConfigurator>) clz);
            getLogger().debug("Using {} class for configuring `index.html` response", clz.getName());
        } else {
            String error = registry.validateClass(clz);
            if (error != null) {
                offendingAnnotations.add(error);
            }
        }
    });
    if (!offendingAnnotations.isEmpty()) {
        if (disregardOffendingAnnotations) {
            boolean hasPwa = offendingAnnotations.stream().anyMatch(err -> err.matches(".*@PWA.*"));
            String message = String.format(hasPwa ? ERROR_HEADER_OFFENDING_PWA : ERROR_HEADER_NO_SHELL, String.join("\n  ", offendingAnnotations));
            getLogger().error(message);
        } else {
            String message = String.format(ERROR_HEADER_NO_SHELL, String.join("\n  ", offendingAnnotations));
            throw new InvalidApplicationConfigurationException(message);
        }
    }
    List<String> classesImplementingPageConfigurator = classes.stream().filter(clz -> PageConfigurator.class.isAssignableFrom(clz)).map(Class::getName).collect(Collectors.toList());
    if (!classesImplementingPageConfigurator.isEmpty()) {
        String message = String.join("\n - ", classesImplementingPageConfigurator);
        if (registry.getShell() != null) {
            message = String.format(ERROR_HEADER_OFFENDING_CONFIGURATOR, registry.getShell().getName(), message);
            throw new InvalidApplicationConfigurationException(message);
        } else {
            message = String.format(ERROR_HEADER_NO_APP_CONFIGURATOR, message);
            getLogger().error(message);
        }
    }
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Arrays(java.util.Arrays) Inline(com.vaadin.flow.component.page.Inline) HandlesTypes(javax.servlet.annotation.HandlesTypes) LoggerFactory(org.slf4j.LoggerFactory) PageConfigurator(com.vaadin.flow.server.PageConfigurator) PageTitle(com.vaadin.flow.router.PageTitle) ArrayList(java.util.ArrayList) NoTheme(com.vaadin.flow.theme.NoTheme) Theme(com.vaadin.flow.theme.Theme) ERROR_HEADER_NO_APP_CONFIGURATOR(com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_NO_APP_CONFIGURATOR) ERROR_HEADER_OFFENDING_CONFIGURATOR(com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_OFFENDING_CONFIGURATOR) Constants(com.vaadin.flow.server.Constants) AppShellConfigurator(com.vaadin.flow.component.page.AppShellConfigurator) ServletContextListener(javax.servlet.ServletContextListener) ERROR_HEADER_OFFENDING_PWA(com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_OFFENDING_PWA) Logger(org.slf4j.Logger) PWA(com.vaadin.flow.server.PWA) Meta(com.vaadin.flow.component.page.Meta) Set(java.util.Set) Collectors(java.util.stream.Collectors) WebListener(javax.servlet.annotation.WebListener) Serializable(java.io.Serializable) List(java.util.List) VaadinContext(com.vaadin.flow.server.VaadinContext) ServletContextEvent(javax.servlet.ServletContextEvent) InvalidApplicationConfigurationException(com.vaadin.flow.server.InvalidApplicationConfigurationException) Annotation(java.lang.annotation.Annotation) BodySize(com.vaadin.flow.component.page.BodySize) ERROR_HEADER_NO_SHELL(com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_NO_SHELL) AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) ServletContext(javax.servlet.ServletContext) Viewport(com.vaadin.flow.component.page.Viewport) Push(com.vaadin.flow.component.page.Push) InvalidApplicationConfigurationException(com.vaadin.flow.server.InvalidApplicationConfigurationException) ArrayList(java.util.ArrayList) AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) PageConfigurator(com.vaadin.flow.server.PageConfigurator)

Example 32 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class RouteConfigurationTest method mockRegistry.

private RouteRegistry mockRegistry() {
    RouteRegistry registry = Mockito.mock(RouteRegistry.class);
    VaadinContext context = new MockVaadinContext();
    Mockito.when(registry.getContext()).thenReturn(context);
    return registry;
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry)

Example 33 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class FrontendUtilsTest method mockResourceProvider.

private ResourceProvider mockResourceProvider(VaadinService service) {
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
    Mockito.when(service.getContext()).thenReturn(context);
    Mockito.when(config.isProductionMode()).thenReturn(true);
    Mockito.when(config.getStringProperty(SERVLET_PARAMETER_STATISTICS_JSON, VAADIN_SERVLET_RESOURCES + STATISTICS_JSON_DEFAULT)).thenReturn("foo");
    return provider;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 34 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class FrontendUtilsTest method getServiceWithResource.

private VaadinService getServiceWithResource(String content) throws ServiceException, IOException {
    MockDeploymentConfiguration configuration = new MockDeploymentConfiguration();
    configuration.setProductionMode(true);
    MockVaadinServletService service = new MockVaadinServletService(configuration);
    VaadinContext context = service.getContext();
    Lookup lookup = Mockito.mock(Lookup.class);
    context.setAttribute(Lookup.class, lookup);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    if (content != null) {
        File tmpFile = tmpDir.newFile();
        try (FileOutputStream outputStream = new FileOutputStream(tmpFile)) {
            IOUtils.write(content, outputStream, StandardCharsets.UTF_8);
        }
        Mockito.when(provider.getApplicationResource(VAADIN_SERVLET_RESOURCES + STATISTICS_JSON_DEFAULT)).thenReturn(tmpFile.toURI().toURL());
    }
    return service;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ResourceProvider(com.vaadin.flow.di.ResourceProvider) FileOutputStream(java.io.FileOutputStream) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File)

Example 35 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class DefaultApplicationConfigurationFactoryTest method create_tokenFileIsReadFromClassloader_externalStatsFileIsReadFromTokenFile_predefinedContext.

@Test
public void create_tokenFileIsReadFromClassloader_externalStatsFileIsReadFromTokenFile_predefinedContext() throws MalformedURLException, IOException {
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinConfig config = Mockito.mock(VaadinConfig.class);
    ResourceProvider resourceProvider = mockResourceProvider(config, context);
    String content = "{ 'externalStatsFile':true }";
    mockClassPathTokenFile(resourceProvider, content);
    DefaultApplicationConfigurationFactory factory = new DefaultApplicationConfigurationFactory();
    ApplicationConfiguration configuration = factory.create(context);
    List<String> propertyNames = Collections.list(configuration.getPropertyNames());
    Assert.assertTrue(propertyNames.contains(Constants.EXTERNAL_STATS_FILE));
    Assert.assertTrue(configuration.getBooleanProperty(Constants.EXTERNAL_STATS_FILE, false));
    Assert.assertFalse(configuration.isProductionMode());
    Assert.assertFalse(configuration.enableDevServer());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinConfig(com.vaadin.flow.server.VaadinConfig) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Test(org.junit.Test)

Aggregations

VaadinContext (com.vaadin.flow.server.VaadinContext)42 Test (org.junit.Test)16 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)15 VaadinService (com.vaadin.flow.server.VaadinService)11 Lookup (com.vaadin.flow.di.Lookup)8 ResourceProvider (com.vaadin.flow.di.ResourceProvider)8 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)5 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)5 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 Properties (java.util.Properties)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)4 File (java.io.File)4 Before (org.junit.Before)4 VaadinSession (com.vaadin.flow.server.VaadinSession)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 AppShellRegistry (com.vaadin.flow.server.AppShellRegistry)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)2