Search in sources :

Example 1 with PwaIcon

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

the class PwaHandler method init.

private void init(PwaRegistry pwaRegistry) {
    // Icon handling
    for (PwaIcon icon : pwaRegistry.getIcons()) {
        requestHandlerMap.put(icon.getRelHref(), (session, request, response) -> {
            response.setContentType(icon.getType());
            // caching
            if (icon.shouldBeCached()) {
                response.setHeader("Cache-Control", "no-cache, must-revalidate");
            }
            try (OutputStream out = response.getOutputStream()) {
                icon.write(out);
            }
            return true;
        });
    }
    // Assume that offline page and offline stub (for display within app)
    // are the same. This may change in the future.
    List<String> offlinePaths = new ArrayList<>();
    if (pwaRegistry.getPwaConfiguration().isOfflinePathEnabled()) {
        offlinePaths.add(pwaRegistry.getPwaConfiguration().relOfflinePath());
    }
    offlinePaths.add("/" + DEFAULT_OFFLINE_STUB_PATH);
    for (String offlinePath : offlinePaths) {
        requestHandlerMap.put(offlinePath, (session, request, response) -> {
            response.setContentType("text/html");
            try (PrintWriter writer = response.getWriter()) {
                writer.write(pwaRegistry.getOfflineHtml());
            }
            return true;
        });
    }
    // manifest.webmanifest handling
    requestHandlerMap.put(pwaRegistry.getPwaConfiguration().relManifestPath(), (session, request, response) -> {
        response.setContentType("application/manifest+json");
        try (PrintWriter writer = response.getWriter()) {
            writer.write(pwaRegistry.getManifestJson());
        }
        return true;
    });
    // sw-runtime.js handling (service worker import for precaching runtime
    // generated assets)
    requestHandlerMap.put(SW_RUNTIME_PRECACHE_PATH, (session, request, response) -> {
        response.setContentType("application/javascript");
        try (PrintWriter writer = response.getWriter()) {
            writer.write(pwaRegistry.getRuntimeServiceWorkerJs());
        }
        return true;
    });
}
Also used : OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) PwaIcon(com.vaadin.flow.server.PwaIcon) PrintWriter(java.io.PrintWriter)

Example 2 with PwaIcon

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noPWA.

@Test
public void writeBootstrapPage_noPWA() throws IOException, ServiceException {
    TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
    PwaRegistry registry = Mockito.mock(PwaRegistry.class);
    PwaConfiguration conf = Mockito.mock(PwaConfiguration.class);
    Mockito.when(registry.getPwaConfiguration()).thenReturn(conf);
    Mockito.when(conf.isEnabled()).thenReturn(true);
    Mockito.when(conf.getManifestPath()).thenReturn("bar");
    PwaIcon icon = Mockito.mock(PwaIcon.class);
    Mockito.when(icon.asElement()).thenReturn(new Element("h1"));
    Mockito.when(registry.getHeaderIcons()).thenReturn(Collections.singletonList(icon));
    VaadinServletService service = new MockVaadinServletService() {

        @Override
        protected PwaRegistry getPwaRegistry() {
            return registry;
        }
    };
    initLookup(service);
    VaadinSession session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(service.getDeploymentConfiguration());
    MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    config.setEnableDevServer(false);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(request.getServletPath()).thenReturn("/");
    VaadinResponse response = getMockResponse(null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.synchronizedHandleRequest(session, request, response);
    String result = stream.toString(StandardCharsets.UTF_8.name());
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("bar")));
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("h1")));
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("baz")));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Element(org.jsoup.nodes.Element) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwaIcon(com.vaadin.flow.server.PwaIcon) VaadinResponse(com.vaadin.flow.server.VaadinResponse) PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) PwaRegistry(com.vaadin.flow.server.PwaRegistry) Test(org.junit.Test)

Aggregations

PwaIcon (com.vaadin.flow.server.PwaIcon)2 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 PwaConfiguration (com.vaadin.flow.server.PwaConfiguration)1 PwaRegistry (com.vaadin.flow.server.PwaRegistry)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Element (org.jsoup.nodes.Element)1 Test (org.junit.Test)1