Search in sources :

Example 11 with ParsedDocument

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());
}
Also used : BlobReference(com.zimbra.cs.store.file.BlobReference) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) MailboxBlobInfo(com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Volume(com.zimbra.cs.volume.Volume) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 12 with ParsedDocument

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());
}
Also used : BlobReference(com.zimbra.cs.store.file.BlobReference) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) MailboxBlobInfo(com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Volume(com.zimbra.cs.volume.Volume) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 13 with ParsedDocument

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;
}
Also used : ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException)

Example 14 with ParsedDocument

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);
        }
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) UserServletException(com.zimbra.cs.service.UserServletException) PatchStore(com.zimbra.cs.octosync.store.PatchStore) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) UploadScanner(com.zimbra.cs.service.mail.UploadScanner) PatchException(com.zimbra.cs.octosync.PatchException) ServletException(javax.servlet.ServletException) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UserServletException(com.zimbra.cs.service.UserServletException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) PatchInputStream(com.zimbra.cs.octosync.PatchInputStream) PatchException(com.zimbra.cs.octosync.PatchException)

Example 15 with ParsedDocument

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);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) MailboxBlobDataSource(com.zimbra.cs.store.MailboxBlobDataSource) AddDocumentRevision(com.zimbra.cs.redolog.op.AddDocumentRevision) IOException(java.io.IOException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) IndexDocument(com.zimbra.cs.index.IndexDocument) SaveDocument(com.zimbra.cs.redolog.op.SaveDocument) StoreManager(com.zimbra.cs.store.StoreManager)

Aggregations

ParsedDocument (com.zimbra.cs.mime.ParsedDocument)18 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)7 Document (com.zimbra.cs.mailbox.Document)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 IOException (java.io.IOException)6 ServiceException (com.zimbra.common.service.ServiceException)4 IndexDocument (com.zimbra.cs.index.IndexDocument)4 MailItem (com.zimbra.cs.mailbox.MailItem)3 MailboxBlob (com.zimbra.cs.store.MailboxBlob)3 Test (org.junit.Test)3 DbMailItem (com.zimbra.cs.db.DbMailItem)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)2 SaveDocument (com.zimbra.cs.redolog.op.SaveDocument)2 UserServletException (com.zimbra.cs.service.UserServletException)2 UploadScanner (com.zimbra.cs.service.mail.UploadScanner)2 MailboxBlobInfo (com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo)2 MailboxBlobDataSource (com.zimbra.cs.store.MailboxBlobDataSource)2 StagedBlob (com.zimbra.cs.store.StagedBlob)2