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());
}
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;
}
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");
}
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;
}
}
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);
}
}
Aggregations