Search in sources :

Example 16 with ServerException

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

the class IndexedFileUpdateConsumer method accept.

@Override
public void accept(Path path) {
    try {
        VirtualFileSystem virtualFileSystem = vfsProvider.getVirtualFileSystem();
        SearcherProvider searcherProvider = virtualFileSystem.getSearcherProvider();
        Searcher searcher = searcherProvider.getSearcher(virtualFileSystem);
        Path innerPath = root.toPath().relativize(path);
        org.eclipse.che.api.vfs.Path vfsPath = org.eclipse.che.api.vfs.Path.of(innerPath.toString());
        VirtualFile child = virtualFileSystem.getRoot().getChild(vfsPath);
        if (child != null) {
            searcher.update(child);
        }
    } catch (ServerException e) {
        LOG.error("Issue happened during updating modified file in index", e);
    }
}
Also used : Path(java.nio.file.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ServerException(org.eclipse.che.api.core.ServerException) VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) Searcher(org.eclipse.che.api.vfs.search.Searcher) SearcherProvider(org.eclipse.che.api.vfs.search.SearcherProvider)

Example 17 with ServerException

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

the class ProjectServiceTest method testMoveFileWithRenameAndOverwrite.

@Test
public void testMoveFileWithRenameAndOverwrite() 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));
    MoveOptions descriptor = DtoFactory.getInstance().createDto(MoveOptions.class);
    descriptor.setName(destinationFileName);
    descriptor.setOverWrite(true);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/move/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));
    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) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) MoveOptions(org.eclipse.che.api.project.shared.dto.MoveOptions) Test(org.testng.annotations.Test)

Example 18 with ServerException

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

the class LocalVirtualFileSystem method doGetChildren.

private List<VirtualFile> doGetChildren(LocalVirtualFile parent, FilenameFilter ioFileFilter, VirtualFileFilter vfsFilter) throws ServerException {
    if (ioFileFilter == null) {
        ioFileFilter = IoUtil.ANY_FILTER;
    }
    final String[] names = parent.toIoFile().list(ioFileFilter);
    if (names == null) {
        throw new ServerException(String.format("Unable get children of '%s'", parent.getPath()));
    }
    if (vfsFilter == null) {
        vfsFilter = VirtualFileFilter.ACCEPT_ALL;
    }
    final List<VirtualFile> children = newArrayListWithCapacity(names.length);
    for (String name : names) {
        final Path childPath = parent.getPath().newPath(name);
        final LocalVirtualFile child = new LocalVirtualFile(new File(ioRoot, toIoPath(childPath)), childPath, this);
        if (vfsFilter.accept(child)) {
            children.add(child);
        }
    }
    return children;
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ServerException(org.eclipse.che.api.core.ServerException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 19 with ServerException

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

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

Aggregations

ServerException (org.eclipse.che.api.core.ServerException)143 IOException (java.io.IOException)51 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)37 NotFoundException (org.eclipse.che.api.core.NotFoundException)37 ConflictException (org.eclipse.che.api.core.ConflictException)32 File (java.io.File)22 Test (org.testng.annotations.Test)20 ArrayList (java.util.ArrayList)19 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)19 List (java.util.List)15 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)15 Map (java.util.Map)13 Transactional (com.google.inject.persist.Transactional)12 BadRequestException (org.eclipse.che.api.core.BadRequestException)12 InputStream (java.io.InputStream)11 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)10 String.format (java.lang.String.format)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 HashMap (java.util.HashMap)8