use of org.nuxeo.drive.adapter.RootlessItemException in project nuxeo-drive-server by nuxeo.
the class DefaultFileSystemItemFactoryFixture method testCollectionMembership.
@Test
public void testCollectionMembership() {
DocumentModel doc = session.createDocumentModel(session.getRootDocument().getPathAsString(), "testDoc", "File");
Blob blob = new StringBlob("Content of Joe's file.");
blob.setFilename("Joe.odt");
doc.setPropertyValue("file:content", (Serializable) blob);
doc = session.createDocument(doc);
log.trace("Try to adapt a document not member of any collection");
try {
defaultFileSystemItemFactory.getFileSystemItem(doc);
fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
} catch (RootlessItemException e) {
log.trace(e);
}
log.trace("Try to adapt a document member of a non sync root collection");
DocumentModel nonSyncrootCollection = collectionManager.createCollection(session, "Non sync root collection", "", session.getRootDocument().getPathAsString());
collectionManager.addToCollection(nonSyncrootCollection, doc, session);
try {
defaultFileSystemItemFactory.getFileSystemItem(doc);
fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
} catch (RootlessItemException e) {
log.trace(e);
}
log.trace("Adapt a document member of a non sync root colllection and a sync root collection");
DocumentModel syncRootCollection = collectionManager.createCollection(session, "Sync root collection", "", session.getRootDocument().getPathAsString());
nuxeoDriveManager.registerSynchronizationRoot(principal, syncRootCollection, session);
collectionManager.addToCollection(syncRootCollection, doc, session);
FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(doc);
assertNotNull(fsItem);
log.trace("Adapt a document member of a sync root collection only");
collectionManager.removeFromCollection(nonSyncrootCollection, doc, session);
assertEquals(fsItem, defaultFileSystemItemFactory.getFileSystemItem(doc));
}
use of org.nuxeo.drive.adapter.RootlessItemException in project nuxeo-drive-server by nuxeo.
the class FileSystemItemAdapterServiceImpl method getFileSystemItem.
/*--------------------------- Protected ---------------------------------------*/
/**
* Tries to adapt the given document as the top level {@link FolderItem}. If it doesn't match, iterates on the
* ordered contributed file system item factories until it finds one that matches and retrieves a non null
* {@link FileSystemItem} for the given document. A file system item factory matches if:
* <ul>
* <li>It is not bound to any docType nor facet (this is the case for the default factory contribution
* {@code defaultFileSystemItemFactory} bound to {@link DefaultFileSystemItemFactory})</li>
* <li>It is bound to a docType that matches the given doc's type</li>
* <li>It is bound to a facet that matches one of the given doc's facets</li>
* </ul>
*/
protected FileSystemItem getFileSystemItem(DocumentModel doc, boolean forceParentItem, FolderItem parentItem, boolean includeDeleted, boolean relaxSyncRootConstraint, boolean getLockInfo) {
FileSystemItem fileSystemItem;
// Try the topLevelFolderItemFactory
if (forceParentItem) {
fileSystemItem = getTopLevelFolderItemFactory().getFileSystemItem(doc, parentItem, includeDeleted, relaxSyncRootConstraint, getLockInfo);
} else {
fileSystemItem = getTopLevelFolderItemFactory().getFileSystemItem(doc, includeDeleted, relaxSyncRootConstraint, getLockInfo);
}
if (fileSystemItem != null) {
return fileSystemItem;
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("The topLevelFolderItemFactory is not able to adapt document %s as a FileSystemItem => trying fileSystemItemFactories.", doc.getId()));
}
}
// Try the fileSystemItemFactories
FileSystemItemFactoryWrapper matchingFactory = null;
Iterator<FileSystemItemFactoryWrapper> factoriesIt = fileSystemItemFactories.iterator();
while (factoriesIt.hasNext()) {
FileSystemItemFactoryWrapper factory = factoriesIt.next();
if (log.isDebugEnabled()) {
log.debug(String.format("Trying to adapt document %s (path: %s) as a FileSystemItem with factory %s", doc.getId(), doc.getPathAsString(), factory.getFactory().getName()));
}
if (generalFactoryMatches(factory) || docTypeFactoryMatches(factory, doc) || facetFactoryMatches(factory, doc, relaxSyncRootConstraint)) {
matchingFactory = factory;
try {
if (forceParentItem) {
fileSystemItem = factory.getFactory().getFileSystemItem(doc, parentItem, includeDeleted, relaxSyncRootConstraint, getLockInfo);
} else {
fileSystemItem = factory.getFactory().getFileSystemItem(doc, includeDeleted, relaxSyncRootConstraint, getLockInfo);
}
} catch (RootlessItemException e) {
// top level item.
throw new RootlessItemException(String.format("Cannot find path to registered top" + " level when adapting document " + " '%s' (path: %s) with factory %s", doc.getTitle(), doc.getPathAsString(), factory.getFactory().getName()), e);
}
if (fileSystemItem != null) {
if (log.isDebugEnabled()) {
log.debug(String.format("Adapted document '%s' (path: %s) to item with path %s with factory %s", doc.getTitle(), doc.getPathAsString(), fileSystemItem.getPath(), factory.getFactory().getName()));
}
return fileSystemItem;
}
}
}
if (matchingFactory == null) {
if (log.isDebugEnabled()) {
log.debug(String.format("None of the fileSystemItemFactories matches document %s => returning null. Please check the contributions to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"fileSystemItemFactory\">.", doc.getId()));
}
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("None of the fileSystemItemFactories matching document %s were able to adapt this document as a FileSystemItem => returning null.", doc.getId()));
}
}
return fileSystemItem;
}
use of org.nuxeo.drive.adapter.RootlessItemException in project nuxeo-drive-server by nuxeo.
the class DefaultFileSystemItemFactoryFixture method testFolderItem.
@Test
public void testFolderItem() throws Exception {
// ------------------------------------------------------
// FolderItem#canCreateChild
// ------------------------------------------------------
// As Administrator
FolderItem folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertTrue(folderItem.getCanCreateChild());
// 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")) {
folder = joeSession.getDocument(folder.getRef());
// should not be mappable as an fs item.
try {
defaultFileSystemItemFactory.getFileSystemItem(folder);
fail("Should have raised RootlessItemException as ");
} catch (RootlessItemException e) {
// expected
}
// Register the sync root for Joe's account
nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertFalse(folderItem.getCanCreateChild());
// As a user with WRITE permission
setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
assertTrue(folderItem.getCanCreateChild());
}
resetPermissions(rootDoc, "joe");
// ------------------------------------------------------
// FolderItem#createFile and FolderItem#createFolder
// ------------------------------------------------------
folder = session.getDocument(folder.getRef());
folderItem = (FolderItem) defaultFileSystemItemFactory.getFileSystemItem(folder);
// Note
Blob childBlob = new StringBlob("This is the Note child.");
childBlob.setFilename("Note child.txt");
folderItem.createFile(childBlob);
// File
childBlob = new StringBlob("This is the File child.");
childBlob.setFilename("File child.odt");
childBlob.setMimeType("application/vnd.oasis.opendocument.text");
folderItem.createFile(childBlob);
// Folder
folderItem.createFolder("Sub-folder");
DocumentModelList children = session.query(String.format("select * from Document where ecm:parentId = '%s' order by ecm:primaryType asc", folder.getId()));
assertEquals(3, children.size());
// Check File
DocumentModel file = children.get(0);
assertEquals("File", file.getType());
assertEquals("File child.odt", file.getTitle());
childBlob = (Blob) file.getPropertyValue("file:content");
assertEquals("File child.odt", childBlob.getFilename());
assertEquals("This is the File child.", childBlob.getString());
// Check Folder
DocumentModel subFolder = children.get(1);
assertEquals("Folder", subFolder.getType());
assertEquals("Sub-folder", subFolder.getTitle());
// Check Note
DocumentModel note = children.get(2);
assertEquals("Note", note.getType());
assertEquals("Note child.txt", note.getTitle());
childBlob = note.getAdapter(BlobHolder.class).getBlob();
assertEquals("Note child.txt", childBlob.getFilename());
assertEquals("This is the Note child.", childBlob.getString());
// --------------------------------------------------------------------------------------------
// FolderItem#getChildren, FolderItem#getCanScrollDescendants and
// FolderItem#scrollDescendants
// --------------------------------------------------------------------------------------------
// Create another child adaptable as a FileSystemItem => should be
// retrieved
DocumentModel adaptableChild = session.createDocumentModel("/syncRoot/aFolder", "adaptableChild", "File");
Blob adaptableChildBlob = new StringBlob("Content of another file.");
adaptableChildBlob.setFilename("Another file.odt");
adaptableChild.setPropertyValue("file:content", (Serializable) adaptableChildBlob);
adaptableChild = session.createDocument(adaptableChild);
// Create another child not adaptable as a FileSystemItem => should
// not be retrieved
session.createDocument(session.createDocumentModel("/syncRoot/aFolder", "notAdaptableChild", "NotSynchronizable"));
session.save();
// Check getChildren
List<FileSystemItem> folderChildren = folderItem.getChildren();
assertEquals(4, folderChildren.size());
// Ordered
checkChildren(folderChildren, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), true);
// Check scrollDescendants
assertTrue(folderItem.getCanScrollDescendants());
// Scroll through all descendants in one breath
ScrollFileSystemItemList folderDescendants = folderItem.scrollDescendants(null, 10, 1000);
String scrollId = folderDescendants.getScrollId();
assertNotNull(scrollId);
assertEquals(4, folderDescendants.size());
// Order is not determined
checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
// Check that next call to scrollDescendants returns an empty list
assertTrue(folderItem.scrollDescendants(scrollId, 10, 1000).isEmpty());
// Scroll through descendants in several steps
folderDescendants.clear();
ScrollFileSystemItemList descendantsBatch;
int batchSize = 2;
scrollId = null;
while (!(descendantsBatch = folderItem.scrollDescendants(scrollId, batchSize, 1000)).isEmpty()) {
assertTrue(descendantsBatch.size() > 0);
scrollId = descendantsBatch.getScrollId();
folderDescendants.addAll(descendantsBatch);
}
assertEquals(4, folderDescendants.size());
// Order is not determined
checkChildren(folderDescendants, folder.getId(), note.getId(), file.getId(), subFolder.getId(), adaptableChild.getId(), false);
// Check batch size limit
try {
folderItem.scrollDescendants(null, 10000, 1000);
fail("Should not be able to scroll through more descendants than the maximum batch size allowed.");
} catch (NuxeoException e) {
log.trace(e);
}
}
use of org.nuxeo.drive.adapter.RootlessItemException in project nuxeo-drive-server by nuxeo.
the class DocumentBackedFolderItem method populateAncestorCache.
protected FolderItem populateAncestorCache(Map<DocumentRef, FolderItem> cache, DocumentModel doc, CoreSession session, boolean cacheItem) {
DocumentRef parentDocRef = session.getParentDocumentRef(doc.getRef());
if (parentDocRef == null) {
throw new RootlessItemException("Reached repository root");
}
FolderItem parentItem = cache.get(parentDocRef);
if (parentItem != null) {
if (log.isTraceEnabled()) {
log.trace(String.format("Found parent FolderItem in cache for doc %s: %s", doc.getPathAsString(), parentItem.getPath()));
}
return getFolderItem(cache, doc, parentItem, cacheItem);
}
if (log.isTraceEnabled()) {
log.trace(String.format("No parent FolderItem found in cache for doc %s, computing ancestor cache", doc.getPathAsString()));
}
DocumentModel parentDoc;
try {
parentDoc = session.getDocument(parentDocRef);
} catch (DocumentSecurityException e) {
throw new RootlessItemException(String.format("User %s has no READ access on parent of document %s (%s).", principal.getName(), doc.getPathAsString(), doc.getId()), e);
}
parentItem = populateAncestorCache(cache, parentDoc, session, true);
if (parentItem == null) {
return null;
}
return getFolderItem(cache, doc, parentItem, cacheItem);
}
use of org.nuxeo.drive.adapter.RootlessItemException in project nuxeo-drive-server by nuxeo.
the class AbstractDocumentBackedFileSystemItem method handleCollectionMember.
protected boolean handleCollectionMember(DocumentModel doc, CoreSession session, boolean relaxSyncRootConstraint, boolean getLockInfo) {
if (!doc.hasSchema(CollectionConstants.COLLECTION_MEMBER_SCHEMA_NAME)) {
return false;
}
CollectionManager cm = Framework.getService(CollectionManager.class);
List<DocumentModel> docCollections = cm.getVisibleCollection(doc, session);
if (docCollections.isEmpty()) {
if (log.isTraceEnabled()) {
log.trace(String.format("Doc %s (%s) is not member of any collection", doc.getPathAsString(), doc.getId()));
}
return false;
} else {
FileSystemItem parent = null;
DocumentModel collection = null;
Iterator<DocumentModel> it = docCollections.iterator();
while (it.hasNext() && parent == null) {
collection = it.next();
// as a FileSystemItem and this collection is not a synchronization root for the current user
if (collection.getPathAsString().startsWith(doc.getPathAsString() + "/") && !Framework.getService(NuxeoDriveManager.class).isSynchronizationRoot(session.getPrincipal(), collection)) {
continue;
}
try {
parent = getFileSystemItemAdapterService().getFileSystemItem(collection, true, relaxSyncRootConstraint, getLockInfo);
} catch (RootlessItemException e) {
if (log.isTraceEnabled()) {
log.trace(String.format("The collection %s (%s) of which doc %s (%s) is a member cannot be adapted as a FileSystemItem.", collection.getPathAsString(), collection.getId(), doc.getPathAsString(), doc.getId()));
}
continue;
}
}
if (parent == null) {
if (log.isTraceEnabled()) {
log.trace(String.format("None of the collections of which doc %s (%s) is a member can be adapted as a FileSystemItem.", doc.getPathAsString(), doc.getId()));
}
return false;
}
if (log.isTraceEnabled()) {
log.trace(String.format("Using first collection %s (%s) of which doc %s (%s) is a member and that is adaptable as a FileSystemItem as a parent FileSystemItem.", collection.getPathAsString(), collection.getId(), doc.getPathAsString(), doc.getId()));
}
parentId = parent.getId();
path = parent.getPath() + '/' + id;
return true;
}
}
Aggregations