Search in sources :

Example 31 with ParsedContact

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

the class ContactTest method existsInContacts.

@Test
public void existsInContacts() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    mbox.createContact(null, new ParsedContact(Collections.singletonMap(ContactConstants.A_email, "test1@zimbra.com")), Mailbox.ID_FOLDER_CONTACTS, null);
    MailboxTestUtil.index(mbox);
    Assert.assertTrue(mbox.index.existsInContacts(ImmutableList.of(new InternetAddress("Test <test1@zimbra.com>"), new InternetAddress("Test <test2@zimbra.com>"))));
    Assert.assertFalse(mbox.index.existsInContacts(ImmutableList.of(new InternetAddress("Test <test2@zimbra.com>"), new InternetAddress("Test <test3@zimbra.com>"))));
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 32 with ParsedContact

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

the class ContactTest method semiColonAndCommaInName.

/**
     * Bug 77746 Test that VCARD formatting escapes ';' and ',' chars which are part of name components
     */
@Test
public void semiColonAndCommaInName() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_lastName, "Last");
    fields.put(ContactConstants.A_firstName, "First ; SemiColon");
    fields.put(ContactConstants.A_middleName, "Middle , Comma");
    fields.put(ContactConstants.A_namePrefix, "Ms.");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    VCard vcard = VCard.formatContact(contact);
    String vcardAsString = vcard.getFormatted();
    String expectedPattern = "N:Last;First \\; SemiColon;Middle \\, Comma;Ms.;";
    String assertMsg = String.format("Vcard\n%s\nshould contain string [%s]", vcardAsString, expectedPattern);
    Assert.assertTrue(assertMsg, vcardAsString.contains(expectedPattern));
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) HashMap(java.util.HashMap) VCard(com.zimbra.cs.service.formatter.VCard) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 33 with ParsedContact

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

the class ContactTest method getAttachmentContent.

/**
     * Tests {@link Attachment#getContent()} (bug 36974).
     */
@Test
public void getAttachmentContent() throws Exception {
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", "Get Attachment Content");
    byte[] attachData = "attachment 1".getBytes();
    Attachment textAttachment = new Attachment(attachData, "text/plain", "customField", "text.txt");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    mbox.createContact(null, new ParsedContact(attrs, Lists.newArrayList(textAttachment)), Mailbox.ID_FOLDER_CONTACTS, null);
    // Call getContent() on all attachments.
    for (Contact contact : mbox.getContactList(null, Mailbox.ID_FOLDER_CONTACTS)) {
        List<Attachment> attachments = contact.getAttachments();
        for (Attachment attach : attachments) {
            attach.getContent();
        }
    }
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 34 with ParsedContact

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

the class ContactAction method handleContact.

private String handleContact(Map<String, Object> context, Element request, String operation) throws ServiceException, SoapFaultException {
    Element action = request.getElement(MailConstants.E_ACTION);
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    // figure out which items are local and which ones are remote, and proxy accordingly
    ArrayList<Integer> local = new ArrayList<Integer>();
    HashMap<String, StringBuilder> remote = new HashMap<String, StringBuilder>();
    partitionItems(zsc, action.getAttribute(MailConstants.A_ID), local, remote);
    StringBuilder successes = proxyRemoteItems(action, remote, request, context);
    if (!local.isEmpty()) {
        String localResults;
        if (operation.equals(OP_UPDATE)) {
            // duplicating code from ItemAction.java for now...
            String folderId = action.getAttribute(MailConstants.A_FOLDER, null);
            ItemId iidFolder = new ItemId(folderId == null ? "-1" : folderId, zsc);
            if (!iidFolder.belongsTo(mbox)) {
                throw ServiceException.INVALID_REQUEST("cannot move item between mailboxes", null);
            } else if (folderId != null && iidFolder.getId() <= 0) {
                throw MailServiceException.NO_SUCH_FOLDER(iidFolder.getId());
            }
            String flags = action.getAttribute(MailConstants.A_FLAGS, null);
            String[] tags = TagUtil.parseTags(action, mbox, octxt);
            Color color = getColor(action);
            ParsedContact pc = null;
            if (!action.listElements(MailConstants.E_ATTRIBUTE).isEmpty()) {
                Contact cn = local.size() == 1 ? mbox.getContactById(octxt, local.get(0)) : null;
                Pair<Map<String, Object>, List<Attachment>> cdata = CreateContact.parseContact(action, zsc, octxt, cn);
                pc = new ParsedContact(cdata.getFirst(), cdata.getSecond());
            }
            localResults = ContactActionHelper.UPDATE(zsc, octxt, mbox, local, iidFolder, flags, tags, color, pc).getResult();
        } else {
            throw ServiceException.INVALID_REQUEST("unknown operation: " + operation, null);
        }
        successes.append(successes.length() > 0 ? "," : "").append(localResults);
    }
    return successes.toString();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Color(com.zimbra.common.mailbox.Color) ArrayList(java.util.ArrayList) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with ParsedContact

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

the class ModifyContact method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    boolean replace = request.getAttributeBool(MailConstants.A_REPLACE, false);
    boolean verbose = request.getAttributeBool(MailConstants.A_VERBOSE, true);
    Element cn = request.getElement(MailConstants.E_CONTACT);
    ItemId iid = new ItemId(cn.getAttribute(MailConstants.A_ID), zsc);
    String tagsAttr = cn.getAttribute(MailConstants.A_TAG_NAMES, null);
    String[] tags = (tagsAttr == null) ? null : TagUtil.decodeTags(tagsAttr);
    Contact contact = mbox.getContactById(octxt, iid.getId());
    ParsedContact pc;
    if (replace) {
        Pair<Map<String, Object>, List<Attachment>> cdata = CreateContact.parseContact(cn, zsc, octxt, contact);
        pc = new ParsedContact(cdata.getFirst(), cdata.getSecond());
    } else {
        pc = CreateContact.parseContactMergeMode(cn, zsc, octxt, contact);
    }
    if (CreateContact.needToMigrateDlist(zsc)) {
        CreateContact.migrateFromDlist(pc);
    }
    mbox.modifyContact(octxt, iid.getId(), pc);
    if (tags != null) {
        mbox.setTags(octxt, iid.getId(), MailItem.Type.CONTACT, MailItem.FLAG_UNCHANGED, tags);
    }
    Contact con = mbox.getContactById(octxt, iid.getId());
    Element response = zsc.createElement(MailConstants.MODIFY_CONTACT_RESPONSE);
    if (con != null) {
        if (verbose)
            ToXML.encodeContact(response, ifmt, octxt, con, true, null);
        else
            response.addElement(MailConstants.E_CONTACT).addAttribute(MailConstants.A_ID, con.getId());
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ParsedContact (com.zimbra.cs.mime.ParsedContact)41 Test (org.junit.Test)24 HashMap (java.util.HashMap)23 Mailbox (com.zimbra.cs.mailbox.Mailbox)15 Contact (com.zimbra.cs.mailbox.Contact)12 OperationContext (com.zimbra.cs.mailbox.OperationContext)12 ItemId (com.zimbra.cs.service.util.ItemId)7 ServiceException (com.zimbra.common.service.ServiceException)6 Element (com.zimbra.common.soap.Element)6 ArrayList (java.util.ArrayList)6 Account (com.zimbra.cs.account.Account)5 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)5 IOException (java.io.IOException)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 InputStream (java.io.InputStream)4 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)3 Member (com.zimbra.cs.mailbox.ContactGroup.Member)3 CreateContact (com.zimbra.cs.redolog.op.CreateContact)3 ModifyContact (com.zimbra.cs.redolog.op.ModifyContact)3 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)3