Search in sources :

Example 1 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method computeCollectionSyncRootMemberIds.

@SuppressWarnings("unchecked")
protected Map<String, Set<String>> computeCollectionSyncRootMemberIds(Principal principal) {
    Map<String, Set<String>> collectionSyncRootMemberIds = new HashMap<String, Set<String>>();
    PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
    RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
    for (String repositoryName : repositoryManager.getRepositoryNames()) {
        Set<String> collectionMemberIds = new HashSet<String>();
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Map<String, Serializable> props = new HashMap<String, Serializable>();
            props.put(CORE_SESSION_PROPERTY, (Serializable) session);
            PageProvider<DocumentModel> collectionPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(CollectionConstants.ALL_COLLECTIONS_PAGE_PROVIDER, null, null, 0L, props);
            List<DocumentModel> collections = collectionPageProvider.getCurrentPage();
            for (DocumentModel collection : collections) {
                if (isSynchronizationRoot(principal, collection)) {
                    PageProvider<DocumentModel> collectionMemberPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER, null, COLLECTION_CONTENT_PAGE_SIZE, 0L, props, collection.getId());
                    List<DocumentModel> collectionMembers = collectionMemberPageProvider.getCurrentPage();
                    for (DocumentModel collectionMember : collectionMembers) {
                        collectionMemberIds.add(collectionMember.getId());
                    }
                }
            }
            collectionSyncRootMemberIds.put(repositoryName, collectionMemberIds);
        }
    }
    return collectionSyncRootMemberIds;
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) PageProviderService(org.nuxeo.ecm.platform.query.api.PageProviderService) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) PageProvider(org.nuxeo.ecm.platform.query.api.PageProvider) RepositoryManager(org.nuxeo.ecm.core.api.repository.RepositoryManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testFileItem.

@Test
public void testFileItem() throws Exception {
    // ------------------------------------------------------
    // FileItem#getDownloadURL
    // ------------------------------------------------------
    FileItem fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
    String downloadURL = fileItem.getDownloadURL();
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", downloadURL);
    // ------------------------------------------------------------
    // FileItem#getDigestAlgorithm
    // ------------------------------------------------------------
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    FileItem noteItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(note);
    assertEquals("MD5", noteItem.getDigestAlgorithm());
    // ------------------------------------------------------------
    // FileItem#getDigest
    // ------------------------------------------------------------
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileItem.getDigest());
    String noteDigest = FileSystemItemHelper.getMD5Digest(note.getAdapter(BlobHolder.class).getBlob());
    assertEquals(noteDigest, noteItem.getDigest());
    assertEquals(custom.getAdapter(BlobHolder.class).getBlob().getDigest(), ((FileItem) defaultFileSystemItemFactory.getFileSystemItem(custom)).getDigest());
    // ------------------------------------------------------------
    // FileItem#getCanUpdate
    // ------------------------------------------------------------
    // As Administrator
    assertTrue(fileItem.getCanUpdate());
    // As a user with READ permission
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    // Under Oracle, the READ ACL optims are not visible from the
    // joe session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertFalse(fileItem.getCanUpdate());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertTrue(fileItem.getCanUpdate());
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        // ------------------------------------------------------
        // FileItem#getBlob
        // ------------------------------------------------------
        Blob fileItemBlob = fileItem.getBlob();
        assertEquals("Joe.odt", fileItemBlob.getFilename());
        assertEquals("Content of Joe's file.", fileItemBlob.getString());
        // Check versioning
        assertVersion("0.0", file);
        // ------------------------------------------------------
        // FileItem#setBlob and versioning
        // ------------------------------------------------------
        Blob newBlob = new StringBlob("This is a new file.");
        newBlob.setFilename("New blob.txt");
        ensureJustModified(file, session);
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        Blob updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("New blob.txt", updatedBlob.getFilename());
        assertEquals("This is a new file.", updatedBlob.getString());
        // Check versioning => should not be versioned since same
        // contributor
        // and last modification was done before the versioning delay
        assertVersion("0.0", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        newBlob.setFilename("File name modified.txt");
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.1", file);
        List<DocumentModel> fileVersions = session.getVersions(file.getRef());
        assertEquals(1, fileVersions.size());
        DocumentModel lastFileVersion = fileVersions.get(0);
        Blob versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("New blob.txt", versionedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        newBlob.setFilename("File name modified by Joe.txt");
        fileItem.setBlob(newBlob);
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.2", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(2, fileVersions.size());
        lastFileVersion = fileVersions.get(1);
        versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("File name modified.txt", versionedBlob.getFilename());
        // ------------------------------------------------------
        // DocumentBackedFileItem#rename and versioning
        // ------------------------------------------------------
        // Save document to trigger the DublinCoreListener and update
        // dc:lastContributor to "Administrator"
        // rename the file to enable dc listener (disable if not dirty)
        file.setPropertyValue("file:content/name", "newTitle");
        file = session.saveDocument(file);
        // Check versioning => should be versioned cause last contributor has changed
        assertVersion("0.3", file);
        // Switch back to Administrator as last contributor
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        ensureJustModified(file, session);
        fileItem.rename("Renamed file.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Check versioning => should not be versioned since same
        // contributor and last modification was done before the
        // versioning delay
        assertVersion("0.3", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        fileItem.rename("Renamed again.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.4", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(4, fileVersions.size());
        lastFileVersion = fileVersions.get(3);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        fileItem.rename("File renamed by Joe.txt");
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File renamed by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.5", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(5, fileVersions.size());
        lastFileVersion = fileVersions.get(4);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 3 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testLockedDocument.

@Test
public void testLockedDocument() {
    setPermission(syncRootFolder, "joe", SecurityConstants.READ_WRITE, true);
    setPermission(syncRootFolder, "jack", SecurityConstants.READ_WRITE, true);
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
        DocumentModel joeFile = joeSession.getDocument(file.getRef());
        log.trace("Check readonly flags on an unlocked document");
        FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        assertNull(fsItem.getLockInfo());
        log.trace("Check readonly flags on an document locked by the current user");
        joeSession.setLock(joeFile.getRef());
        // Re-fetch document to clear lock info
        joeFile = joeSession.getDocument(file.getRef());
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        Lock lockInfo = fsItem.getLockInfo();
        assertNotNull(lockInfo);
        assertEquals("joe", lockInfo.getOwner());
        assertNotNull(lockInfo.getCreated());
        // Check that the lock info is not fetched for FileSystemItem
        // adaptation when calling getChildren or
        // scrollDescendants
        FileSystemItemFactory defaultSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultSyncRootFolderItemFactory");
        FolderItem syncRootFolderItem = (FolderItem) defaultSyncRootFolderItemFactory.getFileSystemItem(syncRootFolder);
        List<FileSystemItem> children = syncRootFolderItem.getChildren();
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        children = syncRootFolderItem.scrollDescendants(null, 10, 1000);
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        try (CloseableCoreSession jackSession = coreFeature.openCoreSession("jack")) {
            nuxeoDriveManager.registerSynchronizationRoot(jackSession.getPrincipal(), syncRootFolder, jackSession);
            DocumentModel jackFile = jackSession.getDocument(file.getRef());
            log.trace("Check readonly flags for a non administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertFalse(fsItem.getCanRename());
            assertFalse(fsItem.getCanDelete());
            assertFalse(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for an administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for a non administrator on an unlocked document");
            joeSession.removeLock(joeFile.getRef());
            // Re-fetch document to clear lock info
            jackFile = jackSession.getDocument(file.getRef());
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            assertNull(fsItem.getLockInfo());
        }
    }
    resetPermissions(syncRootFolder, "jack");
    resetPermissions(syncRootFolder, "joe");
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FolderItem(org.nuxeo.drive.adapter.FolderItem) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Lock(org.nuxeo.ecm.core.api.Lock) Test(org.junit.Test)

Example 4 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession in project nuxeo-drive-server by nuxeo.

the class UserSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    if (isUserWorkspaceSyncRoot) {
        return super.getChildren();
    } else {
        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
        for (String repositoryName : syncRootsByRepo.keySet()) {
            try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
                Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
                Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
                while (syncRootRefsIt.hasNext()) {
                    IdRef idRef = syncRootRefsIt.next();
                    // See https://jira.nuxeo.com/browse/NXP-11146
                    if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                        }
                        continue;
                    }
                    DocumentModel doc = session.getDocument(idRef);
                    // principal)
                    if (session.getPrincipal().getName().equals(doc.getPropertyValue("dc:creator"))) {
                        // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                        FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                        if (child == null) {
                            if (log.isDebugEnabled()) {
                                log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, maybe because user %s doesn't have the required permission on it (default required permission is ReadWrite). Not including it in children.", idRef, session.getPrincipal().getName()));
                            }
                            continue;
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Including synchronization root %s in children.", idRef));
                        }
                        children.add(child);
                    }
                }
            }
        }
        Collections.sort(children);
        return children;
    }
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 5 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession in project nuxeo-drive-server by nuxeo.

the class UserWorkspaceSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    List<FileSystemItem> children = new ArrayList<FileSystemItem>();
    Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
    for (String repositoryName : syncRootsByRepo.keySet()) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
            while (syncRootRefsIt.hasNext()) {
                IdRef idRef = syncRootRefsIt.next();
                // See https://jira.nuxeo.com/browse/NXP-11146
                if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                    }
                    continue;
                }
                DocumentModel doc = session.getDocument(idRef);
                // registered as a synchronization root to avoid recursion
                if (!UserWorkspaceHelper.isUserWorkspace(doc)) {
                    // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                    FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                    if (child == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, not including it in children.", idRef));
                        }
                        continue;
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Including synchronization root %s in children.", idRef));
                    }
                    children.add(child);
                }
            }
        }
    }
    Collections.sort(children);
    return children;
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)35 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)25 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)9 FolderItem (org.nuxeo.drive.adapter.FolderItem)9 Blob (org.nuxeo.ecm.core.api.Blob)7 IdRef (org.nuxeo.ecm.core.api.IdRef)7 HashMap (java.util.HashMap)6 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)6 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)6 FileItem (org.nuxeo.drive.adapter.FileItem)5 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)5 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)5 RepositoryManager (org.nuxeo.ecm.core.api.repository.RepositoryManager)5 Serializable (java.io.Serializable)4 Deploy (org.nuxeo.runtime.test.runner.Deploy)4 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 PageProvider (org.nuxeo.ecm.platform.query.api.PageProvider)3