Package com.microstrategy.utils
Class FileUtil
- java.lang.Object
 - 
- com.microstrategy.utils.FileUtil
 
 
- 
public class FileUtil extends java.lang.Object- Since:
 - MicroStrategy Web 7.3.1 or earlier
 
 
- 
- 
Field Summary
Fields Modifier and Type Field Description static intMAX_ATTRIBUTES_PER_XML_ROOT_NODEstatic intMAX_CHILD_XML_NODES 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAllowDirectoryPath(java.io.File directory)Allow specified directory, its files and subdirectories to be accessed.voidaddAllowFilePath(java.io.File file)Allow specified file/directory to be accessed.voidaddAllowPath(java.lang.String globPattern)Add the allowed path (using glob syntax) to be accessed.static voidclear(java.lang.String fileName)Clears the contents of the file.static java.nio.file.PathconvertFilePath(java.lang.String path)Returns aPathobject constructed from specified abstract path.static voidcopy(java.lang.String srcFileName, java.lang.String desFileName)Copies file srcFileName to another file desFileName.static booleandeleteDir(java.io.File dir)Deletes a directory (folder).static booleanfileExists(java.lang.String filePath)Returns whether the file or directory specified byfilePathexists .static java.lang.StringgetAbsolutePath(java.lang.String filePath)Returns the absolute path.ContainerServicesContextgetContainerServices()ReturnsContainerServicesContextobject associated with the FileUtil instance.static java.lang.StringgetFileExt(java.io.File f)Returns file extension.static java.lang.StringgetFileName(java.lang.String filePath)Returns the name of the file or directory denoted by this file path.static FileUtilgetInstance()Returns a singleton FileUtil instance, this method will be equivalent to a call togetInstance(ContainerServicesContext)with null ContainerServicesContext.static FileUtilgetInstance(ContainerServicesContext cs)Returns a singleton FileUtil instance.static java.nio.file.PathgetPath(java.lang.String filePath)Returns a new Path if the specifiedfilePathis valid to be accessed.static voidinitEnableFilePathValidation()Initialize flag for whether or not enable file path validation based on preferencestatic java.io.FilenewFile(java.io.File parent, java.lang.String child)Creates a new File instance if the path constructed from a parent abstract pathname and a child pathname string is valid to be accessed.static java.io.FilenewFile(java.lang.String filePath)Creates a new File instance if the specifiedfilePathis valid to be accessed.static java.io.FilenewFile(java.lang.String parent, java.lang.String child)Creates a new File instance if the path constructed from a parent pathname string and a child pathname string is valid to be accessed.static java.io.FileInputStreamnewFileInputStream(java.lang.String filePath)Creates a new FileInputStream if the specifiedfilePathis valid to be accessed.static java.io.FileOutputStreamnewFileOutputStream(java.lang.String filePath)Creates a new FileOutputStream if the specifiedfilePathis valid to be accessed.static java.io.FileOutputStreamnewFileOutputStream(java.lang.String filePath, boolean append)Creates a new FileOutputStream if the specifiedfilePathis valid to be accessed.static java.io.FileReadernewFileReader(java.lang.String filePath)Creates a new FileReader if the specifiedfilePathis valid to be accessed.static java.io.FileWriternewFileWriter(java.lang.String filePath, boolean append)Creates a new FileWriter if the specifiedfilePathis valid to be accessed.static booleanrename(java.lang.String srcFileName, java.lang.String desFileName)Renames file srcFileName to desFileName.voidsetContainerServices(ContainerServicesContext cs)SetsContainerServicesContextobject associated with the FileUtil instance.static voidsetEnableFilePathValidation(boolean enableFilePathValidation)Sets flag enableFilePathValidation 
 - 
 
- 
- 
Field Detail
- 
MAX_ATTRIBUTES_PER_XML_ROOT_NODE
public static final int MAX_ATTRIBUTES_PER_XML_ROOT_NODE
- See Also:
 - Constant Field Values
 
 
- 
MAX_CHILD_XML_NODES
public static final int MAX_CHILD_XML_NODES
- See Also:
 - Constant Field Values
 
 
 - 
 
