Search in sources :

Example 1 with Document

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

the class ParseMimeMessageTest method staleReference.

@Test
public void staleReference() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // first, create the original draft
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
    Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
    Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
    el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    attach = el.addElement(MailConstants.E_ATTACH);
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // then, create a new draft that references one of the original draft's attachments
    attach.detach();
    (attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
    mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    // delete the draft itself and then try to save the new draft
    mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
    Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // check that the attachment's content is present and correct
    MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
    Assert.assertEquals("2 parts in draft", 2, multi.getCount());
    Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 2 with Document

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

the class ParseMimeMessageTest method attachZimbraDocument.

@Test
public void attachZimbraDocument() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
    Element el = new Element.JSONElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, "attach message");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
    Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ContentType(com.zimbra.common.mime.ContentType) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 3 with Document

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

the class PatchInputStreamTest method createNewDocOneDataNoRef.

@Test
public void createNewDocOneDataNoRef() throws Exception {
    PatchBuilder patchBuilder = new PatchBuilder();
    patchBuilder.addData("hello world");
    InputStream is = new ByteArrayInputStream(patchBuilder.toByteArray());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    PatchInputStream pis = PatchInputStream.create(is, mbox, null, 0, 0, null, null);
    Document doc = createDocument(mbox, "filename", pis);
    Assert.assertEquals("filename", doc.getName());
    Assert.assertEquals("hello world", doc.getFragment());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Test(org.junit.Test)

Example 4 with Document

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

the class Collection method createItem.

// create Document at the URI
public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException {
    Mailbox mbox = null;
    try {
        mbox = getMailbox(ctxt);
    } catch (ServiceException e) {
        throw new DavException("cannot get mailbox", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
    }
    FileUploadServlet.Upload upload = ctxt.getUpload();
    String ctype = upload.getContentType();
    if (ctype != null && (ctype.startsWith(DavProtocol.VCARD_CONTENT_TYPE) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY2))) {
        // vCard MIME types such as text/x-vcard or text/directory.
        return createVCard(ctxt, name);
    }
    String author = ctxt.getAuthAccount().getName();
    try {
        // add a revision if the resource already exists
        MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), ctxt.getPath());
        if (item.getType() != MailItem.Type.DOCUMENT && item.getType() != MailItem.Type.WIKI) {
            throw new DavException("no DAV resource for " + item.getType(), HttpServletResponse.SC_NOT_ACCEPTABLE, null);
        }
        Document doc = mbox.addDocumentRevision(ctxt.getOperationContext(), item.getId(), author, name, null, upload.getInputStream());
        return new Notebook(ctxt, doc);
    } catch (ServiceException e) {
        if (!(e instanceof NoSuchItemException))
            throw new DavException("cannot get item ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
    }
    // create
    try {
        Document doc = mbox.createDocument(ctxt.getOperationContext(), mId, name, ctype, author, null, upload.getInputStream());
        Notebook notebook = new Notebook(ctxt, doc);
        notebook.mNewlyCreated = true;
        return notebook;
    } catch (ServiceException se) {
        throw new DavException("cannot create ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, se);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavException(com.zimbra.cs.dav.DavException) Document(com.zimbra.cs.mailbox.Document) FileUploadServlet(com.zimbra.cs.service.FileUploadServlet) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)

Example 5 with Document

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

the class UrlNamespace method getResourceFromMailItem.

/* Returns DavResource for the MailItem. */
public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException {
    DavResource resource = null;
    if (item == null) {
        return resource;
    }
    MailItem.Type itemType = item.getType();
    try {
        MailItem.Type viewType;
        switch(itemType) {
            case MOUNTPOINT:
                Mountpoint mp = (Mountpoint) item;
                viewType = mp.getDefaultView();
                // don't expose mounted calendars when using iCal style delegation model.
                if (!ctxt.useIcalDelegation() && (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK)) {
                    resource = new RemoteCalendarCollection(ctxt, mp);
                } else if (viewType == MailItem.Type.CONTACT) {
                    resource = new RemoteAddressbookCollection(ctxt, mp);
                } else {
                    resource = new RemoteCollection(ctxt, mp);
                }
                break;
            case FOLDER:
                Folder f = (Folder) item;
                viewType = f.getDefaultView();
                if (f.getId() == Mailbox.ID_FOLDER_INBOX && DavResource.isSchedulingEnabled()) {
                    resource = new ScheduleInbox(ctxt, f);
                } else if (f.getId() == Mailbox.ID_FOLDER_SENT && DavResource.isSchedulingEnabled()) {
                    resource = new ScheduleOutbox(ctxt, f);
                } else if (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK) {
                    resource = getCalendarCollection(ctxt, f);
                } else if (viewType == MailItem.Type.CONTACT) {
                    resource = new AddressbookCollection(ctxt, f);
                } else {
                    resource = new Collection(ctxt, f);
                }
                break;
            case DOCUMENT:
                resource = new Notebook(ctxt, (Document) item);
                break;
            case APPOINTMENT:
            case TASK:
                resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem) item);
                break;
            case MESSAGE:
                resource = getCalendarItemForMessage(ctxt, (Message) item);
                break;
            case CONTACT:
                resource = new AddressObject(ctxt, (Contact) item);
                break;
            default:
                break;
        }
    } catch (ServiceException e) {
        ZimbraLog.dav.info("cannot create DavResource", e);
    }
    return resource;
}
Also used : Message(com.zimbra.cs.mailbox.Message) Folder(com.zimbra.cs.mailbox.Folder) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Aggregations

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