Search in sources :

Example 16 with MimePart

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

the class ImapMessage method serializeStructure.

static void serializeStructure(PrintStream ps, MimeMessage root, boolean extensions) throws IOException, MessagingException {
    LinkedList<LinkedList<MPartInfo>> queue = new LinkedList<LinkedList<MPartInfo>>();
    LinkedList<MPartInfo> level = new LinkedList<MPartInfo>();
    level.add(Mime.getParts(root).get(0));
    queue.add(level);
    boolean pop = false;
    while (!queue.isEmpty()) {
        level = queue.getLast();
        if (level.isEmpty()) {
            queue.removeLast();
            pop = true;
            continue;
        }
        MPartInfo mpi = level.getFirst();
        MimePart mp = mpi.getMimePart();
        boolean hasChildren = mpi.getChildren() != null && !mpi.getChildren().isEmpty();
        // we used to force unset charsets on text/plain parts to US-ASCII, but that always seemed unwise...
        ContentType ctype = new ContentType(mp.getHeader("Content-Type", null)).setContentType(mpi.getContentType());
        String primary = nATOM(ctype.getPrimaryType()), subtype = nATOM(ctype.getSubType());
        if (!pop)
            ps.write('(');
        if (primary.equals("\"MULTIPART\"")) {
            if (!pop) {
                //         list is the multipart subtype (mixed, digest, parallel, alternative, etc.)."
                if (!hasChildren) {
                    ps.print("NIL");
                } else {
                    queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren()));
                    continue;
                }
            }
            ps.write(' ');
            ps.print(subtype);
            if (extensions) {
                // 7.4.2: "Extension data follows the multipart subtype.  Extension data is never
                //         returned with the BODY fetch, but can be returned with a BODYSTRUCTURE
                //         fetch.  Extension data, if present, MUST be in the defined order.  The
                //         extension data of a multipart body part are in the following order:
                //         body parameter parenthesized list, body disposition, body language,
                //         body location"
                ps.write(' ');
                nparams(ps, ctype);
                ps.write(' ');
                ndisposition(ps, mp.getHeader("Content-Disposition", null));
                ps.write(' ');
                nlist(ps, mp.getContentLanguage());
                ps.write(' ');
                nstring(ps, mp.getHeader("Content-Location", null));
            }
        } else {
            if (!pop) {
                // 7.4.2: "The basic fields of a non-multipart body part are in the following order:
                //         body type, body subtype, body parameter parenthesized list, body id, body
                //         description, body encoding, body size."
                String cte = mp.getEncoding();
                cte = (cte == null || cte.trim().equals("") ? "7bit" : cte);
                aSTRING(ps, ctype.getPrimaryType());
                ps.write(' ');
                aSTRING(ps, ctype.getSubType());
                ps.write(' ');
                nparams(ps, ctype);
                ps.write(' ');
                nstring(ps, mp.getContentID());
                ps.write(' ');
                nstring2047(ps, mp.getDescription());
                ps.write(' ');
                aSTRING(ps, cte);
                ps.write(' ');
                ps.print(Math.max(mp.getSize(), 0));
            }
            boolean rfc822 = primary.equals("\"MESSAGE\"") && subtype.equals("\"RFC822\"");
            if (rfc822) {
                //         size in text lines of the encapsulated message."
                if (!pop) {
                    if (!hasChildren) {
                        ps.print(" NIL NIL");
                    } else {
                        MimeMessage mm = (MimeMessage) mpi.getChildren().get(0).getMimePart();
                        ps.write(' ');
                        serializeEnvelope(ps, mm);
                        ps.write(' ');
                        queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren()));
                        continue;
                    }
                }
                ps.write(' ');
                ps.print(getLineCount(mp));
            } else if (primary.equals("\"TEXT\"")) {
                // 7.4.2: "A body type of type TEXT contains, immediately after the basic fields, the
                //         size of the body in text lines.  Note that this size is the size in its
                //         content transfer encoding and not the resulting size after any decoding."
                ps.write(' ');
                ps.print(getLineCount(mp));
            }
            if (extensions) {
                // 7.4.2: "Extension data follows the basic fields and the type-specific fields
                //         listed above.  Extension data is never returned with the BODY fetch,
                //         but can be returned with a BODYSTRUCTURE fetch.  Extension data, if
                //         present, MUST be in the defined order.  The extension data of a
                //         non-multipart body part are in the following order: body MD5, body
                //         disposition, body language, body location"
                ps.write(' ');
                nstring(ps, mp.getContentMD5());
                ps.write(' ');
                ndisposition(ps, mp.getHeader("Content-Disposition", null));
                ps.write(' ');
                nlist(ps, mp.getContentLanguage());
                ps.write(' ');
                nstring(ps, mp.getHeader("Content-Location", null));
            }
        }
        ps.write(')');
        level.removeFirst();
        pop = false;
    }
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) ContentType(com.zimbra.common.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) LinkedList(java.util.LinkedList)