- 
Method Detail
- 
getInstance
public static FileUtil getInstance(ContainerServicesContext cs)
Returns a singleton FileUtil instance. 
- 
getInstance
public static FileUtil getInstance()
Returns a singleton FileUtil instance, this method will be equivalent to a call togetInstance(ContainerServicesContext)with null ContainerServicesContext. 
- 
getContainerServices
public ContainerServicesContext getContainerServices()
ReturnsContainerServicesContextobject associated with the FileUtil instance. 
- 
setContainerServices
public void setContainerServices(ContainerServicesContext cs)
SetsContainerServicesContextobject associated with the FileUtil instance. 
- 
addAllowDirectoryPath
public void addAllowDirectoryPath(java.io.File directory)
Allow specified directory, its files and subdirectories to be accessed.For allowing the directory itself only to be accessed, using
addAllowFilePath(File)IMPORTANT! To ensure security, do not add paths derived from user requests.
- Parameters:
 directory- Directory to be allowed to access its files and subdirectories.
 
- 
addAllowFilePath
public void addAllowFilePath(java.io.File file)
Allow specified file/directory to be accessed.IMPORTANT! To ensure security, do not add paths derived from user requests.
- Parameters:
 file- File or directory to be allowed to access.
 
- 
addAllowPath
public void addAllowPath(java.lang.String globPattern)
Add the allowed path (using glob syntax) to be accessed. Refer toFileSystem.getPathMatcher(String)for the supported glob syntax.This method is provided for setting allowed path programmingly.
IMPORTANT! To ensure security, do not add paths derived from user requests.
- Parameters:
 globPattern- Path to be allowed to access.
 
- 
copy
public static void copy(java.lang.String srcFileName, java.lang.String desFileName) throws java.io.FileNotFoundException, java.io.IOException, java.lang.IllegalArgumentExceptionCopies file srcFileName to another file desFileName.- Parameters:
 srcFileName- name of source filedesFileName- name of destination file- Throws:
 java.io.FileNotFoundException- if srcFileName cannot be foundjava.io.IOException- if there is a problem during the copy operationjava.lang.IllegalArgumentException- if either parameters are null
 
- 
clear
public static void clear(java.lang.String fileName) throws java.io.FileNotFoundException, java.io.IOExceptionClears the contents of the file.- Parameters:
 fileName- name of file to be cleared- Throws:
 java.io.FileNotFoundException- if file is not foundjava.io.IOException- if there is a problem during the clear operation
 
- 
rename
public static boolean rename(java.lang.String srcFileName, java.lang.String desFileName)Renames file srcFileName to desFileName.- Parameters:
 srcFileName- name of source filedesFileName- new name- Returns:
 trueif renaming was possible- Since:
 - MicroStrategy Web 9.0.0
 - See Also:
 File.renameTo(java.io.File)
 
- 
deleteDir
public static boolean deleteDir(java.io.File dir)
Deletes a directory (folder). Since a directory can only be deleted if it's empty, it first deletes the contents of the directory recursively, then calls delete on the folder itself.- Parameters:
 dir- Directory to delete- Returns:
 - boolean 
trueonly if the folder and its content were deleted successfully. - Since:
 - MicroStrategy Web 9.0.0
 
 
- 
getFileExt
public static java.lang.String getFileExt(java.io.File f)
Returns file extension. Always in lower case. Returns null if not found. File with names that begin with "." are not considered an extension (null returned). PRE: assumes object is a file (not a directory). 
- 
getFileName
public static java.lang.String getFileName(java.lang.String filePath)
Returns the name of the file or directory denoted by this file path. 
- 
fileExists
public static boolean fileExists(java.lang.String filePath)
Returns whether the file or directory specified byfilePathexists .- Returns:
 - true if and only if the file or directory specified by 
filePathexists; false otherwise 
 
- 
setEnableFilePathValidation
public static void setEnableFilePathValidation(boolean enableFilePathValidation)
Sets flag enableFilePathValidation 
- 
initEnableFilePathValidation
public static void initEnableFilePathValidation()
Initialize flag for whether or not enable file path validation based on preference 
- 
newFile
public static java.io.File newFile(java.lang.String filePath) throws MSTRAccessDeniedExceptionCreates a new File instance if the specifiedfilePathis valid to be accessed.- Returns:
 - a File instance if the specified 
