Search in sources :

Example 46 with Contact

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

the class ContactQueryTest method wildcard.

@Test
public void wildcard() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "First*");
    fields.put(ContactConstants.A_lastName, "Las*t");
    fields.put(ContactConstants.A_email, "first.last@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"First\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"First*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*t\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 47 with Contact

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

the class ZimbraQueryTest method quick.

@Test
public void quick() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = mbox.createContact(null, new ParsedContact(Collections.singletonMap(ContactConstants.A_email, "test1@zimbra.com")), Mailbox.ID_FOLDER_CONTACTS, null);
    MailboxTestUtil.index(mbox);
    mbox.createContact(null, new ParsedContact(Collections.singletonMap(ContactConstants.A_email, "test2@zimbra.com")), Mailbox.ID_FOLDER_CONTACTS, null);
    SearchParams params = new SearchParams();
    params.setQueryString("test");
    params.setSortBy(SortBy.NONE);
    params.setTypes(EnumSet.of(MailItem.Type.CONTACT));
    params.setQuick(true);
    ZimbraQuery query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
    ZimbraQueryResults result = query.execute();
    Assert.assertTrue("Expected at least 1 result", result.hasNext());
    Assert.assertEquals("Result item ID not as expected", contact.getId(), result.getNext().getItemId());
    Assert.assertFalse("More hits than expected", result.hasNext());
    IOUtil.closeQuietly(result);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 48 with Contact

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

the class GetContacts 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);
    Account mRequestedAcct = getRequestedAccount(zsc);
    Account mAuthedAcct = getAuthenticatedAccount(zsc);
    GetContactsRequest req = zsc.elementToJaxb(request);
    boolean sync = req.getSync() == null ? false : req.getSync();
    boolean derefContactGroupMember = req.getDerefGroupMember();
    String folderIdStr = req.getFolderId();
    int folderId = ALL_FOLDERS;
    if (folderIdStr != null) {
        ItemId iidFolder = new ItemId(folderIdStr, zsc);
        if (iidFolder.belongsTo(mbox))
            folderId = iidFolder.getId();
        else
            throw ServiceException.FAILURE("Got remote folderId: " + folderIdStr + " but did not proxy", null);
    }
    SortBy sort = SortBy.of(req.getSortBy());
    if (sort == null) {
        sort = SortBy.NONE;
    }
    ArrayList<String> attrs = null;
    ArrayList<String> memberAttrs = null;
    ArrayList<ItemId> ids = null;
    // MailConstants.A_ATTRIBUTE_NAME
    List<AttributeName> reqAttrs = req.getAttributes();
    if (reqAttrs != null && reqAttrs.size() > 0) {
        attrs = new ArrayList<String>();
        for (AttributeName attrName : reqAttrs) {
            attrs.add(attrName.getName());
        }
    }
    // MailConstants.E_CONTACT_GROUP_MEMBER_ATTRIBUTE
    reqAttrs = req.getMemberAttributes();
    if (reqAttrs != null && reqAttrs.size() > 0) {
        memberAttrs = new ArrayList<String>();
        for (AttributeName attrName : reqAttrs) {
            memberAttrs.add(attrName.getName());
        }
    }
    // MailConstants.E_CONTACT
    List<Id> contactIds = req.getContacts();
    if (contactIds != null && contactIds.size() > 0) {
        ids = new ArrayList<ItemId>();
        for (Id target : contactIds) {
            String idStr = target.getId();
            if (idStr.indexOf(",") > 0) {
                // comma-separated IDs. TODO: deprecate this use-case
                String[] toks = idStr.split(",");
                for (int i = 0; i < toks.length; i++) {
                    ids.add(new ItemId(toks[i], zsc));
                }
            } else {
                ids.add(new ItemId(idStr, zsc));
            }
        }
    }
    long maxMembers = DEFAULT_MAX_MEMBERS;
    boolean returnHiddenAttrs = false;
    if (attrs == null) {
        returnHiddenAttrs = req.getReturnHiddenAttrs();
        maxMembers = (req.getMaxMembers() == null) ? DEFAULT_MAX_MEMBERS : req.getMaxMembers();
    }
    boolean returnCertInfo = req.getReturnCertInfo();
    Map<String, Set<String>> memberOfMap = null;
    if (req.getIncludeMemberOf()) {
        memberOfMap = ContactMemberOfMap.getMemberOfMap(mbox, octxt);
    }
    Element response = zsc.createElement(MailConstants.GET_CONTACTS_RESPONSE);
    // want to return modified date only on sync-related requests
    int fields = ToXML.NOTIFY_FIELDS;
    if (sync) {
        fields |= Change.CONFLICT;
    }
    if (req.getWantImapUid()) {
        fields |= Change.IMAP_UID;
    }
    // for perf reason, derefContactGroupMember is not supported in this mode
    if (derefContactGroupMember) {
        if (ids == null) {
            throw ServiceException.INVALID_REQUEST(MailConstants.A_DEREF_CONTACT_GROUP_MEMBER + " is supported only when specific contact ids are specified", null);
        }
    }
    if (ids != null) {
        ArrayList<Integer> local = new ArrayList<Integer>();
        HashMap<String, StringBuffer> remote = new HashMap<String, StringBuffer>();
        partitionItems(zsc, ids, local, remote);
        if (remote.size() > 0) {
            if (folderId > 0)
                throw ServiceException.INVALID_REQUEST("Cannot specify a folder with mixed-mailbox items", null);
            List<Element> responses = proxyRemote(request, remote, context);
            for (Element e : responses) response.addNonUniqueElement(e);
        }
        if (local.size() > 0) {
            boolean migrateDlist = CreateContact.needToMigrateDlist(zsc);
            for (int id : local) {
                Contact con = mbox.getContactById(octxt, id);
                if (con != null && (folderId == ALL_FOLDERS || folderId == con.getFolderId())) {
                    ContactGroup contactGroup = null;
                    String migratedDlist = null;
                    if (migrateDlist) {
                        ContactGroup cg = ContactGroup.init(con, false);
                        if (cg != null) {
                            migratedDlist = cg.migrateToDlist(con.getMailbox(), octxt);
                        }
                    } else if (derefContactGroupMember) {
                        contactGroup = ContactGroup.init(con, false);
                        if (contactGroup != null) {
                            contactGroup.derefAllMembers(con.getMailbox(), octxt, zsc.getResponseProtocol());
                        }
                    }
                    ToXML.encodeContact(response, ifmt, octxt, con, contactGroup, memberAttrs, false, /* summary */
                    attrs, fields, migratedDlist, returnHiddenAttrs, maxMembers, returnCertInfo, ContactMemberOfMap.setOfMemberOf(zsc.getRequestedAccountId(), id, memberOfMap), mRequestedAcct, mAuthedAcct);
                }
            }
        }
    } else {
        for (Contact con : mbox.getContactList(octxt, folderId, sort)) {
            if (con != null) {
                ToXML.encodeContact(response, ifmt, octxt, con, null, null, false, /* summary */
                attrs, fields, null, returnHiddenAttrs, maxMembers, returnCertInfo, ContactMemberOfMap.setOfMemberOf(zsc.getRequestedAccountId(), con.getId(), memberOfMap));
            }
        }
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Set(java.util.Set) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) SortBy(com.zimbra.cs.index.SortBy) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) ItemId(com.zimbra.cs.service.util.ItemId) GetContactsRequest(com.zimbra.soap.mail.message.GetContactsRequest) Mailbox(com.zimbra.cs.mailbox.Mailbox) AttributeName(com.zimbra.soap.type.AttributeName) OperationContext(com.zimbra.cs.mailbox.OperationContext) Contact(com.zimbra.cs.mailbox.Contact) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Id(com.zimbra.soap.type.Id) ItemId(com.zimbra.cs.service.util.ItemId) ContactGroup(com.zimbra.cs.mailbox.ContactGroup)

