Search in sources :

Example 16 with Document

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

the class CreateContact method parseAttachment.

private static Attachment parseAttachment(Element elt, String name, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
    // check for uploaded attachment
    String attachId = elt.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
    if (attachId != null) {
        if (Contact.isSMIMECertField(name)) {
            elt.setText(parseCertificate(elt, name, zsc, octxt, existing));
            return null;
        } else {
            Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
            UploadDataSource uds = new UploadDataSource(up);
            return new Attachment(new DataHandler(uds), name, (int) up.getSize());
        }
    }
    int itemId = (int) elt.getAttributeLong(MailConstants.A_ID, -1);
    String part = elt.getAttribute(MailConstants.A_PART, null);
    if (itemId != -1 || (part != null && existing != null)) {
        MailItem item = itemId == -1 ? existing : getRequestedMailbox(zsc).getItemById(octxt, itemId, MailItem.Type.UNKNOWN);
        try {
            if (item instanceof Contact) {
                Contact contact = (Contact) item;
                if (part != null && !part.equals("")) {
                    try {
                        int partNum = Integer.parseInt(part) - 1;
                        if (partNum >= 0 && partNum < contact.getAttachments().size()) {
                            Attachment att = contact.getAttachments().get(partNum);
                            return new Attachment(att.getDataHandler(), name, att.getSize());
                        }
                    } catch (NumberFormatException nfe) {
                    }
                    throw ServiceException.INVALID_REQUEST("invalid contact part number: " + part, null);
                } else {
                    VCard vcf = VCard.formatContact(contact);
                    return new Attachment(vcf.getFormatted().getBytes("utf-8"), "text/x-vcard; charset=utf-8", name, vcf.fn + ".vcf");
                }
            } else if (item instanceof Message) {
                Message msg = (Message) item;
                if (part != null && !part.equals("")) {
                    try {
                        MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
                        if (mp == null) {
                            throw MailServiceException.NO_SUCH_PART(part);
                        }
                        DataSource ds = new MimePartDataSource(mp);
                        return new Attachment(new DataHandler(ds), name);
                    } catch (MessagingException me) {
                        throw ServiceException.FAILURE("error parsing blob", me);
                    }
                } else {
                    DataSource ds = new MessageDataSource(msg);
                    return new Attachment(new DataHandler(ds), name, (int) msg.getSize());
                }
            } else if (item instanceof Document) {
                Document doc = (Document) item;
                if (part != null && !part.equals("")) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                DataSource ds = new DocumentDataSource(doc);
                return new Attachment(new DataHandler(ds), name, (int) doc.getSize());
            }
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("error attaching existing item data", ioe);
        } catch (MessagingException e) {
            throw ServiceException.FAILURE("error attaching existing item data", e);
        }
    }
    return null;
}
Also used : Message(com.zimbra.cs.mailbox.Message) MessagingException(javax.mail.MessagingException) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) MimePartDataSource(javax.mail.internet.MimePartDataSource) UploadDataSource(com.zimbra.cs.service.UploadDataSource) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) DataSource(javax.activation.DataSource) MailItem(com.zimbra.cs.mailbox.MailItem) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) MimePartDataSource(javax.mail.internet.MimePartDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) MimePart(javax.mail.internet.MimePart) UploadDataSource(com.zimbra.cs.service.UploadDataSource) VCard(com.zimbra.cs.service.formatter.VCard)

Example 17 with Document

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

the class WikiPage method findTemplatePage.

private static WikiPage findTemplatePage(WikiPage.WikiContext ctxt, String wikiAccountName, String template) throws ServiceException {
    Provisioning prov = Provisioning.getInstance();
    Account acct = prov.get(Key.AccountBy.name, wikiAccountName);
    if (acct == null)
        throw ServiceException.FAILURE("wiki account " + wikiAccountName + " does not exist, please check " + Provisioning.A_zimbraNotebookAccount + " on the domain or global config", null);
    WikiPage page = null;
    if (Provisioning.onLocalServer(acct)) {
        try {
            Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
            MailItem item = mbox.getItemByPath(ctxt.octxt, template);
            if (item instanceof Document)
                page = WikiPage.create((Document) item);
        } catch (ServiceException se) {
        }
    } else {
        page = WikiPage.create(acct.getId(), template);
        try {
            page.getContents(ctxt);
        } catch (ServiceException se) {
            page = null;
        }
    }
    return page;
}
Also used : Account(com.zimbra.cs.account.Account) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) DocServiceException(com.zimbra.cs.service.doc.DocServiceException) Document(com.zimbra.cs.mailbox.Document) Provisioning(com.zimbra.cs.account.Provisioning)

Example 18 with Document

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

the class ParseMimeMessageTest method attachPdfDocument.

@Test
public void attachPdfDocument() 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_PDF, "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_APPLICATION_PDF, 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 19 with Document

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

the class DbVolumeBlobsTest method revisionBlobs.

@Test
public void revisionBlobs() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, String> digestToPath = new HashMap<String, String>();
    Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
    InputStream in = new ByteArrayInputStream("testcontent".getBytes());
    ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
    Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
    digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
    int baseId = doc.getId();
    for (int i = 0; i < 10; i++) {
        in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
        pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
        doc = mbox.addDocumentRevision(null, baseId, pd);
        digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
    }
    Iterable<MailboxBlobInfo> allBlobs = null;
    allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
    for (MailboxBlobInfo info : allBlobs) {
        DbVolumeBlobs.addBlobReference(conn, info);
    }
    List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
    Assert.assertEquals(digestToPath.size(), blobs.size());
    for (BlobReference blob : blobs) {
        String path = digestToPath.remove(blob.getDigest());
        Assert.assertNotNull(path);
        Assert.assertEquals(path, getPath(blob));
    }
    Assert.assertTrue(digestToPath.isEmpty());
}
Also used : BlobReference(com.zimbra.cs.store.file.BlobReference) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) MailboxBlobInfo(com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Volume(com.zimbra.cs.volume.Volume) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 20 with Document

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

the class DbVolumeBlobsTest method revisionDumpsterBlobs.

@Test
public void revisionDumpsterBlobs() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, String> digestToPath = new HashMap<String, String>();
    Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
    InputStream in = new ByteArrayInputStream("testcontent".getBytes());
    ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
    Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
    digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
    int baseId = doc.getId();
    for (int i = 0; i < 10; i++) {
        in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
        pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
        doc = mbox.addDocumentRevision(null, baseId, pd);
        digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
    }
    mbox.delete(null, baseId, MailItem.Type.DOCUMENT);
    mbox.emptyFolder(null, Mailbox.ID_FOLDER_TRASH, false);
    Iterable<MailboxBlobInfo> allBlobs = null;
    allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
    for (MailboxBlobInfo info : allBlobs) {
        DbVolumeBlobs.addBlobReference(conn, info);
    }
    List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
    Assert.assertEquals(digestToPath.size(), blobs.size());
    for (BlobReference blob : blobs) {
        String path = digestToPath.remove(blob.getDigest());
        Assert.assertNotNull(path);
        Assert.assertEquals(path, getPath(blob));
    }
    Assert.assertTrue(digestToPath.isEmpty());
}
Also used : BlobReference(com.zimbra.cs.store.file.BlobReference) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) MailboxBlobInfo(com.zimbra.cs.store.MailboxBlob.MailboxBlobInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Volume(com.zimbra.cs.volume.Volume) ByteArrayInputStream(java.io.ByteArrayInputStream) 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