Java Code Examples for com.vaadin.flow.server.VaadinService#getDeploymentConfiguration()

The following examples show how to use com.vaadin.flow.server.VaadinService#getDeploymentConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrontendUtils.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the <code>stats.json</code> file produced by webpack.
 *
 * @param service
 *            the vaadin service.
 * @return the content of the file as a string, null if not found.
 * @throws IOException
 *             on error reading stats file.
 */
public static String getStatsContent(VaadinService service)
        throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    InputStream content = null;

    if (!config.isProductionMode() && config.enableDevServer()) {
        content = getStatsFromWebpack();
    }

    if (config.isStatsExternal()) {
        content = getStatsFromExternalUrl(config.getExternalStatsUrl(),
                service.getContext());
    }

    if (content == null) {
        content = getStatsFromClassPath(service);
    }
    return content != null
            ? IOUtils.toString(content, StandardCharsets.UTF_8)
            : null;
}
 
Example 2
Source File: FrontendUtils.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Get the latest has for the stats file in development mode. This is
 * requested from the webpack-dev-server.
 * <p>
 * In production mode and disabled dev server mode an empty string is
 * returned.
 *
 * @param service
 *            the Vaadin service.
 * @return hash string for the stats.json file, empty string if none found
 * @throws IOException
 *             if an I/O error occurs while creating the input stream.
 */
public static String getStatsHash(VaadinService service)
        throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    if (!config.isProductionMode() && config.enableDevServer()) {
        DevModeHandler handler = DevModeHandler.getDevModeHandler();
        HttpURLConnection statsConnection = handler
                .prepareConnection("/stats.hash", "GET");
        if (statsConnection
                .getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new WebpackConnectionException(String.format(
                    NO_CONNECTION, "getting the stats content hash."));
        }
        return streamToString(statsConnection.getInputStream())
                .replaceAll("\"", "");
    }

    return "";
}
 
Example 3
Source File: FrontendUtils.java    From flow with Apache License 2.0 5 votes vote down vote up
private static String getFileContent(VaadinService service,
        String pathInDevMode, String pathInProductionMode)
        throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    InputStream content = null;

    if (!config.isProductionMode() && config.enableDevServer()) {
        content = getFileFromWebpack(pathInDevMode);
    }

    if (content == null) {
        content = getFileFromClassPath(service, pathInProductionMode);
    }
    return content != null ? streamToString(content) : null;
}
 
Example 4
Source File: FrontendUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
/**
 * Load the asset chunks from stats.json. We will only read the file until
 * we have reached the assetsByChunkName json and return that as a json
 * object string.
 *
 * @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();
    if (!config.isProductionMode() && config.enableDevServer()) {
        DevModeHandler handler = DevModeHandler.getDevModeHandler();
        HttpURLConnection assetsConnection = handler
                .prepareConnection("/assetsByChunkName", "GET");
        if (assetsConnection
                .getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new WebpackConnectionException(String.format(
                    NO_CONNECTION, "getting assets by chunk name."));
        }
        return streamToString(assetsConnection.getInputStream());
    }
    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("{")) {
                // Encountering { means something is wrong as the assets
                // should only contain key-value pairs.
                break;
            }
            assets.append(line);
        }
        getLogger()
                .error("Could not parse assetsByChunkName from stats.json");
    }
    return null;
}
 
Example 5
Source File: NpmTemplateParser.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Check status to see if stats.json needs to be loaded and parsed.
 * <p>
 * Always load if jsonStats is null, never load again when we have a bundle
 * as it never changes, always load a new stats if the hash has changed and
 * we do not have a bundle.
 *
 * @param service
 *            the Vaadin service.
 * @return {@code true} if we need to re-load and parse stats.json, else
 *         {@code false}
 */
protected boolean isStatsFileReadNeeded(VaadinService service)
        throws IOException {
    DeploymentConfiguration config = service.getDeploymentConfiguration();
    if (jsonStats == null) {
        return true;
    } else if (usesBundleFile(config)) {
        return false;
    }
    return !jsonStats.get("hash").asString()
            .equals(FrontendUtils.getStatsHash(service));
}