filePathis valid to be accessed, otherwise aMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedException
 
- 
newFile
public static java.io.File newFile(java.lang.String parent, java.lang.String child) throws MSTRAccessDeniedExceptionCreates a new File instance if the path constructed from a parent pathname string and a child pathname string is valid to be accessed.- Parameters:
 parent- The parent pathname stringchild- The child pathname string- Returns:
 - a File instance if the constructed file path from input is valid to be accessed, 
 otherwise a 
MSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedException
 
- 
newFile
public static java.io.File newFile(java.io.File parent, java.lang.String child) throws MSTRAccessDeniedExceptionCreates a new File instance if the path constructed from a parent abstract pathname and a child pathname string is valid to be accessed.- Parameters:
 parent- The parent abstract pathnamechild- The child pathname string- Returns:
 - a File instance if the constructed file path from input is valid to be accessed, 
 otherwise a 
IOExceptionis thrown. - Throws:
 MSTRAccessDeniedException
 
- 
newFileReader
public static java.io.FileReader newFileReader(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundExceptionCreates a new FileReader if the specifiedfilePathis valid to be accessed.- Returns:
 - a FileReader instance if the specified 
filePathis valid to be accessed, otherwise anMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedExceptionjava.io.FileNotFoundException
 
- 
newFileWriter
public static java.io.FileWriter newFileWriter(java.lang.String filePath, boolean append) throws MSTRAccessDeniedException, java.io.IOExceptionCreates a new FileWriter if the specifiedfilePathis valid to be accessed.- Parameters:
 filePath- the file pathappend- if true, bytes will be written to the end of the file rather than the beginning- Returns:
 - a FileWriter instance if the specified 
filePathis valid to be accessed, otherwise anIOExceptionis thrown. - Throws:
 MSTRAccessDeniedExceptionjava.io.IOException
 
- 
newFileInputStream
public static java.io.FileInputStream newFileInputStream(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundExceptionCreates a new FileInputStream if the specifiedfilePathis valid to be accessed.- Returns:
 - a FileInputStream instance if the specified 
filePathis valid to be accessed, otherwise anMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedExceptionjava.io.FileNotFoundException
 
- 
newFileOutputStream
public static java.io.FileOutputStream newFileOutputStream(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundExceptionCreates a new FileOutputStream if the specifiedfilePathis valid to be accessed.- Returns:
 - a FileOutputStream instance if the specified 
filePathis valid to be accessed, otherwise anMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedExceptionjava.io.FileNotFoundException
 
- 
newFileOutputStream
public static java.io.FileOutputStream newFileOutputStream(java.lang.String filePath, boolean append) throws MSTRAccessDeniedException, java.io.FileNotFoundExceptionCreates a new FileOutputStream if the specifiedfilePathis valid to be accessed.- Parameters:
 filePath- the file pathappend- if true, bytes will be written to the end of the file rather than the beginning- Returns:
 - a FileOutputStream instance if the specified 
filePathis valid to be accessed, otherwise anMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedExceptionjava.io.FileNotFoundException
 
- 
getPath
public static java.nio.file.Path getPath(java.lang.String filePath) throws MSTRAccessDeniedExceptionReturns a new Path if the specifiedfilePathis valid to be accessed.- Returns:
 - a Path instance if the specified 
filePathis valid to be accessed, otherwise anMSTRAccessDeniedExceptionis thrown. - Throws:
 MSTRAccessDeniedException
 
- 
getAbsolutePath
public static java.lang.String getAbsolutePath(java.lang.String filePath)
Returns the absolute path.- Returns:
 - The absolute path denoting the same file or directory specified by 
filePath 
 
- 
convertFilePath
public static java.nio.file.Path convertFilePath(java.lang.String path)
Returns aPathobject constructed from specified abstract path.- Parameters:
 path- the path- Returns:
 - a 
Pathconstructed from specified abstract path - Throws:
 java.nio.file.InvalidPathException- if the path string cannot be converted to aPath
 
 - 
 
 -