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 int
MAX_ATTRIBUTES_PER_XML_ROOT_NODE
static int
MAX_CHILD_XML_NODES
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAllowDirectoryPath(java.io.File directory)
Allow specified directory, its files and subdirectories to be accessed.void
addAllowFilePath(java.io.File file)
Allow specified file/directory to be accessed.void
addAllowPath(java.lang.String globPattern)
Add the allowed path (using glob syntax) to be accessed.static void
clear(java.lang.String fileName)
Clears the contents of the file.static java.nio.file.Path
convertFilePath(java.lang.String path)
Returns aPath
object constructed from specified abstract path.static void
copy(java.lang.String srcFileName, java.lang.String desFileName)
Copies file srcFileName to another file desFileName.static boolean
deleteDir(java.io.File dir)
Deletes a directory (folder).static boolean
fileExists(java.lang.String filePath)
Returns whether the file or directory specified byfilePath
exists .static java.lang.String
getAbsolutePath(java.lang.String filePath)
Returns the absolute path.ContainerServicesContext
getContainerServices()
ReturnsContainerServicesContext
object associated with the FileUtil instance.static java.lang.String
getFileExt(java.io.File f)
Returns file extension.static java.lang.String
getFileName(java.lang.String filePath)
Returns the name of the file or directory denoted by this file path.static FileUtil
getInstance()
Returns a singleton FileUtil instance, this method will be equivalent to a call togetInstance(ContainerServicesContext)
with null ContainerServicesContext.static FileUtil
getInstance(ContainerServicesContext cs)
Returns a singleton FileUtil instance.static java.nio.file.Path
getPath(java.lang.String filePath)
Returns a new Path if the specifiedfilePath
is valid to be accessed.static void
initEnableFilePathValidation()
Initialize flag for whether or not enable file path validation based on preferencestatic java.io.File
newFile(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.File
newFile(java.lang.String filePath)
Creates a new File instance if the specifiedfilePath
is valid to be accessed.static java.io.File
newFile(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.FileInputStream
newFileInputStream(java.lang.String filePath)
Creates a new FileInputStream if the specifiedfilePath
is valid to be accessed.static java.io.FileOutputStream
newFileOutputStream(java.lang.String filePath)
Creates a new FileOutputStream if the specifiedfilePath
is valid to be accessed.static java.io.FileOutputStream
newFileOutputStream(java.lang.String filePath, boolean append)
Creates a new FileOutputStream if the specifiedfilePath
is valid to be accessed.static java.io.FileReader
newFileReader(java.lang.String filePath)
Creates a new FileReader if the specifiedfilePath
is valid to be accessed.static java.io.FileWriter
newFileWriter(java.lang.String filePath, boolean append)
Creates a new FileWriter if the specifiedfilePath
is valid to be accessed.static boolean
rename(java.lang.String srcFileName, java.lang.String desFileName)
Renames file srcFileName to desFileName.void
setContainerServices(ContainerServicesContext cs)
SetsContainerServicesContext
object associated with the FileUtil instance.static void
setEnableFilePathValidation(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()
ReturnsContainerServicesContext
object associated with the FileUtil instance.
-
setContainerServices
public void setContainerServices(ContainerServicesContext cs)
SetsContainerServicesContext
object 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.IllegalArgumentException
Copies 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.IOException
Clears 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:
true
if 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
true
only 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 byfilePath
exists .- Returns:
- true if and only if the file or directory specified by
filePath
exists; 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 MSTRAccessDeniedException
Creates a new File instance if the specifiedfilePath
is valid to be accessed.- Returns:
- a File instance if the specified
filePath
is valid to be accessed, otherwise aMSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
-
newFile
public static java.io.File newFile(java.lang.String parent, java.lang.String child) throws MSTRAccessDeniedException
Creates 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
MSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
-
newFile
public static java.io.File newFile(java.io.File parent, java.lang.String child) throws MSTRAccessDeniedException
Creates 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
IOException
is thrown. - Throws:
MSTRAccessDeniedException
-
newFileReader
public static java.io.FileReader newFileReader(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundException
Creates a new FileReader if the specifiedfilePath
is valid to be accessed.- Returns:
- a FileReader instance if the specified
filePath
is valid to be accessed, otherwise anMSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
java.io.FileNotFoundException
-
newFileWriter
public static java.io.FileWriter newFileWriter(java.lang.String filePath, boolean append) throws MSTRAccessDeniedException, java.io.IOException
Creates a new FileWriter if the specifiedfilePath
is 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
filePath
is valid to be accessed, otherwise anIOException
is thrown. - Throws:
MSTRAccessDeniedException
java.io.IOException
-
newFileInputStream
public static java.io.FileInputStream newFileInputStream(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundException
Creates a new FileInputStream if the specifiedfilePath
is valid to be accessed.- Returns:
- a FileInputStream instance if the specified
filePath
is valid to be accessed, otherwise anMSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
java.io.FileNotFoundException
-
newFileOutputStream
public static java.io.FileOutputStream newFileOutputStream(java.lang.String filePath) throws MSTRAccessDeniedException, java.io.FileNotFoundException
Creates a new FileOutputStream if the specifiedfilePath
is valid to be accessed.- Returns:
- a FileOutputStream instance if the specified
filePath
is valid to be accessed, otherwise anMSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
java.io.FileNotFoundException
-
newFileOutputStream
public static java.io.FileOutputStream newFileOutputStream(java.lang.String filePath, boolean append) throws MSTRAccessDeniedException, java.io.FileNotFoundException
Creates a new FileOutputStream if the specifiedfilePath
is 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
filePath
is valid to be accessed, otherwise anMSTRAccessDeniedException
is thrown. - Throws:
MSTRAccessDeniedException
java.io.FileNotFoundException
-
getPath
public static java.nio.file.Path getPath(java.lang.String filePath) throws MSTRAccessDeniedException
Returns a new Path if the specifiedfilePath
is valid to be accessed.- Returns:
- a Path instance if the specified
filePath
is valid to be accessed, otherwise anMSTRAccessDeniedException
is 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 aPath
object constructed from specified abstract path.- Parameters:
path
- the path- Returns:
- a
Path
constructed from specified abstract path - Throws:
java.nio.file.InvalidPathException
- if the path string cannot be converted to aPath
-
-