Search in sources :

Example 26 with CloseableCoreSession

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

the class AbstractFileSystemItemFactory method exists.

/**
 * The default factory considers that a {@link FileSystemItem} with the given id exists if the backing
 * {@link DocumentModel} can be fetched and {@link #isFileSystemItem(DocumentModel)} returns true.
 *
 * @see #isFileSystemItem(DocumentModel)
 */
@Override
public boolean exists(String id, Principal principal) {
    String[] idFragments = parseFileSystemId(id);
    String repositoryName = idFragments[1];
    String docId = idFragments[2];
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        DocumentModel doc = getDocumentById(docId, session);
        return isFileSystemItem(doc);
    } catch (DocumentNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("No doc related to id %s, returning false.", docId));
        }
        return false;
    } catch (DocumentSecurityException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("User %s cannot access doc %s, returning false.", principal.getName(), docId));
        }
        return false;
    }
}
Also used : DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 27 with CloseableCoreSession

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

the class NuxeoDriveManagerImpl method getChangeSummary.

protected FileSystemChangeSummary getChangeSummary(Principal principal, Map<String, Set<IdRef>> lastActiveRootRefs, Map<String, SynchronizationRoots> roots, Map<String, Set<String>> collectionSyncRootMemberIds, long lowerBound, boolean integerBounds) {
    List<FileSystemItemChange> allChanges = new ArrayList<FileSystemItemChange>();
    // Compute the list of all repositories to consider for the aggregate summary
    Set<String> allRepositories = new TreeSet<String>();
    allRepositories.addAll(roots.keySet());
    allRepositories.addAll(lastActiveRootRefs.keySet());
    allRepositories.addAll(collectionSyncRootMemberIds.keySet());
    long syncDate;
    long upperBound;
    if (integerBounds) {
        upperBound = changeFinder.getUpperBound(allRepositories);
        // Truncate sync date to 0 milliseconds
        syncDate = System.currentTimeMillis();
        syncDate = syncDate - (syncDate % 1000);
    } else {
        upperBound = changeFinder.getCurrentDate();
        syncDate = upperBound;
    }
    Boolean hasTooManyChanges = Boolean.FALSE;
    int limit = Integer.parseInt(Framework.getProperty(DOCUMENT_CHANGE_LIMIT_PROPERTY, "1000"));
    if (!allRepositories.isEmpty() && lowerBound >= 0 && upperBound > lowerBound) {
        for (String repositoryName : allRepositories) {
            try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
                // Get document changes
                Set<IdRef> lastRefs = lastActiveRootRefs.get(repositoryName);
                if (lastRefs == null) {
                    lastRefs = Collections.emptySet();
                }
                SynchronizationRoots activeRoots = roots.get(repositoryName);
                if (activeRoots == null) {
                    activeRoots = SynchronizationRoots.getEmptyRoots(repositoryName);
                }
                Set<String> repoCollectionSyncRootMemberIds = collectionSyncRootMemberIds.get(repositoryName);
                if (repoCollectionSyncRootMemberIds == null) {
                    repoCollectionSyncRootMemberIds = Collections.emptySet();
                }
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Start: getting FileSystemItem changes for repository %s / user %s between %s and %s with activeRoots = %s", repositoryName, principal.getName(), lowerBound, upperBound, activeRoots.getPaths()));
                }
                List<FileSystemItemChange> changes;
                if (integerBounds) {
                    changes = changeFinder.getFileSystemChangesIntegerBounds(session, lastRefs, activeRoots, repoCollectionSyncRootMemberIds, lowerBound, upperBound, limit);
                } else {
                    changes = changeFinder.getFileSystemChanges(session, lastRefs, activeRoots, lowerBound, upperBound, limit);
                }
                allChanges.addAll(changes);
            } catch (TooManyChangesException e) {
                hasTooManyChanges = Boolean.TRUE;
                allChanges.clear();
                break;
            }
        }
    }
    // Send back to the client the list of currently active roots to be able
    // to efficiently detect root unregistration events for the next
    // incremental change summary
    Map<String, Set<IdRef>> activeRootRefs = new HashMap<String, Set<IdRef>>();
    for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
        activeRootRefs.put(rootsEntry.getKey(), rootsEntry.getValue().getRefs());
    }
    FileSystemChangeSummary summary = new FileSystemChangeSummaryImpl(allChanges, activeRootRefs, syncDate, upperBound, hasTooManyChanges);
    if (log.isDebugEnabled()) {
        log.debug(String.format("End: getting %d FileSystemItem changes for user %s between %s and %s with activeRoots = %s -> %s", allChanges.size(), principal.getName(), lowerBound, upperBound, roots, summary));
    }
    return summary;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) FileSystemChangeSummary(org.nuxeo.drive.service.FileSystemChangeSummary) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) TreeSet(java.util.TreeSet) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) TooManyChangesException(org.nuxeo.drive.service.TooManyChangesException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with CloseableCoreSession

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

the class NuxeoDriveManagerImpl method computeSynchronizationRoots.

protected Map<String, SynchronizationRoots> computeSynchronizationRoots(String query, Principal principal) {
    Map<String, SynchronizationRoots> syncRoots = new HashMap<String, SynchronizationRoots>();
    RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
    for (String repositoryName : repositoryManager.getRepositoryNames()) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            syncRoots.putAll(queryAndFetchSynchronizationRoots(session, query));
        }
    }
    return syncRoots;
}
Also used : HashMap(java.util.HashMap) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) RepositoryManager(org.nuxeo.ecm.core.api.repository.RepositoryManager)

