public class FilesPlugin extends PluginActivator implements FilesService, StaticResourceFilterListener, PathMapper
Modifier and Type | Field and Description |
---|---|
static DMXEvent |
CHECK_DISK_QUOTA |
private ConfigService |
configService |
static int |
DISK_QUOTA_MB |
static String |
FILE_REPOSITORY_PATH |
static boolean |
FILE_REPOSITORY_PER_WORKSPACE |
private static String |
FILE_REPOSITORY_URI |
private Logger |
logger |
private static Pattern |
PER_WORKSPACE_PATH_PATTERN |
private static String |
WORKSPACE_DIRECTORY_PREFIX |
bundle, dmx, mf
Constructor and Description |
---|
FilesPlugin() |
Modifier and Type | Method and Description |
---|---|
private String |
_pathPrefix(long workspaceId) |
private File |
absolutePath(String repoPath)
Maps a repository path to an absolute path.
|
private void |
checkAuthorization(String repoPath,
javax.servlet.http.HttpServletRequest request)
Checks if the user associated with a request is authorized to access a repository file.
|
private void |
checkExistence(File path) |
private File |
checkPath(File path)
Checks if the absolute path represents a directory traversal attack.
|
Topic |
createFile(InputStream in,
String repoPath)
Creates a file in the file repository and a corresponding File topic.
|
private Topic |
createFileOrFolderTopic(TopicModel model) |
private Topic |
createFileTopic(File path)
Creates a File topic representing the file at the given absolute path.
|
void |
createFolder(String folderName,
String repoPath)
Creates a folder in the file repository.
|
private void |
createFolderAssociation(long folderTopicId,
Topic topic) |
private Topic |
createFolderTopic(File path)
Creates a Folder topic representing the directory at the given absolute path.
|
private void |
createWorkspaceAssignment(DMXObject object,
String repoPath)
Creates a workspace assignment for a File topic, a Folder topic, or a folder association (type "Aggregation").
|
private void |
createWorkspaceFileRepository(File repo) |
private Topic |
fetchFileOrFolderTopic(String repoPath,
String topicTypeUri)
Fetches the File/Folder topic representing the file/directory at the given repository path.
|
private Topic |
fetchFileTopic(String repoPath)
Fetches the File topic representing the file at the given repository path.
|
private Topic |
fetchFolderTopic(String repoPath)
Fetches the Folder topic representing the folder at the given repository path.
|
private Topic |
fetchPathTopic(String repoPath) |
boolean |
fileExists(String repoPath)
Checks if a file/directory with the given repository path exists in the file repository.
|
Topic |
getChildFileTopic(long folderTopicId,
String repoPath)
Returns the File topic representing the file at a given repository path.
|
Topic |
getChildFolderTopic(long folderTopicId,
String repoPath)
Returns the Folder topic representing the folder at a given repository path.
|
DirectoryListing |
getDirectoryListing(String repoPath) |
File |
getFile(long fileTopicId)
Convenience method to access the file/directory in the file repository that is represented by the given
File/Folder topic.
|
File |
getFile(String repoPath)
Accesses a file/directory in the file repository by the given repository path.
|
Topic |
getFileTopic(String repoPath)
Returns the File topic representing the file at a given repository path.
|
Topic |
getFolderTopic(String repoPath)
Returns the Folder topic representing the folder at a given repository path.
|
String |
getRepositoryPath(URL url)
Checks if the given URL refers to the file repository of this DMX installation.
|
ResourceInfo |
getResourceInfo(String repoPath) |
private long |
getWorkspaceId() |
private long |
getWorkspaceId(String repoPath) |
void |
init() |
int |
openFile(long fileTopicId) |
private File |
path(File directory,
String fileName)
Constructs an absolute path from an absolute path and a file name.
|
String |
pathPrefix()
Returns a prefix that can be used for constructing a repository path.
|
String |
pathPrefix(long workspaceId)
Returns a prefix that can be used for constructing a repository path.
|
void |
preInstall() |
String |
repoPath(File path)
Maps an absolute path to a repository path.
|
private String |
repoPath(javax.servlet.http.HttpServletRequest request)
Returns the repository path of a filerepo request.
|
private String |
repoPath(long fileTopicId)
Returns the repository path of a File/Folder topic.
|
private String |
repoPath(Topic topic)
Returns the repository path of a File/Folder topic.
|
void |
shutdown() |
void |
staticResourceFilter(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
StoredFile |
storeFile(UploadedFile file,
String repoPath)
Receives an uploaded file, stores it in the file repository, and creates a corresponding File topic.
|
private File |
unusedPath(File directory,
UploadedFile file)
Constructs an absolute path for storing an uploaded file.
|
private String |
userInfo(String username) |
getBundleContext, getPluginName, getStaticResource, getUri, publishFileSystem, serviceArrived, serviceGone, setCoreService, start, stop, toString
public static final String FILE_REPOSITORY_PATH
public static final boolean FILE_REPOSITORY_PER_WORKSPACE
public static final int DISK_QUOTA_MB
private static final String FILE_REPOSITORY_URI
private static final String WORKSPACE_DIRECTORY_PREFIX
private static final Pattern PER_WORKSPACE_PATH_PATTERN
public static DMXEvent CHECK_DISK_QUOTA
private ConfigService configService
public FilesPlugin()
public Topic getFileTopic(String repoPath)
FilesService
getFileTopic
in interface FilesService
repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public Topic getFolderTopic(String repoPath)
FilesService
getFolderTopic
in interface FilesService
repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public Topic getChildFileTopic(long folderTopicId, String repoPath)
FilesService
Creates an association (type "Aggregation") between the File topic (role type "Child") and its parent Folder topic (role type "Parent"), if not exists already.
getChildFileTopic
in interface FilesService
folderTopicId
- ID of the parent Folder topic.repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public Topic getChildFolderTopic(long folderTopicId, String repoPath)
FilesService
Creates an association (type "Aggregation") between the Folder topic (role type "Child") and its parent Folder topic (role type "Parent"), if not exists already.
getChildFolderTopic
in interface FilesService
folderTopicId
- ID of the parent Folder topic.repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public StoredFile storeFile(UploadedFile file, String repoPath)
FilesService
storeFile
in interface FilesService
repoPath
- The directory where to store the uploaded file.
The directory must exist.
A repository path. Relative to the repository base path. Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public Topic createFile(InputStream in, String repoPath)
FilesService
createFile
in interface FilesService
in
- The input stream the file content is read from.repoPath
- The path and filename of the file to be created.
If that file exists already it is overwritten. ### TODO: rethink overwriting
A repository path. Relative to the repository base path. Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public void createFolder(String folderName, String repoPath)
FilesService
createFolder
in interface FilesService
repoPath
- The directory where to create the folder.
A repository path. Relative to the repository base path. Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public ResourceInfo getResourceInfo(String repoPath)
getResourceInfo
in interface FilesService
public DirectoryListing getDirectoryListing(String repoPath)
getDirectoryListing
in interface FilesService
public String getRepositoryPath(URL url)
FilesService
getRepositoryPath
in interface FilesService
null
if the URL
does not refer to the file repository of this DMX installation.public File getFile(String repoPath)
FilesService
Note: this method does not require the corresponding File/Folder topic to exist.
getFile
in interface FilesService
repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
public File getFile(long fileTopicId)
FilesService
getFile
in interface FilesService
fileTopicId
- ID of a File/Folder topic.public boolean fileExists(String repoPath)
FilesService
fileExists
in interface FilesService
repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.
If per-workspace file repos are active (dmx.filerepo.per_workspace=true
)
the repository path must contain the workspace prefix as the first path segment,
e.g. "/workspace-1234"
where 1234
is the workspace ID.
However there is one exception to that rule: if and only if "/"
is passed
as the repository path the workspace prefix is determined automatically with the
semantics of current workspace, based on the request's workspace cookie.
For support with constructing a repository path see the pathPrefix
methods.
true
if the file exists, false
otherwise.public String pathPrefix()
FilesService
dmx.filerepo.per_workspace=true
) the prefix
represents the current workspace (e.g. "/workspace-1234"
), based on the workspace cookie.
In case of per-workspace file repos are not active an empty string is returned.pathPrefix
in interface FilesService
public String pathPrefix(long workspaceId)
FilesService
dmx.filerepo.per_workspace=true
) the prefix
represents the given workspace (e.g. "/workspace-1234"
).
In case of per-workspace file repos are not active an empty string is returned.pathPrefix
in interface FilesService
public int openFile(long fileTopicId)
openFile
in interface FilesService
public void preInstall()
preInstall
in interface PluginContext
preInstall
in class PluginActivator
public void init()
init
in interface PluginContext
init
in class PluginActivator
public void shutdown()
shutdown
in interface PluginContext
shutdown
in class PluginActivator
public void staticResourceFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
staticResourceFilter
in interface StaticResourceFilterListener
public String repoPath(File path)
PathMapper
repoPath
in interface PathMapper
path
- A canonic absolute path.private Topic fetchFileTopic(String repoPath)
null
is returned.repoPath
- A repository path. Must be canonic.private Topic fetchFolderTopic(String repoPath)
null
is returned.repoPath
- A repository path. Must be canonic.private Topic fetchFileOrFolderTopic(String repoPath, String topicTypeUri)
null
is returned.repoPath
- A repository path. Must be canonic.topicTypeUri
- The type of the topic to fetch: either "dmx.files.file" or "dmx.files.folder".private Topic fetchPathTopic(String repoPath)
repoPath
- A repository path. Must be canonic.private Topic createFileTopic(File path) throws Exception
path
- A canonic absolute path.Exception
private Topic createFolderTopic(File path) throws Exception
path
- A canonic absolute path.Exception
private Topic createFileOrFolderTopic(TopicModel model) throws Exception
repoPath
- A repository path. Must be canonic.Exception
private void createFolderAssociation(long folderTopicId, Topic topic)
topic
- a File topic, or a Folder topic.private void createWorkspaceAssignment(DMXObject object, String repoPath)
object
- a File topic, a Folder topic, or a folder association (type "Aggregation").private File absolutePath(String repoPath) throws FileRepositoryException
Checks the repository path to fight directory traversal attacks.
repoPath
- A repository path. Relative to the repository base path.
Must begin with slash, no slash at the end.FileRepositoryException
private File checkPath(File path) throws FileRepositoryException, IOException
path
- The absolute path to check.FileRepositoryException
IOException
private void checkExistence(File path) throws FileRepositoryException
FileRepositoryException
private void checkAuthorization(String repoPath, javax.servlet.http.HttpServletRequest request) throws FileRepositoryException
repoPath
- The repository path of the file to check. Must be canonic.request
- The request.FileRepositoryException
private File path(File directory, String fileName)
directory
- An absolute path.private File unusedPath(File directory, UploadedFile file)
directory
- The directory to store the uploaded file to.
A canonic absolute path.private String repoPath(long fileTopicId)
private String repoPath(Topic topic)
private String repoPath(javax.servlet.http.HttpServletRequest request)
null
if the request is not a filerepo request.
Note: the returned path is not canonized.private void createWorkspaceFileRepository(File repo)
private long getWorkspaceId()
private long getWorkspaceId(String repoPath)
private String _pathPrefix(long workspaceId)
Copyright © 2018. All Rights Reserved.