Search in sources :

Example 36 with MimePart

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

the class NativeFormatter method handleCalendarItem.

private void handleCalendarItem(UserServletContext context, CalendarItem calItem) throws IOException, ServiceException, MessagingException, ServletException {
    if (context.hasPart()) {
        MimePart mp;
        if (context.itemId.hasSubpart()) {
            MimeMessage mbp = calItem.getSubpartMessage(context.itemId.getSubpartId());
            mp = Mime.getMimePart(mbp, context.getPart());
        } else {
            mp = getMimePart(calItem, context.getPart());
        }
        handleMessagePart(context, mp, calItem);
    } else {
        context.resp.setContentType(MimeConstants.CT_TEXT_PLAIN);
        InputStream is = calItem.getRawMessage();
        if (is != null)
            ByteUtil.copy(is, true, context.resp.getOutputStream(), false);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) MimePart(javax.mail.internet.MimePart)

Example 37 with MimePart

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

the class ToXML method encodeInviteAsMP.

/** Encodes an Invite stored within a calendar item object into <m> element
     *  with <mp> elements.
     * @param parent  The Element to add the new <tt>&lt;m></tt> to.
     * @param ifmt    The SOAP request's context.
     * @param calItem The calendar item to serialize.
     * @param iid     The requested item; the contained subpart will be used to
     *                pick the Invite out of the calendar item's blob & metadata.
     * @param part    If non-null, we'll serialuize this message/rfc822 subpart
     *                of the specified Message instead of the Message itself.
     * @param maxSize The maximum amount of content to inline (<=0 is unlimited).
     * @param wantHTML  <tt>true</tt> to prefer HTML parts as the "body",
     *                  <tt>false</tt> to prefer text/plain parts.
     * @param neuter  Whether to rename "src" attributes on HTML <img> tags.
     * @param headers Extra message headers to include in the returned element.
     * @param serializeType If <tt>false</tt>, always serializes as an
     *                      <tt>&lt;m></tt> element.
     * @return The newly-created <tt>&lt;m></tt> Element, which has already
     *         been added as a child to the passed-in <tt>parent</tt>.
     * @throws ServiceException */
public static Element encodeInviteAsMP(Element parent, ItemIdFormatter ifmt, OperationContext octxt, CalendarItem calItem, String recurIdZ, ItemId iid, String part, int maxSize, boolean wantHTML, boolean neuter, Set<String> headers, boolean serializeType, boolean wantExpandGroupInfo) throws ServiceException {
    int invId = iid.getSubpartId();
    Invite[] invites = calItem.getInvites(invId);
    boolean isPublic = calItem.isPublic();
    boolean showAll = isPublic || allowPrivateAccess(octxt, calItem);
    boolean wholeMessage = (part == null || part.trim().isEmpty());
    Element m;
    if (wholeMessage) {
        // We want to return the MODIFIED_CONFLICT fields to enable conflict detection on modify.
        int fields = NOTIFY_FIELDS | Change.CONFLICT;
        m = encodeMessageCommon(parent, ifmt, octxt, calItem, fields, serializeType);
        m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
    } else {
        m = parent.addElement(MailConstants.E_MSG);
        m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
        m.addAttribute(MailConstants.A_PART, part);
    }
    try {
        MimeMessage mm = calItem.getSubpartMessage(invId);
        if (mm != null) {
            if (!wholeMessage) {
                MimePart mp = Mime.getMimePart(mm, part);
                if (mp == null) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                Object content = Mime.getMessageContent(mp);
                if (!(content instanceof MimeMessage)) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                mm = (MimeMessage) content;
            } else {
                part = "";
            }
            if (showAll) {
                addEmails(m, Mime.parseAddressHeader(mm, "From"), EmailType.FROM);
                addEmails(m, Mime.parseAddressHeader(mm, "Sender"), EmailType.SENDER);
                addEmails(m, Mime.parseAddressHeader(mm, "Reply-To"), EmailType.REPLY_TO);
                addEmails(m, Mime.parseAddressHeader(mm, "To"), EmailType.TO);
                addEmails(m, Mime.parseAddressHeader(mm, "Cc"), EmailType.CC);
                addEmails(m, Mime.parseAddressHeader(mm, "Bcc"), EmailType.BCC);
                String subject = Mime.getSubject(mm);
                if (subject != null) {
                    m.addAttribute(MailConstants.E_SUBJECT, StringUtil.stripControlCharacters(subject), Element.Disposition.CONTENT);
                }
                String messageID = mm.getMessageID();
                if (messageID != null && !messageID.trim().isEmpty()) {
                    m.addAttribute(MailConstants.E_MSG_ID_HDR, StringUtil.stripControlCharacters(messageID), Element.Disposition.CONTENT);
                }
                if (!wholeMessage) {
                    m.addAttribute(MailConstants.A_SIZE, mm.getSize());
                }
                java.util.Date sent = mm.getSentDate();
                if (sent != null) {
                    m.addAttribute(MailConstants.A_SENT_DATE, sent.getTime());
                }
            }
        }
        Element invElt = m.addElement(MailConstants.E_INVITE);
        setCalendarItemType(invElt, calItem.getType());
        encodeTimeZoneMap(invElt, calItem.getTimeZoneMap());
        if (invites.length > 0) {
            if (showAll) {
                encodeCalendarReplies(invElt, calItem, invites[0], recurIdZ);
            }
            for (Invite inv : invites) {
                encodeInviteComponent(invElt, ifmt, octxt, calItem, (ItemId) null, inv, NOTIFY_FIELDS, neuter);
            }
        }
        if (mm != null && showAll) {
            if (headers != null) {
                for (String name : headers) {
                    String[] values = mm.getHeader(name);
                    if (values == null) {
                        continue;
                    }
                    for (int i = 0; i < values.length; i++) {
                        m.addKeyValuePair(name, values[i], MailConstants.A_HEADER, MailConstants.A_ATTRIBUTE_NAME);
                    }
                }
            }
            List<MPartInfo> parts = Mime.getParts(mm, getDefaultCharset(calItem));
            if (parts != null && !parts.isEmpty()) {
                Set<MPartInfo> bodies = Mime.getBody(parts, wantHTML);
                addParts(m, parts.get(0), bodies, part, maxSize, neuter, true, getDefaultCharset(calItem), true);
            }
        }
        if (wantExpandGroupInfo) {
            Account authedAcct = octxt.getAuthenticatedUser();
            Account requestedAcct = calItem.getMailbox().getAccount();
            encodeAddrsWithGroupInfo(m, requestedAcct, authedAcct);
        }
    } catch (IOException ex) {
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    } catch (MessagingException ex) {
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    }
    return m;
}
Also used : Account(com.zimbra.cs.account.Account) MessagingException(javax.mail.MessagingException) Date(java.util.Date) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) MPartInfo(com.zimbra.cs.mime.MPartInfo) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 38 with MimePart

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

