use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method createFile.
LocalVirtualFile createFile(LocalVirtualFile parent, String name, InputStream content) throws ForbiddenException, ConflictException, ServerException {
checkName(name);
if (Path.of(name).length() > 1) {
throw new ServerException(String.format("Invalid name '%s'", name));
}
if (parent.isFolder()) {
final Path newPath = parent.getPath().newPath(name);
final File newIoFile = new File(ioRoot, toIoPath(newPath));
try {
if (!newIoFile.createNewFile()) {
throw new ConflictException(String.format("Item '%s' already exists", newPath));
}
} catch (IOException e) {
String errorMessage = String.format("Unable create new file '%s'", newPath);
LOG.error(errorMessage + "\n" + e.getMessage(), e);
throw new ServerException(errorMessage);
}
final LocalVirtualFile newVirtualFile = new LocalVirtualFile(newIoFile, newPath, this);
if (content != null) {
doUpdateContent(newVirtualFile, content);
}
addInSearcher(newVirtualFile);
return newVirtualFile;
} else {
throw new ForbiddenException("Unable create new file. Item specified as parent is not a folder");
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method delete.
void delete(LocalVirtualFile virtualFile, String lockToken) throws ForbiddenException, ServerException {
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable delete root folder");
}
final Path path = virtualFile.getPath();
final boolean isFile = virtualFile.isFile();
doDelete(virtualFile, lockToken);
deleteInSearcher(path, isFile);
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method move.
LocalVirtualFile move(LocalVirtualFile virtualFile, LocalVirtualFile parent, String name, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable move root folder");
}
if (virtualFile.getPath().equals(parent.getPath())) {
throw new ForbiddenException("Item cannot be moved to itself");
}
if (!parent.isFolder()) {
throw new ForbiddenException("Unable move. Item specified as parent is not a folder");
}
final Path sourcePath = virtualFile.getPath();
final Path parentPath = parent.getPath();
if (virtualFile.isFolder() && parent.getPath().isChild(virtualFile.getPath())) {
throw new ForbiddenException(String.format("Unable move item '%s' to '%s'. Item may not have itself as parent", sourcePath, parentPath));
}
if (virtualFile.isFile()) {
if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable move file '%s'. File is locked", sourcePath));
}
} else {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable move folder '%s'. Child items '%s' are locked", virtualFile, lockedFiles));
}
}
String newName = isNullOrEmpty(name) ? virtualFile.getName() : name;
final Path newPath = parent.getPath().newPath(newName);
LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
if (newVirtualFile.exists()) {
if (overwrite) {
delete(newVirtualFile, null);
} else {
throw new ConflictException(String.format("Item '%s' already exists", newPath));
}
}
doCopy(virtualFile, newVirtualFile);
addInSearcher(newVirtualFile);
final Path path = virtualFile.getPath();
final boolean isFile = virtualFile.isFile();
doDelete(virtualFile, lockToken);
deleteInSearcher(path, isFile);
return newVirtualFile;
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method unzip.
void unzip(LocalVirtualFile parent, InputStream zipped, boolean overwrite, int stripNumber) throws ForbiddenException, ConflictException, ServerException {
if (archiverFactory == null)
throw new ServerException("VFS: Could not create zip archiver. Archiver Factory is not properly configured (is null)");
if (parent.isFolder()) {
extract(archiverFactory.createArchiver(parent, "zip"), zipped, overwrite, stripNumber);
addInSearcher(parent);
} else {
throw new ForbiddenException(String.format("Unable import zip content. Item '%s' is not a folder", parent.getPath()));
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class ProjectManager method doImportProject.
/** Note: Use {@link FileWatcherManager#suspend()} and {@link FileWatcherManager#resume()} while importing source code */
private RegisteredProject doImportProject(String path, SourceStorage sourceStorage, boolean rewrite, LineConsumerFactory lineConsumerFactory) throws ServerException, IOException, ForbiddenException, UnauthorizedException, ConflictException, NotFoundException {
final ProjectImporter importer = importers.getImporter(sourceStorage.getType());
if (importer == null) {
throw new NotFoundException(format("Unable import sources project from '%s'. Sources type '%s' is not supported.", sourceStorage.getLocation(), sourceStorage.getType()));
}
String normalizePath = (path.startsWith("/")) ? path : "/".concat(path);
FolderEntry folder = asFolder(normalizePath);
if (folder != null && !rewrite) {
throw new ConflictException(format("Project %s already exists ", path));
}
if (folder == null) {
folder = getProjectsRoot().createFolder(normalizePath);
}
try {
importer.importSources(folder, sourceStorage, lineConsumerFactory);
} catch (final Exception e) {
folder.remove();
throw e;
}
final String name = folder.getPath().getName();
for (ProjectConfig project : workspaceProjectsHolder.getProjects()) {
if (normalizePath.equals(project.getPath())) {
// TODO Needed for factory project importing with keepDir. It needs to find more appropriate solution
List<String> innerProjects = projectRegistry.getProjects(normalizePath);
for (String innerProject : innerProjects) {
RegisteredProject registeredProject = projectRegistry.getProject(innerProject);
projectRegistry.putProject(registeredProject, asFolder(registeredProject.getPath()), true, false);
}
RegisteredProject rp = projectRegistry.putProject(project, folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
}
RegisteredProject rp = projectRegistry.putProject(new NewProjectConfigImpl(normalizePath, name, BaseProjectType.ID, sourceStorage), folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
Aggregations