Search in sources :

Example 36 with ContentType

use of javax.mail.internet.ContentType in project zm-mailbox by Zimbra.

the class CalendarItem method modifyBlob.

/**
     * Upate the Blob for this CalendarItem object: possibly remove one or more entries from it,
     * possibly add an entry to it.
     *
     * It IS okay to have newInv != null and invPm==null....this would mean an invite-add where the
     * new invite had no ParsedMessage: IE because it didn't actually come in via an RFC822 msg
     *
     * @param toRemove
     * @param removeAllExistingInvites if true, all existing invites are removed regardless of
     *                                 what's passed in toRemove list
     * @param toUpdate
     * @param invPm
     * @param newInv
     * @param isCancel if the method is being called while processing a cancel request
     * @param allowPrivateAccess
     * @param forceSave if TRUE then this call is guaranteed to save the current metadata state
     * @param replaceExceptionBodyWithSeriesBody if TRUE bodies of exceptions are replaced with series body
     *                                           for invites in toUpdate list
     * @throws ServiceException
     */
private void modifyBlob(List<Invite> toRemove, boolean removeAllExistingInvites, List<Invite> toUpdate, ParsedMessage invPm, Invite newInv, boolean isCancel, boolean allowPrivateAccess, boolean forceSave, boolean replaceExceptionBodyWithSeriesBody) throws ServiceException {
    //         if a single incoming Message has multiple invites in it, all for this CalendarItem)
    try {
        // now, make sure the message is in our blob already...
        MimeMessage mm = null;
        if (getSize() > 0) {
            try {
                mm = getMimeMessage();
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Error reading blob for calendar item " + getId() + " in mailbox " + getMailboxId(), e);
            }
        }
        if (mm == null) {
            if (newInv != null && invPm != null) {
                // if the blob isn't already there, and we're going to add one, then
                // just go into create
                createBlob(invPm, newInv);
            } else {
                if (forceSave)
                    saveMetadata();
            }
            return;
        }
        // It should be a multipart/digest.
        MimeMultipart mmp;
        Object obj = mm.getContent();
        if (obj instanceof MimeMultipart)
            mmp = (MimeMultipart) obj;
        else
            throw ServiceException.FAILURE("Expected MimeMultipart, but got " + obj.getClass().getName() + ": id=" + mId + ", content=" + obj.toString(), null);
        boolean updated = false;
        // remove invites
        if (removeAllExistingInvites) {
            int numParts = mmp.getCount();
            if (numParts > 0) {
                for (int i = numParts - 1; i >= 0; i--) {
                    mmp.removeBodyPart(i);
                }
                updated = true;
            }
        } else {
            // Remove specific invites.
            for (Invite inv : toRemove) {
                int matchedIdx;
                do {
                    // find the matching parts...
                    int numParts = mmp.getCount();
                    matchedIdx = -1;
                    for (int i = 0; i < numParts; i++) {
                        MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
                        String[] hdrs = mbp.getHeader("invId");
                        if (hdrs == null || hdrs.length == 0) {
                            matchedIdx = i;
                            break;
                        }
                        if (hdrs != null && hdrs.length > 0 && (Integer.parseInt(hdrs[0]) == inv.getMailItemId())) {
                            matchedIdx = i;
                            break;
                        }
                    }
                    if (matchedIdx > -1) {
                        mmp.removeBodyPart(matchedIdx);
                        updated = true;
                    }
                } while (matchedIdx > -1);
            }
        }
        // Update some invites.
        for (Invite inv : toUpdate) {
            MimeBodyPart mbpInv = null;
            int numParts = mmp.getCount();
            for (int i = 0; i < numParts; i++) {
                MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
                String[] hdrs = mbp.getHeader("invId");
                if (hdrs != null && hdrs.length > 0 && (Integer.parseInt(hdrs[0]) == inv.getMailItemId())) {
                    mbpInv = mbp;
                    break;
                }
            }
            if (mbpInv == null)
                continue;
            if (replaceExceptionBodyWithSeriesBody) {
                // Throw away the existing part.  Replace it with body from new invite.
                mmp.removeBodyPart(mbpInv);
                mbpInv = new ZMimeBodyPart();
                mbpInv.setDataHandler(new DataHandler(new PMDataSource(invPm)));
                mbpInv.addHeader("invId", Integer.toString(inv.getMailItemId()));
                mmp.addBodyPart(mbpInv);
                // required by JavaMail for some magical reason
                mm.saveChanges();
            }
            // Find the text/calendar part and replace it.
            String mbpInvCt = mbpInv.getContentType();
            Object objInv = mbpInv.getContent();
            if (!(objInv instanceof MimeMessage))
                continue;
            MimeMessage mmInv = (MimeMessage) objInv;
            Object objMulti = mmInv.getContent();
            if (!(objMulti instanceof MimeMultipart))
                continue;
            MimeMultipart multi = (MimeMultipart) objMulti;
            int numSubParts = multi.getCount();
            int icalPartNum = -1;
            for (int j = 0; j < numSubParts; j++) {
                MimeBodyPart part = (MimeBodyPart) multi.getBodyPart(j);
                ContentType ct = new ContentType(part.getContentType());
                if (ct.match(MimeConstants.CT_TEXT_CALENDAR)) {
                    icalPartNum = j;
                    break;
                }
            }
            if (icalPartNum != -1) {
                updated = true;
                multi.removeBodyPart(icalPartNum);
                ZVCalendar cal = inv.newToICalendar(allowPrivateAccess);
                MimeBodyPart icalPart = CalendarMailSender.makeICalIntoMimePart(cal);
                multi.addBodyPart(icalPart, icalPartNum);
                // Courtesy of JavaMail.  All three lines are necessary.
                // Reasons unclear from JavaMail docs.
                mmInv.setContent(multi);
                mmInv.saveChanges();
                mbpInv.setContent(mmInv, mbpInvCt);
            // End of courtesy of JavaMail
            }
        }
        if (newInv != null) {
            MimeBodyPart mbp = new ZMimeBodyPart();
            mbp.setDataHandler(new DataHandler(new PMDataSource(invPm)));
            mmp.addBodyPart(mbp);
            mbp.addHeader("invId", Integer.toString(newInv.getMailItemId()));
            updated = true;
        }
        if (!updated) {
            if (forceSave)
                saveMetadata();
            return;
        }
        if (mmp.getCount() == 0) {
            markBlobForDeletion();
            setContent(null, null);
            if (forceSave)
                saveMetadata();
        } else {
            // must call this explicitly or else new part won't be added...
            mm.setContent(mmp);
            mm.saveChanges();
            storeUpdatedBlob(mm);
        }
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("MessagingException", e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("IOException", e);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ServiceException(com.zimbra.common.service.ServiceException) 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) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 37 with ContentType

use of javax.mail.internet.ContentType in project zm-mailbox by Zimbra.

the class CalendarDataSource method getContentType.

public String getContentType() {
    ContentType ct = new ContentType();
    ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
    ct.setPrimaryType("text");
    ct.setSubType("calendar");
    ct.setParameter("method", mMethod);
    if (!mAttachName.toLowerCase().endsWith(".ics")) {
        mAttachName = mAttachName + ".ics";
    }
    ct.setParameter("name", mAttachName);
    return ct.toString();
}
Also used : ContentType(javax.mail.internet.ContentType)

Example 38 with ContentType

use of javax.mail.internet.ContentType in project zm-mailbox by Zimbra.

the class PlainTextFinder method getPlainText.

public String getPlainText() throws MessagingException, IOException {
    if (mPlainTextPart == null)
        return null;
    ContentType ct = new ContentType(mPlainTextPart.getContentType());
    String charset = ct.getParameter(MimeConstants.P_CHARSET);
    if (charset == null)
        charset = MimeConstants.P_CHARSET_DEFAULT;
    byte[] descBytes = ByteUtil.getContent(mPlainTextPart.getInputStream(), mPlainTextPart.getSize());
    return new String(descBytes, charset);
}
Also used : ContentType(javax.mail.internet.ContentType)

Example 39 with ContentType

use of javax.mail.internet.ContentType in project jmeter by apache.

the class SmtpSampler method getSamplerData.

private String getSamplerData(Message message) throws MessagingException, IOException {
    StringBuilder sb = new StringBuilder();
    // throws ME
    Object content = message.getContent();
    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        String contentType = multipart.getContentType();
        ContentType ct = new ContentType(contentType);
        String boundary = ct.getParameter("boundary");
        for (int i = 0; i < multipart.getCount(); i++) {
            // throws ME
            sb.append("--");
            sb.append(boundary);
            sb.append("\n");
            // throws ME
            BodyPart bodyPart = multipart.getBodyPart(i);
            // throws IOE, ME
            writeBodyPart(sb, bodyPart);
        }
        sb.append("--");
        sb.append(boundary);
        sb.append("--");
        sb.append("\n");
    } else if (content instanceof BodyPart) {
        BodyPart bodyPart = (BodyPart) content;
        // throws IOE, ME
        writeBodyPart(sb, bodyPart);
    } else if (content instanceof String) {
        sb.append(content);
    } else {
        sb.append("Content has class: ");
        sb.append(content.getClass().getCanonicalName());
    }
    return sb.toString();
}
Also used : BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ContentType(javax.mail.internet.ContentType)

Example 40 with ContentType

use of javax.mail.internet.ContentType in project lucene-solr by apache.

the class MailEntityProcessor method addPartToDocument.

public void addPartToDocument(Part part, Map<String, Object> row, boolean outerMost) throws Exception {
    if (part instanceof Message) {
        addEnvelopeToDocument(part, row);
    }
    String ct = part.getContentType().toLowerCase(Locale.ROOT);
    ContentType ctype = new ContentType(ct);
    if (part.isMimeType("multipart/*")) {
        Object content = part.getContent();
        if (content != null && content instanceof Multipart) {
            Multipart mp = (Multipart) part.getContent();
            int count = mp.getCount();
            if (part.isMimeType("multipart/alternative"))
                count = 1;
            for (int i = 0; i < count; i++) addPartToDocument(mp.getBodyPart(i), row, false);
        } else {
            LOG.warn("Multipart content is a not an instance of Multipart! Content is: " + (content != null ? content.getClass().getName() : "null") + ". Typically, this is due to the Java Activation JAR being loaded by the wrong classloader.");
        }
    } else if (part.isMimeType("message/rfc822")) {
        addPartToDocument((Part) part.getContent(), row, false);
    } else {
        String disp = part.getDisposition();
        if (includeContent && !(disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT))) {
            InputStream is = part.getInputStream();
            Metadata contentTypeHint = new Metadata();
            contentTypeHint.set(Metadata.CONTENT_TYPE, ctype.getBaseType().toLowerCase(Locale.ENGLISH));
            String content = (new Tika()).parseToString(is, contentTypeHint);
            if (row.get(CONTENT) == null)
                row.put(CONTENT, new ArrayList<String>());
            List<String> contents = (List<String>) row.get(CONTENT);
            contents.add(content.trim());
            row.put(CONTENT, contents);
        }
        if (!processAttachment || disp == null || !disp.equalsIgnoreCase(Part.ATTACHMENT))
            return;
        InputStream is = part.getInputStream();
        String fileName = part.getFileName();
        Metadata contentTypeHint = new Metadata();
        contentTypeHint.set(Metadata.CONTENT_TYPE, ctype.getBaseType().toLowerCase(Locale.ENGLISH));
        String content = (new Tika()).parseToString(is, contentTypeHint);
        if (content == null || content.trim().length() == 0)
            return;
        if (row.get(ATTACHMENT) == null)
            row.put(ATTACHMENT, new ArrayList<String>());
        List<String> contents = (List<String>) row.get(ATTACHMENT);
        contents.add(content.trim());
        row.put(ATTACHMENT, contents);
        if (row.get(ATTACHMENT_NAMES) == null)
            row.put(ATTACHMENT_NAMES, new ArrayList<String>());
        List<String> names = (List<String>) row.get(ATTACHMENT_NAMES);
        names.add(fileName);
        row.put(ATTACHMENT_NAMES, names);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) IMAPMessage(com.sun.mail.imap.IMAPMessage) ContentType(javax.mail.internet.ContentType) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) Tika(org.apache.tika.Tika)

Aggregations

ContentType (javax.mail.internet.ContentType)40 IOException (java.io.IOException)17 MessagingException (javax.mail.MessagingException)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 ByteString (com.linkedin.data.ByteString)10 MimeMessage (javax.mail.internet.MimeMessage)9 MimeMultipart (javax.mail.internet.MimeMultipart)9 InputStream (java.io.InputStream)8 ParseException (javax.mail.internet.ParseException)8 MimePart (javax.mail.internet.MimePart)6 PackageException (com.axway.ats.action.objects.model.PackageException)5 DataHandler (javax.activation.DataHandler)5 BodyPart (javax.mail.BodyPart)5 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)4 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URI (java.net.URI)3