the class Mime method getMimePart.

public static MimePart getMimePart(MimePart mp, String part) throws IOException, MessagingException {
    if (mp == null) {
        return null;
    }
    if (part == null || part.trim().isEmpty()) {
        return mp;
    }
    part = part.trim();
    boolean digestParent = false;
    String[] subpart = part.split("\\.");
    for (int i = 0; i < subpart.length; i++) {
        int index = Integer.parseInt(subpart[i]);
        if (index <= 0) {
            return null;
        }
        // the content-type determines the expected substructure
        String ct = getContentType(mp, digestParent ? MimeConstants.CT_MESSAGE_RFC822 : MimeConstants.CT_DEFAULT);
        if (ct == null) {
            return null;
        }
        digestParent = ct.equals(MimeConstants.CT_MULTIPART_DIGEST);
        if (ct.startsWith(MimeConstants.CT_MULTIPART_PREFIX)) {
            MimeMultipart mmp = getMultipartContent(mp, ct);
            if (mmp != null && mmp.getCount() >= index) {
                BodyPart bp = mmp.getBodyPart(index - 1);
                if (bp instanceof MimePart) {
                    mp = (MimePart) bp;
                    continue;
                }
            }
        } else if (ct.equals(MimeConstants.CT_MESSAGE_RFC822) || (ct.equals(MimeConstants.CT_APPLICATION_OCTET_STREAM) && isEmlAttachment(mp))) {
            MimeMessage content = getMessageContent(mp);
            if (content != null) {
                if (mp instanceof MimeMessage) {
                    // the top-level part of a non-multipart message is numbered "1"
                    if (index != 1) {
                        return null;
                    }
                } else {
                    i--;
                }
                mp = content;
                continue;
            }
        } else if (mp instanceof MimeMessage && index == 1 && i == subpart.length - 1) {
            // the top-level part of a non-multipart message is numbered "1"
            break;
        }
        return null;
    }
    return mp;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) ZMimePart(com.zimbra.common.zmime.ZMimePart)

Example 39 with MimePart

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

the class CreateContact method fetchItemPart.

