Search in sources :

Example 51 with ForbiddenException

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

the class ZipArchiver method addZipEntry.

private void addZipEntry(VirtualFile virtualFile, ZipOutputStream zipOutputStream) throws ServerException {
    try {
        ZipEntry zipEntry = new ZipEntry(getZipEntryName(virtualFile));
        zipOutputStream.putNextEntry(zipEntry);
        if (virtualFile.isFolder()) {
            zipEntry.setTime(0);
        } else {
            try (InputStream content = virtualFile.getContent()) {
                ByteStreams.copy(content, zipOutputStream);
            }
            zipEntry.setTime(virtualFile.getLastModificationDate());
        }
        zipOutputStream.closeEntry();
    } catch (ForbiddenException e) {
        throw new ServerException(e.getServiceError());
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ZipInputStream(java.util.zip.ZipInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 52 with ForbiddenException

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

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

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

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

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