Search in sources :

Example 41 with ConflictException

use of org.eclipse.che.api.core.ConflictException in project che by eclipse.

the class LocalVirtualFileSystem method doLock.

private String doLock(LocalVirtualFile virtualFile, long timeout) throws ConflictException, ServerException {
    try {
        if (NO_LOCK == lockTokensCache.get(virtualFile.getPath())) {
            final FileLock lock = createLock(timeout);
            final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
            fileLockIoFile.getParentFile().mkdirs();
            try (DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileLockIoFile)))) {
                locksSerializer.write(dos, lock);
            }
            lockTokensCache.put(virtualFile.getPath(), lock);
            return lock.getLockToken();
        }
        throw new ConflictException(String.format("Unable lock file '%s'. File already locked", virtualFile.getPath()));
    } catch (IOException | ExecutionException e) {
        String errorMessage = String.format("Unable lock file '%s'", virtualFile.getPath());
        if (e instanceof ExecutionException) {
            LOG.error(errorMessage + "\n" + e.getCause().getMessage(), e.getCause());
        } else {
            LOG.error(errorMessage + "\n" + e.getMessage(), e);
        }
        throw new ServerException(errorMessage);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 42 with ConflictException

use of org.eclipse.che.api.core.ConflictException 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 43 with ConflictException

use of org.eclipse.che.api.core.ConflictException in project che by eclipse.

the class ProjectManager method validateProjectConfigurations.

private void validateProjectConfigurations(List<? extends NewProjectConfig> projectConfigList, boolean rewrite) throws NotFoundException, ServerException, ConflictException, ForbiddenException, BadRequestException {
    for (NewProjectConfig projectConfig : projectConfigList) {
        final String pathToProject = projectConfig.getPath();
        if (isNullOrEmpty(pathToProject)) {
            throw new BadRequestException("Path for new project should be defined");
        }
        final String path = ProjectRegistry.absolutizePath(pathToProject);
        final RegisteredProject registeredProject = projectRegistry.getProject(path);
        if (registeredProject != null && rewrite) {
            delete(path);
        } else if (registeredProject != null) {
            throw new ConflictException(format("Project config already exists for %s", path));
        }
        final String projectTypeId = projectConfig.getType();
        if (isNullOrEmpty(projectTypeId)) {
            projectConfig.setType(BaseProjectType.ID);
        }
    }
}
Also used : ConflictException(org.eclipse.che.api.core.ConflictException) BadRequestException(org.eclipse.che.api.core.BadRequestException) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig)

Example 44 with ConflictException

use of org.eclipse.che.api.core.ConflictException 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)

Example 45 with ConflictException

use of org.eclipse.che.api.core.ConflictException in project che by eclipse.

the class TarArchiver method extract.

@Override
public void extract(InputStream tarInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
    try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) {
        InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream);
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
            VirtualFile extractFolder = folder;
            Path relativePath = Path.of(tarEntry.getName());
            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }
                relativePath = relativePath.subPath(stripNumber);
            }
            if (tarEntry.isDirectory()) {
                if (!extractFolder.hasChild(relativePath)) {
                    extractFolder.createFolder(relativePath.toString());
                }
                continue;
            }
            if (relativePath.length() > 1) {
                Path neededParentPath = relativePath.getParent();
                VirtualFile neededParent = extractFolder.getChild(neededParentPath);
                if (neededParent == null) {
                    neededParent = extractFolder.createFolder(neededParentPath.toString());
                }
                extractFolder = neededParent;
            }
            String fileName = relativePath.getName();
            VirtualFile file = extractFolder.getChild(Path.of(fileName));
            if (file == null) {
                extractFolder.createFile(fileName, notClosableInputStream);
            } else {
                if (overwrite) {
                    file.updateContent(notClosableInputStream);
                } else {
                    throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
                }
            }
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ConflictException(org.eclipse.che.api.core.ConflictException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

ConflictException (org.eclipse.che.api.core.ConflictException)81 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)28 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)25 ServerException (org.eclipse.che.api.core.ServerException)24 Test (org.junit.Test)24 NotFoundException (org.eclipse.che.api.core.NotFoundException)17 Path (org.eclipse.che.api.vfs.Path)12 IOException (java.io.IOException)10 NewProjectConfig (org.eclipse.che.api.core.model.project.NewProjectConfig)8 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)8 ArrayList (java.util.ArrayList)7 BadRequestException (org.eclipse.che.api.core.BadRequestException)7 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)7 File (java.io.File)6 ProjectConfig (org.eclipse.che.api.core.model.project.ProjectConfig)6 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)6 List (java.util.List)5 Map (java.util.Map)5 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)5 String.format (java.lang.String.format)4