use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-filesystem-connectors by nuxeo.
the class PathCache method get.
public DocumentModel get(String path) {
Value value = pathToUuidCache.get(path);
if (value == null) {
return null;
}
if (value.getExpiredTime() < System.currentTimeMillis()) {
pathToUuidCache.remove(path);
return null;
}
String uuid = value.getValue();
DocumentModel model = null;
try {
model = session.getDocument(new IdRef(uuid));
} catch (DocumentNotFoundException e) {
// do nothing
}
if (model == null) {
pathToUuidCache.remove(path);
}
return model;
}
use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.
@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
// Add document to "Locally Edited" collection, creating if if not
// exists
CollectionManager cm = Framework.getService(CollectionManager.class);
DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
DocumentModel locallyEditedCollection = null;
if (session.exists(locallyEditedCollectionRef)) {
locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
cm.addToCollection(locallyEditedCollection, doc, session);
} else {
cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
}
// Register "Locally Edited" collection as a synchronization root if not
// already the case
Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
}
}
use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.
the class AbstractChangeFinderTestCase method toSimpleFileSystemItemChange.
protected SimpleFileSystemItemChange toSimpleFileSystemItemChange(FileSystemItemChange change) {
SimpleFileSystemItemChange simpleChange = new SimpleFileSystemItemChange(change.getDocUuid(), change.getEventId(), change.getRepositoryId(), change.getFileSystemItemId(), change.getFileSystemItemName());
DocumentRef changeDocRef = new IdRef(change.getDocUuid());
if (session.exists(changeDocRef)) {
simpleChange.setLifeCycleState(session.getDocument(changeDocRef).getCurrentLifeCycleState());
}
return simpleChange;
}
use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.
the class AuditChangeFinderTestSuite method testSyncUnsyncRootsAsAnotherUser.
@Test
public void testSyncUnsyncRootsAsAnotherUser() throws Exception {
Principal user1Principal = user1Session.getPrincipal();
List<FileSystemItemChange> changes;
try {
// No sync roots expected for user1
changes = getChanges(user1Principal);
assertNotNull(changes);
assertTrue(changes.isEmpty());
// Register sync roots for user1 as Administrator
nuxeoDriveManager.registerSynchronizationRoot(user1Principal, folder1, session);
nuxeoDriveManager.registerSynchronizationRoot(user1Principal, folder2, session);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// user1 should have 2 sync roots
Set<IdRef> activeRootRefs = nuxeoDriveManager.getSynchronizationRootReferences(user1Session);
assertNotNull(activeRootRefs);
assertEquals(2, activeRootRefs.size());
assertTrue(activeRootRefs.contains(folder1.getRef()));
assertTrue(activeRootRefs.contains(folder2.getRef()));
// There should be 2 changes detected in the audit
changes = getChanges(user1Principal);
assertEquals(2, changes.size());
Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder2.getId(), "folder2"));
expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered", "test", "defaultSyncRootFolderItemFactory#test#" + folder1.getId(), "folder1"));
assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
for (FileSystemItemChange change : changes) {
assertNotNull(change.getFileSystemItem());
}
// Unregister sync roots for user1 as Administrator
nuxeoDriveManager.unregisterSynchronizationRoot(user1Principal, folder1, session);
nuxeoDriveManager.unregisterSynchronizationRoot(user1Principal, folder2, session);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// user1 should have no sync roots
Set<IdRef> activeRootRefs = nuxeoDriveManager.getSynchronizationRootReferences(user1Session);
assertNotNull(activeRootRefs);
assertTrue(activeRootRefs.isEmpty());
// There should be 2 changes detected in the audit
changes = getChanges(user1Principal);
assertEquals(2, changes.size());
Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "deleted", "test", "test#" + folder2.getId(), "folder2"));
expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "deleted", "test", "test#" + folder1.getId(), "folder1"));
assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
// Not adaptable as a FileSystemItem since unregistered
for (FileSystemItemChange change : changes) {
assertNull(change.getFileSystemItem());
}
} finally {
commitAndWaitForAsyncCompletion();
}
}
use of org.nuxeo.ecm.core.api.IdRef in project nuxeo-drive-server by nuxeo.
the class AuditChangeFinder method getFileSystemChanges.
protected List<FileSystemItemChange> getFileSystemChanges(CoreSession session, Set<IdRef> lastActiveRootRefs, SynchronizationRoots activeRoots, Set<String> collectionSyncRootMemberIds, long lowerBound, long upperBound, boolean integerBounds, int limit) throws TooManyChangesException {
String principalName = session.getPrincipal().getName();
List<FileSystemItemChange> changes = new ArrayList<FileSystemItemChange>();
// Note: lastActiveRootRefs is not used: we could remove it from the
// public API
// and from the client as well but it might be useful to optimize future
// alternative implementations FileSystemChangeFinder component so it
// might
// be better to leave it part of the public API as currently.
// Find changes from the log under active roots or events that are
// linked to the un-registration or deletion of formerly synchronized
// roots
List<LogEntry> entries = queryAuditEntries(session, activeRoots, collectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
// query with the actual active roots.
for (LogEntry entry : entries) {
if (NuxeoDriveEvents.EVENT_CATEGORY.equals(entry.getCategory())) {
if (log.isDebugEnabled()) {
log.debug(String.format("Detected sync root change for user '%s' in audit log:" + " invalidating the root cache and refetching the changes.", principalName));
}
NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
driveManager.invalidateSynchronizationRootsCache(principalName);
driveManager.invalidateCollectionSyncRootMemberCache(principalName);
Map<String, SynchronizationRoots> synchronizationRoots = driveManager.getSynchronizationRoots(session.getPrincipal());
SynchronizationRoots updatedActiveRoots = synchronizationRoots.get(session.getRepositoryName());
Set<String> updatedCollectionSyncRootMemberIds = driveManager.getCollectionSyncRootMemberIds(session.getPrincipal()).get(session.getRepositoryName());
entries = queryAuditEntries(session, updatedActiveRoots, updatedCollectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
break;
}
}
if (entries.size() >= limit) {
throw new TooManyChangesException("Too many changes found in the audit logs.");
}
for (LogEntry entry : entries) {
if (log.isDebugEnabled()) {
log.debug(String.format("Handling log entry %s", entry));
}
FileSystemItemChange change = null;
DocumentRef docRef = new IdRef(entry.getDocUUID());
ExtendedInfo fsIdInfo = entry.getExtendedInfos().get("fileSystemItemId");
if (fsIdInfo != null) {
// been updated, we just know the FileSystemItem id and name.
if (log.isDebugEnabled()) {
log.debug(String.format("Found extended info in audit log entry: document has been deleted, moved," + " is an unregistered synchronization root or its security has been updated," + " we just know the FileSystemItem id and name."));
}
boolean isChangeSet = false;
// current user still has access to the document.
if (!"deleted".equals(entry.getEventId()) && session.exists(docRef)) {
change = getFileSystemItemChange(session, docRef, entry, fsIdInfo.getValue(String.class));
if (change != null) {
if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
// virtual event.
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) has been moved to another synchronzation root, not adding entry to the change summary.", entry.getDocPath(), docRef));
}
continue;
}
isChangeSet = true;
}
}
if (!isChangeSet) {
// FileSystemItem id and name to the FileSystemItemChange entry.
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) doesn't exist or is not adaptable as a FileSystemItem, only providing the FileSystemItem id and name to the FileSystemItemChange entry.", entry.getDocPath(), docRef));
}
String fsId = fsIdInfo.getValue(String.class);
String eventId;
if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
// Move to a non synchronization root
eventId = NuxeoDriveEvents.DELETED_EVENT;
} else {
// Deletion, unregistration or security update
eventId = entry.getEventId();
}
change = new FileSystemItemChangeImpl(eventId, entry.getEventDate().getTime(), entry.getRepositoryId(), entry.getDocUUID(), fsId, null);
}
if (log.isDebugEnabled()) {
log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
}
changes.add(change);
} else {
// unregistered synchronization root nor a security update denying access to the current user.
if (log.isDebugEnabled()) {
log.debug(String.format("No extended info found in audit log entry %s (%s): this is not a deleted document, a moved document," + " an unregistered synchronization root nor a security update denying access to the current user.", entry.getDocPath(), docRef));
}
if (!session.exists(docRef)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) doesn't exist, not adding entry to the change summary.", entry.getDocPath(), docRef));
}
// try to propagate this event.
continue;
}
// Let's try to adapt the document as a FileSystemItem to
// provide it to the FileSystemItemChange entry.
change = getFileSystemItemChange(session, docRef, entry, null);
if (change == null) {
// Non-adaptable documents are ignored
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) is not adaptable as a FileSystemItem, not adding any entry to the change summary.", entry.getDocPath(), docRef));
}
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
}
changes.add(change);
}
}
}
return changes;
}
Aggregations