Search in sources :

Example 31 with Contact

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

the class ToXML method encodeContactGroup.

private static void encodeContactGroup(Element elem, ContactGroup contactGroup, Collection<String> memberAttrFilter, ItemIdFormatter ifmt, OperationContext octxt, boolean summary, int fields) throws ServiceException {
    for (ContactGroup.Member member : contactGroup.getMembers(true)) {
        Element eMember = elem.addNonUniqueElement(MailConstants.E_CONTACT_GROUP_MEMBER);
        encodeContactGroupMemberBasic(eMember, member);
        Object derefedMember = member.getDerefedObj();
        if (derefedMember != null) {
            if (derefedMember instanceof String) {
            // inline member, do nothing
            } else if (derefedMember instanceof Contact) {
                // only expand one level for now.
                // If this member is a group, do not create/pass down a ContactGroup object from the member.
                encodeContact(eMember, ifmt, octxt, (Contact) derefedMember, summary, memberAttrFilter, fields);
            } else if (derefedMember instanceof GalContact) {
                encodeGalContact(eMember, (GalContact) derefedMember, memberAttrFilter);
            } else if (derefedMember instanceof Element) {
                // proxied GAL or Contact entry
                Element eContact = (Element) derefedMember;
                if (memberAttrFilter != null) {
                    for (Element eAttr : eContact.listElements()) {
                        String name = eAttr.getAttribute(MailConstants.A_ATTRIBUTE_NAME, null);
                        if (!memberAttrFilter.contains(name)) {
                            eAttr.detach();
                        }
                    }
                }
                eMember.addNonUniqueElement(eContact);
            }
        }
    }
}
Also used : GalContact(com.zimbra.cs.account.GalContact) Element(com.zimbra.common.soap.Element) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) GalContact(com.zimbra.cs.account.GalContact) Contact(com.zimbra.cs.mailbox.Contact)

Example 32 with Contact

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

the class ContactAction method handleContact.

private ItemActionResult 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);
    ItemActionResult successes = proxyRemoteItems(action, remote, request, context);
    if (!local.isEmpty()) {
        ItemActionResult 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.appendSuccessIds(localResults.getSuccessIds());
    }
    return successes;
}
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 33 with Contact

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

the class ExportContacts 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);
    ExportContactsRequest req = zsc.elementToJaxb(request);
    String folder = req.getFolderId();
    ItemId iidFolder = folder == null ? null : new ItemId(folder, zsc);
    String ct = req.getContentType();
    if (ct == null)
        throw ServiceException.INVALID_REQUEST("content type missing", null);
    if (!ct.equals("csv"))
        throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
    String format = req.getCsvFormat();
    String locale = req.getCsvLocale();
    String separator = req.getCsvDelimiter();
    Character sepChar = null;
    if ((separator != null) && (separator.length() > 0))
        sepChar = separator.charAt(0);
    List<Contact> contacts = mbox.getContactList(octxt, iidFolder != null ? iidFolder.getId() : -1);
    StringBuilder sb = new StringBuilder();
    if (contacts == null)
        contacts = new ArrayList<Contact>();
    try {
        ContactCSV contactCSV = new ContactCSV(mbox, octxt);
        contactCSV.toCSV(format, locale, sepChar, contacts.iterator(), sb);
    } catch (ContactCSV.ParseException e) {
        throw MailServiceException.UNABLE_TO_EXPORT_CONTACTS(e.getMessage(), e);
    }
    ExportContactsResponse resp = new ExportContactsResponse(sb.toString());
    return zsc.jaxbToElement(resp);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ContactCSV(com.zimbra.cs.service.formatter.ContactCSV) ExportContactsResponse(com.zimbra.soap.mail.message.ExportContactsResponse) ArrayList(java.util.ArrayList) ExportContactsRequest(com.zimbra.soap.mail.message.ExportContactsRequest) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 34 with Contact

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

the class VcfFormatter method formatCallback.

@Override
public void formatCallback(UserServletContext context) throws IOException, ServiceException {
    Charset charset = context.getCharset();
    Iterator<? extends MailItem> iterator = null;
    try {
        iterator = getMailItems(context, getDefaultStartTime(), getDefaultEndTime(), Integer.MAX_VALUE);
        String filename = context.target instanceof Contact ? ((Contact) context.target).getFileAsString() : "contacts";
        String cd = HttpUtil.createContentDisposition(context.req, Part.ATTACHMENT, filename + ".vcf");
        context.resp.addHeader("Content-Disposition", cd);
        // for backward compatibility
        context.resp.setContentType(MimeConstants.CT_TEXT_VCARD_LEGACY);
        context.resp.setCharacterEncoding(charset.name());
        int count = 0;
        while (iterator.hasNext()) {
            MailItem item = iterator.next();
            if (!(item instanceof Contact))
                continue;
            VCard vcf = VCard.formatContact((Contact) item);
            context.resp.getOutputStream().write(vcf.getFormatted().getBytes(charset));
            count++;
        }
    } finally {
        if (iterator instanceof QueryResultIterator)
            ((QueryResultIterator) iterator).finished();
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Charset(java.nio.charset.Charset) Contact(com.zimbra.cs.mailbox.Contact)

Example 35 with Contact

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

the class ArchiveFormatter method findContact.

/**
 * Find a contact in contact list of a mailbox
 * @param octxt The operation context
 * @param mbox The mailbox
 * @param ct The contact that is to be searched
 * @param fldr The folder in which contact should be searched
 * @return The contact if it exists in the contact list else return null
 */
private Contact findContact(OperationContext octxt, Mailbox mbox, Contact ct, Folder fldr) {
    int folderId = fldr.getId();
    List<Contact> contactList = contacts.get(folderId);
    if (contactList == null) {
        try {
            contactList = mbox.getContactList(octxt, folderId);
            contacts.put(folderId, contactList);
        } catch (ServiceException e) {
            ZimbraLog.mailbox.info("unable to get contact list for mailbox %s", mbox.getId());
            return null;
        }
    }
    for (Contact contact : contactList) {
        HashSet<String> emailAdresses = new HashSet<String>(contact.getEmailAddresses());
        for (String emailId : ct.getEmailAddresses()) {
            if (emailAdresses.contains(emailId)) {
                return contact;
            }
        }
    }
    return null;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) ParsedContact(com.zimbra.cs.mime.ParsedContact) Contact(com.zimbra.cs.mailbox.Contact) HashSet(java.util.HashSet)

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