Search in sources :

Example 71 with DocumentModel

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

the class DocumentBackedFileItem method setBlob.

@Override
public void setBlob(Blob blob) {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        /* Update doc properties */
        DocumentModel doc = getDocument(session);
        // If blob's filename is empty, set it to the current name
        String blobFileName = blob.getFilename();
        if (StringUtils.isEmpty(blobFileName)) {
            blob.setFilename(name);
        } else {
            updateDocTitleIfNeeded(doc, blobFileName);
            name = blobFileName;
            updateDownloadURL();
        }
        BlobHolder bh = getBlobHolder(doc);
        bh.setBlob(blob);
        doc.putContextData(CoreSession.SOURCE, "drive");
        doc = session.saveDocument(doc);
        session.save();
        /* Update FileSystemItem attributes */
        updateLastModificationDate(doc);
        updateDigest(getBlob(doc));
    }
}
Also used : BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 72 with DocumentModel

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

the class AbstractFileSystemItemFactory method getFileSystemItemById.

@Override
public FileSystemItem getFileSystemItemById(String id, String parentId, Principal principal) {
    String[] idFragments = parseFileSystemId(id);
    String repositoryName = idFragments[1];
    String docId = idFragments[2];
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        FileSystemItem parentItem = Framework.getService(FileSystemItemAdapterService.class).getFileSystemItemFactoryForId(parentId).getFileSystemItemById(parentId, principal);
        if (!(parentItem instanceof FolderItem)) {
            throw new NuxeoException(String.format("FileSystemItem with id %s should be a FolderItem", parentId));
        }
        DocumentModel doc = getDocumentById(docId, session);
        return getFileSystemItem(doc, (FolderItem) parentItem);
    } catch (DocumentNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("No doc related to id %s, returning null.", docId));
        }
        return null;
    } catch (DocumentSecurityException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("User %s cannot access doc %s, returning null.", principal.getName(), docId));
        }
        return null;
    }
}
Also used : AbstractFileSystemItem(org.nuxeo.drive.adapter.impl.AbstractFileSystemItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 73 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel 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 74 with DocumentModel

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

the class NuxeoDriveManagerImpl method registerSynchronizationRoot.

@Override
public void registerSynchronizationRoot(Principal principal, final DocumentModel newRootContainer, CoreSession session) {
    final String userName = principal.getName();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Registering synchronization root %s for %s", newRootContainer, userName));
    }
    // If new root is child of a sync root, ignore registration, except for
    // the 'Locally Edited' collection: it is under the personal workspace
    // and we want to allow both the personal workspace and the 'Locally
    // Edited' collection to be registered as sync roots
    Map<String, SynchronizationRoots> syncRoots = getSynchronizationRoots(principal);
    SynchronizationRoots synchronizationRoots = syncRoots.get(session.getRepositoryName());
    if (!NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME.equals(newRootContainer.getName())) {
        for (String syncRootPath : synchronizationRoots.getPaths()) {
            String syncRootPrefixedPath = syncRootPath + "/";
            if (newRootContainer.getPathAsString().startsWith(syncRootPrefixedPath)) {
                // the only exception is when the right inheritance is
                // blocked
                // in the hierarchy
                boolean rightInheritanceBlockedInTheHierarchy = false;
                // should get only parents up to the sync root
                Path parentPath = newRootContainer.getPath().removeLastSegments(1);
                while (!"/".equals(parentPath.toString())) {
                    String parentPathAsString = parentPath.toString() + "/";
                    if (!parentPathAsString.startsWith(syncRootPrefixedPath)) {
                        break;
                    }
                    PathRef parentRef = new PathRef(parentPathAsString);
                    if (!session.hasPermission(principal, parentRef, SecurityConstants.READ)) {
                        rightInheritanceBlockedInTheHierarchy = true;
                        break;
                    }
                    parentPath = parentPath.removeLastSegments(1);
                }
                if (!rightInheritanceBlockedInTheHierarchy) {
                    return;
                }
            }
        }
    }
    checkCanUpdateSynchronizationRoot(newRootContainer, session);
    // Unregister any sub-folder of the new root, except for the 'Locally
    // Edited' collection
    String newRootPrefixedPath = newRootContainer.getPathAsString() + "/";
    for (String existingRootPath : synchronizationRoots.getPaths()) {
        if (!existingRootPath.endsWith(NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME)) {
            if (existingRootPath.startsWith(newRootPrefixedPath)) {
                // Unregister the nested root sub-folder first
                PathRef ref = new PathRef(existingRootPath);
                if (session.exists(ref)) {
                    DocumentModel subFolder = session.getDocument(ref);
                    unregisterSynchronizationRoot(principal, subFolder, session);
                }
            }
        }
    }
    UnrestrictedSessionRunner runner = new UnrestrictedSessionRunner(session) {

        @Override
        public void run() {
            if (!newRootContainer.hasFacet(NUXEO_DRIVE_FACET)) {
                newRootContainer.addFacet(NUXEO_DRIVE_FACET);
            }
            fireEvent(newRootContainer, session, NuxeoDriveEvents.ABOUT_TO_REGISTER_ROOT, userName);
            @SuppressWarnings("unchecked") List<Map<String, Object>> subscriptions = (List<Map<String, Object>>) newRootContainer.getPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY);
            boolean updated = false;
            for (Map<String, Object> subscription : subscriptions) {
                if (userName.equals(subscription.get("username"))) {
                    subscription.put("enabled", Boolean.TRUE);
                    subscription.put("lastChangeDate", Calendar.getInstance(UTC));
                    updated = true;
                    break;
                }
            }
            if (!updated) {
                Map<String, Object> subscription = new HashMap<String, Object>();
                subscription.put("username", userName);
                subscription.put("enabled", Boolean.TRUE);
                subscription.put("lastChangeDate", Calendar.getInstance(UTC));
                subscriptions.add(subscription);
            }
            newRootContainer.setPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY, (Serializable) subscriptions);
            newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true);
            newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true);
            newRootContainer.putContextData(CoreSession.SOURCE, "drive");
            DocumentModel savedNewRootContainer = session.saveDocument(newRootContainer);
            newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, false);
            newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, false);
            fireEvent(savedNewRootContainer, session, NuxeoDriveEvents.ROOT_REGISTERED, userName);
            session.save();
        }
    };
    runner.runUnrestricted();
    invalidateSynchronizationRootsCache(userName);
    invalidateCollectionSyncRootMemberCache(userName);
}
Also used : Path(org.nuxeo.common.utils.Path) PathRef(org.nuxeo.ecm.core.api.PathRef) HashMap(java.util.HashMap) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) UnrestrictedSessionRunner(org.nuxeo.ecm.core.api.UnrestrictedSessionRunner) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 75 with DocumentModel

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

