Search in sources :

Example 11 with Document

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

the class DiffDocument method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element doc = request.getElement(MailConstants.E_DOC);
    String idStr = doc.getAttribute(MailConstants.A_ID, null);
    int v1 = (int) doc.getAttributeLong(MailConstants.A_V1, -1);
    int v2 = (int) doc.getAttributeLong(MailConstants.A_V2, -1);
    ItemId id = new ItemId(idStr, zsc);
    Document r1 = (Document) mbox.getItemRevision(octxt, id.getId(), MailItem.Type.UNKNOWN, v1);
    Document r2 = (Document) mbox.getItemRevision(octxt, id.getId(), MailItem.Type.UNKNOWN, v2);
    Element response = zsc.createElement(MailConstants.DIFF_DOCUMENT_RESPONSE);
    try {
        Collection<Chunk> diffResult = Diff.getResult(r1.getContentStream(), r2.getContentStream());
        for (Chunk c : diffResult) {
            Element chunk = response.addElement(MailConstants.E_CHUNK);
            chunk.addAttribute(MailConstants.A_DISP, c.disposition.toString());
            chunk.setText(StringUtil.join("\n", c.content));
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("can't diff documents", e);
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException) Document(com.zimbra.cs.mailbox.Document) Chunk(com.zimbra.cs.doc.Diff.Chunk) ItemId(com.zimbra.cs.service.util.ItemId)

Example 12 with Document

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

the class ListDocumentRevisions method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    Element doc = request.getElement(MailConstants.E_DOC);
    String id = doc.getAttribute(MailConstants.A_ID);
    int version = (int) doc.getAttributeLong(MailConstants.A_VERSION, -1);
    int count = (int) doc.getAttributeLong(MailConstants.A_COUNT, 1);
    Element response = zsc.createElement(MailConstants.LIST_DOCUMENT_REVISIONS_RESPONSE);
    Document item;
    ItemId iid = new ItemId(id, zsc);
    item = mbox.getDocumentById(octxt, iid.getId());
    if (version < 0) {
        version = item.getVersion();
    }
    MailItem.Type type = item.getType();
    HashSet<Account> accounts = new HashSet<Account>();
    Provisioning prov = Provisioning.getInstance();
    while (version > 0 && count > 0) {
        item = (Document) mbox.getItemRevision(octxt, iid.getId(), type, version);
        if (item != null) {
            ToXML.encodeDocument(response, ifmt, octxt, item);
            Account a = prov.getAccountByName(item.getCreator());
            if (a != null)
                accounts.add(a);
        }
        version--;
        count--;
    }
    for (Account a : accounts) {
        Element user = response.addElement(MailConstants.A_USER);
        user.addAttribute(MailConstants.A_ID, a.getId());
        user.addAttribute(MailConstants.A_EMAIL, a.getName());
        user.addAttribute(MailConstants.A_NAME, a.getDisplayName());
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) HashSet(java.util.HashSet)

Example 13 with Document

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

the class ToXML method encodeDocumentCommon.

public static Element encodeDocumentCommon(Element m, ItemIdFormatter ifmt, OperationContext octxt, Document doc, int fields) throws ServiceException {
    m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(doc));
    m.addAttribute(MailConstants.A_UUID, doc.getUuid());
    if (needToOutput(fields, Change.NAME)) {
        m.addAttribute(MailConstants.A_NAME, doc.getName());
    }
    if (needToOutput(fields, Change.SIZE)) {
        m.addAttribute(MailConstants.A_SIZE, doc.getSize());
    }
    if (needToOutput(fields, Change.DATE)) {
        m.addAttribute(MailConstants.A_DATE, doc.getDate());
    }
    if (needToOutput(fields, Change.FOLDER)) {
        m.addAttribute(MailConstants.A_FOLDER, ifmt.formatItemId(new ItemId(doc.getMailbox().getAccountId(), doc.getFolderId())));
        m.addAttribute(MailConstants.A_FOLDER_UUID, doc.getFolderUuid());
    }
    if (needToOutput(fields, Change.CONFLICT)) {
        m.addAttribute(MailConstants.A_MODIFIED_SEQUENCE, doc.getModifiedSequence());
        m.addAttribute(MailConstants.A_METADATA_VERSION, doc.getMetadataVersion());
        m.addAttribute(MailConstants.A_CHANGE_DATE, (doc.getChangeDate() / 1000));
        m.addAttribute(MailConstants.A_REVISION, doc.getSavedSequence());
    }
    recordItemTags(m, doc, octxt, fields | Change.FLAGS);
    if (needToOutput(fields, Change.METADATA)) {
        encodeAllCustomMetadata(m, doc, fields);
        String description = doc.getDescription();
        if (!Strings.isNullOrEmpty(description)) {
            m.addAttribute(MailConstants.A_DESC, description);
        }
        m.addAttribute(MailConstants.A_CONTENT_TYPE, doc.getContentType());
        m.addAttribute(MailConstants.A_DESC_ENABLED, doc.isDescriptionEnabled());
    }
    if (needToOutput(fields, Change.CONTENT) || needToOutput(fields, Change.NAME)) {
        try {
            m.addAttribute(MailConstants.A_VERSION, doc.getVersion());
            m.addAttribute(MailConstants.A_LAST_EDITED_BY, doc.getCreator());
            String fragment = doc.getFragment();
            if (!Strings.isNullOrEmpty(fragment)) {
                m.addAttribute(MailConstants.E_FRAG, fragment, Element.Disposition.CONTENT);
            }
            Document revision = null;
            int v = 1;
            while (revision == null && v <= doc.getVersion()) {
                revision = (Document) doc.getMailbox().getItemRevision(octxt, doc.getId(), doc.getType(), v++, doc.inDumpster());
            }
            if (revision != null) {
                m.addAttribute(MailConstants.A_CREATOR, revision.getCreator());
                m.addAttribute(MailConstants.A_CREATED_DATE, revision.getDate());
            }
        } catch (Exception e) {
            LOG.warn("ignoring exception while fetching revision for document %s", doc.getSubject(), e);
        }
    }
    // return ACLs when they are set
    if (needToOutput(fields, Change.ACL)) {
        if (fields != NOTIFY_FIELDS || doc.isTagged(Flag.FlagInfo.NO_INHERIT)) {
            encodeACL(octxt, m, doc.getEffectiveACL(), false);
        }
    }
    for (ToXMLExtension ext : extensions) {
        ext.encodeDocumentAdditionalAttribute(m, ifmt, octxt, doc, fields);
    }
    return m;
}
Also used : Document(com.zimbra.cs.mailbox.Document) ItemId(com.zimbra.cs.service.util.ItemId) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) JSONException(org.json.JSONException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with Document

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

the class TestDocument method testPurgeRevision.

/**
     * Tests deletion of blob when document revision is deleted.
     */
@Test
public void testPurgeRevision() throws Exception {
    TestUtil.createAccount(USER_NAME);
    // Create document
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
    Folder folder = mbox.createFolder(null, "/" + NAME_PREFIX + " testPurgeRevisions", fopt);
    Document doc = mbox.createDocument(null, folder.getId(), "test1.txt", "text/plain", NAME_PREFIX, "testPurgeRevisions", new ByteArrayInputStream("One".getBytes()));
    int id = doc.getId();
    File file1 = doc.getBlob().getLocalBlob().getFile();
    Assert.assertTrue(file1.exists());
    // Create revisions
    doc = mbox.addDocumentRevision(null, id, NAME_PREFIX, "test1.txt", "testPurgeRevisions", new ByteArrayInputStream("Two".getBytes()));
    int version = doc.getVersion();
    File file2 = doc.getBlob().getLocalBlob().getFile();
    Assert.assertTrue(file2.exists());
    mbox.addDocumentRevision(null, id, NAME_PREFIX, "test1.txt", "testPurgeRevisions", new ByteArrayInputStream("Three".getBytes()));
    // remove the first revision
    mbox.purgeRevision(null, id, version, false);
    Assert.assertTrue(file1.exists());
    Assert.assertFalse(file2.exists());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) Folder(com.zimbra.cs.mailbox.Folder) ZDocument(com.zimbra.client.ZDocument) Document(com.zimbra.cs.mailbox.Document) File(java.io.File) Test(org.junit.Test)

Example 15 with Document

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

the class TestDocument method testPurgeRevisions.

/**
     * Tests deletion of blobs when document revisions are deleted.
     */
@Test
public void testPurgeRevisions() throws Exception {
    TestUtil.createAccount(USER_NAME);
    // Create document
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
    Folder folder = mbox.createFolder(null, "/" + NAME_PREFIX + " testPurgeRevisions", fopt);
    Document doc = mbox.createDocument(null, folder.getId(), "test2.txt", "text/plain", NAME_PREFIX, "testPurgeRevisions", new ByteArrayInputStream("One".getBytes()));
    int id = doc.getId();
    File file1 = doc.getBlob().getLocalBlob().getFile();
    Assert.assertTrue(file1.exists());
    // Create revisions
    doc = mbox.addDocumentRevision(null, id, NAME_PREFIX, "test2.txt", "testPurgeRevisions", new ByteArrayInputStream("Two".getBytes()));
    int version = doc.getVersion();
    File file2 = doc.getBlob().getLocalBlob().getFile();
    Assert.assertTrue(file2.exists());
    mbox.addDocumentRevision(null, id, NAME_PREFIX, "test2.txt", "testPurgeRevisions", new ByteArrayInputStream("Three".getBytes()));
    // remove the first two revisions
    mbox.purgeRevision(null, id, version, true);
    Assert.assertFalse(file1.exists());
    Assert.assertFalse(file2.exists());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) Folder(com.zimbra.cs.mailbox.Folder) ZDocument(com.zimbra.client.ZDocument) Document(com.zimbra.cs.mailbox.Document) File(java.io.File) Test(org.junit.Test)

Aggregations

Document (com.zimbra.cs.mailbox.Document)34 Mailbox (com.zimbra.cs.mailbox.Mailbox)23 MailItem (com.zimbra.cs.mailbox.MailItem)14 Test (org.junit.Test)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)12 ServiceException (com.zimbra.common.service.ServiceException)9 InputStream (java.io.InputStream)9 Element (com.zimbra.common.soap.Element)8 Account (com.zimbra.cs.account.Account)8 Folder (com.zimbra.cs.mailbox.Folder)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)8 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)8 OperationContext (com.zimbra.cs.mailbox.OperationContext)8 IOException (java.io.IOException)8 Message (com.zimbra.cs.mailbox.Message)7 MimeMessage (javax.mail.internet.MimeMessage)6 ZMailbox (com.zimbra.client.ZMailbox)5 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)5 Contact (com.zimbra.cs.mailbox.Contact)5