Search in sources :

Example 1 with Backend

use of org.nuxeo.ecm.webdav.backend.Backend 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 2 with Backend

use of org.nuxeo.ecm.webdav.backend.Backend in project nuxeo-filesystem-connectors by nuxeo.

the class RootResource method findResource.

@Path("{path:.+}")
public Object findResource(@PathParam("path") String path) {
    try {
        path = new String(path.getBytes(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new NuxeoException(e);
    }
    Backend backend = BackendHelper.getBackend(path, request);
    if (backend == null) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    if (backend.isVirtual()) {
        return new VirtualFolderResource(path, request, backend.getVirtualFolderNames());
    }
    DocumentModel doc = null;
    try {
        doc = backend.getDocument(path);
    } catch (DocumentNotFoundException e) {
        log.error("Error during resolving path: " + path, e);
        throw new WebApplicationException(Response.Status.CONFLICT);
    }
    if (doc == null) {
        return new UnknownResource(path, request, backend);
    }
    // Send 401 error if not authorised to read.
    if (!backend.hasPermission(doc.getRef(), SecurityConstants.READ)) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
    }
    if (doc.isFolder()) {
        return new FolderResource(getDocumentPath(doc), doc, request, backend);
    } else {
        return new FileResource(getDocumentPath(doc), doc, request, backend);
    }
}
Also used : Backend(org.nuxeo.ecm.webdav.backend.Backend) WebApplicationException(javax.ws.rs.WebApplicationException) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Path(javax.ws.rs.Path)

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)2 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)2 Backend (org.nuxeo.ecm.webdav.backend.Backend)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Path (org.nuxeo.common.utils.Path)1 DocumentNotFoundException (org.nuxeo.ecm.core.api.DocumentNotFoundException)1 PathRef (org.nuxeo.ecm.core.api.PathRef)1