Search in sources :

Example 1 with BlobHolder

use of org.nuxeo.ecm.core.api.blobholder.BlobHolder 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 2 with BlobHolder

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

the class ExistingResource method getPropStatBuilderExt.

protected PropStatBuilderExt getPropStatBuilderExt(DocumentModel doc, UriInfo uriInfo) throws URISyntaxException {
    Date lastModified = getTimePropertyWrapper(doc, "dc:modified");
    Date creationDate = getTimePropertyWrapper(doc, "dc:created");
    String displayName = new URI(null, backend.getDisplayName(doc), null).toASCIIString();
    PropStatBuilderExt props = new PropStatBuilderExt();
    props.lastModified(lastModified).creationDate(creationDate).displayName(displayName).status(OK);
    if (doc.isFolder()) {
        props.isCollection();
    } else {
        String mimeType = "application/octet-stream";
        long size = 0;
        BlobHolder bh = doc.getAdapter(BlobHolder.class);
        if (bh != null) {
            Blob blob = bh.getBlob();
            if (blob != null) {
                size = blob.getLength();
                mimeType = blob.getMimeType();
            }
        }
        if (StringUtils.isEmpty(mimeType) || "???".equals(mimeType)) {
            mimeType = "application/octet-stream";
        }
        props.isResource(size, mimeType);
    }
    return props;
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) URI(java.net.URI) Date(java.util.Date)

Example 3 with BlobHolder

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

the class DefaultFileSystemItemFactoryFixture method testFileItem.

@Test
public void testFileItem() throws Exception {
    // ------------------------------------------------------
    // FileItem#getDownloadURL
    // ------------------------------------------------------
    FileItem fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
    String downloadURL = fileItem.getDownloadURL();
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", downloadURL);
    // ------------------------------------------------------------
    // FileItem#getDigestAlgorithm
    // ------------------------------------------------------------
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    FileItem noteItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(note);
    assertEquals("MD5", noteItem.getDigestAlgorithm());
    // ------------------------------------------------------------
    // FileItem#getDigest
    // ------------------------------------------------------------
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileItem.getDigest());
    String noteDigest = FileSystemItemHelper.getMD5Digest(note.getAdapter(BlobHolder.class).getBlob());
    assertEquals(noteDigest, noteItem.getDigest());
    assertEquals(custom.getAdapter(BlobHolder.class).getBlob().getDigest(), ((FileItem) defaultFileSystemItemFactory.getFileSystemItem(custom)).getDigest());
    // ------------------------------------------------------------
    // FileItem#getCanUpdate
    // ------------------------------------------------------------
    // As Administrator
    assertTrue(fileItem.getCanUpdate());
    // As a user with READ permission
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    // Under Oracle, the READ ACL optims are not visible from the
    // joe session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertFalse(fileItem.getCanUpdate());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertTrue(fileItem.getCanUpdate());
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        // ------------------------------------------------------
        // FileItem#getBlob
        // ------------------------------------------------------
        Blob fileItemBlob = fileItem.getBlob();
        assertEquals("Joe.odt", fileItemBlob.getFilename());
        assertEquals("Content of Joe's file.", fileItemBlob.getString());
        // Check versioning
        assertVersion("0.0", file);
        // ------------------------------------------------------
        // FileItem#setBlob and versioning
        // ------------------------------------------------------
        Blob newBlob = new StringBlob("This is a new file.");
        newBlob.setFilename("New blob.txt");
        ensureJustModified(file, session);
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        Blob updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("New blob.txt", updatedBlob.getFilename());
        assertEquals("This is a new file.", updatedBlob.getString());
        // Check versioning => should not be versioned since same
        // contributor
        // and last modification was done before the versioning delay
        assertVersion("0.0", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        newBlob.setFilename("File name modified.txt");
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.1", file);
        List<DocumentModel> fileVersions = session.getVersions(file.getRef());
        assertEquals(1, fileVersions.size());
        DocumentModel lastFileVersion = fileVersions.get(0);
        Blob versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("New blob.txt", versionedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        newBlob.setFilename("File name modified by Joe.txt");
        fileItem.setBlob(newBlob);
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.2", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(2, fileVersions.size());
        lastFileVersion = fileVersions.get(1);
        versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("File name modified.txt", versionedBlob.getFilename());
        // ------------------------------------------------------
        // DocumentBackedFileItem#rename and versioning
        // ------------------------------------------------------
        // Save document to trigger the DublinCoreListener and update
        // dc:lastContributor to "Administrator"
        // rename the file to enable dc listener (disable if not dirty)
        file.setPropertyValue("file:content/name", "newTitle");
        file = session.saveDocument(file);
        // Check versioning => should be versioned cause last contributor has changed
        assertVersion("0.3", file);
        // Switch back to Administrator as last contributor
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        ensureJustModified(file, session);
        fileItem.rename("Renamed file.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Check versioning => should not be versioned since same
        // contributor and last modification was done before the
        // versioning delay
        assertVersion("0.3", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        fileItem.rename("Renamed again.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.4", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(4, fileVersions.size());
        lastFileVersion = fileVersions.get(3);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        fileItem.rename("File renamed by Joe.txt");
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File renamed by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.5", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(5, fileVersions.size());
        lastFileVersion = fileVersions.get(4);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 4 with BlobHolder

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

the class NuxeoDriveFileSystemDeletionListener method handleBeforeDocUpdate.

protected DocumentModel handleBeforeDocUpdate(DocumentEventContext ctx, DocumentModel doc) {
    // Interested in update of a BlobHolder whose blob has been removed
    boolean blobRemoved = false;
    DocumentModel previousDoc = (DocumentModel) ctx.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
    if (previousDoc != null) {
        BlobHolder previousBh = previousDoc.getAdapter(BlobHolder.class);
        if (previousBh != null) {
            BlobHolder bh = doc.getAdapter(BlobHolder.class);
            if (bh != null) {
                blobRemoved = previousBh.getBlob() != null && bh.getBlob() == null;
            }
        }
    }
    if (blobRemoved) {
        // FileSystemItem
        return previousDoc;
    } else {
        return null;
    }
}
Also used : BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 5 with BlobHolder

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

the class DocumentBackedFileItem method rename.

/*--------------------- FileSystemItem ---------------------*/
@Override
public void rename(String name) {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        /* Update doc properties */
        DocumentModel doc = getDocument(session);
        BlobHolder bh = getBlobHolder(doc);
        Blob blob = getBlob(bh);
        blob.setFilename(name);
        bh.setBlob(blob);
        updateDocTitleIfNeeded(doc, name);
        doc.putContextData(CoreSession.SOURCE, "drive");
        doc = session.saveDocument(doc);
        session.save();
        /* Update FileSystemItem attributes */
        this.name = name;
        updateDownloadURL();
        updateLastModificationDate(doc);
    }
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)14 Blob (org.nuxeo.ecm.core.api.Blob)10 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)8 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)3 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)3 Test (org.junit.Test)2 FileItem (org.nuxeo.drive.adapter.FileItem)2 PathRef (org.nuxeo.ecm.core.api.PathRef)2 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 Principal (java.security.Principal)1 Date (java.util.Date)1 ServletRequest (javax.servlet.ServletRequest)1 GET (javax.ws.rs.GET)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPut (org.apache.http.client.methods.HttpPut)1