Search in sources :

Example 6 with FixedMimeMessage

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

the class CalendarItem method decomposeBlob.

/**
     * Break up a multipart/digest blob into separate MimeMessages keyed by InvId header value.
     * @param digestStream
     * @return
     * @throws MessagingException
     */
public static Map<Integer, MimeMessage> decomposeBlob(byte[] digestBlob) throws ServiceException {
    Map<Integer, MimeMessage> map = new HashMap<Integer, MimeMessage>();
    try {
        InputStream bais = new SharedByteArrayInputStream(digestBlob);
        FixedMimeMessage digestMm = new FixedMimeMessage(JMSession.getSession(), bais);
        // It should be multipart/digest.
        MimeMultipart mmp;
        Object obj = digestMm.getContent();
        if (obj instanceof MimeMultipart)
            mmp = (MimeMultipart) obj;
        else
            throw ServiceException.FAILURE("Expected MimeMultipart, but got " + obj.getClass().getName() + ": " + obj.toString(), null);
        int numParts = mmp.getCount();
        for (int i = 0; i < numParts; i++) {
            MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
            int invId = 0;
            String[] hdrs = mbp.getHeader("invId");
            if (hdrs != null && hdrs.length > 0) {
                invId = Integer.parseInt(hdrs[0]);
                MimeMessage mm;
                Object objMbp = mbp.getContent();
                if (objMbp instanceof MimeMessage)
                    mm = (MimeMessage) objMbp;
                else
                    throw ServiceException.FAILURE("Expected MimeMessage, but got " + objMbp.getClass().getName() + ": " + objMbp.toString(), null);
                map.put(invId, mm);
            }
        }
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    } catch (NumberFormatException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) IOException(java.io.IOException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

FixedMimeMessage (com.zimbra.cs.mime.Mime.FixedMimeMessage)6 MimeMessage (javax.mail.internet.MimeMessage)5 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)5 Test (org.junit.Test)4 Account (com.zimbra.cs.account.Account)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MessagingException (javax.mail.MessagingException)2 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)1 ZSharedFileInputStream (com.zimbra.common.zmime.ZSharedFileInputStream)1 MockProvisioning (com.zimbra.cs.account.MockProvisioning)1 Provisioning (com.zimbra.cs.account.Provisioning)1 Server (com.zimbra.cs.account.Server)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 AddInviteData (com.zimbra.cs.mailbox.Mailbox.AddInviteData)1 ItemId (com.zimbra.cs.service.util.ItemId)1 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)1