Search in sources :

Example 1 with VCardParamsAndValue

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

the class VCardTest method multiXwithParams.

@Test
public void multiXwithParams() throws ServiceException {
    List<VCard> cards = VCard.parseVCard(multiXwithParams);
    Assert.assertNotNull("List of cards", cards);
    Assert.assertEquals("Number of cards", 1, cards.size());
    VCard vcard = cards.get(0);
    String outCard = vcard.getFormatted();
    Assert.assertEquals("Round tripped card", multiXwithParams, outCard);
    String xprops = vcard.fields.get(ContactConstants.A_vCardXProps);
    ListMultimap<String, VCardParamsAndValue> xp = Contact.decodeUnknownVCardProps(xprops);
    Assert.assertEquals(String.format("Number of unknown properties in %s", xp), 4, xp.size());
    List<VCardParamsAndValue> xfoos = xp.get("X-FOO");
    Assert.assertEquals("Number of xfoos ", 3, xfoos.size());
    VCardParamsAndValue pandv = xfoos.get(0);
    Assert.assertEquals("first x-foo value", "one", pandv.getValue());
    Set<String> params = pandv.getParams();
    Assert.assertEquals("first x-foo Number of params ", 2, params.size());
    Assert.assertTrue("first x-foo params contains P1=A", params.contains("P1=A"));
    Assert.assertTrue("first x-foo params contains P2=B", params.contains("P2=B"));
    pandv = xfoos.get(1);
    Assert.assertEquals("2nd x-foo value", "two", pandv.getValue());
    params = pandv.getParams();
    Assert.assertEquals("2nd x-foo Number of params ", 1, params.size());
    Assert.assertTrue("2nd x-foo params contains P3=C", params.contains("P3=C"));
    pandv = xfoos.get(2);
    Assert.assertEquals("3rd x-foo value", "three", pandv.getValue());
    params = pandv.getParams();
    Assert.assertEquals("3rd x-foo Number of params ", 0, params.size());
    List<VCardParamsAndValue> nonstandards = xp.get("NON-STANDARD");
    Assert.assertEquals("Number of NON-STANDARD ", 1, nonstandards.size());
    pandv = nonstandards.get(0);
    Assert.assertEquals("non-standard value", "this is fun", pandv.getValue());
    params = pandv.getParams();
    Assert.assertTrue(String.format("non-standard params '%s' contains PARAMETER=pValue", params), params.contains("PARAMETER=pValue"));
    Assert.assertEquals("non-standard Number of params ", 1, params.size());
}
Also used : VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) Test(org.junit.Test)

Example 2 with VCardParamsAndValue

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

the class VCardTest method smallBusyMacAttach.

@Test
public void smallBusyMacAttach() throws ServiceException {
    List<VCard> cards = VCard.parseVCard(smallBusyMacAttach);
    Assert.assertNotNull("List of cards", cards);
    Assert.assertEquals("Number of cards", 1, cards.size());
    VCard vcard = cards.get(0);
    String xprops = vcard.fields.get(ContactConstants.A_vCardXProps);
    ListMultimap<String, VCardParamsAndValue> xp = Contact.decodeUnknownVCardProps(xprops);
    Assert.assertEquals(String.format("Number of unknown properties in %s", xp), 3, xp.size());
    List<VCardParamsAndValue> busymacAttaches = xp.get("X-BUSYMAC-ATTACH");
    Assert.assertEquals(String.format("Number of %s", "X-BUSYMAC-ATTACH"), 1, busymacAttaches.size());
    VCardParamsAndValue attach = busymacAttaches.get(0);
    Assert.assertTrue(String.format("Value fo X-BUSYMAC-ATTACH property %s starts as expected", attach.getValue()), attach.getValue().startsWith("AAABAAEAEBAAAAEAIABoBAAAF"));
    Assert.assertEquals(String.format("X-BUSYMAC-ATTACH params=%s number", attach.getParams()), 2, attach.getParams().size());
}
Also used : VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) Test(org.junit.Test)

Example 3 with VCardParamsAndValue

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

