Search in sources :

Example 26 with Document

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());
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) ZDocument(com.zimbra.client.ZDocument) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZDocument(com.zimbra.client.ZDocument) Document(com.zimbra.cs.mailbox.Document) Test(org.junit.Test)

Example 27 with Document

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"));
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Folder(com.zimbra.cs.mailbox.Folder) ZDocument(com.zimbra.client.ZDocument) Document(com.zimbra.cs.mailbox.Document) Test(org.junit.Test)

Example 28 with Document

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);
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Document(com.zimbra.cs.mailbox.Document) HttpMethod(org.apache.commons.httpclient.HttpMethod) Pair(com.zimbra.common.util.Pair)

Example 29 with Document

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));
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) File(java.io.File) Test(org.junit.Test)

Example 30 with Document

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);
    }
}
Also used : Document(com.zimbra.cs.mailbox.Document) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)

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