Search in sources :

Example 6 with NuxeoException

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

the class DefaultFileSystemItemFactory method isFileSystemItem.

/**
 * The default factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
 * <ul>
 * <li>It is not a version</li>
 * <li>AND it is not HiddenInNavigation</li>
 * <li>AND it is not in the trash, unless {@code includeDeleted} is true</li>
 * <li>AND it is Folderish or it can be adapted as a {@link BlobHolder} with a blob</li>
 * <li>AND its blob is not backed by an extended blob provider</li>
 * <li>AND it is not a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint}
 * is true</li>
 * </ul>
 */
@Override
public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
    // Check version
    if (doc.isVersion()) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is a version, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check Collections
    if (CollectionConstants.COLLECTIONS_TYPE.equals(doc.getType())) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is the collection root folder (type=%s, path=%s), it cannot be adapted as a FileSystemItem.", doc.getId(), CollectionConstants.COLLECTIONS_TYPE, doc.getPathAsString()));
        }
        return false;
    }
    // Check HiddenInNavigation
    if (doc.hasFacet("HiddenInNavigation")) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check if document is in the trash
    if (!includeDeleted && doc.isTrashed()) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is trashed, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Try to fetch blob
    Blob blob = null;
    try {
        blob = getBlob(doc);
    } catch (NuxeoException e) {
        log.error(String.format("Error while fetching blob for document %s, it cannot be adapted as a FileSystemItem.", doc.getId()), e);
        return false;
    }
    // Check Folderish or BlobHolder with a blob
    if (!doc.isFolder() && blob == null) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is not Folderish nor a BlobHolder with a blob, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check for blobs backed by extended blob providers (ex: Google Drive)
    if (!doc.isFolder()) {
        BlobManager blobManager = Framework.getService(BlobManager.class);
        BlobProvider blobProvider = blobManager.getBlobProvider(blob);
        if (blobProvider != null && (!blobProvider.supportsUserUpdate() || blobProvider.getBinaryManager() == null)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Blob for Document %s is backed by a BlobProvider preventing updates, it cannot be adapted as a FileSystemItem.", doc.getId()));
            }
            return false;
        }
    }
    if (!relaxSyncRootConstraint && doc.isFolder()) {
        // Check not a synchronization root registered for the current user
        NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class);
        Principal principal = doc.getCoreSession().getPrincipal();
        boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
        if (isSyncRoot) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Document %s is a registered synchronization root for user %s, it cannot be adapted as a DefaultFileSystemItem.", doc.getId(), principal.getName()));
            }
            return false;
        }
    }
    return true;
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobManager(org.nuxeo.ecm.core.blob.BlobManager) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) Principal(java.security.Principal) BlobProvider(org.nuxeo.ecm.core.blob.BlobProvider)

Example 7 with NuxeoException

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

the class PermissionSyncRootFactory method getParentItem.

/*------------------ AbstractSyncRootFolderItemFactory ------------------*/
@Override
protected FolderItem getParentItem(DocumentModel doc) {
    Principal principal = doc.getCoreSession().getPrincipal();
    String docCreator = (String) doc.getPropertyValue("dc:creator");
    if (principal.getName().equals(docCreator)) {
        FolderItem parent = getFileSystemAdapterService().getVirtualFolderItemFactory(userSyncRootParentFactoryName).getVirtualFolderItem(principal);
        if (parent == null) {
            throw new NuxeoException(String.format("Cannot find the parent of document %s: virtual folder from factory %s.", doc.getId(), userSyncRootParentFactoryName));
        }
        return parent;
    } else {
        FolderItem parent = getFileSystemAdapterService().getVirtualFolderItemFactory(sharedSyncRootParentFactoryName).getVirtualFolderItem(principal);
        if (parent == null) {
            throw new NuxeoException(String.format("Cannot find the parent of document %s: virtual folder from factory %s.", doc.getId(), sharedSyncRootParentFactoryName));
        }
        return parent;
    }
}
Also used : FolderItem(org.nuxeo.drive.adapter.FolderItem) DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) Principal(java.security.Principal)

Example 8 with NuxeoException

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

the class SharedSyncRootParentFactory method getVirtualFolderItem.

@Override
public FolderItem getVirtualFolderItem(Principal principal) {
    FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
    FolderItem topLevelFolder = fileSystemItemManager.getTopLevelFolder(principal);
    if (topLevelFolder == null) {
        throw new NuxeoException("Found no top level folder item. Please check your contribution to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"topLevelFolderItemFactory\">.");
    }
    return new SharedSyncRootParentFolderItem(getName(), principal, topLevelFolder.getId(), topLevelFolder.getPath(), folderName);
}
Also used : FileSystemItemManager(org.nuxeo.drive.service.FileSystemItemManager) SharedSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.SharedSyncRootParentFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) SharedSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.permission.adapter.SharedSyncRootParentFolderItem)

Example 9 with NuxeoException

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

the class UserWorkspaceSyncRootFactory method getParentItem.

/*------------------ AbstractSyncRootFolderItemFactory ------------------*/
@Override
protected FolderItem getParentItem(DocumentModel doc) {
    Principal principal = doc.getCoreSession().getPrincipal();
    FolderItem parent = getFileSystemAdapterService().getVirtualFolderItemFactory(syncRootParentFactoryName).getVirtualFolderItem(principal);
    if (parent == null) {
        throw new NuxeoException(String.format("Cannot find the parent of document %s: virtual folder from factory %s.", doc.getId(), syncRootParentFactoryName));
    }
    return parent;
}
Also used : DefaultSyncRootFolderItem(org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) Principal(java.security.Principal)

Example 10 with NuxeoException

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

the class FileSystemItemManagerImpl method getSession.

@Deprecated
@Override
public CoreSession getSession(String repositoryName, Principal principal) {
    final String sessionKey = repositoryName + "/" + principal.getName();
    CoreSession session = openedSessions.get().get(sessionKey);
    if (session == null) {
        Map<String, Serializable> context = new HashMap<String, Serializable>();
        context.put("principal", (Serializable) principal);
        final CloseableCoreSession newSession = CoreInstance.openCoreSession(repositoryName, principal);
        openedSessions.get().put(sessionKey, newSession);
        try {
            Transaction t = TransactionHelper.lookupTransactionManager().getTransaction();
            if (t == null) {
                throw new RuntimeException("FileSystemItemManagerImpl requires an active transaction.");
            }
            t.registerSynchronization(new SessionCloser(newSession, sessionKey));
        } catch (SystemException | NamingException | RollbackException e) {
            throw new NuxeoException(e);
        }
        session = newSession;
    }
    return session;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RollbackException(javax.transaction.RollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NamingException(javax.naming.NamingException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) CoreSession(org.nuxeo.ecm.core.api.CoreSession) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException)

Aggregations

NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)31 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)11 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)9 IOException (java.io.IOException)5 Blob (org.nuxeo.ecm.core.api.Blob)5 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)5 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)4 Principal (java.security.Principal)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Path (org.nuxeo.common.utils.Path)2