use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class TestDocument method testMoveNote.
/**
* Tests moving of documents created with the {@code Note} flag set.
*/
@Test
public void testMoveNote() throws Exception {
TestUtil.createAccount(USER_NAME);
TestUtil.createAccount(USER2_NAME);
String filename = NAME_PREFIX + "-testMoveNote.txt";
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER_NAME));
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER2_NAME));
// Create a note.
ZMailbox zmbx = TestUtil.getZMailbox(USER_NAME);
String folderId = Integer.toString(Mailbox.ID_FOLDER_BRIEFCASE);
ZDocument note = TestUtil.createDocument(zmbx, folderId, filename, "text/plain", "note".getBytes(), true);
String flags = Character.toString(ZItem.Flag.note.getFlagChar());
// Confirm that note flag is set.
Assert.assertEquals(flags, note.getFlags());
Mailbox remoteMbox = MailboxManager.getInstance().getMailboxByAccount(acct2);
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct);
// Clean up test data for user2.
TestUtil.deleteTestData(USER2_NAME, NAME_PREFIX);
// reset the access if there are any present.
remoteMbox.revokeAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct.getId());
mbox1.revokeAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct2.getId());
// Give write permissions on user2's Briefcase for user1 and user1's Briefcase for user2.
remoteMbox.grantAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
mbox1.grantAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct2.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
// move the note to user2
zmbx.moveItem(note.getId(), acct2.getId() + ":" + Mailbox.ID_FOLDER_BRIEFCASE, null);
ZMailbox remoteZmbx = TestUtil.getZMailbox(USER2_NAME);
String idStr = TestUtil.search(remoteZmbx, "in:briefcase " + filename, ZSearchParams.TYPE_DOCUMENT).get(0);
Document doc = remoteMbox.getDocumentById(null, Integer.parseInt(idStr));
// make sure moved document has note flag.
Assert.assertEquals(note.getFlags(), doc.getFlagString());
// move the note back to user1
remoteZmbx.moveItem(String.valueOf(doc.getId()), acct.getId() + ":" + Mailbox.ID_FOLDER_BRIEFCASE, null);
// reset the access
remoteMbox.revokeAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct.getId());
mbox1.revokeAccess(null, Mailbox.ID_FOLDER_BRIEFCASE, acct2.getId());
idStr = TestUtil.search(zmbx, "in:briefcase " + filename, ZSearchParams.TYPE_DOCUMENT).get(0);
doc = mbox1.getDocumentById(null, Integer.parseInt(idStr));
// make sure moved document has note flag.
Assert.assertEquals(note.getFlags(), doc.getFlagString());
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class TestDocument method testPublicShare.
/**
* Test REST access to publicly shared folder
* @throws Exception
*/
@Test
public void testPublicShare() throws Exception {
TestUtil.createAccount(USER_NAME);
Mailbox mbox = TestUtil.getMailbox(USER_NAME);
String folderName = NAME_PREFIX + "testPublicSharing";
Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
Folder folder = mbox.createFolder(null, "/" + folderName, fopt);
Document doc = mbox.createDocument(null, folder.getId(), "test2.txt", "text/plain", NAME_PREFIX, "testPublicSharing", new ByteArrayInputStream("A Test String".getBytes()));
mbox.grantAccess(null, folder.getId(), null, ACL.GRANTEE_PUBLIC, ACL.stringToRights("rw"), null);
String URL = TestUtil.getBaseUrl() + "/home/" + mbox.getAccount().getName() + "/" + folderName;
HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
GetMethod get = new GetMethod(URL);
int statusCode = HttpClientUtil.executeMethod(eve, get);
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String respStr = get.getResponseBodyAsString();
Assert.assertFalse("Should not contain AUTH_EXPIRED", respStr.contains("AUTH_EXPIRED"));
Assert.assertTrue("Should contain shared content ", respStr.contains("test2.txt"));
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class UserServlet method putMailItem.
public static Pair<Header[], HttpInputStream> putMailItem(ZAuthToken authToken, String url, MailItem item) throws ServiceException, IOException {
if (item instanceof Document) {
Document doc = (Document) item;
StringBuilder u = new StringBuilder(url);
u.append("?").append(QP_AUTH).append('=').append(AUTH_COOKIE);
if (doc.getType() == MailItem.Type.WIKI) {
u.append("&fmt=wiki");
}
PutMethod method = new PutMethod(u.toString());
String contentType = doc.getContentType();
method.addRequestHeader("Content-Type", contentType);
method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
method = HttpClientUtil.addInputStreamToHttpMethod(method, doc.getContentStream(), doc.getSize(), contentType);
method.addRequestHeader("X-Zimbra-Description", doc.getDescription());
method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
Pair<Header[], HttpMethod> pair = doHttpOp(authToken, method);
return new Pair<Header[], HttpInputStream>(pair.getFirst(), new HttpInputStream(pair.getSecond()));
}
return putRemoteResource(authToken, url, item.getContentStream(), null);
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class TestDocumentServer method testDeleteRevisions.
/**
* Server-side test that confirms that all blobs are cleaned up when
* a document with multiple revisions is deleted.
*/
@Test
public void testDeleteRevisions() throws Exception {
Mailbox mbox = TestUtil.getMailbox(USER_NAME);
// Create first revision.
String content = "one";
ParsedDocument pd = new ParsedDocument(new ByteArrayInputStream(content.getBytes()), NAME_PREFIX + "-testDeleteRevisions.txt", "text/plain", System.currentTimeMillis(), USER_NAME, "one", true);
Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
int docId = doc.getId();
MailItem.Type type = doc.getType();
File blobDir = getBlobDir(doc);
List<Document> revisions = mbox.getAllRevisions(null, docId, type);
Assert.assertEquals(1, revisions.size());
if (TestUtil.checkLocalBlobs()) {
Assert.assertEquals(1, getBlobCount(blobDir, docId));
}
Assert.assertEquals(true, doc.isDescriptionEnabled());
// Add a second revision.
content = "two";
pd = new ParsedDocument(new ByteArrayInputStream(content.getBytes()), NAME_PREFIX + "-testDeleteRevisions2.txt", "text/plain", System.currentTimeMillis(), USER_NAME, "two", false);
doc = mbox.addDocumentRevision(null, docId, pd);
Assert.assertEquals(2, mbox.getAllRevisions(null, docId, type).size());
if (TestUtil.checkLocalBlobs()) {
Assert.assertEquals(2, getBlobCount(blobDir, docId));
}
Assert.assertEquals(false, doc.isDescriptionEnabled());
// Move to trash, empty trash, and confirm that both blobs were deleted.
mbox.move(null, doc.getId(), doc.getType(), Mailbox.ID_FOLDER_TRASH);
mbox.emptyFolder(null, Mailbox.ID_FOLDER_TRASH, false);
mbox.emptyDumpster(null);
if (TestUtil.checkLocalBlobs()) {
Assert.assertEquals(0, getBlobCount(blobDir, docId));
}
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class BlobAccessViaMailbox method getBlobInputStream.
// BlobAccess API
@Override
public InputStream getBlobInputStream(int fileId, int version) throws ServiceException, InvalidPatchReferenceException {
if (fileId == 0 && version == 0) {
fileId = defaultFileId;
version = defaultVersion;
}
try {
Document doc = (Document) mbox.getItemRevision(opContext, fileId, MailItem.Type.DOCUMENT, version);
if (doc == null) {
throw new InvalidPatchReferenceException("Reference to non-existing version " + version + " of document " + fileId);
}
return doc.getContentStream();
} catch (NoSuchItemException e) {
throw new InvalidPatchReferenceException("Reference to non-existing document: " + fileId + " (version: " + version + ")", e);
}
}
Aggregations