static String fetchItemPart(ZimbraSoapContext zsc, OperationContext octxt, Mailbox mbox, ItemId iid, String part, String[] acceptableMimeTypes, String charsetWanted) throws ServiceException {
    String text = null;
    try {
        if (iid.isLocal()) {
            // fetch from local store
            if (!mbox.getAccountId().equals(iid.getAccountId())) {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
            }
            Message msg = mbox.getMessageById(octxt, iid.getId());
            MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
            String ctype = new ContentType(mp.getContentType()).getContentType();
            String fname = mp.getFileName();
            if (fname != null && (MimeConstants.CT_APPLICATION_OCTET_STREAM.equals(ctype) || MimeConstants.CT_APPLICATION_TNEF.equals(ctype))) {
                String guess = MimeDetect.getMimeDetect().detect(fname);
                if (guess != null) {
                    ctype = guess;
                }
            }
            boolean typeAcceptable;
            if (acceptableMimeTypes != null) {
                typeAcceptable = false;
                for (String type : acceptableMimeTypes) {
                    if (type != null && type.equalsIgnoreCase(ctype)) {
                        typeAcceptable = true;
                        break;
                    }
                }
            } else {
                typeAcceptable = true;
            }
            if (!typeAcceptable)
                throw MailServiceException.INVALID_CONTENT_TYPE(ctype);
            text = Mime.getStringContent(mp, charsetWanted);
        } else {
            // fetch from remote store
            Map<String, String> params = new HashMap<String, String>();
            params.put(UserServlet.QP_PART, part);
            byte[] content = UserServlet.getRemoteContent(zsc.getAuthToken(), iid, params);
            text = new String(content, MimeConstants.P_CHARSET_UTF8);
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("error fetching message part: iid=" + iid + ", part=" + part, e);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("error fetching message part: iid=" + iid + ", part=" + part, e);
    }
    return text;
}
Also used : Message(com.zimbra.cs.mailbox.Message) ContentType(com.zimbra.common.mime.ContentType) HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) MimePart(javax.mail.internet.MimePart) IOException(java.io.IOException)

Example 40 with MimePart

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

the class ToXML method addContent.

/** Adds the decoded text content of a message part to the {@link Element}.
     *  The content is added as the value of a new <tt>&lt;content></tt>
     *  sub-element.  <i>Note: This method will only extract the content of a
     *  <b>text/*</b> or <b>xml/*</b> message part.</i>
     *
     * @param elt     The element to add the <tt>&lt;content></tt> to.
     * @param mpi     The message part to extract the content from.
     * @param maxSize The maximum number of characters to inline (<=0 is unlimited).
     * @param neuter  Whether to "neuter" image <tt>src</tt> attributes.
     * @parame defaultCharset  The user's default charset preference.
     * @throws MessagingException when message parsing or CTE-decoding fails
     * @throws IOException on error during parsing or defanging
     * @see HtmlDefang#defang(String, boolean) */
