Search in sources :

Example 1 with DocumentModel

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

the class WebDavClientTest method testMoveWithRenaming.

@Test
public void testMoveWithRenaming() throws Exception {
    // create a fake bin tmp file which will finally be a docx file
    String name = "tmpfile.tmp";
    String mimeType = MimetypeRegistry.DEFAULT_MIMETYPE;
    byte[] bytes = "Fake BIN".getBytes("UTF-8");
    String expectedType = "File";
    doTestPutFile(name, bytes, mimeType, expectedType);
    PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
    assertTrue(session.exists(pathRef));
    DocumentModel doc = session.getDocument(pathRef);
    assertEquals(name, doc.getTitle());
    Blob blob = (Blob) doc.getPropertyValue("file:content");
    assertEquals(name, blob.getFilename());
    assertEquals(MimetypeRegistry.DEFAULT_MIMETYPE, blob.getMimeType());
    // rename it to a docx file
    String newName = "sample.docx";
    HttpMove request = new HttpMove(ROOT_URI + name, ROOT_URI + newName, false);
    int status;
    try (CloseableHttpResponse response = client.execute(request, context)) {
        status = response.getStatusLine().getStatusCode();
    }
    assertEquals(HttpStatus.SC_CREATED, status);
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    doc = session.getDocument(pathRef);
    assertEquals(newName, doc.getTitle());
    blob = (Blob) doc.getPropertyValue("file:content");
    assertEquals(newName, blob.getFilename());
    assertEquals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", blob.getMimeType());
}
Also used : HttpMove(org.apache.jackrabbit.webdav.client.methods.HttpMove) Blob(org.nuxeo.ecm.core.api.Blob) PathRef(org.nuxeo.ecm.core.api.PathRef) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 2 with DocumentModel

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

the class WebDavClientTest method doTestPutFile.

protected void doTestPutFile(String name, byte[] bytes, String mimeType, String expectedType) throws Exception {
    InputStream is = new ByteArrayInputStream(bytes);
    HttpPut request = new HttpPut(ROOT_URI + name);
    request.setEntity(new InputStreamEntity(is, bytes.length, ContentType.create(mimeType)));
    int status;
    try (CloseableHttpResponse response = client.execute(request, context)) {
        status = response.getStatusLine().getStatusCode();
    }
    assertEquals(HttpStatus.SC_CREATED, status);
    // check using Nuxeo Core APIs
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
    assertTrue(session.exists(pathRef));
    DocumentModel doc = session.getDocument(pathRef);
    assertEquals(expectedType, doc.getType());
    assertEquals(name, doc.getTitle());
    BlobHolder bh = doc.getAdapter(BlobHolder.class);
    assertNotNull(bh);
    Blob blob = bh.getBlob();
    assertNotNull(blob);
    assertEquals(bytes.length, blob.getLength());
    assertEquals(mimeType, blob.getMimeType());
    assertArrayEquals(bytes, blob.getByteArray());
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) PathRef(org.nuxeo.ecm.core.api.PathRef) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 3 with DocumentModel

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

the class WebDavRepoInit method populate.

@Override
public void populate(CoreSession session) {
    LogFactory.getLog(WebDavRepoInit.class).trace("enter populate webdav");
    DocumentModel ws = session.createDocumentModel("/", "workspaces", "WorkspaceRoot");
    ws.setPropertyValue("dc:title", "Workspaces");
    session.createDocument(ws);
    DocumentModel w = session.createDocumentModel("/workspaces", "workspace", "Workspace");
    w.setPropertyValue("dc:title", "Workspace");
    session.createDocument(w);
    createFile(w, "quality.jpg", "image/jpg", session);
    createFile(w, "test.html", "text/html", session);
    createFile(w, "test.txt", "text/plain", session);
    session.save();
}
Also used : DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 4 with DocumentModel

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

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

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)128 Test (org.junit.Test)58 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)26 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)25 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)24 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)22 PathRef (org.nuxeo.ecm.core.api.PathRef)21 IdRef (org.nuxeo.ecm.core.api.IdRef)18 HashSet (java.util.HashSet)17 Blob (org.nuxeo.ecm.core.api.Blob)17 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)17 ArrayList (java.util.ArrayList)16 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)11 Principal (java.security.Principal)10 ACE (org.nuxeo.ecm.core.api.security.ACE)10 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)9 FileItem (org.nuxeo.drive.adapter.FileItem)8 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)8 HashMap (java.util.HashMap)7