Example 49 with Contact

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

the class TestZClient method testImapUIDForNonStandardContact.

@Test
public void testImapUIDForNonStandardContact() throws ServiceException {
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    Contact contact = TestUtil.createContactInDefaultFolder(mbox, "testzclient@example.com");
    ItemIdentifier contactId = ItemIdentifier.fromAccountIdAndItemId(zmbox.getAccountId(), contact.getId());
    Element reqEl = zmbox.newRequestElement(MailConstants.GET_CONTACTS_REQUEST);
    // Specifically do NOT do reqEl.addAttribute(MailConstants.A_WANT_IMAP_UID, true);
    reqEl.addNonUniqueElement(MailConstants.E_CONTACT).addAttribute(MailConstants.A_ID, contactId.id);
    Element respEl = zmbox.invoke(reqEl);
    ZContact zcontact = new ZContact(respEl.getElement(MailConstants.E_CONTACT), zmbox);
    assertNotNull("ZContact object", zcontact);
    // The IMAP UID should be gotten via a refetch of the contact - asking for the IMAP UID
    assertEquals("Expected=ZContact ID Actual=ZContact IMAP UID", zcontact.getIdInMailbox(), zcontact.getImapUid());
    assertEquals("IMAP UID Expected=Contact Actual=ZContact", contact.getImapUid(), zcontact.getImapUid());
    assertEquals("ModifiedSequence Expected=Contact Actual=ZContact", contact.getModifiedSequence(), zcontact.getModifiedSequence());
}
Also used : ItemIdentifier(com.zimbra.common.mailbox.ItemIdentifier) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) Element(com.zimbra.common.soap.Element) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Example 50 with Contact

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

the class TestZClient method testZMailboxGetItemById.

