Search in sources :

Example 46 with ConflictException

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

the class LocalVirtualFileSystem method copy.

LocalVirtualFile copy(LocalVirtualFile source, LocalVirtualFile parent, String name, boolean overwrite) throws ForbiddenException, ConflictException, ServerException {
    if (source.getPath().equals(parent.getPath())) {
        throw new ForbiddenException("Item cannot be copied to itself");
    }
    if (parent.isFolder()) {
        final String newName = isNullOrEmpty(name) ? source.getName() : name;
        LocalVirtualFile destination = (LocalVirtualFile) parent.getChild(Path.of(newName));
        if (destination != null) {
            if (overwrite) {
                delete(destination, null);
            } else {
                throw new ConflictException(String.format("Item '%s' already exists", destination.getPath()));
            }
        } else {
            final Path newPath = parent.getPath().newPath(newName);
            final File newIoFile = new File(ioRoot, toIoPath(newPath));
            destination = new LocalVirtualFile(newIoFile, newPath, this);
        }
        doCopy(source, destination);
        addInSearcher(destination);
        return destination;
    } else {
        throw new ForbiddenException("Unable copy item. Item specified as parent is not a folder");
    }
}
Also used : Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ConflictException(org.eclipse.che.api.core.ConflictException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 47 with ConflictException

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

the class MemoryVirtualFile method createFolder.

@Override
public VirtualFile createFolder(String name) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    checkName(name);
    if (name.charAt(0) == '/') {
        name = name.substring(1);
    }
    checkName(name);
    if (isFolder()) {
        MemoryVirtualFile newFolder = null;
        MemoryVirtualFile current = this;
        if (name.indexOf('/') > 0) {
            final Path internPath = Path.of(name);
            for (String element : internPath.elements()) {
                MemoryVirtualFile folder = newFolder(current, element);
                if (current.addChild(folder)) {
                    newFolder = folder;
                    current = folder;
                } else {
                    current = current.children.get(element);
                }
            }
            if (newFolder == null) {
                throw new ConflictException(String.format("Item with the name '%s' already exists", name));
            }
        } else {
            newFolder = newFolder(this, name);
            if (!addChild(newFolder)) {
                throw new ConflictException(String.format("Item with the name '%s' already exists", name));
            }
        }
        return newFolder;
    } else {
        throw new ForbiddenException("Unable create new folder. Item specified as parent is not a folder");
    }
}
Also used : Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ConflictException(org.eclipse.che.api.core.ConflictException)

Example 48 with ConflictException

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

the class MemoryVirtualFile method doCopy.

private VirtualFile doCopy(MemoryVirtualFile parent, String newName, boolean overwrite) throws ConflictException, ForbiddenException, ServerException {
    if (overwrite) {
        MemoryVirtualFile existedItem = parent.children.get(newName);
        if (existedItem != null) {
            existedItem.delete();
        }
    }
    MemoryVirtualFile virtualFile;
    if (isFile()) {
        virtualFile = newFile(parent, newName, Arrays.copyOf(content, content.length));
    } else {
        virtualFile = newFolder(parent, newName);
        for (VirtualFile child : getChildren()) {
            child.copyTo(virtualFile);
        }
    }
    virtualFile.properties.putAll(this.properties);
    if (parent.addChild(virtualFile)) {
        return virtualFile;
    }
    throw new ConflictException(String.format("Item '%s' already exists", parent.getPath().newPath(newName)));
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ConflictException(org.eclipse.che.api.core.ConflictException)

Example 49 with ConflictException

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

the class MemoryVirtualFile method createFile.

@Override
public VirtualFile createFile(String name, InputStream content) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    checkName(name);
    if (Path.of(name).length() > 1) {
        throw new ServerException(String.format("Invalid name '%s'", name));
    }
    if (isFolder()) {
        final MemoryVirtualFile newFile;
        try {
            newFile = newFile(this, name, content);
        } catch (IOException e) {
            throw new ServerException(String.format("Unable set content of '%s'. Error: %s", getPath(), e.getMessage()));
        }
        if (!addChild(newFile)) {
            throw new ConflictException(String.format("Item with the name '%s' already exists", name));
        }
        addInSearcher(newFile);
        return newFile;
    } else {
        throw new ForbiddenException("Unable create new file. Item specified as parent is not a folder");
    }
}
Also used : 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)

Example 50 with ConflictException

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

the class MemoryVirtualFile method rename.

@Override
public VirtualFile rename(String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    checkName(newName);
    boolean isFile = isFile();
    if (isRoot()) {
        throw new ForbiddenException("We were unable to rename a root folder.");
    }
    final Path myPath = getPath();
    final boolean isFolder = isFolder();
    if (isFolder) {
        final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable rename item '%s'. Child items '%s' are locked", getName(), lockedFiles));
        }
    } else {
        if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
            throw new ForbiddenException(String.format("We were unable to rename an item '%s'." + " The item is currently locked by the system", getPath()));
        }
    }
    if (parent.children.get(newName) != null) {
        throw new ConflictException(String.format("Item '%s' already exists", newName));
    }
    parent.children.remove(name);
    parent.children.put(newName, this);
    name = newName;
    lock = null;
    lastModificationDate = System.currentTimeMillis();
    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)

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