the class AddressObject method constructContactGroupFromAppleXProps.

private static void constructContactGroupFromAppleXProps(DavContext ctxt, Account ownerAccount, VCard vcard, Contact existingContact, int folderId) {
    ListMultimap<String, VCardParamsAndValue> xprops = Contact.decodeUnknownVCardProps(vcard.fields.get(ContactConstants.A_vCardXProps));
    String kind = VCardParamsAndValue.getFirstValue(XABSKIND, xprops);
    if (kind != null && kind.compareTo("group") == 0) {
        ContactGroup contactGroup;
        List<VCardParamsAndValue> xabsmembers = xprops.get(XABSMEMBER);
        try {
            if (existingContact == null) {
                // create
                contactGroup = ContactGroup.init();
            } else {
                // modify
                contactGroup = ContactGroup.init(existingContact, true);
                // remove all the contacts of type CONTACT_REF that belong to the collection same as the group
                ArrayList<Member> membersToRemove = new ArrayList<Member>();
                for (Member member : contactGroup.getMembers()) {
                    if (Member.Type.CONTACT_REF.equals(member.getType())) {
                        ItemId itemId = new ItemId(member.getValue(), existingContact.getAccount().getId());
                        if (itemId.belongsTo(existingContact.getAccount())) {
                            // make sure member belongs to the same collection as the group.
                            Contact c = getContactByUID(ctxt, itemId.toString(), existingContact.getAccount(), folderId);
                            if (c != null) {
                                membersToRemove.add(member);
                            }
                        }
                    }
                }
                for (Member member : membersToRemove) {
                    contactGroup.removeMember(member.getType(), member.getValue());
                }
            }
            for (VCardParamsAndValue memberProp : xabsmembers) {
                String member = memberProp.getValue();
                if (member.startsWith("urn:uuid:")) {
                    member = member.substring(9);
                }
                Contact c = getContactByUID(ctxt, member, ownerAccount, folderId);
                if (c != null) {
                    // add to the group as a CONTACT_REF
                    ItemId itemId = new ItemId(c);
                    contactGroup.addMember(Member.Type.CONTACT_REF, itemId.toString());
                }
            }
            vcard.fields.put(ContactConstants.A_type, ContactConstants.TYPE_GROUP);
            vcard.fields.put(ContactConstants.A_groupMember, contactGroup.encode());
            // remove the Apple x-props and preserve the rest.
            xprops.removeAll(XABSKIND);
            xprops.removeAll(XABSMEMBER);
            vcard.fields.put(ContactConstants.A_vCardXProps, Contact.encodeUnknownVCardProps(xprops));
        } catch (ServiceException e) {
            ZimbraLog.dav.debug("can't parse xprop %s", xabsmembers, e);
        }
    }
}
Also used : VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Member(com.zimbra.cs.mailbox.ContactGroup.Member) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact)

Example 4 with VCardParamsAndValue

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

the class VCard method formatContact.

