Package com.iizix.server.wsfile
Class WSFileProvider
- java.lang.Object
- com.iizix.server.wsfile.WSFileProvider
- All Implemented Interfaces:
IWSFileProvider
public class WSFileProvider extends java.lang.Object implements IWSFileProvider
The class is used to provide files to the web server for various contexts:- Server session,
- Per application (and version),
- Per user (all sessions),
- Per application session (on user basis),
- Per client session.
The server places these files in subdirectories in the file provider directory configured for the server (default "wsfiles" in
ServerSettings.DEFAULT_FILE_PROVIDER_ROOT
).- Author:
- Christopher Mindus
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description IWSFileDownload
createDownloadInstance(java.net.URL url, java.lang.Object id, java.lang.String description)
Constructs an instance of a file to download.boolean
deleteAllFiles(java.util.Map<java.io.File,java.io.IOException> errorsMap)
Deletes all the files.boolean
deleteAllFiles(java.util.Map<java.io.File,java.io.IOException> errorsMap, boolean logErrors)
Deletes all the files.void
dispose(boolean doDeleteAllFiles, boolean throwError)
Disposes of the file provider and optionally deletes all files from its root.void
disposeNoThrow(boolean doDeleteAllFiles)
Disposes of the file provider and optionally deletes all files from its root.void
downloadFiles(IAppSessionGyro appGyro, IWSFileDownload[] files, java.lang.Runnable completed)
Downloads the collection of files.IWSFile
getFileFromFileName(java.lang.String fileName)
Gets a WSFile from a file name that is located inside the root of the provider.java.io.File
getRoot()
Gets the root directory.URNResourceType
getType()
Returns the file type.java.lang.String
getURLPath()
Gets the URL path to the provider in the web server.boolean
isDisposed()
Checks if the provider is disposed of.IWSFile
reserveFile(java.io.File file, java.lang.String descr, java.lang.Object id)
Reserves an existing file on disk, i.e.java.lang.String
toString()
Gets the debug information string.
Method Detail
getType
public URNResourceType getType()
Returns the file type.- Specified by:
getType
in interfaceIWSFileProvider
getRoot
public java.io.File getRoot() throws java.io.IOException
Gets the root directory.- Specified by:
getRoot
in interfaceIWSFileProvider
- Returns:
- The canonical root directory.
- Throws:
java.io.IOException
- If the root doesn't exist anymore or the provider is disposed of, or if the root doesn't exist and the directory cannot be created.
isDisposed
public boolean isDisposed()
Checks if the provider is disposed of.- Specified by:
isDisposed
in interfaceIWSFileProvider
- Returns:
- true if disposed, false otherwise.
deleteAllFiles
public boolean deleteAllFiles(java.util.Map<java.io.File,java.io.IOException> errorsMap) throws java.io.IOException
Deletes all the files.- Parameters:
errorsMap
- Error map that is filled in for unsuccessful operations, null for none.- Returns:
- The success flag.
- Throws:
java.io.IOException
- If theerrorMap
isnull
and an exception deleting a file occurs.
deleteAllFiles
public boolean deleteAllFiles(java.util.Map<java.io.File,java.io.IOException> errorsMap, boolean logErrors) throws java.io.IOException
Deletes all the files.- Parameters:
errorsMap
- Error map that is filled in for unsuccessful operations, null for none.logErrors
- Flag to log all errors.- Returns:
- The success flag.
- Throws:
java.io.IOException
- If theerrorMap
isnull
and an exception deleting a file occurs.
disposeNoThrow
public void disposeNoThrow(boolean doDeleteAllFiles)
Disposes of the file provider and optionally deletes all files from its root. If already disposed of, nothing will happen.- Parameters:
doDeleteAllFiles
- Flag to delete all files.
dispose
public void dispose(boolean doDeleteAllFiles, boolean throwError) throws java.io.IOException
Disposes of the file provider and optionally deletes all files from its root. If already disposed of, nothing will happen.- Parameters:
doDeleteAllFiles
- Flag to delete all files.throwError
- Flag to throw an error for failures, otherwise it is logged in the server.- Throws:
java.io.IOException
- Upon delete failures andthrowError
istrue
.
createDownloadInstance
public IWSFileDownload createDownloadInstance(java.net.URL url, java.lang.Object id, java.lang.String description)
Constructs an instance of a file to download.- Specified by:
createDownloadInstance
in interfaceIWSFileProvider
- Parameters:
url
- The URL of the file to download.id
- The ID used for database identification, Long or String,null
when database is not used.This value depends on the URNResourceType. The types are:
Long
:URNResourceType.COMMON_DATABASE_PERSISTENT
: Common database persistent files.String
:URNResourceType.APPLICATION_DATABASE_PERSISTENT
: Application ID database persistent files.Long
:URNResourceType.GROUP_DATABASE_FILES
: Group ID database persistent files.Long
:URNResourceType.USER_DATABASE_FILES
: User ID database persistent files.
description
- Description of file, used to store in e.g. database, max 128 characters long.- Throws:
java.lang.IllegalArgumentException
- If thedescription
is longer than 128 characters, or if theid
is of wrong class (not String or Long) depending on the{@link URNResourceType}
.java.lang.NullPointerException
- Ifurl
ordescription
isnull
, or that theid
is required to be specified for the{@link URNResourceType}
.
downloadFiles
public void downloadFiles(IAppSessionGyro appGyro, IWSFileDownload[] files, java.lang.Runnable completed)
Downloads the collection of files. Once all downloads a completed, the callback is invoked. If the application session is disposed of, the callback is not called.- Specified by:
downloadFiles
in interfaceIWSFileProvider
- Parameters:
appGyro
- The application session gyro, or null for system.files
- The array of files to download. The results are placed in the respective instances.completed
- Callback invoked upon completion of the downloads (with error or not).
reserveFile
public IWSFile reserveFile(java.io.File file, java.lang.String descr, java.lang.Object id) throws NotFoundException, java.lang.IllegalArgumentException, java.io.IOException
Reserves an existing file on disk, i.e. inside the root. If the file already is reserved, the previous WSFile instance is returned and no new reservation is done.- Specified by:
reserveFile
in interfaceIWSFileProvider
- Parameters:
file
- The file.descr
- The description.id
- When a backing database is used, specify the user or group identifier as aLong
, the Application ID as aString
, ornull
when there is no special identifier, i.e. for global files.- Returns:
- A new WSFile instance with a reserved name.
- Throws:
NotFoundException
- If the file doesn't exist.java.lang.IllegalArgumentException
- If the file parent is not this providers root directory, or if the ID does not match the database ID type (Long
orString
).java.io.IOException
- For read errors of the file, or if the filename is not "compatible" with the database table.
getURLPath
public java.lang.String getURLPath()
Gets the URL path to the provider in the web server.- Specified by:
getURLPath
in interfaceIWSFileProvider
- Returns:
- The path on the web server, starting and ending with '/'.
getFileFromFileName
public IWSFile getFileFromFileName(java.lang.String fileName)
Gets a WSFile from a file name that is located inside the root of the provider.- Specified by:
getFileFromFileName
in interfaceIWSFileProvider
- Parameters:
fileName
- The file name to look up.- Returns:
- The WSFile instance, or null if not found.
toString
public java.lang.String toString()
Gets the debug information string.- Overrides:
toString
in classjava.lang.Object
- Returns:
- Used for logging, etc.