Search in sources :

Example 1 with DevModeHandler

use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.

the class JavaScriptBootstrapHandler method getErrors.

private JsonValue getErrors(VaadinService service) {
    JsonObject errors = Json.createObject();
    Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
    if (devModeHandler.isPresent()) {
        String errorMsg = devModeHandler.get().getFailedOutput();
        if (errorMsg != null) {
            errors.put("webpack-dev-server", errorMsg);
        }
    }
    return errors.keys().length > 0 ? errors : Json.createNull();
}
Also used : DevModeHandler(com.vaadin.flow.internal.DevModeHandler) JsonObject(elemental.json.JsonObject)

Example 2 with DevModeHandler

use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.

the class AbstractDevServerRunnerTest method shouldPassEncodedUrlToDevServer.

@Test
public void shouldPassEncodedUrlToDevServer() throws Exception {
    handler = new DummyRunner();
    DevModeHandler devServer = Mockito.spy(handler);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.when(response.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class));
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getRequestURI()).thenReturn("/foo%20bar");
    Mockito.when(request.getPathInfo()).thenReturn("foo bar");
    Mockito.when(request.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
    AtomicReference<String> requestedPath = new AtomicReference<>();
    Mockito.when(devServer.prepareConnection(Mockito.any(), Mockito.any())).then(invocation -> {
        requestedPath.set((String) invocation.getArguments()[0]);
        return Mockito.mock(HttpURLConnection.class);
    });
    devServer.serveDevModeRequest(request, response);
    Assert.assertEquals("foo%20bar", requestedPath.get());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractDevModeTest(com.vaadin.base.devserver.startup.AbstractDevModeTest) Test(org.junit.Test)

Example 3 with DevModeHandler

use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.

the class IndexHtmlRequestHandlerTest method should_attachWebpackErrors.

@Test
public void should_attachWebpackErrors() throws Exception {
    deploymentConfiguration.setEnableDevServer(true);
    deploymentConfiguration.setProductionMode(false);
    DevModeHandler devServer = Mockito.mock(DevModeHandler.class);
    Mockito.when(devServer.getFailedOutput()).thenReturn("Failed to compile");
    Mockito.when(devServer.prepareConnection(Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(HttpURLConnection.class));
    service.setContext(context);
    DevModeHandlerManager devModeHandlerManager = new DevModeHandlerManager() {

        @Override
        public Class<?>[] getHandlesTypes() {
            return new Class[0];
        }

        @Override
        public void initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
        }

        @Override
        public void setDevModeHandler(DevModeHandler devModeHandler) {
        }

        @Override
        public DevModeHandler getDevModeHandler() {
            return devServer;
        }
    };
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Lookup lookup = Lookup.compose(Lookup.of(devModeHandlerManager, DevModeHandlerManager.class), Lookup.of(resourceProvider, ResourceProvider.class));
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    mockApplicationConfiguration(appConfig);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(new ByteArrayInputStream("<html><head></head></html>".getBytes()));
    // Send the request
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
    String indexHtml = responseOutput.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue("Should have a system error dialog", indexHtml.contains("<div class=\"v-system-error\">"));
    Assert.assertTrue("Should show webpack failure error", indexHtml.contains("Failed to compile"));
}
Also used : DevModeHandlerManager(com.vaadin.flow.internal.DevModeHandlerManager) VaadinContext(com.vaadin.flow.server.VaadinContext) HttpURLConnection(java.net.HttpURLConnection) Set(java.util.Set) ByteArrayInputStream(java.io.ByteArrayInputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 4 with DevModeHandler

use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.

the class FrontendUtils method getFileContent.

private static String getFileContent(VaadinService service, String path) throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    InputStream content = null;
    try {
        Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
        if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
            content = getFileFromDevModeHandler(devModeHandler.get(), path);
        }
        if (content == null) {
            content = getFileFromClassPath(service, path);
        }
        return content != null ? streamToString(content) : null;
    } finally {
        IOUtils.closeQuietly(content);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 5 with DevModeHandler

use of com.vaadin.flow.internal.DevModeHandler in project flow by vaadin.

the class FrontendUtils method getStatsAssetsByChunkName.

/**
 * Load the asset chunks from <code>stats.json</code>. We will only read the
 * file until we have reached the assetsByChunkName json and return that as
 * a json object string.
 *
 * Note: The <code>stats.json</code> is cached when external stats is
 * enabled or <code>stats.json</code> is provided from the class path. To
 * clear the cache use {@link #clearCachedStatsContent(VaadinService)}.
 *
 * @param service
 *            the Vaadin service.
 * @return json for assetsByChunkName object in stats.json or {@code null}
 *         if stats.json not found or content not found.
 * @throws IOException
 *             if an I/O error occurs while creating the input stream.
 */
public static String getStatsAssetsByChunkName(VaadinService service) throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    Optional<DevModeHandler> devModeHandler = DevModeHandlerManager.getDevModeHandler(service);
    if (!config.isProductionMode() && config.enableDevServer() && devModeHandler.isPresent()) {
        return streamToString(getResourceFromWebpack(devModeHandler.get(), "/assetsByChunkName", "getting assets by chunk name."));
    }
    InputStream resourceAsStream;
    if (config.isStatsExternal()) {
        resourceAsStream = getStatsFromExternalUrl(config.getExternalStatsUrl(), service.getContext());
    } else {
        resourceAsStream = getStatsFromClassPath(service);
    }
    if (resourceAsStream == null) {
        return null;
    }
    try (Scanner scan = new Scanner(resourceAsStream, StandardCharsets.UTF_8.name())) {
        StringBuilder assets = new StringBuilder();
        assets.append("{");
        // Scan until we reach the assetsByChunkName object line
        scanToAssetChunkStart(scan, assets);
        // Add lines until we reach the first } breaking the object
        while (scan.hasNextLine()) {
            String line = scan.nextLine().trim();
            if ("}".equals(line) || "},".equals(line)) {
                // Encountering } or }, means end of asset chunk
                return assets.append("}").toString();
            } else if (line.endsWith("}") || line.endsWith("},")) {
                return assets.append(line.substring(0, line.indexOf('}')).trim()).append("}").toString();
            } else if (line.contains("{")) {
                // should only contain key-value pairs.
                break;
            }
            assets.append(line);
        }
        getLogger().error("Could not parse assetsByChunkName from stats.json");
    }
    return null;
}
Also used : Scanner(java.util.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Aggregations

DevModeHandler (com.vaadin.flow.internal.DevModeHandler)8 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Test (org.junit.Test)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 VaadinContext (com.vaadin.flow.server.VaadinContext)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 AbstractDevModeTest (com.vaadin.base.devserver.startup.AbstractDevModeTest)1 Lookup (com.vaadin.flow.di.Lookup)1 ResourceProvider (com.vaadin.flow.di.ResourceProvider)1 DevModeHandlerManager (com.vaadin.flow.internal.DevModeHandlerManager)1 FaviconHandler (com.vaadin.flow.server.communication.FaviconHandler)1 IndexHtmlRequestHandler (com.vaadin.flow.server.communication.IndexHtmlRequestHandler)1 PushRequestHandler (com.vaadin.flow.server.communication.PushRequestHandler)1 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)1 JsonObject (elemental.json.JsonObject)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Scanner (java.util.Scanner)1 Set (java.util.Set)1