public static VCard formatContact(Contact con, Collection<String> vcattrs, boolean includeXProps, boolean includeZimbraXProps) {
    Map<String, String> fields = con.getFields();
    List<Attachment> attachments = con.getAttachments();
    List<String> emails = con.getEmailAddresses(DerefGroupMembersOption.NONE);
    StringBuilder sb = new StringBuilder();
    sb.append("BEGIN:VCARD\r\n");
    if (vcattrs == null || vcattrs.contains("VERSION"))
        sb.append("VERSION:3.0\r\n");
    // FN is a mandatory component of the vCard -- try our best to find or generate one
    String fn = fields.get(ContactConstants.A_fullName);
    if (vcattrs == null || vcattrs.contains("FN")) {
        if (fn == null || fn.trim().equals(""))
            try {
                fn = con.getFileAsString();
            } catch (ServiceException e) {
                fn = "";
            }
        if (fn.trim().equals("") && !emails.isEmpty())
            fn = emails.get(0);
        if (fn.trim().equals("")) {
            String org = fields.get(ContactConstants.A_company);
            if (org != null && !org.trim().equals("")) {
                fn = org;
            }
        }
        sb.append("FN:").append(vcfEncode(fn)).append("\r\n");
    }
    if (vcattrs == null || vcattrs.contains("N")) {
        StringBuilder nSb = new StringBuilder();
        nSb.append(vcfEncode(fields.get(ContactConstants.A_lastName))).append(';').append(vcfEncode(fields.get(ContactConstants.A_firstName))).append(';').append(vcfEncode(fields.get(ContactConstants.A_middleName))).append(';').append(vcfEncode(fields.get(ContactConstants.A_namePrefix))).append(';').append(vcfEncode(fields.get(ContactConstants.A_nameSuffix)));
        String n = nSb.toString();
        // so, try to avoid that.
        if (";;;;".equals(n)) {
            n = vcfEncode(fn) + ";;;;";
        }
        sb.append("N:").append(n).append("\r\n");
    }
    if (vcattrs == null || vcattrs.contains("NICKNAME"))
        encodeField(sb, "NICKNAME", fields.get(ContactConstants.A_nickname));
    if (vcattrs == null || vcattrs.contains("PHOTO"))
        encodeField(sb, "PHOTO;VALUE=URI", fields.get(ContactConstants.A_image));
    if (vcattrs == null || vcattrs.contains("BDAY")) {
        String bday = fields.get(ContactConstants.A_birthday);
        if (bday != null) {
            Date date = DateUtil.parseDateSpecifier(bday);
            if (date != null)
                sb.append("BDAY;VALUE=date:").append(new SimpleDateFormat("yyyy-MM-dd").format(date)).append("\r\n");
        }
    }
    if (vcattrs == null || vcattrs.contains("ADR")) {
        encodeAddress(sb, "home,postal,parcel", ContactConstants.A_homeStreet, ContactConstants.A_homeCity, ContactConstants.A_homeState, ContactConstants.A_homePostalCode, ContactConstants.A_homeCountry, 2, fields);
        encodeAddress(sb, "work,postal,parcel", ContactConstants.A_workStreet, ContactConstants.A_workCity, ContactConstants.A_workState, ContactConstants.A_workPostalCode, ContactConstants.A_workCountry, 2, fields);
        encodeAddress(sb, "postal,parcel", ContactConstants.A_otherStreet, ContactConstants.A_otherCity, ContactConstants.A_otherState, ContactConstants.A_otherPostalCode, ContactConstants.A_otherCountry, 2, fields);
    }
    if (vcattrs == null || vcattrs.contains("TEL")) {
        // omitting callback phone for now
        encodePhone(sb, "car,voice", ContactConstants.A_carPhone, 2, fields);
        encodePhone(sb, "home,fax", ContactConstants.A_homeFax, 2, fields);
        encodePhone(sb, "home,voice", ContactConstants.A_homePhone, 2, fields);
        encodePhone(sb, "cell,voice", ContactConstants.A_mobilePhone, 2, fields);
        encodePhone(sb, "fax", ContactConstants.A_otherFax, 2, fields);
        encodePhone(sb, "voice", ContactConstants.A_otherPhone, 2, fields);
        encodePhone(sb, "pager", ContactConstants.A_pager, 2, fields);
        encodePhone(sb, "work,fax", ContactConstants.A_workFax, 2, fields);
        encodePhone(sb, "work,voice", ContactConstants.A_workPhone, 2, fields);
    }
    if (vcattrs == null || vcattrs.contains("EMAIL")) {
        encodeField(sb, "EMAIL;TYPE=internet", ContactConstants.A_email, false, 2, fields);
        encodeField(sb, "EMAIL;TYPE=internet", "workEmail", true, 1, fields);
    }
    if (vcattrs == null || vcattrs.contains("URL")) {
        encodeField(sb, "URL;TYPE=home", ContactConstants.A_homeURL, false, 2, fields);
        encodeField(sb, "URL", ContactConstants.A_otherURL, false, 2, fields);
        encodeField(sb, "URL;TYPE=work", ContactConstants.A_workURL, false, 2, fields);
    }
    if (vcattrs == null || vcattrs.contains("ORG")) {
        String org = fields.get(ContactConstants.A_company);
        if (org != null && !org.trim().equals("")) {
            org = vcfEncode(org);
            String dept = fields.get(ContactConstants.A_department);
            if (dept != null && !dept.trim().equals("")) {
                org += ';' + vcfEncode(dept);
            }
            sb.append("ORG:").append(org).append("\r\n");
        }
    }
    if (vcattrs == null || vcattrs.contains("TITLE"))
        encodeField(sb, "TITLE", fields.get(ContactConstants.A_jobTitle));
    if (vcattrs == null || vcattrs.contains("NOTE"))
        encodeField(sb, "NOTE", fields.get(ContactConstants.A_notes));
    if ((vcattrs == null || vcattrs.contains("PHOTO")) && attachments != null) {
        for (Attachment attach : attachments) {
            try {
                if (attach.getName().equalsIgnoreCase(ContactConstants.A_image)) {
                    String field = "PHOTO;ENCODING=B";
                    if (attach.getContentType().startsWith("image/")) {
                        // We want just the subtype, ignoring any name etc
                        try {
                            ContentType ct = new ContentType(attach.getContentType());
                            if (ct != null) {
                                String subType = ct.getSubType();
                                if (!Strings.isNullOrEmpty(subType)) {
                                    field += ";TYPE=" + ct.getSubType().toUpperCase();
                                }
                            }
                        } catch (ParseException e) {
                        }
                    }
                    String encoded = new String(Base64.encodeBase64Chunked(attach.getContent())).trim().replace("\r\n", "\r\n ");
                    sb.append(field).append(":\r\n ").append(encoded).append("\r\n");
                }
            } catch (OutOfMemoryError e) {
                Zimbra.halt("out of memory", e);
            } catch (Throwable t) {
                ZimbraLog.misc.info("error fetching attachment content: " + attach.getName(), t);
            }
        }
    }
    if (vcattrs == null || vcattrs.contains("KEY")) {
        String smimeCert = fields.get(ContactConstants.A_userSMIMECertificate);
        if (smimeCert == null) {
            smimeCert = fields.get(ContactConstants.A_userCertificate);
        }
        if (smimeCert != null) {
            smimeCert = smimeCert.trim().replace("\r\n", "\r\n ");
            String field = "KEY;ENCODING=B";
            sb.append(field).append(":\r\n ").append(smimeCert).append("\r\n");
        }
    }
    if (vcattrs == null || vcattrs.contains("CATEGORIES")) {
        String[] tags = con.getTags();
        if (tags.length > 0) {
            StringBuilder sbtags = new StringBuilder();
            for (String tagName : tags) {
                sbtags.append(sbtags.length() == 0 ? "" : ",").append(vcfEncode(tagName));
            }
            sb.append("CATEGORIES:").append(sbtags).append("\r\n");
        }
    }
    String uid = getUid(con);
    if (vcattrs == null || vcattrs.contains("REV")) {
        sb.append("REV:").append(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(new Date(con.getDate()))).append("\r\n");
    }
    if (vcattrs == null || vcattrs.contains("UID")) {
        sb.append("UID:").append(uid).append("\r\n");
    }
    // sb.append("MAILER:Zimbra ").append(BuildInfo.VERSION).append("\r\n");
    if ((vcattrs == null && includeZimbraXProps) || (vcattrs != null && vcattrs.contains("X-ZIMBRA-IMADDRESS"))) {
        encodeField(sb, "X-ZIMBRA-IMADDRESS", "imAddress", true, 1, fields);
    }
    if ((vcattrs == null && includeZimbraXProps) || (vcattrs != null && vcattrs.contains("X-ZIMBRA-ANNIVERSARY"))) {
        encodeField(sb, "X-ZIMBRA-ANNIVERSARY", ContactConstants.A_anniversary, false, 2, fields);
    }
    if ((vcattrs == null && includeZimbraXProps) || (vcattrs != null && vcattrs.contains("X-ZIMBRA-MAIDENNAME"))) {
        String maidenName = con.get(ContactConstants.A_maidenName);
        if (maidenName != null)
            sb.append("X-ZIMBRA-MAIDENNAME:").append(maidenName).append("\r\n");
    }
    if (includeXProps) {
        ListMultimap<String, VCardParamsAndValue> unknownVCardProps = con.getUnknownVCardProps();
        for (String key : unknownVCardProps.keySet()) {
            for (VCardParamsAndValue paramsAndValue : unknownVCardProps.get(key)) {
                StringWriter sw = new StringWriter();
                try (FoldingWriter writer = new FoldingWriter(sw)) {
                    writer.write(key);
                    String value = paramsAndValue.getValue();
                    Set<String> params = paramsAndValue.getParams();
                    if (!params.isEmpty()) {
                        writer.write(";");
                        writer.write(Joiner.on(";").join(params));
                    }
                    String vcfEncodedValue;
                    if (params.contains("ENCODING=B")) {
                        // should be raw BASE64
                        vcfEncodedValue = value;
                    } else {
                        vcfEncodedValue = vcfEncode(value);
                    }
                    writer.write(":");
                    writer.write(vcfEncodedValue);
                    writer.write("\r\n");
                    sb.append(sw.toString());
                } catch (IOException e) {
                    ZimbraLog.misc.debug("Problem with adding property '%s' to VCARD - ignoring", key, e);
                }
            }
        }
    }
    sb.append("END:VCARD\r\n");
    return new VCard(fn, sb.toString(), fields, attachments, uid);
}
Also used : ContentType(javax.mail.internet.ContentType) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) IOException(java.io.IOException) Date(java.util.Date) VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) ServiceException(com.zimbra.common.service.ServiceException) StringWriter(java.io.StringWriter) ParseException(javax.mail.internet.ParseException) FoldingWriter(net.fortuna.ical4j.data.FoldingWriter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with VCardParamsAndValue

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

the class AddressObject method populateContactGroupAppleXProps.

private static void populateContactGroupAppleXProps(DavContext ctxt, Contact contact) {
    if (contact.isContactGroup() == false) {
        return;
    }
    ContactGroup contactGroup = null;
    try {
        contactGroup = ContactGroup.init(contact.get(ContactConstants.A_groupMember));
    } catch (ServiceException e) {
        ZimbraLog.dav.warn("can't get group members for Contact %d", contact.getId(), e);
    }
    ListMultimap<String, VCardParamsAndValue> xprops = contact.getUnknownVCardProps();
    xprops.put(XABSKIND, new VCardParamsAndValue("group"));
    if (contactGroup != null) {
        try {
            for (Member member : contactGroup.getMembers()) {
                if (member.getType().equals(Member.Type.CONTACT_REF)) {
                    ItemId itemId = new ItemId(member.getValue(), contact.getAccount().getId());
                    if (itemId.belongsTo(contact.getAccount())) {
                        // make sure member belongs to the same collection as the group.
                        Contact c = getContactByUID(ctxt, itemId.toString(), contact.getAccount(), contact.getFolderId());
                        if (c != null) {
                            xprops.put(XABSMEMBER, new VCardParamsAndValue("urn:uuid:" + VCard.getUid(c)));
                        }
                    }
                }
            }
        } catch (ServiceException e) {
            ZimbraLog.dav.warn("can't create group members xprops for Contact %d", contact.getId(), e);
        }
    }
    contact.setUnknownVCardProps(xprops);
}
Also used : VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) ServiceException(com.zimbra.common.service.ServiceException) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Member(com.zimbra.cs.mailbox.ContactGroup.Member) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact)

Aggregations

VCardParamsAndValue (com.zimbra.cs.mailbox.VCardParamsAndValue)6 ServiceException (com.zimbra.common.service.ServiceException)3 Contact (com.zimbra.cs.mailbox.Contact)2 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)2 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)2 Member (com.zimbra.cs.mailbox.ContactGroup.Member)2 ItemId (com.zimbra.cs.service.util.ItemId)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 FoldingWriter (net.fortuna.ical4j.data.FoldingWriter)1