Search in sources :

Example 16 with Attachment

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

the class ParsedContact method analyzeContact.

private void analyzeContact(Account acct, boolean indexAttachments) throws ServiceException {
    if (indexDocs != null) {
        return;
    }
    indexDocs = new ArrayList<IndexDocument>();
    StringBuilder attachContent = new StringBuilder();
    ServiceException conversionError = null;
    if (contactAttachments != null) {
        for (Attachment attach : contactAttachments) {
            try {
                analyzeAttachment(attach, attachContent, indexAttachments);
            } catch (MimeHandlerException e) {
                String part = attach.getPartName();
                String ctype = attach.getContentType();
                ZimbraLog.index.warn("Parse error on attachment " + part + " (" + ctype + ")", e);
                if (conversionError == null && ConversionException.isTemporaryCauseOf(e)) {
                    conversionError = ServiceException.FAILURE("failed to analyze part", e.getCause());
                    mHasTemporaryAnalysisFailure = true;
                }
            } catch (ObjectHandlerException e) {
                String part = attach.getPartName();
                String ctype = attach.getContentType();
                ZimbraLog.index.warn("Parse error on attachment " + part + " (" + ctype + ")", e);
            }
        }
    }
    indexDocs.add(getPrimaryDocument(acct, attachContent.toString()));
}
Also used : IndexDocument(com.zimbra.cs.index.IndexDocument) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ObjectHandlerException(com.zimbra.cs.object.ObjectHandlerException)

Example 17 with Attachment

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

the class TestContacts method testServerAttachment.

/**
     * Tests the server-side {@link Attachment} class.
     */
@Test
public void testServerAttachment() throws Exception {
    // Specify the attachment size.
    byte[] data = "test".getBytes();
    ByteArrayDataSource ds = new ByteArrayDataSource(data, "text/plain");
    ds.setName("attachment.txt");
    DataHandler dh = new DataHandler(ds);
    Attachment attach = new Attachment(dh, "attachment", data.length);
    // Don't specify the attachment size.
    attach = new Attachment(dh, "attachment");
    checkServerAttachment(data, attach);
    // Create attachment from byte[].
    attach = new Attachment(data, "text/plain", "attachment", "attachment.txt");
    checkServerAttachment(data, attach);
}
Also used : Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 18 with Attachment

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

the class TestContacts method testMoveContact.

@Test
public void testMoveContact() throws Exception {
    ZMailbox zmbx = TestUtil.getZMailbox(USER_NAME);
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER_NAME));
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", NAME_PREFIX + " testMoveContact");
    String attachment1Text = "attachment 1";
    int timeout = (int) Constants.MILLIS_PER_MINUTE;
    String folderId = Integer.toString(Mailbox.ID_FOLDER_CONTACTS);
    String attachment1Id = zmbx.uploadAttachment("file1.txt", attachment1Text.getBytes(), "text/plain", timeout);
    Map<String, ZAttachmentInfo> attachments = new HashMap<String, ZAttachmentInfo>();
    ZAttachmentInfo info = new ZAttachmentInfo().setAttachmentId(attachment1Id);
    attachments.put("file1", info);
    ZContact contact = zmbx.createContact(folderId, null, attrs, attachments);
    //bug 80659 add an attribute after initial save so rev changes
    attrs = new HashMap<String, String>();
    attrs.put("phone", NAME_PREFIX + " testMoveContact");
    zmbx.modifyContact(contact.getId(), false, attrs);
    Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER2_NAME));
    Mailbox remoteMbox = MailboxManager.getInstance().getMailboxByAccount(acct2);
    Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct);
    remoteMbox.grantAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
    mbox1.grantAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct2.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
    // move the contact to user2
    zmbx.moveContact(contact.getId(), acct2.getId() + ":" + Mailbox.ID_FOLDER_CONTACTS);
    ZMailbox remoteZmbx = TestUtil.getZMailbox(USER2_NAME);
    String idStr = TestUtil.search(remoteZmbx, "in:Contacts testMoveContact", ZSearchParams.TYPE_CONTACT).get(0);
    Contact ct = remoteMbox.getContactById(null, Integer.parseInt(idStr));
    // make sure contact has attachment
    List<Attachment> list = ct.getAttachments();
    Assert.assertFalse(list.isEmpty());
    Attachment att = list.get(0);
    Assert.assertEquals("file1.txt", att.getFilename());
    Assert.assertEquals("text/plain", att.getContentType());
    Assert.assertEquals("attachment 1", new String(att.getContent()));
    // move the contact back to user1
    remoteZmbx.moveContact(String.valueOf(ct.getId()), acct.getId() + ":" + Mailbox.ID_FOLDER_CONTACTS);
    // reset the access
    remoteMbox.revokeAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct.getId());
    mbox1.revokeAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct2.getId());
    idStr = TestUtil.search(zmbx, "in:Contacts testMoveContact", ZSearchParams.TYPE_CONTACT).get(0);
    ct = mbox1.getContactById(null, Integer.parseInt(idStr));
    // make sure contact has attachment
    list = ct.getAttachments();
    Assert.assertFalse(list.isEmpty());
    att = list.get(0);
    Assert.assertEquals("file1.txt", att.getFilename());
    Assert.assertEquals("text/plain", att.getContentType());
    Assert.assertEquals("attachment 1", new String(att.getContent()));
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZAttachmentInfo(com.zimbra.client.ZMailbox.ZAttachmentInfo) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Aggregations

Attachment (com.zimbra.cs.mailbox.Contact.Attachment)18 HashMap (java.util.HashMap)9 ServiceException (com.zimbra.common.service.ServiceException)6 ParsedContact (com.zimbra.cs.mime.ParsedContact)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 Element (com.zimbra.common.soap.Element)4 ItemId (com.zimbra.cs.service.util.ItemId)4 ArrayList (java.util.ArrayList)4 MessagingException (javax.mail.MessagingException)4 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 DataHandler (javax.activation.DataHandler)3 MimeMessage (javax.mail.internet.MimeMessage)3 ContentDisposition (com.zimbra.common.mime.ContentDisposition)2 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)2 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)2 Contact (com.zimbra.cs.mailbox.Contact)2 Member (com.zimbra.cs.mailbox.ContactGroup.Member)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2