private static void addContent(Element elt, MPartInfo mpi, int maxSize, boolean neuter, String defaultCharset, MsgContent wantContent) throws IOException, MessagingException {
    // TODO: support other parts
    String ctype = mpi.getContentType();
    if (!ctype.matches(MimeConstants.CT_TEXT_WILD) && !ctype.matches(MimeConstants.CT_XML_WILD)) {
        return;
    }
    MimePart mp = mpi.getMimePart();
    Mime.repairTransferEncoding(mp);
    String data = null;
    String originalContent = null;
    wantContent = wantContent == null ? MsgContent.full : wantContent;
    try {
        if (maxSize <= 0) {
            maxSize = (int) Provisioning.getInstance().getLocalServer().getMailContentMaxSize();
        } else {
            maxSize = Math.min(maxSize, (int) Provisioning.getInstance().getLocalServer().getMailContentMaxSize());
        }
    } catch (ServiceException e) {
        ZimbraLog.soap.warn("Unable to determine max content size", e);
    }
    boolean wasTruncated = false;
    if (ctype.equals(MimeConstants.CT_TEXT_HTML)) {
        String charset = mpi.getContentTypeParameter(MimeConstants.P_CHARSET);
        InputStream stream = null;
        StringWriter sw = new StringWriter();
        TruncatingWriter tw = null;
        Writer out = sw;
        if (maxSize > 0) {
            tw = new TruncatingWriter(sw, maxSize + 1);
            out = tw;
        }
        Reader reader = null;
        try {
            stream = mp.getInputStream();
            if (charset != null && !charset.trim().isEmpty()) {
                // make sure to feed getTextReader() a full Content-Type header, not just the primary/subtype portion
                reader = Mime.getTextReader(stream, mp.getContentType(), defaultCharset);
                BrowserDefang defanger = DefangFactory.getDefanger(mp.getContentType());
                defanger.defang(reader, neuter, out);
                data = sw.toString();
            } else {
                String cte = mp.getEncoding();
                if (cte != null && !cte.trim().toLowerCase().equals(MimeConstants.ET_7BIT)) {
                    try {
                        DefangFactory.getDefanger(ctype).defang(stream, neuter, out);
                        data = sw.toString();
                    } catch (IOException e) {
                    }
                }
                if (data == null) {
                    reader = Mime.getTextReader(stream, mp.getContentType(), defaultCharset);
                    DefangFactory.getDefanger(ctype).defang(reader, neuter, out);
                    data = sw.toString();
                }
            }
            if (wantContent.equals(MsgContent.original) || wantContent.equals(MsgContent.both)) {
                originalContent = removeQuotedText(data, true);
            }
        } finally {
            if (tw != null) {
                wasTruncated = tw.wasTruncated();
            }
            ByteUtil.closeStream(stream);
            Closeables.closeQuietly(reader);
        }
    } else if (ctype.equals(MimeConstants.CT_TEXT_ENRICHED)) {
        // Enriched text handling is a little funky because TextEnrichedHandler
        // doesn't use Reader and Writer.  As a result, we truncate
        // the source before converting to HTML.
        InputStream stream = mp.getInputStream();
        Reader reader = null;
        try {
            reader = Mime.getTextReader(stream, mp.getContentType(), defaultCharset);
            int maxChars = (maxSize > 0 ? maxSize + 1 : -1);
            String enriched = ByteUtil.getContent(reader, maxChars, false);
            if (enriched.length() == maxChars) {
                // The normal check for truncated data won't work because
                // the length of the converted text is different than the length
                // of the source, so set the attr here.
                wasTruncated = true;
            }
            if (wantContent.equals(MsgContent.original) || wantContent.equals(MsgContent.both)) {
                originalContent = TextEnrichedHandler.convertToHTML(removeQuotedText(enriched, false));
            }
            data = TextEnrichedHandler.convertToHTML(enriched);
        } finally {
            ByteUtil.closeStream(stream);
            Closeables.closeQuietly(reader);
        }
    } else {
        InputStream stream = mp.getInputStream();
        Reader reader = null;
        try {
            reader = Mime.getTextReader(stream, mp.getContentType(), defaultCharset);
            int maxChars = (maxSize > 0 ? maxSize + 1 : -1);
            data = ByteUtil.getContent(reader, maxChars, false);
            if (wantContent.equals(MsgContent.original) || wantContent.equals(MsgContent.both)) {
                originalContent = removeQuotedText(data, false);
            }
            if (data.length() == maxChars) {
                wasTruncated = true;
            }
        } finally {
            ByteUtil.closeStream(stream);
            Closeables.closeQuietly(reader);
        }
    }
    if (data != null && !wantContent.equals(MsgContent.original)) {
        data = StringUtil.stripControlCharacters(data);
        if (wasTruncated) {
            elt.addAttribute(MailConstants.A_TRUNCATED_CONTENT, true);
            if (data.length() > maxSize) {
                data = data.substring(0, maxSize);
            }
        }
        elt.addAttribute(MailConstants.E_CONTENT, data, Element.Disposition.CONTENT);
    }
    if (originalContent != null && (wantContent.equals(MsgContent.original) || wantContent.equals(MsgContent.both))) {
        originalContent = StringUtil.stripControlCharacters(originalContent);
        elt.addUniqueElement(MailConstants.E_ORIG_CONTENT).setText(originalContent);
    }
// TODO: CDATA worth the effort?
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) TruncatingWriter(com.zimbra.common.util.TruncatingWriter) MimePart(javax.mail.internet.MimePart) Reader(java.io.Reader) BrowserDefang(com.zimbra.cs.html.BrowserDefang) IOException(java.io.IOException) TruncatingWriter(com.zimbra.common.util.TruncatingWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

MimePart (javax.mail.internet.MimePart)46 MessagingException (javax.mail.MessagingException)22 MimeMessage (javax.mail.internet.MimeMessage)22 IOException (java.io.IOException)15 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)12 PackageException (com.axway.ats.action.objects.model.PackageException)11 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)10 PublicAtsApi (com.axway.ats.common.PublicAtsApi)10 InputStream (java.io.InputStream)10 ServiceException (com.zimbra.common.service.ServiceException)8 Test (org.junit.Test)8 MimePackage (com.axway.ats.action.objects.MimePackage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ContentType (javax.mail.internet.ContentType)6 BaseTest (com.axway.ats.action.BaseTest)5 Message (com.zimbra.cs.mailbox.Message)5 MPartInfo (com.zimbra.cs.mime.MPartInfo)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ContentType (com.zimbra.common.mime.ContentType)4 ArrayList (java.util.ArrayList)4