Example 29 with CloseableCoreSession

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

the class DefaultFileSystemItemFactoryFixture method testGetFileSystemItem.

@Test
public void testGetFileSystemItem() throws Exception {
    // ------------------------------------------------------
    // Check downloadable FileSystemItems
    // ------------------------------------------------------
    // File
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(file));
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Joe.odt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    Blob fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("Joe.odt", fileItemBlob.getFilename());
    assertEquals("Content of Joe's file.", fileItemBlob.getString());
    // Note
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(note));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("aNote.txt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("aNote.txt", fileItemBlob.getFilename());
    assertEquals("Content of Bob's note.", fileItemBlob.getString());
    // Custom doc type with the "file" schema
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FileItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Bonnie's file.odt", fsItem.getName());
    assertFalse(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    fileItemBlob = ((FileItem) fsItem).getBlob();
    assertEquals("Bonnie's file.odt", fileItemBlob.getFilename());
    assertEquals("Content of Bonnie's file.", fileItemBlob.getString());
    // File without a blob => not adaptable as a FileSystemItem
    file.setPropertyValue("file:content", null);
    file = session.saveDocument(file);
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(file));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
    assertNull(fsItem);
    // Deleted file => not adaptable as a FileSystemItem
    custom.followTransition("delete");
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(custom));
    assertNull(defaultFileSystemItemFactory.getFileSystemItem(custom));
    // Deleted file with explicit "includeDeleted" => adaptable as a
    // FileSystemItem
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom, true));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom, true);
    assertNotNull(fsItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
    assertEquals("Bonnie's file.odt", fsItem.getName());
    // Version
    // Note is now automatically versioned at each save
    assertEquals("0.1", note.getVersionLabel());
    note.checkOut();
    DocumentRef versionRef = session.checkIn(note.getRef(), VersioningOption.MINOR, null);
    DocumentModel version = session.getDocument(versionRef);
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(version));
    // Proxy
    DocumentModel proxy = session.createProxy(note.getRef(), folder.getRef());
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(proxy));
    // HiddenInNavigation
    note.addFacet("HiddenInNavigation");
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(note));
    note.removeFacet("HiddenInNavigation");
    // ------------------------------------------------------
    // Check folderish FileSystemItems
    // ------------------------------------------------------
    // Folder
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folder));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(folder);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Jack's folder", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    FolderItem folderItem = (FolderItem) fsItem;
    List<FileSystemItem> children = folderItem.getChildren();
    assertNotNull(children);
    assertEquals(0, children.size());
    assertTrue(folderItem.getCanScrollDescendants());
    ScrollFileSystemItemList descendants = folderItem.scrollDescendants(null, 10, 1000);
    assertNotNull(descendants);
    assertNotNull(descendants.getScrollId());
    assertEquals(0, descendants.size());
    // FolderishFile => adaptable as a FolderItem since the default
    // FileSystemItem factory gives precedence to the Folderish facet
    assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folderishFile));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(folderishFile);
    assertNotNull(fsItem);
    assertTrue(fsItem instanceof FolderItem);
    assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folderishFile.getId(), fsItem.getId());
    assertEquals(syncRootItemId, fsItem.getParentId());
    assertEquals("Sarah's folderish file", fsItem.getName());
    assertTrue(fsItem.isFolder());
    assertEquals("Administrator", fsItem.getCreator());
    assertEquals("Administrator", fsItem.getLastContributor());
    // ------------------------------------------------------
    // Check not downloadable nor folderish
    // ------------------------------------------------------
    assertFalse(defaultFileSystemItemFactory.isFileSystemItem(notAFileSystemItem));
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(notAFileSystemItem);
    assertNull(fsItem);
    // -------------------------------------------------------------
    // Check #getFileSystemItem(DocumentModel doc, FolderItem parentItem)
    // -------------------------------------------------------------
    FolderItem syncRootSystemItem = (FolderItem) fileSystemItemAdapterService.getFileSystemItemFactoryForId(syncRootItemId).getFileSystemItemById(syncRootItemId, principal);
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, syncRootSystemItem);
    assertEquals(syncRootItemId, fsItem.getParentId());
    // Passing a null parent will force a null parentId
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, null);
    assertNull(fsItem.getParentId());
    // ------------------------------------------------------------------
    // Check FileSystemItem#getCanRename and FileSystemItem#getCanDelete
    // ------------------------------------------------------------------
    // As Administrator
    fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
    assertTrue(fsItem.getCanRename());
    assertTrue(fsItem.getCanDelete());
    // 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);
        note = joeSession.getDocument(note.getRef());
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
        assertFalse(fsItem.getCanRename());
        assertFalse(fsItem.getCanDelete());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : ScrollFileSystemItemList(org.nuxeo.drive.adapter.ScrollFileSystemItemList) FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 30 with CloseableCoreSession

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