the class NuxeoDriveManagerImpl method queryAndFetchSynchronizationRoots.

protected Map<String, SynchronizationRoots> queryAndFetchSynchronizationRoots(CoreSession session, String query) {
    Map<String, SynchronizationRoots> syncRoots = new HashMap<String, SynchronizationRoots>();
    Set<IdRef> references = new LinkedHashSet<IdRef>();
    Set<String> paths = new LinkedHashSet<String>();
    IterableQueryResult results = session.queryAndFetch(query, NXQL.NXQL);
    try {
        for (Map<String, Serializable> result : results) {
            IdRef docRef = new IdRef(result.get("ecm:uuid").toString());
            try {
                DocumentModel doc = session.getDocument(docRef);
                references.add(docRef);
                paths.add(doc.getPathAsString());
            } catch (DocumentNotFoundException e) {
                log.warn(String.format("Document %s not found, not adding it to the list of synchronization roots for user %s.", docRef, session.getPrincipal().getName()));
            } catch (DocumentSecurityException e) {
                log.warn(String.format("User %s cannot access document %s, not adding it to the list of synchronization roots.", session.getPrincipal().getName(), docRef));
            }
        }
    } finally {
        results.close();
    }
    SynchronizationRoots repoSyncRoots = new SynchronizationRoots(session.getRepositoryName(), paths, references);
    syncRoots.put(session.getRepositoryName(), repoSyncRoots);
    return syncRoots;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Serializable(java.io.Serializable) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) HashMap(java.util.HashMap) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) IterableQueryResult(org.nuxeo.ecm.core.api.IterableQueryResult)

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)128 Test (org.junit.Test)58 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)26 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)25 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)24 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)22 PathRef (org.nuxeo.ecm.core.api.PathRef)21 IdRef (org.nuxeo.ecm.core.api.IdRef)18 HashSet (java.util.HashSet)17 Blob (org.nuxeo.ecm.core.api.Blob)17 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)17 ArrayList (java.util.ArrayList)16 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)11 Principal (java.security.Principal)10 ACE (org.nuxeo.ecm.core.api.security.ACE)10 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)9 FileItem (org.nuxeo.drive.adapter.FileItem)8 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)8 HashMap (java.util.HashMap)7