Search in sources :

Example 56 with ForbiddenException

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

the class MemoryVirtualFile method unlock.

@Override
public VirtualFile unlock(String lockToken) throws ForbiddenException, ConflictException {
    checkExistence();
    if (isFile()) {
        final LockHolder theLock = lock;
        if (theLock == null) {
            throw new ConflictException("File is not locked");
        } else if (isExpired(theLock)) {
            lock = null;
            throw new ConflictException("File is not locked");
        }
        if (theLock.lockToken.equals(lockToken)) {
            lock = null;
            lastModificationDate = System.currentTimeMillis();
        } else {
            throw new ForbiddenException("Unable remove lock from file. Lock token does not match");
        }
        lastModificationDate = System.currentTimeMillis();
        return this;
    } else {
        throw new ForbiddenException(String.format("Unable unlock '%s'. Locking allowed for files only", getPath()));
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ConflictException(org.eclipse.che.api.core.ConflictException)

Example 57 with ForbiddenException

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

the class MemoryVirtualFile method delete.

@Override
public void delete(String lockToken) throws ForbiddenException, ServerException {
    checkExistence();
    boolean isFile = isFile();
    if (isRoot()) {
        throw new ForbiddenException("Unable delete root folder");
    }
    final Path myPath = getPath();
    final boolean folder = isFolder();
    if (folder) {
        final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable delete item '%s'. Child items '%s' are locked", getName(), lockedFiles));
        }
        for (VirtualFile virtualFile : getTreeAsList(this)) {
            ((MemoryVirtualFile) virtualFile).exists = false;
        }
    } else {
        if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
            throw new ForbiddenException(String.format("Unable delete item '%s'. Item is locked", getPath()));
        }
    }
    parent.children.remove(name);
    exists = false;
    parent = null;
    deleteFromSearcher(myPath, isFile);
}
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)

Example 58 with ForbiddenException

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

the class LocalVirtualFileTest method failsUpdateContentOfLockedFileByBytesWhenLockTokenIsInvalid.

@Test
public void failsUpdateContentOfLockedFileByBytesWhenLockTokenIsInvalid() throws Exception {
    VirtualFile root = getRoot();
    VirtualFile file = root.createFile(generateFileName(), DEFAULT_CONTENT);
    String invalidLockToken = invalidateLockToken(file.lock(0));
    try {
        file.updateContent("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) Test(org.junit.Test)

Example 59 with ForbiddenException

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

the class LocalVirtualFileTest method failsDeleteFolderWhenItContainsLockedFile.

@Test
public void failsDeleteFolderWhenItContainsLockedFile() throws Exception {
    VirtualFile folder = getRoot().createFolder(generateFolderName());
    folder.setProperty("property1", "value1");
    Path folderPath = folder.getPath();
    VirtualFile file = folder.createFile(generateFileName(), DEFAULT_CONTENT);
    file.setProperty("property2", "value2");
    Path filePath = file.getPath();
    file.lock(0);
    try {
        folder.delete();
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException e) {
        assertionHelper.assertThatIoFileExists(folderPath);
        assertionHelper.assertThatMetadataIoFileHasContent(folder.getPath(), serializeVirtualFileMetadata(ImmutableMap.of("property1", "value1")));
        assertionHelper.assertThatIoFileHasContent(filePath, DEFAULT_CONTENT_BYTES);
        assertionHelper.assertThatMetadataIoFileHasContent(file.getPath(), serializeVirtualFileMetadata(ImmutableMap.of("property2", "value2")));
    }
}
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 60 with ForbiddenException

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

the class LocalVirtualFileTest method failsLockFolder.

@Test
public void failsLockFolder() throws Exception {
    VirtualFile folder = getRoot().createFolder(generateFolderName());
    try {
        folder.lock(0);
        thrown.expect(ForbiddenException.class);
    } catch (ForbiddenException e) {
        assertionHelper.assertThatLockIoFileDoesNotExist(folder.getPath());
        assertFalse(folder.isLocked());
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Test(org.junit.Test)

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