@Test
public void testZMailboxGetItemById() throws Exception {
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    Integer id = Integer.valueOf(TestUtil.addMessage(zmbox, "testGetItemById test msg"));
    ItemIdentifier msgItemId = ItemIdentifier.fromAccountIdAndItemId(zmbox.getAccountId(), id);
    Contact contact = TestUtil.createContactInDefaultFolder(mbox, "testzclient@example.com");
    ItemIdentifier contactId = ItemIdentifier.fromAccountIdAndItemId(zmbox.getAccountId(), contact.getId());
    /* getting message using message id */
    ZimbraMailItem mItemAsMsg = zmbox.getItemById((OpContext) null, msgItemId, MailItemType.MESSAGE);
    assertNotNull("getItemById returned null when got with type MESSAGE", mItemAsMsg);
    assertEquals("Different ID when got with type MESSAGE", id, Integer.valueOf(mItemAsMsg.getIdInMailbox()));
    assertTrue(String.format("%s Not a ZMessage when got with type MESSAGE", mItemAsMsg.getClass().getName()), mItemAsMsg instanceof ZMessage);
    /* getting item using message id */
    mItemAsMsg = zmbox.getItemById((OpContext) null, msgItemId, MailItemType.UNKNOWN);
    assertNotNull("getItemById returned null when got with type UNKNOWN", mItemAsMsg);
    assertEquals("Different ID when got with type UNKNOWN", id, Integer.valueOf(mItemAsMsg.getIdInMailbox()));
    assertTrue(String.format("%s Not a ZMessage when got with type UNKNOWN", mItemAsMsg.getClass().getName()), mItemAsMsg instanceof ZMessage);
    /* getting contact using id of contact */
    ZimbraMailItem mItemAsContact = zmbox.getItemById((OpContext) null, contactId, MailItemType.CONTACT);
    assertNotNull("getItemById returned null when got with type CONTACT", mItemAsContact);
    assertEquals("Different ID when got with type CONTACT", contactId.id, mItemAsContact.getIdInMailbox());
    assertTrue(String.format("%s Not a ZContact when got with type CONTACT", mItemAsContact.getClass().getName()), mItemAsContact instanceof ZContact);
    ZContact zContact = (ZContact) mItemAsContact;
    assertEquals("Imap UID of ZContact should be same as Contact", contact.getImapUid(), zContact.getImapUid());
    assertTrue(String.format("IMAP UID %s of ZContact not 0", zContact.getImapUid()), 0 != zContact.getImapUid());
    /* getting message using contact id */
    try {
        zmbox.getItemById((OpContext) null, contactId, MailItemType.MESSAGE);
        fail("ZClientNoSuchItemException was not thrown when getting contact as message");
    } catch (ZClientException.ZClientNoSuchItemException zcnsie) {
    }
    /* getting message using non-existent id */
    ItemIdentifier nonexistent = ItemIdentifier.fromAccountIdAndItemId(zmbox.getAccountId(), 9099);
    try {
        zmbox.getItemById((OpContext) null, nonexistent, MailItemType.UNKNOWN);
        fail("ZClientNoSuchItemException was not thrown");
    } catch (ZClientException.ZClientNoSuchItemException zcnsie) {
    }
    /* getting contact using id of message */
    try {
        zmbox.getItemById((OpContext) null, msgItemId, MailItemType.CONTACT);
        fail("ZClientNoSuchItemException was not thrown");
    } catch (ZClientException.ZClientNoSuchContactException zcnsce) {
    }
    /* getting document using id of message */
    try {
        zmbox.getItemById((OpContext) null, msgItemId, MailItemType.DOCUMENT);
        fail("ZClientNoSuchItemException was not thrown");
    } catch (ZClientException.ZClientNoSuchItemException zcnsce) {
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) ItemIdentifier(com.zimbra.common.mailbox.ItemIdentifier) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZimbraMailItem(com.zimbra.common.mailbox.ZimbraMailItem) ZClientException(com.zimbra.common.zclient.ZClientException) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) OpContext(com.zimbra.common.mailbox.OpContext) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Aggregations

Contact (com.zimbra.cs.mailbox.Contact)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)32 ParsedContact (com.zimbra.cs.mime.ParsedContact)26 Test (org.junit.Test)23 OperationContext (com.zimbra.cs.mailbox.OperationContext)13 HashMap (java.util.HashMap)13 ZMailbox (com.zimbra.client.ZMailbox)10 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)10 MailItem (com.zimbra.cs.mailbox.MailItem)10 ItemId (com.zimbra.cs.service.util.ItemId)10 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 ZContact (com.zimbra.client.ZContact)8 Folder (com.zimbra.cs.mailbox.Folder)7 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)7 Term (org.apache.lucene.index.Term)7 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)6 Message (com.zimbra.cs.mailbox.Message)6 List (java.util.List)6