com.vaadin.flow.server.frontend.

Class FrontendUtils


  • public class FrontendUtils
    extends Object

    A class for static methods and definitions that might be used in different locations.

    For internal use only. May be renamed or removed in a future release.

    Since:

    2.0

    • Method Detail

      • getOsName

        public static String getOsName()

        Get the Operating System name from the os.name system property.

        Returns:

        operating system name

      • isWindows

        public static boolean isWindows()

        Check if the current os is Windows.

        Returns:

        true if windows

      • streamToString

        public static String streamToString(InputStream inputStream)

        Read a stream and copy the content into a String using system line separators for all 'carriage return' characters.

        Parameters:

        inputStream - the input stream

        Returns:

        the string

      • createProcessBuilder

        public static ProcessBuilder createProcessBuilder(List<String> command)

        Creates a process builder for the given list of program and arguments. If the program is defined as an absolute path, then the directory that contains the program is also appended to PATH so that the it can locate related tools.

        Parameters:

        command - a list with the program and arguments

        Returns:

        a configured process builder

      • getStatsContent

        public static String getStatsContent(VaadinService service)
                                      throws IOException

        Gets the content of the stats.json file produced by webpack. Note: Caches the stats.json when external stats is enabled or stats.json is provided from the class path. To clear the cache use clearCachedStatsContent(VaadinService).

        Parameters:

        service - the vaadin service.

        Returns:

        the content of the file as a string, null if not found.

        Throws:

        IOException - on error reading stats file.

      • clearCachedStatsContent

        public static void clearCachedStatsContent(VaadinService service)

        Clears the stats.json cache within this VaadinContext.

        Parameters:

        service - the vaadin service.

      • getIndexHtmlContent

        public static String getIndexHtmlContent(VaadinService service)
                                          throws IOException

        Gets the content of the frontend/index.html file which is served by webpack-dev-server in dev-mode and read from classpath in production mode. NOTE: In dev mode, the file content file is fetched via webpack http request. So that we don't need to have a separate index.html's content watcher, auto-reloading will work automatically, like other files managed by webpack in `frontend/` folder.

        Parameters:

        service - the vaadin service

        Returns:

        the content of the index html file as a string, null if not found.

        Throws:

        IOException - on error when reading file

      • getStatsHash

        public static String getStatsHash(VaadinService service)
                                   throws IOException

        Get the latest hash for the stats file in development mode. This is requested from the webpack-dev-server.

        In production mode and disabled dev server mode an empty string is returned.

        Parameters:

        service - the Vaadin service.

        Returns:

        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.

      • getStatsAssetsByChunkName

        public static String getStatsAssetsByChunkName(VaadinService service)
                                                throws IOException

        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. Note: The stats.json is cached when external stats is enabled or stats.json is provided from the class path. To clear the cache use clearCachedStatsContent(VaadinService).

        Parameters:

        service - the Vaadin service.

        Returns:

        json for assetsByChunkName object in stats.json or null if stats.json not found or content not found.

        Throws:

        IOException - if an I/O error occurs while creating the input stream.

      • isWebpackConfigFile

        public static boolean isWebpackConfigFile(File file)
                                           throws IOException

        Checks whether the file is a webpack configuration file with the expected content (includes a configuration generated by Flow).

        Parameters:

        file - a file to check

        Returns:

        true iff the file exists and includes a generated configuration

        Throws:

        IOException - if an I/O error occurs while reading the file

      • getUnixRelativePath

        public static String getUnixRelativePath(Path source,
                                                 Path target)

        Get relative path from a source path to a target path in Unix form. All the Windows' path separator will be replaced.

        Parameters:

        source - the source path

        target - the target path

        Returns:

        unix relative path from source to target

      • getUnixPath

        public static String getUnixPath(Path source)

        Get path as a String in Unix form.

        Parameters:

        source - path to get

        Returns:

        path as a String in Unix form.

      • readFallbackChunk

        public static FallbackChunk readFallbackChunk(elemental.json.JsonObject object)

        Read fallback chunk data from a json object.

        Parameters:

        object - json object to read fallback chunk data

        Returns:

        a fallback chunk data

      • parseFrontendVersion

        public static FrontendVersion parseFrontendVersion(String versionString)
                                                    throws IOException

        Parse the version number of node/npm from version output string.

        Parameters:

        versionString - string containing version output, typically produced by tool --version

        Returns:

        FrontendVersion of versionString

        Throws:

        IOException - if parsing fails

      • getVaadinHomeDirectory

        public static File getVaadinHomeDirectory()

        Gets vaadin home directory (".vaadin" folder in the user home dir).

        The directory is created if it's doesn't exist.

        Returns:

        a vaadin home directory

      • commandToString

        public static String commandToString(String baseDir,
                                             List<String> command)

        Pretty prints a command line order. It split in lines adapting to 80 columns, and allowing copy and paste in console. It also removes the current directory to avoid security issues in log files.

        Parameters:

        baseDir - the current directory

        command - the command and it's arguments

        Returns:

        the string for printing in logs

      • parseManifestPaths

        public static List<String> parseManifestPaths(String manifestJson)

        Parse "manifest.json" file contents obtained from webpack and extract the list of request paths to handle as static resources.

        Parameters:

        manifestJson - "manifest.json" file contents

        Returns:

        list of paths, each starting with "/"

      • getPackageVersionFromJson

        public static FrontendVersion getPackageVersionFromJson(elemental.json.JsonObject sourceJson,
                                                                String pkg,
                                                                String versionOrigin)

        Tries to parse the given package's frontend version or if it doesn't exist, returns null. In case the value cannot be parsed, logs an error and returns null.

        Parameters:

        sourceJson - json object that has the package

        pkg - the package name

        versionOrigin - origin of the version (like a file), used in error message

        Returns:

        the frontend version the package or null

      • console

        public static void console(String format,
                                   Object message)

        Intentionally send to console instead to log, useful when executing external processes.

        Parameters:

        format - Format of the line to send to console, it must contain a `%s` outlet for the message

        message - the string to show