Example 17 with MimePart

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

the class ParseMimeMessage method attachPart.

private static void attachPart(MimeMultipart mmp, ItemId iid, String part, String contentID, ParseMessageContext ctxt, String contentDisposition) throws IOException, MessagingException, ServiceException {
    if (!iid.isLocal()) {
        Map<String, String> params = new HashMap<String, String>(3);
        params.put(UserServlet.QP_PART, part);
        attachRemoteItem(mmp, iid, contentID, ctxt, params, null);
        return;
    }
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
    MimeMessage mm;
    if (iid.hasSubpart()) {
        mm = mbox.getCalendarItemById(ctxt.octxt, iid.getId()).getSubpartMessage(iid.getSubpartId());
    } else {
        mm = mbox.getMessageById(ctxt.octxt, iid.getId()).getMimeMessage();
    }
    MimePart mp = Mime.getMimePart(mm, part);
    if (mp == null) {
        throw MailServiceException.NO_SUCH_PART(part);
    }
    String filename = Mime.getFilename(mp);
    String ctypeHdr = mp.getContentType(), contentType = null;
    if (ctypeHdr != null) {
        contentType = new ContentType(ctypeHdr, ctxt.use2231).cleanup().setCharset(ctxt.defaultCharset).setParameter("name", filename).toString();
    }
    // bug 70015: two concurrent SaveDrafts each reference the same attachment in the original draft
    //   -- avoid race condition by copying attached part to FileUploadServlet, so
    //      deleting original blob doesn't lead to stale BlobInputStream references
    Upload up = FileUploadServlet.saveUpload(mp.getInputStream(), filename, contentType, DocumentHandler.getRequestedAccount(ctxt.zsc).getId());
    ctxt.out.addFetch(up);
    String[] contentDesc = mp.getHeader("Content-Description");
    attachUpload(mmp, up, contentID, ctxt, null, (contentDesc == null || contentDesc.length == 0) ? null : contentDesc[0], contentDisposition);
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ContentType(com.zimbra.common.mime.ContentType) HashMap(java.util.HashMap) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) Upload(com.zimbra.cs.service.FileUploadServlet.Upload)

Example 18 with MimePart

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

the class RemoveAttachments method stripPart.

