Search in sources :

Example 46 with ForbiddenException

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");
    }
}
Also used : Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) IOException(java.io.IOException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 47 with ForbiddenException

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);
}
Also used : Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException)

Example 48 with ForbiddenException

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;
}
Also used : Path(org.eclipse.che.api.vfs.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ConflictException(org.eclipse.che.api.core.ConflictException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 49 with ForbiddenException

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()));
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException)

Example 50 with ForbiddenException

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;
}
Also used : ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ConflictException(org.eclipse.che.api.core.ConflictException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ProjectImporter(org.eclipse.che.api.project.server.importer.ProjectImporter)

Aggregations

ForbiddenException (org.eclipse.che.api.core.ForbiddenException)88 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)54 Test (org.junit.Test)44 ServerException (org.eclipse.che.api.core.ServerException)33 Path (org.eclipse.che.api.vfs.Path)29 ConflictException (org.eclipse.che.api.core.ConflictException)25 IOException (java.io.IOException)13 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 File (java.io.File)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 List (java.util.List)6 BadRequestException (org.eclipse.che.api.core.BadRequestException)6 VirtualFileEntry (org.eclipse.che.api.project.server.VirtualFileEntry)6 LockedFileFinder (org.eclipse.che.api.vfs.LockedFileFinder)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiParam (io.swagger.annotations.ApiParam)3 ApiResponse (io.swagger.annotations.ApiResponse)3 ApiResponses (io.swagger.annotations.ApiResponses)3