use of org.nuxeo.common.utils.Path in project nuxeo-drive-server by nuxeo.
the class UserWorkspaceHelper method isUserWorkspace.
public static boolean isUserWorkspace(DocumentModel doc) {
// TODO: find a better way than checking the path?
Path path = doc.getPath();
int pathLength = path.segmentCount();
return pathLength > 1 && USER_WORKSPACE_ROOT.equals(path.segment(pathLength - 2));
}
use of org.nuxeo.common.utils.Path 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);
}
use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.
the class AbstractVirtualBackend method getBackend.
@Override
public Backend getBackend(String uri) {
Path path = new Path(uri);
if (path.segmentCount() == 0) {
return this;
} else {
String key = path.segment(0);
initIfNeed();
if (backendMap == null) {
return null;
}
Backend backend = backendMap.get(key);
if (backend == null) {
return null;
}
String location = path.removeFirstSegments(1).toString();
return backend.getBackend(location);
}
}
use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.
the class SimpleBackend method cleanTrashPath.
protected boolean cleanTrashPath(DocumentModel parent, String name) {
Path checkedPath = new Path(parent.getPathAsString()).append(name);
if (getSession().exists(new PathRef(checkedPath.toString()))) {
DocumentModel model = getSession().getDocument(new PathRef(checkedPath.toString()));
if (model != null && model.isTrashed()) {
name = name + "." + System.currentTimeMillis();
getSession().move(model.getRef(), parent.getRef(), name);
return true;
}
}
return false;
}
use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.
the class SimpleBackend method resolveLocation.
@Override
public DocumentModel resolveLocation(String location) {
Path resolvedLocation = parseLocation(location);
DocumentModel doc = null;
doc = getPathCache().get(resolvedLocation.toString());
if (doc != null) {
return doc;
}
DocumentRef docRef = new PathRef(resolvedLocation.toString());
if (exists(docRef)) {
doc = getSession().getDocument(docRef);
} else {
String encodedPath = urlEncode(resolvedLocation.toString());
if (!resolvedLocation.toString().equals(encodedPath)) {
DocumentRef encodedPathRef = new PathRef(encodedPath);
if (exists(encodedPathRef)) {
doc = getSession().getDocument(encodedPathRef);
}
}
if (doc == null) {
String filename = resolvedLocation.lastSegment();
Path parentLocation = resolvedLocation.removeLastSegments(1);
// first try with spaces (for create New Folder)
String folderName = filename;
DocumentRef folderRef = new PathRef(parentLocation.append(folderName).toString());
if (exists(folderRef)) {
doc = getSession().getDocument(folderRef);
}
// look for a child
DocumentModel parentDocument = resolveParent(parentLocation.toString());
if (parentDocument == null) {
// parent doesn't exist, no use looking for a child
return null;
}
List<DocumentModel> children = getChildren(parentDocument.getRef());
for (DocumentModel child : children) {
BlobHolder bh = child.getAdapter(BlobHolder.class);
if (bh != null) {
Blob blob = bh.getBlob();
if (blob != null) {
try {
String blobFilename = blob.getFilename();
if (filename.equals(blobFilename)) {
doc = child;
break;
} else if (urlEncode(filename).equals(blobFilename)) {
doc = child;
break;
} else if (URLEncoder.encode(filename, "UTF-8").equals(blobFilename)) {
doc = child;
break;
} else if (encode(blobFilename.getBytes(), "ISO-8859-1").equals(filename)) {
doc = child;
break;
}
} catch (UnsupportedEncodingException e) {
// cannot happen for UTF-8
throw new RuntimeException(e);
}
}
}
}
}
}
getPathCache().put(resolvedLocation.toString(), doc);
return doc;
}
Aggregations