Search in sources :

Example 1 with ParsedDocument

use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.

the class Mailbox method createDocument.

public Document createDocument(OperationContext octxt, int folderId, ParsedDocument pd, MailItem.Type type, int flags, MailItem parent, CustomMetadata custom, boolean indexing) throws IOException, ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = sm.stage(pd.getBlob(), this);
    SaveDocument redoRecorder = new SaveDocument(mId, pd.getDigest(), pd.getSize(), folderId, flags);
    boolean success = false;
    try {
        long start = System.currentTimeMillis();
        beginTransaction("createDoc", octxt, redoRecorder);
        SaveDocument redoPlayer = (octxt == null ? null : (SaveDocument) octxt.getPlayer());
        int itemId = getNextItemId(redoPlayer == null ? ID_AUTO_INCREMENT : redoPlayer.getMessageId());
        String uuid = redoPlayer == null ? UUIDUtil.generateUUID() : redoPlayer.getUuid();
        Document doc;
        switch(type) {
            case DOCUMENT:
                doc = Document.create(itemId, uuid, getFolderById(folderId), pd.getFilename(), pd.getContentType(), pd, custom, flags, parent);
                break;
            case WIKI:
                doc = WikiItem.create(itemId, uuid, getFolderById(folderId), pd.getFilename(), pd, null);
                break;
            default:
                throw MailServiceException.INVALID_TYPE(type.toString());
        }
        redoRecorder.setMessageId(itemId);
        redoRecorder.setUuid(doc.getUuid());
        redoRecorder.setDocument(pd);
        redoRecorder.setItemType(type);
        redoRecorder.setDescription(pd.getDescription());
        redoRecorder.setFlags(doc.getFlagBitmask());
        // 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());
        if (indexing) {
            index.add(doc);
        }
        success = true;
        long elapsed = System.currentTimeMillis() - start;
        ZimbraLog.mailbox.debug("createDocument elapsed=" + elapsed);
        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) SaveDocument(com.zimbra.cs.redolog.op.SaveDocument) IOException(java.io.IOException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) IndexDocument(com.zimbra.cs.index.IndexDocument) SaveDocument(com.zimbra.cs.redolog.op.SaveDocument) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) StoreManager(com.zimbra.cs.store.StoreManager)

Example 2 with ParsedDocument

use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.

the class Document method create.

static Document create(int id, String uuid, Folder folder, String filename, String type, ParsedDocument pd, CustomMetadata custom, int flags, MailItem parent) throws ServiceException {
    assert (id != Mailbox.ID_AUTO_INCREMENT);
    Mailbox mbox = folder.getMailbox();
    UnderlyingData data = prepareCreate(Type.DOCUMENT, id, uuid, folder, filename, type, pd, null, custom, flags);
    if (parent != null) {
        data.parentId = parent.mId;
    }
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding Document %s: id=%d, folderId=%d, folderName=%s", filename, data.id, folder.getId(), folder.getName());
    new DbMailItem(mbox).create(data);
    Document doc = new Document(mbox, data);
    doc.finishCreation(parent);
    pd.setVersion(doc.getVersion());
    return doc;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem) IndexDocument(com.zimbra.cs.index.IndexDocument) ParsedDocument(com.zimbra.cs.mime.ParsedDocument)

Example 3 with ParsedDocument

use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.

the class Document method generateIndexData.

@Override
public List<IndexDocument> generateIndexData() throws TemporaryIndexingException {
    try {
        MailboxBlob mblob = getBlob();
        if (mblob == null) {
            ZimbraLog.index.warn("Unable to fetch blob for Document id=%d,ver=%d,vol=%s", mId, mVersion, getLocator());
            throw new MailItem.TemporaryIndexingException();
        }
        ParsedDocument pd = null;
        pd = new ParsedDocument(mblob.getLocalBlob(), getName(), getContentType(), getChangeDate(), getCreator(), getDescription(), isDescriptionEnabled());
        if (pd.hasTemporaryAnalysisFailure()) {
            throw new MailItem.TemporaryIndexingException();
        }
        IndexDocument doc = pd.getDocument();
        if (doc != null) {
            List<IndexDocument> toRet = new ArrayList<IndexDocument>(1);
            toRet.add(doc);
            return toRet;
        } else {
            return new ArrayList<IndexDocument>(0);
        }
    } catch (IOException e) {
        ZimbraLog.index.warn("Error generating index data for Wiki Document " + getId() + ". Item will not be indexed", e);
        return new ArrayList<IndexDocument>(0);
    } catch (ServiceException e) {
        ZimbraLog.index.warn("Error generating index data for Wiki Document " + getId() + ". Item will not be indexed", e);
        return new ArrayList<IndexDocument>(0);
    }
}
Also used : IndexDocument(com.zimbra.cs.index.IndexDocument) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 4 with ParsedDocument

use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.

the class DocumentTest method createDocument.

static Document createDocument(Mailbox mbox, String name, String content, boolean isNote) throws Exception {
    InputStream in = new ByteArrayInputStream(content.getBytes());
    ParsedDocument pd = new ParsedDocument(in, name, "text/plain", System.currentTimeMillis(), null, null);
    int flags = (isNote ? Flag.BITMASK_NOTE : 0);
    return mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, flags);
}
Also used : ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 5 with ParsedDocument

use of com.zimbra.cs.mime.ParsedDocument in project zm-mailbox by Zimbra.

the class DocumentTest method checkName.

private static void checkName(Mailbox mbox, String name, boolean valid) throws Exception {
    Document doc = null;
    ParsedDocument pd = new ParsedDocument(new ByteArrayInputStream("test".getBytes()), name, "text/plain", System.currentTimeMillis(), null, null);
    try {
        doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
        if (!valid) {
            Assert.fail("should not have been allowed to create document: [" + name + "]");
        }
    } catch (ServiceException e) {
        Assert.assertEquals("unexpected error code", MailServiceException.INVALID_NAME, e.getCode());
        if (valid) {
            Assert.fail("should have been allowed to create document: [" + name + "]");
        }
    }
    // clean up after ourselves
    if (doc != null) {
        mbox.delete(null, doc.getId(), MailItem.Type.DOCUMENT);
    }
}
Also used : ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ServiceException(com.zimbra.common.service.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParsedDocument(com.zimbra.cs.mime.ParsedDocument)

Aggregations

ParsedDocument (com.zimbra.cs.mime.ParsedDocument)19 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 Document (com.zimbra.cs.mailbox.Document)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 ServiceException (com.zimbra.common.service.ServiceException)5 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