use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.
the class DbVolumeBlobsTest method revisionBlobs.
@Test
public void revisionBlobs() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Map<String, String> digestToPath = new HashMap<String, String>();
Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
InputStream in = new ByteArrayInputStream("testcontent".getBytes());
ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
int baseId = doc.getId();
for (int i = 0; i < 10; i++) {
in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
doc = mbox.addDocumentRevision(null, baseId, pd);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
}
Iterable<MailboxBlobInfo> allBlobs = null;
allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
for (MailboxBlobInfo info : allBlobs) {
DbVolumeBlobs.addBlobReference(conn, info);
}
List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
Assert.assertEquals(digestToPath.size(), blobs.size());
for (BlobReference blob : blobs) {
String path = digestToPath.remove(blob.getDigest());
Assert.assertNotNull(path);
Assert.assertEquals(path, getPath(blob));
}
Assert.assertTrue(digestToPath.isEmpty());
}
use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.
the class DbVolumeBlobsTest method revisionDumpsterBlobs.
@Test
public void revisionDumpsterBlobs() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Map<String, String> digestToPath = new HashMap<String, String>();
Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
InputStream in = new ByteArrayInputStream("testcontent".getBytes());
ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
int baseId = doc.getId();
for (int i = 0; i < 10; i++) {
in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
doc = mbox.addDocumentRevision(null, baseId, pd);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
}
mbox.delete(null, baseId, MailItem.Type.DOCUMENT);
mbox.emptyFolder(null, Mailbox.ID_FOLDER_TRASH, false);
Iterable<MailboxBlobInfo> allBlobs = null;
allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
for (MailboxBlobInfo info : allBlobs) {
DbVolumeBlobs.addBlobReference(conn, info);
}
List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
Assert.assertEquals(digestToPath.size(), blobs.size());
for (BlobReference blob : blobs) {
String path = digestToPath.remove(blob.getDigest());
Assert.assertNotNull(path);
Assert.assertEquals(path, getPath(blob));
}
Assert.assertTrue(digestToPath.isEmpty());
}
use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.
the class MigrateToDocuments method addRevision.
private Document addRevision(String name, Document main, Document revision, Folder to) {
InputStream in = null;
try {
in = getContentStream(revision);
String contentType = revision.getContentType();
if (revision.getType() == MailItem.Type.WIKI)
contentType = "application/x-zimbra-doc; charset=utf-8";
ParsedDocument pd = new ParsedDocument(in, name, contentType, revision.getDate(), revision.getCreator(), revision.getDescription());
if (main == null) {
main = mbox.createDocument(octxt, to.getId(), pd, MailItem.Type.DOCUMENT, revision.getFlagBitmask());
} else {
mbox.addDocumentRevision(octxt, main.getId(), pd);
}
} catch (Exception e) {
ZimbraLog.misc.warn("Can't add new revision for " + name + " revision " + revision.getVersion(), e);
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
}
}
return main;
}
use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.
the class OctopusPatchFormatter method saveCallback.
// Formatter API
@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws IOException, ServiceException, UserServletException {
log.info("Uploading patch for " + filename);
if (filename == null) {
throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "Missing filename");
}
MailItem item = null;
Mailbox mbox = folder.getMailbox();
try {
item = mbox.getItemByPath(context.opContext, filename, folder.getId());
if (!(item instanceof Document))
throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "cannot overwrite existing object at that path");
} catch (NoSuchItemException e) {
log.debug("No document found at " + filename + "(folder id=" + folder.getId() + "; will create new one");
}
PatchInputStream pis = null;
PatchStore.IncomingPatch ip = null;
try {
ip = patchStore.createIncomingPatch(context.targetAccount.getId());
int defaultFileId = 0;
int defaultVersion = 0;
if (item != null) {
defaultFileId = item.getId();
defaultVersion = item.getVersion();
}
pis = PatchInputStream.create(context.getRequestInputStream(), // the current (target) user's view
context.targetMailbox, context.opContext, defaultFileId, defaultVersion, ip.getOutputStream(), ip.getManifest());
String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
if (contentType == null) {
contentType = MimeDetect.getMimeDetect().detect(filename);
if (contentType == null)
contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
}
log.debug("Storing blob");
Blob blob = StoreManager.getInstance().storeIncoming(pis);
log.debug("Creating parsed document; filename=" + filename + ", contentType=" + contentType + ", creator=" + creator);
ParsedDocument pd = new ParsedDocument(blob, filename, contentType, System.currentTimeMillis(), creator, context.req.getHeader("X-Zimbra-Description"), true);
log.debug("Parsed document created " + filename);
// scan upload for viruses
StringBuffer info = new StringBuffer();
UploadScanner.Result result = UploadScanner.acceptStream(pis, info);
if (result == UploadScanner.REJECT)
throw MailServiceException.UPLOAD_REJECTED(filename, info.toString());
if (result == UploadScanner.ERROR)
throw MailServiceException.SCAN_ERROR(filename);
if (item == null) {
log.debug("Creating new document " + filename);
item = mbox.createDocument(context.opContext, folder.getId(), pd, MailItem.Type.DOCUMENT, 0);
} else {
log.debug("Creating new version of the document " + filename + ", current version: " + item.getVersion());
item = mbox.addDocumentRevision(context.opContext, item.getId(), pd);
}
patchStore.acceptPatch(ip, item.getId(), item.getVersion());
NativeFormatter.sendZimbraHeaders(context, context.resp, item);
} catch (PatchException e) {
log.error("Patch upload failed: " + e);
patchStore.rejectPatch(ip);
throw new UserServletException(HttpServletResponse.SC_CONFLICT, "patch cannot be applied, try uploading whole file", e);
} finally {
try {
pis.close();
} catch (Exception e) {
log.error("Exception during PatchInputStream close, ignored: " + e);
}
}
}
use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.
the class Mailbox method addDocumentRevision.
public Document addDocumentRevision(OperationContext octxt, int docId, ParsedDocument pd) throws IOException, ServiceException {
StoreManager sm = StoreManager.getInstance();
StagedBlob staged = sm.stage(pd.getBlob(), this);
AddDocumentRevision redoRecorder = new AddDocumentRevision(mId, pd.getDigest(), pd.getSize(), 0);
boolean success = false;
try {
beginTransaction("addDocumentRevision", octxt, redoRecorder);
Document doc = getDocumentById(docId);
redoRecorder.setDocument(pd);
redoRecorder.setDocId(docId);
redoRecorder.setItemType(doc.getType());
// TODO: simplify the redoRecorder by not subclassing from CreateMessage
// Get the redolog data from the mailbox blob. This is less than ideal in the
// HTTP store case because it will result in network access, and possibly an
// extra write to local disk. If this becomes a problem, we should update the
// ParsedDocument constructor to take a DataSource instead of an InputStream.
MailboxBlob mailboxBlob = doc.setContent(staged, pd);
redoRecorder.setMessageBodyInfo(new MailboxBlobDataSource(mailboxBlob), mailboxBlob.getSize());
index.add(doc);
success = true;
return doc;
} catch (IOException ioe) {
throw ServiceException.FAILURE("error writing document blob", ioe);
} finally {
endTransaction(success);
sm.quietDelete(staged);
}
}
Aggregations