Search in sources :

Example 1 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-filesystem-connectors by nuxeo.

the class FileResource method put.

@PUT
public Response put() {
    if (backend.isLocked(doc.getRef()) && !backend.canUnlock(doc.getRef())) {
        return Response.status(423).build();
    }
    try {
        Blob content = Blobs.createBlob(request.getInputStream());
        String contentType = request.getContentType();
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        content.setMimeType(contentType);
        content.setFilename(name);
        backend.updateDocument(doc, name, content);
        try {
            return Response.created(new URI(URLEncoder.encode(path, "UTF8"))).build();
        } catch (URISyntaxException e) {
            throw new NuxeoException(e);
        }
    } catch (IOException e) {
        log.error("Error during PUT method execution", e);
        return Response.status(409).build();
    }
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) URISyntaxException(java.net.URISyntaxException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) IOException(java.io.IOException) URI(java.net.URI) PUT(javax.ws.rs.PUT)

Example 2 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException 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 NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException 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)

Example 4 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method createFolder.

@Override
public DocumentModel createFolder(String parentPath, String name) {
    DocumentModel parent = resolveLocation(parentPath);
    if (!parent.isFolder()) {
        throw new NuxeoException("Can not create a child in a non folderish node");
    }
    String targetType = "Folder";
    if ("WorkspaceRoot".equals(parent.getType())) {
        targetType = "Workspace";
    }
    // name = cleanName(name);
    cleanTrashPath(parent, name);
    DocumentModel folder = getSession().createDocumentModel(parent.getPathAsString(), name, targetType);
    folder.setPropertyValue("dc:title", name);
    folder = getSession().createDocument(folder);
    getPathCache().put(parseLocation(parentPath) + "/" + name, folder);
    return folder;
}
Also used : NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 5 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException 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

NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)31 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)11 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)9 IOException (java.io.IOException)5 Blob (org.nuxeo.ecm.core.api.Blob)5 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)5 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)4 Principal (java.security.Principal)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Path (org.nuxeo.common.utils.Path)2