Search in sources :

Example 26 with ForbiddenException

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

the class LocalVirtualFileTest method failsUpdateContentOfLockedFileByStreamWhenLockTokenIsInvalid.

@Test
public void failsUpdateContentOfLockedFileByStreamWhenLockTokenIsInvalid() throws Exception {
    VirtualFile root = getRoot();
    VirtualFile file = root.createFile(generateFileName(), DEFAULT_CONTENT);
    String invalidLockToken = invalidateLockToken(file.lock(0));
    try {
        file.updateContent(new ByteArrayInputStream("updated content".getBytes()), invalidLockToken);
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException expected) {
        assertionHelper.assertThatIoFileHasContent(file.getPath(), DEFAULT_CONTENT_BYTES);
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 27 with ForbiddenException

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

the class LocalVirtualFileTest method failsMoveLockedFileWithoutLockToken.

@Test
public void failsMoveLockedFileWithoutLockToken() throws Exception {
    VirtualFile file = getRoot().createFile(generateFileName(), DEFAULT_CONTENT);
    file.setProperty("property1", "value1");
    Path filePath = file.getPath();
    file.lock(0);
    VirtualFile targetFolder = getRoot().createFolder(generateFolderName());
    Path movedFilePath = targetFolder.getPath().newPath(file.getName());
    try {
        file.moveTo(targetFolder);
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException e) {
        assertionHelper.assertThatIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatLockIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatMetadataIoFileDoesNotExist(movedFilePath);
        assertionHelper.assertThatIoFileHasContent(filePath, DEFAULT_CONTENT_BYTES);
        assertionHelper.assertThatMetadataIoFileHasContent(filePath, serializeVirtualFileMetadata(ImmutableMap.of("property1", "value1")));
        assertionHelper.assertThatLockIoFileExists(filePath);
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Test(org.junit.Test)

Example 28 with ForbiddenException

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

the class LocalVirtualFileSystem method untar.

void untar(LocalVirtualFile parent, InputStream tarArchive, boolean overwrite, int stripNumber) throws ForbiddenException, ConflictException, ServerException {
    if (archiverFactory == null)
        throw new ServerException("VFS: Could not create tar archiver. Archiver Factory is not properly configured (is null)");
    if (parent.isFolder()) {
        extract(archiverFactory.createArchiver(parent, "tar"), tarArchive, overwrite, stripNumber);
        addInSearcher(parent);
    } else {
        throw new ForbiddenException(String.format("Unable import tar archive. 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 29 with ForbiddenException

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

the class MemoryVirtualFile method copyTo.

@Override
public VirtualFile copyTo(VirtualFile parent, String newName, boolean overwrite) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    ((MemoryVirtualFile) parent).checkExistence();
    if (isRoot()) {
        throw new ServerException("Unable copy root folder");
    }
    if (newName == null || newName.trim().isEmpty()) {
        newName = this.getName();
    }
    if (parent.isFolder()) {
        VirtualFile copy = doCopy((MemoryVirtualFile) parent, newName, overwrite);
        addInSearcher(copy);
        return copy;
    } else {
        throw new ForbiddenException(String.format("Unable create copy of '%s'. Item '%s' specified as parent is not a folder.", getPath(), parent.getPath()));
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException)

Example 30 with ForbiddenException

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

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