Search in sources :

Example 6 with ConflictException

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

the class MemoryVirtualFile method moveTo.

@Override
public VirtualFile moveTo(VirtualFile parent, String newName, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    MemoryVirtualFile memoryParent = (MemoryVirtualFile) parent;
    memoryParent.checkExistence();
    if (isRoot()) {
        throw new ForbiddenException("Unable move root folder");
    }
    if (!parent.isFolder()) {
        throw new ForbiddenException("Unable move item. Item specified as parent is not a folder");
    }
    if (newName == null || newName.trim().isEmpty()) {
        newName = this.getName();
    }
    final boolean isFile = isFile();
    final Path myPath = getPath();
    final Path newParentPath = parent.getPath();
    final boolean folder = isFolder();
    if (folder) {
        if (newParentPath.isChild(myPath)) {
            throw new ForbiddenException(String.format("Unable move item %s to %s. Item may not have itself as parent", myPath, newParentPath));
        }
        final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable move item '%s'. Child items '%s' are locked", getName(), lockedFiles));
        }
    } else if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
        throw new ForbiddenException(String.format("Unable move item %s. Item is locked", myPath));
    }
    if (overwrite) {
        MemoryVirtualFile existedItem = memoryParent.children.get(newName);
        if (existedItem != null) {
            existedItem.delete();
        }
    }
    if (memoryParent.children.containsKey(newName)) {
        throw new ConflictException(String.format("Item '%s' already exists", parent.getPath().newPath(newName)));
    }
    this.parent.children.remove(name);
    memoryParent.children.put(newName, this);
    this.parent = memoryParent;
    this.name = newName;
    lock = null;
    deleteFromSearcher(myPath, isFile);
    addInSearcher(this);
    return this;
}
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)

Example 7 with ConflictException

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

the class ZipArchiver method extract.

@Override
public void extract(InputStream zipInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
    try (ZipInputStream zip = new ZipInputStream(ZipContent.of(zipInput).getContent())) {
        InputStream notClosableInputStream = new NotClosableInputStream(zip);
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            VirtualFile extractFolder = folder;
            Path relativePath = Path.of(zipEntry.getName());
            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }
                relativePath = relativePath.subPath(stripNumber);
            }
            if (zipEntry.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()));
                }
            }
            zip.closeEntry();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ConflictException(org.eclipse.che.api.core.ConflictException) ZipInputStream(java.util.zip.ZipInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream)

Example 8 with ConflictException

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

the class ProjectManagerWriteTest method registerImporter.

private void registerImporter(String importType, InputStream zip) throws Exception {
    final ValueHolder<FolderEntry> folderHolder = new ValueHolder<>();
    importerRegistry.register(new ProjectImporter() {

        @Override
        public String getId() {
            return importType;
        }

        @Override
        public boolean isInternal() {
            return false;
        }

        @Override
        public String getDescription() {
            return "importer";
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage) throws ConflictException, ServerException, ForbiddenException {
            importSources(baseFolder, storage, LineConsumerFactory.NULL);
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage, LineConsumerFactory importOutputConsumerFactory) throws ConflictException, ServerException, ForbiddenException {
            // Don't really use location in this test.
            baseFolder.getVirtualFile().unzip(zip, true, 0);
            folderHolder.set(baseFolder);
        }

        @Override
        public ImporterCategory getCategory() {
            return ProjectImporter.ImporterCategory.ARCHIVE;
        }
    });
}
Also used : SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) LineConsumerFactory(org.eclipse.che.api.core.util.LineConsumerFactory) ValueHolder(org.eclipse.che.api.core.util.ValueHolder) ProjectImporter(org.eclipse.che.api.project.server.importer.ProjectImporter)

Example 9 with ConflictException

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

the class ProjectManagerWriteTest method testSamePathProjectCreateFailed.

@Test
public void testSamePathProjectCreateFailed() throws Exception {
    // SPECS:
    // If there is a project with the same path ConflictException will be thrown on create project
    ProjectConfig pc = new NewProjectConfigImpl("/testSamePathProjectCreateFailed", "blank", null, "name", "descr", null, null, null);
    pm.createProject(pc, null);
    pc = new NewProjectConfigImpl("/testSamePathProjectCreateFailed", "blank", null, "name", "descr", null, null, null);
    try {
        pm.createProject(pc, null);
        fail("ConflictException: Project config already exists /testSamePathProjectCreateFailed");
    } catch (ConflictException e) {
    }
    assertNotNull(projectRegistry.getProject("/testSamePathProjectCreateFailed"));
}
Also used : NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) ConflictException(org.eclipse.che.api.core.ConflictException) Test(org.junit.Test)

Example 10 with ConflictException

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

the class ProjectManagerWriteTest method testInvalidConfigProjectCreateFailed.

@Test
public void testInvalidConfigProjectCreateFailed() throws Exception {
    // SPECS:
    // If project path is not defined
    // Project creation failed with ConflictException
    ProjectConfig pc = new NewProjectConfigImpl(null, "pt2", null, "name", "descr", null, null, null);
    try {
        pm.createProject(pc, null);
        fail("ConflictException: Path for new project should be defined ");
    } catch (ConflictException e) {
    }
}
Also used : NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) ConflictException(org.eclipse.che.api.core.ConflictException) Test(org.junit.Test)

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