Search in sources :

Example 1 with Path

use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.

the class AbstractVirtualBackend method registerSimpleBackends.

protected void registerSimpleBackends(List<DocumentModel> docs) {
    List<String> paths = new ArrayList<String>();
    for (DocumentModel doc : docs) {
        paths.add(doc.getPathAsString());
    }
    List<String> heads = new ArrayList<String>();
    for (int idx = 0; idx < paths.size(); idx++) {
        String path = paths.get(idx);
        if (isHead(path, paths, idx)) {
            heads.add(path);
        }
    }
    for (String head : heads) {
        String headName = new Path(head).lastSegment();
        String name = headName;
        int idx = 1;
        while (backendMap.containsKey(name)) {
            name = headName + "-" + idx;
            idx = idx + 1;
        }
        Backend backend = realBackendFactory.createBackend(name, head, new Path(this.rootUrl).append(name).toString(), getSession());
        registerBackend(backend);
    }
}
Also used : Path(org.nuxeo.common.utils.Path) ArrayList(java.util.ArrayList) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 2 with Path

use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.

the class ExistingResource method copyOrMove.

private Response copyOrMove(String method, @HeaderParam("Destination") String destination, @HeaderParam("Overwrite") String overwrite) {
    if (backend.isLocked(doc.getRef()) && !backend.canUnlock(doc.getRef())) {
        return Response.status(423).build();
    }
    destination = encode(destination.getBytes(), "ISO-8859-1");
    try {
        destination = new URI(destination).getPath();
    } catch (URISyntaxException e) {
        throw new NuxeoException(e);
    }
    Backend root = BackendHelper.getBackend("/", request);
    Set<String> names = new HashSet<String>(root.getVirtualFolderNames());
    Path destinationPath = new Path(destination);
    String[] segments = destinationPath.segments();
    int removeSegments = 0;
    for (String segment : segments) {
        if (names.contains(segment)) {
            break;
        } else {
            removeSegments++;
        }
    }
    destinationPath = destinationPath.removeFirstSegments(removeSegments);
    String destPath = destinationPath.toString();
    String davDestPath = destPath;
    Backend destinationBackend = BackendHelper.getBackend(davDestPath, request);
    destPath = destinationBackend.parseLocation(destPath).toString();
    log.debug("to " + davDestPath);
    // Remove dest if it exists and the Overwrite header is set to "T".
    int status = 201;
    if (destinationBackend.exists(davDestPath)) {
        if ("F".equals(overwrite)) {
            return Response.status(412).build();
        }
        destinationBackend.removeItem(davDestPath);
        status = 204;
    }
    // Check if parent exists
    String destParentPath = getParentPath(destPath);
    PathRef destParentRef = new PathRef(destParentPath);
    if (!destinationBackend.exists(getParentPath(davDestPath))) {
        return Response.status(409).build();
    }
    if ("COPY".equals(method)) {
        DocumentModel destDoc = backend.copyItem(doc, destParentRef);
        backend.renameItem(destDoc, getNameFromPath(destPath));
    } else if ("MOVE".equals(method)) {
        if (backend.isRename(doc.getPathAsString(), destPath)) {
            backend.renameItem(doc, getNameFromPath(destPath));
        } else {
            backend.moveItem(doc, destParentRef);
        }
    }
    backend.saveChanges();
    return Response.status(status).build();
}
Also used : Path(org.nuxeo.common.utils.Path) Backend(org.nuxeo.ecm.webdav.backend.Backend) PathRef(org.nuxeo.ecm.core.api.PathRef) URISyntaxException(java.net.URISyntaxException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) URI(java.net.URI) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet)

Example 3 with Path

use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method parseLocation.

@Override
public Path parseLocation(String location) {
    Path finalLocation = new Path(rootPath);
    Path rootUrlPath = new Path(rootUrl);
    Path urlLocation = new Path(location);
    Path cutLocation = urlLocation.removeFirstSegments(rootUrlPath.segmentCount());
    finalLocation = finalLocation.append(cutLocation);
    String fileName = finalLocation.lastSegment();
    String parentPath = finalLocation.removeLastSegments(1).toString();
    return new Path(parentPath).append(fileName);
}
Also used : Path(org.nuxeo.common.utils.Path)

Example 4 with Path

use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method isRename.

@Override
public boolean isRename(String source, String destination) {
    Path sourcePath = new Path(source);
    Path destinationPath = new Path(destination);
    return sourcePath.removeLastSegments(1).toString().equals(destinationPath.removeLastSegments(1).toString());
}
Also used : Path(org.nuxeo.common.utils.Path)

Example 5 with Path

use of org.nuxeo.common.utils.Path in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method updateDocument.

@Override
public DocumentModel updateDocument(DocumentModel doc, String name, Blob content) {
    FileManager fileManager = Framework.getService(FileManager.class);
    String parentPath = new Path(doc.getPathAsString()).removeLastSegments(1).toString();
    try {
        // this cannot be done before the update anymore
        // doc.putContextData(SOURCE_EDIT_KEYWORD, "webdav");
        // overwrite=true
        doc = fileManager.createDocumentFromBlob(getSession(), content, parentPath, true, name);
    } catch (IOException e) {
        throw new NuxeoException("Error while updating document", e);
    }
    return doc;
}
Also used : Path(org.nuxeo.common.utils.Path) IOException(java.io.IOException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) FileManager(org.nuxeo.ecm.platform.filemanager.api.FileManager)

Aggregations

Path (org.nuxeo.common.utils.Path)11 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)6 PathRef (org.nuxeo.ecm.core.api.PathRef)5 ArrayList (java.util.ArrayList)2 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)2 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)1 Blob (org.nuxeo.ecm.core.api.Blob)1 UnrestrictedSessionRunner (org.nuxeo.ecm.core.api.UnrestrictedSessionRunner)1 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)1 FileManager (org.nuxeo.ecm.platform.filemanager.api.FileManager)1 Backend (org.nuxeo.ecm.webdav.backend.Backend)1