Search in sources :

Example 1 with PathRef

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

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

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

the class WebDavClientTest method testOverwriteExistingFile.

@Test
public // NXP-12735: disabled because failing under windows + pgsql
void testOverwriteExistingFile() throws Exception {
    // this file already exists
    String name = "test.txt";
    String mimeType = "text/plain";
    PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
    assertTrue(session.exists(pathRef));
    byte[] bytes = new byte[] { 1, 2, 3, 4, 5 };
    String expectedType = "File";
    doTestPutFile(name, bytes, mimeType, expectedType);
}
Also used : PathRef(org.nuxeo.ecm.core.api.PathRef) Test(org.junit.Test)

Example 4 with PathRef

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

use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.

@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
    // Add document to "Locally Edited" collection, creating if if not
    // exists
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
    DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
    DocumentModel locallyEditedCollection = null;
    if (session.exists(locallyEditedCollectionRef)) {
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        cm.addToCollection(locallyEditedCollection, doc, session);
    } else {
        cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
    }
    // Register "Locally Edited" collection as a synchronization root if not
    // already the case
    Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
    if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
        registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) PathRef(org.nuxeo.ecm.core.api.PathRef) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

PathRef (org.nuxeo.ecm.core.api.PathRef)26 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)21 Test (org.junit.Test)14 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)8 Path (org.nuxeo.common.utils.Path)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 Blob (org.nuxeo.ecm.core.api.Blob)4 ArrayList (java.util.ArrayList)3 Blob (org.nuxeo.ecm.automation.client.model.Blob)3 IdRef (org.nuxeo.ecm.core.api.IdRef)3 ACE (org.nuxeo.ecm.core.api.security.ACE)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 StringBlob (org.nuxeo.ecm.automation.client.model.StringBlob)2 CollectionManager (org.nuxeo.ecm.collections.api.CollectionManager)2 CoreSession (org.nuxeo.ecm.core.api.CoreSession)2 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)2 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)2 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)2 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)2