Search in sources :

Example 6 with AppShellRegistry

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

the class IndexHtmlRequestHandlerTest method should_throwUnSupportedException_when_usingAppShellToConfigureLoadingIndicator.

@Test(expected = UnsupportedOperationException.class)
public void should_throwUnSupportedException_when_usingAppShellToConfigureLoadingIndicator() throws Exception {
    // Set class in context and do not call initializer
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    registry.setShell(MyAppShellWithLoadingIndicatorConfig.class);
    mocks.setAppShellRegistry(registry);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
}
Also used : AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) Test(org.junit.Test)

Example 7 with AppShellRegistry

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

the class IndexHtmlRequestHandlerTest method should_throwUnSupportedException_when_usingAppShellToConfigureReconnectionDialog.

@Test(expected = UnsupportedOperationException.class)
public void should_throwUnSupportedException_when_usingAppShellToConfigureReconnectionDialog() throws Exception {
    // Set class in context and do not call initializer
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    registry.setShell(MyAppShellWithReconnectionDialogConfig.class);
    mocks.setAppShellRegistry(registry);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
}
Also used : AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) Test(org.junit.Test)

Example 8 with AppShellRegistry

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

the class IndexHtmlRequestHandlerTest method should_add_metaAndPwa_Inline_Elements_when_appShellPresent.

@Test
public void should_add_metaAndPwa_Inline_Elements_when_appShellPresent() throws Exception {
    // Set class in context and do not call initializer
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    registry.setShell(AppShellWithPWA.class);
    mocks.setAppShellRegistry(registry);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
    String indexHtml = responseOutput.toString(StandardCharsets.UTF_8.name());
    Document document = Jsoup.parse(indexHtml);
    Elements elements = document.head().getElementsByTag("meta");
    assertEquals(5, elements.size());
    assertEquals("viewport", elements.get(1).attr("name"));
    assertEquals("my-viewport", elements.get(1).attr("content"));
    assertEquals("apple-mobile-web-app-capable", elements.get(2).attr("name"));
    assertEquals("yes", elements.get(2).attr("content"));
    assertEquals("theme-color", elements.get(3).attr("name"));
    assertEquals("#ffffff", elements.get(3).attr("content"));
    assertEquals("apple-mobile-web-app-status-bar-style", elements.get(4).attr("name"));
    assertEquals("#ffffff", elements.get(4).attr("content"));
    Elements headInlineAndStyleElements = document.head().getElementsByTag("style");
    assertEquals(2, headInlineAndStyleElements.size());
    assertEquals("text/css", headInlineAndStyleElements.get(1).attr("type"));
    assertEquals("body,#outlet{width:my-width;height:my-height;}", headInlineAndStyleElements.get(1).childNode(0).toString());
}
Also used : Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) Test(org.junit.Test)

Example 9 with AppShellRegistry

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

the class IndexHtmlRequestHandlerTest method should_throwUnSupportedException_when_usingAppShellToConfigurePushMode.

@Test(expected = UnsupportedOperationException.class)
public void should_throwUnSupportedException_when_usingAppShellToConfigurePushMode() throws Exception {
    // Set class in context and do not call initializer
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    registry.setShell(MyAppShellWithPushConfig.class);
    mocks.setAppShellRegistry(registry);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
}
Also used : AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) Test(org.junit.Test)

Example 10 with AppShellRegistry

use of com.vaadin.flow.server.AppShellRegistry 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)

Aggregations

AppShellRegistry (com.vaadin.flow.server.AppShellRegistry)11 Test (org.junit.Test)8 JsonObject (elemental.json.JsonObject)3 Document (org.jsoup.nodes.Document)3 PushConfiguration (com.vaadin.flow.component.PushConfiguration)2 VaadinContext (com.vaadin.flow.server.VaadinContext)2 VaadinRequest (com.vaadin.flow.server.VaadinRequest)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)2 Elements (org.jsoup.select.Elements)2 AppShellConfigurator (com.vaadin.flow.component.page.AppShellConfigurator)1 BodySize (com.vaadin.flow.component.page.BodySize)1 Inline (com.vaadin.flow.component.page.Inline)1 Meta (com.vaadin.flow.component.page.Meta)1 Push (com.vaadin.flow.component.page.Push)1 Viewport (com.vaadin.flow.component.page.Viewport)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 PageTitle (com.vaadin.flow.router.PageTitle)1 ERROR_HEADER_NO_APP_CONFIGURATOR (com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_NO_APP_CONFIGURATOR)1 ERROR_HEADER_NO_SHELL (com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_NO_SHELL)1 ERROR_HEADER_OFFENDING_CONFIGURATOR (com.vaadin.flow.server.AppShellRegistry.ERROR_HEADER_OFFENDING_CONFIGURATOR)1