the class DefaultFileSystemItemFactoryFixture method testPermissionCheckOptimized.

@Test
@Deploy("org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-permissions-contrib.xml")
public void testPermissionCheckOptimized() {
    setPermission(syncRootFolder, "joe", SecurityConstants.READ, true);
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        log.trace("Register the sync root for Joe's account");
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
        folder = joeSession.getDocument(folder.getRef());
        log.trace("Check canDelete/canCreateChild flags on folder for user joe with Read granted on parent folder");
        FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        assertFalse(folderItem.getCanDelete());
        assertFalse(folderItem.getCanCreateChild());
        log.trace("Check canDelete/canCreateChild flags on folder for user joe with Write granted on folder, AddChildren not granted on folder and RemoveChildren not granted on parent folder");
        setPermission(folder, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // True here as optimized => no explicit check of AddChildren on
        // folder nor RemoveChildren on parent folder
        assertTrue(folderItem.getCanDelete());
        assertTrue(folderItem.getCanCreateChild());
        log.trace("Check canDelete flag on folder for user joe with Write (thus RemoveChildren) granted on parent folder");
        setPermission(syncRootFolder, "joe", SecurityConstants.WRITE, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // Still true with RemoveChildren on the parent folder
        assertTrue(folderItem.getCanDelete());
        log.trace("Check canCreateChild flag on folder for user joe with AddChildren granted on folder");
        setPermission(folder, "joe", SecurityConstants.ADD_CHILDREN, true);
        folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
        // Still true with AddChildren on the folder
        assertTrue(folderItem.getCanCreateChild());
    }
    resetPermissions(folder, "joe");
    resetPermissions(syncRootFolder, "joe");
}
Also used : FolderItem(org.nuxeo.drive.adapter.FolderItem) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) Test(org.junit.Test) Deploy(org.nuxeo.runtime.test.runner.Deploy)

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