Class DefaultFileHandler

  • All Implemented Interfaces:
    FileHandler, Loggable

    public class DefaultFileHandler
    extends LoggedObject
    implements FileHandler
    File operations that are performed in Cargo. All file operations must use this class.
    • Field Detail

      • FILE_BUFFER_SIZE

        public static final int FILE_BUFFER_SIZE
        Default file buffer size.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DefaultFileHandler

        public DefaultFileHandler()
    • Method Detail

      • sanitizeFilename

        public static String sanitizeFilename​(String filename,
                                              Logger logger)
        Sanitize a given name to turn it into a safe file name, removing for example leading or trailing slashes, as well as intermediate parent path jumps.
        Parameters:
        filename - name to sanitize
        logger - Logger to log when sanitization happens (optional)
        Returns:
        sanitized name
      • copyFile

        public void copyFile​(String source,
                             String target)
        Copy a file from a source to a destination. If destination file already exists, it is not overwritten.
        Specified by:
        copyFile in interface FileHandler
        Parameters:
        source - the file to copy from. Must not be null.
        target - the file to copy to. Must not be null.
      • copyFile

        public void copyFile​(String source,
                             String target,
                             boolean overwrite)
        Copy a file from a source to a destination specifying if source files may overwrite newer destination files.
        Specified by:
        copyFile in interface FileHandler
        Parameters:
        source - the file to copy from. Must not be null.
        target - the file to copy to. Must not be null.
        overwrite - to overwrite the file if it already exists.
      • copyFile

        public void copyFile​(String source,
                             String target,
                             Map<String,​String> replacements,
                             Charset encoding)
        Copy a file from a source to a destination using token replacement.
        Specified by:
        copyFile in interface FileHandler
        Parameters:
        source - the file to copy from. Must not be null
        target - the file to copy to. Must not be null
        replacements - the token replacements to use. Must not be null
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
      • copyDirectory

        public void copyDirectory​(String source,
                                  String target)
        Copy a directory from a source to a destination.
        Specified by:
        copyDirectory in interface FileHandler
        Parameters:
        source - the directory to copy from. Must not be null.
        target - the directory to copy to. Must not be null.
      • copyDirectory

        public void copyDirectory​(String source,
                                  String target,
                                  List<String> excludes)
        Copy a directory from a source to a destination specifying files to be excluded.
        Specified by:
        copyDirectory in interface FileHandler
        Parameters:
        source - the directory to copy from. Must not be null.
        target - the directory to copy to. Must not be null.
        excludes - the list of files to be excluded, can be null. Each entry in the list is an Ant file pattern.
      • copyDirectory

        public void copyDirectory​(String source,
                                  String target,
                                  Map<String,​String> replacements,
                                  Charset encoding)
        Copy a directory from a source to a destination using a token replacement.
        Specified by:
        copyDirectory in interface FileHandler
        Parameters:
        source - the directory to copy from. Must not be null
        target - the directory to copy to. Must not be null
        replacements - the token replacements to use. Must not be null
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
      • explode

        public void explode​(String war,
                            String exploded)
        Extracts a war file into a directory.
        Specified by:
        explode in interface FileHandler
        Parameters:
        war - the War archive to be extracted.
        exploded - the directory that receives files.
      • createDirectory

        public String createDirectory​(String parentDir,
                                      String name)
        Convenience method for creating a new directory inside another one. If the directory already exists, it will return the already created directory.
        Specified by:
        createDirectory in interface FileHandler
        Parameters:
        parentDir - The directory in which the new directory should be created
        name - The name of the directory to create
        Returns:
        The new directory
      • copy

        public void copy​(InputStream in,
                         OutputStream out,
                         int bufSize)
        Copies data from an InputStream to an OutputStream.
        Specified by:
        copy in interface FileHandler
        Parameters:
        in - InputStream to copy data from
        out - OutputStream to copy data to
        bufSize - size of the copy buffer
      • copy

        public void copy​(InputStream in,
                         OutputStream out)
        Copies data from an InputStream to an OutputStream using a default buffer size.. The default buffer size is FILE_BUFFER_SIZE.
        Specified by:
        copy in interface FileHandler
        Parameters:
        in - InputStream to copy data from
        out - OutputStream to copy data to
      • replaceInFile

        public void replaceInFile​(String file,
                                  Map<String,​String> replacements,
                                  Charset encoding)
                           throws CargoException
        Replaces using a map of replacements in a given file.
        Specified by:
        replaceInFile in interface FileHandler
        Parameters:
        file - File to replace in.
        replacements - Map containing replacements.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • replaceInFile

        public void replaceInFile​(String file,
                                  Map<String,​String> replacements,
                                  Charset encoding,
                                  boolean ignoreNonExistingProperties)
                           throws CargoException
        Replaces using a map of replacements in a given file.
        Specified by:
        replaceInFile in interface FileHandler
        Parameters:
        file - File to replace in.
        replacements - Map containing replacements.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        ignoreNonExistingProperties - Whether to ignore if one of the keys cannot be found.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • replaceInXmlFile

        public void replaceInXmlFile​(XmlReplacement... xmlReplacements)
                              throws CargoException
        Replaces using a map of XML replacements in a given file.
        Specified by:
        replaceInXmlFile in interface FileHandler
        Parameters:
        xmlReplacements - Array of XML replacements.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • getTmpPath

        public String getTmpPath​(String name)
        Compute the location of a temporary directory.
        Specified by:
        getTmpPath in interface FileHandler
        Parameters:
        name - The name of the directory to compute inside the tmp directory
        Returns:
        the location of the computed temporary directory
      • createUniqueTmpDirectory

        public String createUniqueTmpDirectory()
        Creates a unique temporary directory.
        Specified by:
        createUniqueTmpDirectory in interface FileHandler
        Returns:
        the newly created temporary directory
      • delete

        public void delete​(String path)
        Deletes a file or directory, removing any children as appropriate.
        Specified by:
        delete in interface FileHandler
        Parameters:
        path - the path to the file or directory to remove
      • getSize

        public long getSize​(String file)
        Specified by:
        getSize in interface FileHandler
        Parameters:
        file - the file for which to get the size
        Returns:
        long Size of the file, in bytes
      • getInputStream

        public InputStream getInputStream​(String file)
        Specified by:
        getInputStream in interface FileHandler
        Parameters:
        file - the file for which to get an InputStream for
        Returns:
        an InputStream pointing to the file
      • getOutputStream

        public OutputStream getOutputStream​(String file)
        Specified by:
        getOutputStream in interface FileHandler
        Parameters:
        file - the file for which to get an OutputStream for
        Returns:
        an OutputStream pointing to the file
      • append

        public String append​(String path,
                             String suffixToAppend)
        Append a suffix path to an existing path.
        Specified by:
        append in interface FileHandler
        Parameters:
        path - the path to append to
        suffixToAppend - the suffix to append to the path
        Returns:
        the new full path
      • mkdirs

        public void mkdirs​(String path)
        Create directories for the passed path.
        Specified by:
        mkdirs in interface FileHandler
        Parameters:
        path - the path for which to create all directories leading to it
      • getParent

        public String getParent​(String path)
        Specified by:
        getParent in interface FileHandler
        Parameters:
        path - the path for which to return its parent
        Returns:
        the parent path of the passed path
      • exists

        public boolean exists​(String path)
        Specified by:
        exists in interface FileHandler
        Parameters:
        path - the path to check the existence for
        Returns:
        true if the path exists or false otherwise
      • createFile

        public void createFile​(String file)
        Create a file.
        Specified by:
        createFile in interface FileHandler
        Parameters:
        file - the file to create
      • isDirectoryEmpty

        public boolean isDirectoryEmpty​(String dir)
        Specified by:
        isDirectoryEmpty in interface FileHandler
        Parameters:
        dir - the directory to check
        Returns:
        true if the passed directory is empty
      • getName

        public String getName​(String file)
        Specified by:
        getName in interface FileHandler
        Parameters:
        file - the file name for which to return the file name
        Returns:
        the file name (without path)
      • getURL

        public String getURL​(String path)
        Specified by:
        getURL in interface FileHandler
        Parameters:
        path - the path for which to return the URL
        Returns:
        the URL for the passed path
      • isDirectory

        public boolean isDirectory​(String path)
        Specified by:
        isDirectory in interface FileHandler
        Parameters:
        path - the path to verify
        Returns:
        true if the path passed is a directory or false otherwise
      • getChildren

        public String[] getChildren​(String directory)
        Specified by:
        getChildren in interface FileHandler
        Parameters:
        directory - the directory for which to return all children
        Returns:
        the children of the passed directory
      • getChildren

        public String[] getChildren​(String directory,
                                    List<String> filters)
        Specified by:
        getChildren in interface FileHandler
        Parameters:
        directory - the directory for which to return all children
        filters - any filters to apply when listing, can be null Each entry in the list is an Ant file pattern.
        Returns:
        the children of the passed directory
      • getAbsolutePath

        public String getAbsolutePath​(String path)
        gets the absolute path from a file that may be relative to the current directory.
        Specified by:
        getAbsolutePath in interface FileHandler
        Parameters:
        path - - what to extract the file path from
        Returns:
        - absolute path to the file
      • readTextFile

        public String readTextFile​(String file,
                                   Charset encoding)
        Specified by:
        readTextFile in interface FileHandler
        Parameters:
        file - the file for which to load into a String object.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        Returns:
        a String with the file's contents.
      • writeTextFile

        public void writeTextFile​(String file,
                                  String content,
                                  Charset encoding)
        Specified by:
        writeTextFile in interface FileHandler
        Parameters:
        file - the file in which to write the String object.
        content - String to write into the file.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.