Search in sources :

Example 36 with ForbiddenException

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

the class LocalVirtualFileSystem method doDelete.

private void doDelete(LocalVirtualFile virtualFile, String lockToken) throws ForbiddenException, ServerException {
    if (virtualFile.isFolder()) {
        final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable delete folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
        }
    } else if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
        throw new ForbiddenException(String.format("Unable delete file '%s'. File is locked", virtualFile.getPath()));
    }
    cleanUpCaches();
    final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
    if (fileLockIoFile.delete()) {
        if (fileLockIoFile.exists()) {
            LOG.error("Unable delete lock file {}", fileLockIoFile);
            throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
        }
    }
    final File metadataIoFile = getMetadataIoFile(virtualFile.getPath());
    if (metadataIoFile.delete()) {
        if (metadataIoFile.exists()) {
            LOG.error("Unable delete metadata file {}", metadataIoFile);
            throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
        }
    }
    if (!deleteRecursive(virtualFile.toIoFile())) {
        LOG.error("Unable delete file {}", virtualFile.toIoFile());
        throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 37 with ForbiddenException

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

the class LocalVirtualFileSystem method getContent.

InputStream getContent(LocalVirtualFile virtualFile) throws ForbiddenException, ServerException {
    if (virtualFile.isFile()) {
        final PathLockFactory.PathLock lock = pathLockFactory.getLock(virtualFile.getPath(), false).acquire(WAIT_FOR_FILE_LOCK_TIMEOUT);
        File spoolFile = null;
        try {
            final File ioFile = virtualFile.toIoFile();
            final long fileLength = ioFile.length();
            if (fileLength <= MAX_BUFFER_SIZE) {
                return new ByteArrayInputStream(Files.toByteArray(ioFile));
            }
            // Copy this file to be able release the file lock before leave this method.
            spoolFile = File.createTempFile("spool_file", null);
            Files.copy(ioFile, spoolFile);
            return new DeleteOnCloseFileInputStream(spoolFile);
        } catch (IOException e) {
            if (spoolFile != null) {
                FileCleaner.addFile(spoolFile);
            }
            String errorMessage = String.format("Unable get content of '%s'", virtualFile.getPath());
            LOG.error(errorMessage + "\n" + e.getMessage(), e);
            throw new ServerException(errorMessage);
        } finally {
            lock.release();
        }
    } else {
        throw new ForbiddenException(String.format("Unable get content. Item '%s' is not a file", virtualFile.getPath()));
    }
}
Also used : PathLockFactory(org.eclipse.che.api.vfs.PathLockFactory) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DeleteOnCloseFileInputStream(org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 38 with ForbiddenException

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

the class LocalVirtualFileSystem method rename.

LocalVirtualFile rename(LocalVirtualFile virtualFile, String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
    checkName(newName);
    if (virtualFile.isRoot()) {
        throw new ForbiddenException("Unable rename root folder");
    }
    if (virtualFile.isFile()) {
        if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
            throw new ForbiddenException(String.format("Unable rename file '%s'. File is locked", virtualFile.getPath()));
        }
    } else {
        final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable rename folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
        }
    }
    if (newName.equals(virtualFile.getName())) {
        return virtualFile;
    } else {
        final Path newPath = virtualFile.getPath().getParent().newPath(newName);
        final LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
        if (newVirtualFile.exists()) {
            throw new ConflictException(String.format("Item '%s' already exists", newVirtualFile.getName()));
        }
        doCopy(virtualFile, newVirtualFile);
        addInSearcher(newVirtualFile);
        final Path path = virtualFile.getPath();
        final boolean isFile = virtualFile.isFile();
        doDelete(virtualFile, lockToken);
        deleteInSearcher(path, isFile);
        return newVirtualFile;
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ConflictException(org.eclipse.che.api.core.ConflictException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 39 with ForbiddenException

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

the class RemoteOAuthTokenProvider method getToken.

/** {@inheritDoc} */
@Override
public OAuthToken getToken(String oauthProviderName, String userId) throws IOException {
    if (userId.isEmpty()) {
        return null;
    }
    try {
        UriBuilder ub = UriBuilder.fromUri(apiEndpoint).path(OAuthAuthenticationService.class).path(OAuthAuthenticationService.class, "token").queryParam("oauth_provider", oauthProviderName);
        Link getTokenLink = DtoFactory.newDto(Link.class).withHref(ub.build().toString()).withMethod("GET");
        return httpJsonRequestFactory.fromLink(getTokenLink).request().asDto(OAuthToken.class);
    } catch (NotFoundException ne) {
        LOG.warn("Token not found for user {}", userId);
        return null;
    } catch (ServerException | UnauthorizedException | ForbiddenException | ConflictException | BadRequestException e) {
        LOG.warn("Exception on token retrieval, message : {}", e.getLocalizedMessage());
        return null;
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) NotFoundException(org.eclipse.che.api.core.NotFoundException) BadRequestException(org.eclipse.che.api.core.BadRequestException) UriBuilder(javax.ws.rs.core.UriBuilder) Link(org.eclipse.che.api.core.rest.shared.dto.Link)

Example 40 with ForbiddenException

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

the class ProjectServiceTest method testCopyFileWithRenameAndOverwrite.

@Test
public void testCopyFileWithRenameAndOverwrite() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b/c");
    // File names
    String originFileName = "test.txt";
    String destinationFileName = "overwriteMe.txt";
    // File contents
    String originContent = "to be or not no be";
    String overwrittenContent = "that is the question";
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile(originFileName, originContent.getBytes(Charset.defaultCharset()));
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b/c")).createFile(destinationFileName, overwrittenContent.getBytes(Charset.defaultCharset()));
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
    CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
    descriptor.setName(destinationFileName);
    descriptor.setOverWrite(true);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/my_project/a/b/" + originFileName + "?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/file/my_project/a/b/c/" + destinationFileName));
    // new
    assertNotNull(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName));
    // old
    assertNotNull(myProject.getBaseFolder().getChild("a/b/" + originFileName));
    Scanner inputStreamScanner = null;
    String theFirstLineFromDestinationFile;
    try {
        inputStreamScanner = new Scanner(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName).getVirtualFile().getContent());
        theFirstLineFromDestinationFile = inputStreamScanner.nextLine();
        // destination should contain original file's content
        assertEquals(theFirstLineFromDestinationFile, originContent);
    } catch (ForbiddenException | ServerException e) {
        Assert.fail(e.getMessage());
    } finally {
        if (inputStreamScanner != null) {
            inputStreamScanner.close();
        }
    }
}
Also used : Scanner(java.util.Scanner) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.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