private static void stripPart(MimeMessage mm, String part) throws IOException, MessagingException, ServiceException {
    MimePart mp = mm;
    int dot = (part = part.trim()).lastIndexOf('.');
    if (dot > 0)
        mp = Mime.getMimePart(mm, part.substring(0, dot));
    if (mp == null)
        throw MailServiceException.NO_SUCH_PART(part);
    String subpart = dot > 0 ? part.substring(dot + 1) : part;
    if (subpart.equalsIgnoreCase("TEXT")) {
        if (!(mp instanceof MimeMessage))
            throw MailServiceException.NO_SUCH_PART(part);
        mp.setText("");
    } else {
        try {
            int partid = Integer.parseInt(subpart);
            if (Mime.getContentType(mp, MimeConstants.CT_DEFAULT).startsWith(MimeConstants.CT_MULTIPART_PREFIX)) {
                MimeMultipart mmp = Mime.getMultipartContent(mp, mp.getContentType());
                if (partid <= 0 || partid > mmp.getCount())
                    throw MailServiceException.NO_SUCH_PART(part);
                mmp.removeBodyPart(partid - 1);
            } else if (mp instanceof MimeMessage && partid == 1) {
                mp.setText("");
            } else {
                throw MailServiceException.NO_SUCH_PART(part);
            }
        } catch (NumberFormatException nfe) {
            throw ServiceException.INVALID_REQUEST("invalid part id: " + part, null);
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimePart(javax.mail.internet.MimePart)

Example 19 with MimePart

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

the class Invite method addInlineATTACHes.

protected ZComponent addInlineATTACHes(ZComponent comp) {
    MimeMessage mimeMsg = null;
    try {
        mimeMsg = getMimeMessage();
    } catch (ServiceException e1) {
        return comp;
    }
    if (mimeMsg == null) {
        return comp;
    }
    try {
        List<MPartInfo> parts = Mime.getParts(mimeMsg, MimeConstants.P_CHARSET_UTF8);
        if (parts != null && !parts.isEmpty()) {
            for (MPartInfo body : parts.get(0).getChildren()) {
                if (body.isMultipart()) {
                    continue;
                }
                MimePart mp = body.getMimePart();
                String ctype = StringUtil.stripControlCharacters(body.getContentType());
                if (MimeConstants.CT_TEXT_CALENDAR.equalsIgnoreCase(ctype)) {
                    // Otherwise it's just an attachment that happens to be a .ics file.
                    try {
                        ContentType ct = new ContentType(body.getMimePart().getContentType());
                        if (ct.getParameter("method") != null) {
                            continue;
                        }
                    } catch (MessagingException e) {
                    }
                }
                String contentType = StringUtil.stripControlCharacters(body.getContentType());
                String fileName = Mime.getFilename(mp);
                try (InputStream in = mp.getInputStream()) {
                    byte[] rawBytes = IOUtils.toByteArray(in);
                    Attach attachment = Attach.fromUnencodedAndContentType(rawBytes, contentType);
                    if (!Strings.isNullOrEmpty(fileName)) {
                        attachment.setFileName(fileName);
                    }
                    comp.addProperty(attachment.toZProperty());
                }
            }
        }
    } catch (MessagingException | IOException e) {
        ZimbraLog.calendar.warn("Problem adding inline ATTACHes", e);
    }
    return comp;
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attach(com.zimbra.common.calendar.Attach) IOException(java.io.IOException) MPartInfo(com.zimbra.cs.mime.MPartInfo) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart)

Example 20 with MimePart

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

the class NativeFormatter method handleMessage.

private void handleMessage(UserServletContext context, Message msg) throws IOException, ServiceException, MessagingException, ServletException {
    if (context.hasBody()) {
        List<MPartInfo> parts = Mime.getParts(msg.getMimeMessage());
        MPartInfo body = Mime.getTextBody(parts, false);
        if (body != null) {
            handleMessagePart(context, body.getMimePart(), msg);
        } else {
            context.resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "body not found");
        }
    } else if (context.hasPart()) {
        MimePart mp = getMimePart(msg, context.getPart());
        handleMessagePart(context, mp, msg);
    } else {
        context.resp.setContentType(MimeConstants.CT_TEXT_PLAIN);
        long size = msg.getSize();
        if (size > 0)
            context.resp.setContentLength((int) size);
        InputStream is = msg.getContentStream();
        ByteUtil.copy(is, true, context.resp.getOutputStream(), false);
    }
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) MimePart(javax.mail.internet.MimePart)

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