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