Search in sources :

Example 11 with Attachment

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

the class ContactTest method modifyInvalidImageAttachment.

/**
     * Tests Invalid image attachment (bug 71868).
     */
@Test
public void modifyInvalidImageAttachment() throws Exception {
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", "Contact Initial Content");
    byte[] attachData = "attachment 1".getBytes();
    Attachment attachment1 = new Attachment(attachData, "image/png", "customField", "image.png");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = mbox.createContact(null, new ParsedContact(attrs, Lists.newArrayList(attachment1)), Mailbox.ID_FOLDER_CONTACTS, null);
    Attachment attachment2 = new Attachment(attachData, "image/png", "image", "image2.png");
    try {
        ParsedContact pc = new ParsedContact(contact).modify(new ParsedContact.FieldDeltaList(), Lists.newArrayList(attachment2));
    } catch (ServiceException se) {
        Assert.assertEquals("check the INVALID_IMAGE exception", "mail.INVALID_IMAGE", se.getCode());
    }
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 12 with Attachment

use of com.zimbra.cs.mailbox.Contact.Attachment 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 13 with Attachment

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

the class VCard method parseVCard.

public static List<VCard> parseVCard(String vcard) throws ServiceException {
    List<VCard> cards = new ArrayList<VCard>();
    Map<String, String> fields = new HashMap<String, String>();
    ListMultimap<String, VCardParamsAndValue> xprops = ArrayListMultimap.create();
    List<Attachment> attachments = new ArrayList<Attachment>();
    VCardProperty vcprop = new VCardProperty();
    int depth = 0;
    int cardstart = 0;
    String uid = null;
    StringBuilder line = new StringBuilder(256);
    for (int start, pos = 0, limit = vcard.length(); pos < limit; ) {
        // unfold the next line in the vcard
        line.setLength(0);
        String name = null;
        String value;
        int linestart = pos;
        boolean folded = true;
        do {
            start = pos;
            while (pos < limit && vcard.charAt(pos) != '\r' && vcard.charAt(pos) != '\n') {
                pos++;
            }
            line.append(vcard.substring(start, pos));
            if (pos < limit) {
                if (pos < limit && vcard.charAt(pos) == '\r')
                    pos++;
                if (pos < limit && vcard.charAt(pos) == '\n')
                    pos++;
            }
            if (pos < limit && (vcard.charAt(pos) == ' ' || vcard.charAt(pos) == '\t')) {
                pos++;
            } else {
                name = vcprop.parse(line);
                if ((vcprop.getEncoding() != Encoding.Q) || !((line.length() > 0) && ('=' == (line.charAt(line.length() - 1))))) {
                    folded = false;
                }
            }
        } while (folded);
        if (vcprop.isEmpty()) {
            continue;
        }
        if (Strings.isNullOrEmpty(name)) {
            throw ServiceException.PARSE_ERROR("missing property name in line " + shortFormForLogging(line), null);
        } else if (name.equals("VERSION") || name.equals("REV") || name.equals("PRODID")) {
            continue;
        } else if ((name.startsWith("X-") && !name.startsWith("X-ZIMBRA-")) || (!PROPERTY_NAMES.contains(name))) {
            VCardParamsAndValue ppav;
            if (Encoding.B.equals(vcprop.encoding)) {
                // Keep value encoded as don't trust binary data stored in metadata
                Set<String> params = vcprop.params;
                params.add("ENCODING=B");
                if ((vcprop.charset != null) && (!MimeConstants.P_CHARSET_UTF8.equals(vcprop.charset))) {
                    params.add(String.format("CHARSET=%s", vcprop.charset));
                }
                ppav = new VCardParamsAndValue(vcprop.value, vcprop.params);
            } else {
                ppav = new VCardParamsAndValue(vcfDecode(vcprop.getValue()), vcprop.params);
            }
            // handle multiple occurrences of xprops with the same key
            String group = vcprop.getGroup();
            String key = (group == null) ? name : group + "." + name;
            xprops.put(key, ppav);
        } else if (name.equals("BEGIN")) {
            if (++depth == 1) {
                // starting a top-level vCard; reset state
                fields = new HashMap<String, String>();
                xprops = ArrayListMultimap.create();
                attachments = new ArrayList<Attachment>();
                cardstart = linestart;
                uid = null;
            }
            continue;
        } else if (name.equals("END")) {
            if (depth > 0 && depth-- == 1) {
                if (!xprops.isEmpty()) {
                    fields.put(ContactConstants.A_vCardXProps, Contact.encodeUnknownVCardProps(xprops));
                }
                // finished a vCard; add to list if non-empty
                if (!fields.isEmpty()) {
                    Contact.normalizeFileAs(fields);
                    cards.add(new VCard(fields.get(ContactConstants.A_fullName), vcard.substring(cardstart, pos), fields, attachments, uid));
                }
            }
            continue;
        } else if (depth <= 0) {
            continue;
        } else if (name.equals("AGENT")) {
            // catch AGENT on same line as BEGIN block when rest of AGENT is not on the same line
            if (vcprop.getValue().trim().toUpperCase().matches("BEGIN\\s*:\\s*VCARD"))
                depth++;
            continue;
        }
        if (vcprop.getEncoding() == Encoding.B && !vcprop.containsParam("VALUE=URI")) {
            if (name.equals("PHOTO")) {
                String suffix = vcprop.getParamValue("TYPE").toUpperCase();
                String ctype = null;
                if (!Strings.isNullOrEmpty(suffix)) {
                    ctype = "image/" + suffix.toLowerCase();
                    suffix = '.' + suffix;
                }
                attachments.add(new Attachment(vcprop.getDecoded(), ctype, "image", "image" + suffix));
                continue;
            }
            if (name.equals("KEY")) {
                String encoded = new String(Base64.encodeBase64Chunked(vcprop.getDecoded()));
                fields.put(ContactConstants.A_userCertificate, encoded);
                continue;
            }
        }
        value = vcprop.getValue();
        // decode the property's value and assign to the appropriate contact field(s)
        if (name.equals("FN"))
            addField(ContactConstants.A_fullName, vcfDecode(value), "altFullName", 2, fields);
        else if (name.equals("N"))
            decodeStructured(value, NAME_FIELDS, fields);
        else if (// TODO: VCARD 4 NICKNAME is multi-valued (COMMA separated)
        name.equals("NICKNAME"))
            //       It is treated as a single value here
            addField(ContactConstants.A_nickname, vcfDecode(value), "altNickName", 2, fields);
        else if (// Assumption: Do not want multiple photos.
        name.equals("PHOTO"))
            // Assumption: Do not want multiple photos.
            fields.put(ContactConstants.A_image, vcfDecode(value));
        else if (name.equals("BDAY"))
            addField(ContactConstants.A_birthday, vcfDecode(value), null, 2, fields);
        else if (name.equals("ADR"))
            decodeAddress(value, vcprop, fields);
        else if (name.equals("TEL"))
            decodeTelephone(value, vcprop, fields);
        else if (name.equals("URL"))
            decodeURL(value, vcprop, fields);
        else if (name.equals("ORG"))
            decodeStructured(value, ORG_FIELDS, fields);
        else if (name.equals("TITLE"))
            addField(ContactConstants.A_jobTitle, vcfDecode(value), "altJobTitle", 2, fields);
        else if (name.equals("NOTE"))
            addField(ContactConstants.A_notes, vcfDecode(value), null, 2, fields);
        else if (name.equals("EMAIL"))
            addField(ContactConstants.A_email, vcfDecode(value), null, 2, fields);
        else if (name.equals("X-ZIMBRA-MAIDENNAME"))
            fields.put(ContactConstants.A_maidenName, vcfDecode(value));
        else if (name.startsWith("X-ZIMBRA-IMADDRESS"))
            addField("imAddress", true, vcfDecode(value), null, 1, fields);
        else if (name.equals("X-ZIMBRA-ANNIVERSARY"))
            addField(ContactConstants.A_anniversary, vcfDecode(value), null, 2, fields);
        else if (name.equals("UID"))
            uid = value;
    }
    return cards;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue)

Example 14 with Attachment

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

the class ToXML method encodeContact.

public static Element encodeContact(Element parent, ItemIdFormatter ifmt, OperationContext octxt, Contact contact, ContactGroup contactGroup, Collection<String> memberAttrFilter, boolean summary, Collection<String> attrFilter, int fields, String migratedDlist, boolean returnHiddenAttrs, long maxMembers, boolean returnCertInfo) throws ServiceException {
    Element el = parent.addElement(MailConstants.E_CONTACT);
    el.addAttribute(MailConstants.A_ID, ifmt.formatItemId(contact));
    if (needToOutput(fields, Change.FOLDER)) {
        el.addAttribute(MailConstants.A_FOLDER, ifmt.formatItemId(new ItemId(contact.getMailbox().getAccountId(), contact.getFolderId())));
    }
    recordItemTags(el, contact, octxt, fields);
    if (needToOutput(fields, Change.CONFLICT)) {
        el.addAttribute(MailConstants.A_CHANGE_DATE, contact.getChangeDate() / 1000);
        el.addAttribute(MailConstants.A_MODIFIED_SEQUENCE, contact.getModifiedSequence());
        el.addAttribute(MailConstants.A_DATE, contact.getDate());
        el.addAttribute(MailConstants.A_REVISION, contact.getSavedSequence());
    } else if (needToOutput(fields, Change.CONTENT)) {
        el.addAttribute(MailConstants.A_DATE, contact.getDate());
        el.addAttribute(MailConstants.A_REVISION, contact.getSavedSequence());
    }
    if (needToOutput(fields, Change.METADATA)) {
        encodeAllCustomMetadata(el, contact, fields);
    }
    if (!needToOutput(fields, Change.CONTENT)) {
        if (summary) {
            try {
                el.addAttribute(MailConstants.A_FILE_AS_STR, contact.getFileAsString());
            } catch (ServiceException e) {
            }
            el.addAttribute(ContactConstants.A_email, contact.get(ContactConstants.A_email));
            el.addAttribute(ContactConstants.A_email2, contact.get(ContactConstants.A_email2));
            el.addAttribute(ContactConstants.A_email3, contact.get(ContactConstants.A_email3));
            String type = contact.get(ContactConstants.A_type);
            el.addAttribute(ContactConstants.A_type, type);
            // send back date with summary via search results
            el.addAttribute(MailConstants.A_CHANGE_DATE, contact.getChangeDate() / 1000);
        }
        // stop here if we're not returning the actual contact content
        return el;
    }
    try {
        el.addAttribute(MailConstants.A_FILE_AS_STR, contact.getFileAsString());
    } catch (ServiceException e) {
    }
    List<Attachment> attachments = contact.getAttachments();
    // encode contact group members (not derefed) if we don't have a
    // already derefed contactGroup, and we don't have a migrated dlist
    boolean encodeContactGroupMembersBasic = (contactGroup == null) && (migratedDlist == null);
    if (attrFilter != null) {
        for (String name : attrFilter) {
            // XXX: How to distinguish between a non-existent attribute and
            //      an existing attribute with null or empty string value?
            String value = contact.get(name);
            if (!Strings.isNullOrEmpty(value)) {
                encodeContactAttr(el, name, value, contact, encodeContactGroupMembersBasic, octxt, returnCertInfo);
            } else if (attachments != null) {
                for (Attachment attach : attachments) {
                    if (attach.getName().equals(name)) {
                        encodeContactAttachment(el, attach);
                    }
                }
            }
        }
    } else {
        Map<String, String> contactFields = returnHiddenAttrs ? contact.getAllFields() : contact.getFields();
        for (Map.Entry<String, String> me : contactFields.entrySet()) {
            String name = me.getKey();
            String value = me.getValue();
            // can be configured as a hidden or non-hidden field
            if (ContactConstants.A_member.equals(name) && maxMembers != GetContacts.NO_LIMIT_MAX_MEMBERS) {
                // there is a limit on the max number of members to return
                ContactDLMembers dlMembers = new ContactDLMembers(contact);
                if (dlMembers.getTotal() > maxMembers) {
                    el.addAttribute(MailConstants.A_TOO_MANY_MEMBERS, true);
                    continue;
                }
            }
            if (name != null && !name.trim().isEmpty() && !Strings.isNullOrEmpty(value)) {
                encodeContactAttr(el, name, value, contact, encodeContactGroupMembersBasic, octxt, returnCertInfo);
            }
        }
        if (attachments != null) {
            for (Attachment attach : attachments) {
                encodeContactAttachment(el, attach);
            }
        }
    }
    if (migratedDlist != null) {
        encodeContactAttr(el, ContactConstants.A_dlist, migratedDlist, contact, false, octxt, returnCertInfo);
    } else if (contactGroup != null) {
        encodeContactGroup(el, contactGroup, memberAttrFilter, ifmt, octxt, summary, fields);
    }
    return el;
}
Also used : ContactDLMembers(com.zimbra.cs.gal.GalGroupMembers.ContactDLMembers) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Element(com.zimbra.common.soap.Element) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ItemId(com.zimbra.cs.service.util.ItemId) Map(java.util.Map) IcalXmlStrMap(com.zimbra.cs.mailbox.calendar.IcalXmlStrMap) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap)

Example 15 with Attachment

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

the class ParsedContact method parseBlob.

private static List<Attachment> parseBlob(InputStream in) throws ServiceException, MessagingException, IOException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), in);
    MimeMultipart multi = null;
    try {
        multi = (MimeMultipart) mm.getContent();
    } catch (ClassCastException x) {
        throw ServiceException.FAILURE("MimeMultipart content expected but got " + mm.getContent().toString(), x);
    }
    List<Attachment> attachments = new ArrayList<Attachment>(multi.getCount());
    for (int i = 1; i <= multi.getCount(); i++) {
        MimeBodyPart bp = (MimeBodyPart) multi.getBodyPart(i - 1);
        ContentDisposition cdisp = new ContentDisposition(bp.getHeader("Content-Disposition", null));
        Attachment attachment = new Attachment(bp.getDataHandler(), cdisp.getParameter("field"));
        attachment.setPartName(Integer.toString(i));
        attachments.add(attachment);
    }
    return attachments;
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ArrayList(java.util.ArrayList) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

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