Copyright© 2008-2022 Sitevision AB, all rights reserved.
@Requireable(value="FileUtil") public interface FileUtil
sv:file
and sv:temporaryFile
.
Note! This interface is used to create files. You would typically use
StructureUtil
to move a file
and TrashcanUtil
to delete a file.
To check MIME/Content-Type for a file you would typically use MimeTypeUtil
.
An instance of the Sitevision class implementing this interface can be obtained via Utils.getFileUtil()
.
See Utils
for how to obtain an instance of the Utils
interface.
Modifier and Type | Method and Description |
---|---|
Node |
createFile(Node aParent,
String aFileName,
String aUri)
Creates a file using a uri string.
|
Node |
createFileFromBase64(Node aParent,
String aFileName,
String aBase64EncodedString)
Creates a file using a base64 string.
|
Node |
createFileFromTemporary(Node aParent,
Node aTemporaryFile)
Creates a file using a sv:temporaryFile.
|
void |
createFiles(Node aParent,
Map<String,String> aFileMappings)
Adds several files to a sv:localFileRepository, sv:fileRepository, sv:personalFileRepository or a sv:folder
residing as sub node to a file repository.
|
String |
getContentAsString(Node aFile)
Gets the contents of a file as a String using the UTF-8 character encoding.
|
String |
getContentAsString(Node aFile,
String aCharacterEncoding)
Gets the contents of a file as a String using a given character encoding.
|
void |
renameFile(Node aFile,
String aNewName)
Alters the name of a file.
|
void |
setDownloadProtected(Node aFile,
boolean aDownloadProtected)
Modifies download protection for a file.
|
void |
setVersioned(Node aFile,
boolean aVersioned)
Modifies versioning for a file.
|
void |
updateBinaryContent(Node aFile,
String aUri)
Updates the binary content of an existing file using a uri string.
|
void |
updateBinaryContentFromBase64(Node aFile,
String aBase64EncodedString)
Updates the binary content of an existing file using a base64 string.
|
void |
updateBinaryContentFromTemporary(Node aFile,
Node aTemporaryFile)
Updates the binary content of an existing file using a sv:temporaryFile.
|
void createFiles(Node aParent, Map<String,String> aFileMappings) throws ConstraintViolationException, RepositoryException
Adds several files to a sv:localFileRepository, sv:fileRepository, sv:personalFileRepository or a sv:folder residing as sub node to a file repository.
Files are specified by a map where the keys denotes the file names (will correspond to the property fileName of the sv:file nodes) and the values denotes the URIs pointing to the file resources. The file resources (i.e. the URIs) must be accessible from the Sitevision server. Sitevision opens a stream with the URI value when creating/uploading the binary content of the file. If a firewall or such blocks outgoing communication from the server - file creation will fail.
Permission note! Current user must be authorized to alter the parent node
(e.g. PermissionUtil.Permission.WRITE
).
A warning will be written to the system log if Sitevision for any reason is unable to create one of the files.
aParent
- the parent node for the filesaFileMappings
- a map of file name to URI pairsNullPointerException
- if aParent
or aFileMappings
is null
ConstraintViolationException
- if the user is not authorized to alter the parent node, if an invalid
parent node is specified or if file exceeds file size limitRepositoryException
- if something else goes wrongNode createFile(Node aParent, String aFileName, String aUri) throws ConstraintViolationException, RepositoryException
Note that the aParent
of the file must be a sv:localFileRepository
, sv:fileRepository
,
sv:personalFileRepository
or a sv:folder
residing as sub node to a file repository.
Permission note! Current user must be authorized to alter the parent node
(e.g. PermissionUtil.Permission.WRITE
).
URI note! The file resource must be accessible from the Sitevision server! Sitevision opens
a stream to an URL with the aUri
value when creating/uploading the binary content of the file.
If a firewall or such blocks outgoing communication from the server - file creation will fail.
aParent
- the parent node of the fileaFileName
- the filenameaUri
- the URI pointing to the file resource retrievable from the Sitevision servernull
NullPointerException
- if aParent
, aFileName
or aUri
is null
ConstraintViolationException
- if the user is not authorized to alter the parent node, if an invalid
parent node is specified or if file exceeds file size limitRepositoryException
- if something else goes wrongNode createFileFromBase64(Node aParent, String aFileName, String aBase64EncodedString) throws ConstraintViolationException, RepositoryException
The base64 decoder uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for decoding operation. The decoder rejects data that contains characters outside the base64 alphabet.
Note that the aParent
of the file must be a sv:localFileRepository
, sv:fileRepository
,
sv:personalFileRepository
or a sv:folder
residing as sub node to a file repository.
Permission note! Current user must be authorized to alter the parent node
(e.g. PermissionUtil.Permission.WRITE
).
aParent
- the parent node of the fileaFileName
- the filenameaBase64EncodedString
- a base64 encoded stringnull
NullPointerException
- if aParent
, aFileName
or aBase64EncodedString
is null
ConstraintViolationException
- if the user is not authorized to alter the parent node, if an invalid parent node is specified, if
aBase64EncodedString
is not base64 encoded or if file exceeds file size limitRepositoryException
- if something else goes wrongNode createFileFromTemporary(Node aParent, Node aTemporaryFile) throws ConstraintViolationException, RepositoryException
Note that the aParent
of the file must be a sv:localFileRepository
, sv:fileRepository
,
sv:personalFileRepository
or a sv:folder
residing as sub node to a file repository.
Permission note! Current user must be authorized to alter the parent node
(e.g. PermissionUtil.Permission.WRITE
).
aParent
- the parent node of the fileaTemporaryFile
- a sv:temporaryFile (typically created via TemporaryFileNodeBuilder
)null
ConstraintViolationException
- if the user is not authorized to alter the parent node, if an invalid
parent node is specified, if an invalid temporary node is specified or if file exceeds file size limitRepositoryException
- if something else goes wrongvoid updateBinaryContent(Node aFile, String aUri) throws ConstraintViolationException, RepositoryException
Properties related to the binary content will also be updated (e.g. length
). A new version will
be created if the binary content of a group file is updated.
URI note! The URI must be accessible from the Sitevision server and the ending part
of the URI must match the file extension of the existing file that should be updated! Sitevision opens
a stream to an URL with the anUri
value when uploading the binary content of the file.
If a firewall or such blocks outgoing communication from the server - file update will fail.
Permission note! Current user must be authorized to alter the file node that
should be updated (e.g. PermissionUtil.Permission.WRITE
).
aFile
- the fileaUri
- the URI pointing to the file resource to fetch new binary content fromConstraintViolationException
- if the user is not authorized to alter the file node,
if an invalid node is specified, if the end part of aUri
doesn't match the file extension
of aFile
or if file exceeds file size limitRepositoryException
- if something else goes wrongvoid updateBinaryContentFromBase64(Node aFile, String aBase64EncodedString) throws ConstraintViolationException, RepositoryException
The base64 decoder uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for decoding operation. The decoder rejects data that contains characters outside the base64 alphabet.
Properties related to the binary content will also be updated (e.g. length
). A new version will
be created if the binary content of a group file is updated.
Permission note! Current user must be authorized to alter the file node that
should be updated (e.g. PermissionUtil.Permission.WRITE
).
aFile
- the fileaBase64EncodedString
- a base64 encoded stringConstraintViolationException
- if the user is not authorized to alter the file node, if an invalid node is specified, if
aBase64EncodedString
is not base64 encoded, if file exceeds file size limit
or if file exceeds file size limitRepositoryException
- if something else goes wrongvoid updateBinaryContentFromTemporary(Node aFile, Node aTemporaryFile) throws ConstraintViolationException, RepositoryException
Properties related to the binary content will also be updated (e.g. length
). A new version will
be created if the binary content of a group file is updated.
Permission note! Current user must be authorized to alter the file node that
should be updated (e.g. PermissionUtil.Permission.WRITE
).
aFile
- the fileaTemporaryFile
- the sv:temporaryFile (typically created via TemporaryFileNodeBuilder
)ConstraintViolationException
- if the user is not authorized to alter the file node,
if an invalid node is specified, if the end part of the file represented by aTemporaryFile
doesn't match the file extension
of aFile
or if file exceeds file size limitRepositoryException
- if something else goes wrongString getContentAsString(Node aFile)
Note! This is mere a convenience method for getContentAsString(Node, String)
using UTF-8 as character encoding.
aFile
- the file Node (sv:file or sv:temporaryFile)getContentAsString(Node, String)
String getContentAsString(Node aFile, String aCharacterEncoding)
Note! This method operates on character-based file nodes only! That is:
MimeTypeUtil.isTextType(String)
).
MimeTypeUtil.isXmlType(String)
).
aFile
- the file Node (sv:file or sv:temporaryFile)aCharacterEncoding
- the character encoding (Note! UTF-8 will be used instead for null, empty and illegal/unsupported encoding)void renameFile(Node aFile, String aNewName) throws ConstraintViolationException, RepositoryException
If no file is specified a NullPointerException
is thrown.
If the node is not a sv:file an IllegalArgumentException
is thrown.
If no name is provided a NullPointerException
is thrown.
Permission note! Current user (the invoker of this method) must have WRITE permission
on the file node that should be updated (see PermissionUtil.Permission.WRITE
).
Updating a file for a sv:userIdentity
requires
PermissionUtil.Permission.MANAGE_USER_IDENTITIES
on current page.
aFile
- the sv:file that should be renamed. May not be null
aNewName
- the new name of the file. May not be null
ConstraintViolationException
- if the current user is not authorized to alter the name of the file or if the file extension doesn't matchRepositoryException
- if something else goes wrongvoid setVersioned(Node aFile, boolean aVersioned) throws ConstraintViolationException, RepositoryException
Permission note! Current user (the invoker of this method) must have WRITE permission
on the file node that should be updated (see PermissionUtil.Permission.WRITE
).
aFile
- the sv:file. May not be nullaVersioned
- the new state of versioning for this fileConstraintViolationException
- if the current user is not authorized to mutate aFile or if aFile is not a sv:fileRepositoryException
- if something else goes wrongvoid setDownloadProtected(Node aFile, boolean aDownloadProtected) throws RepositoryException
Permission note! Current user (the invoker of this method) must have WRITE permission and MANAGE_DOWNLOAD_PROTECTION
permission on the file node that should be updated (see PermissionUtil.Permission.WRITE
and PermissionUtil.Permission.MANAGE_DOWNLOAD_PROTECTION
).
Tip! Use PropertyUtil
or Properties
to check current state, i.e. the "downloadProtected" boolean property.
aFile
- the sv:file. May not be nullaDownloadProtected
- the new state of download protection for aFileConstraintViolationException
- if the current user is not authorized to mutate aFile or if aFile is not a sv:fileRepositoryException
- if something else goes wrongSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.