Search in sources :

Example 11 with MimePart

use of javax.mail.internet.MimePart in project ats-framework by Axway.

the class MimePackage method getAttachmentFileName.

/**
     * Get an attachment's file name
     *
     * @param partIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getAttachmentFileName(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= attachmentPartIndices.size()) {
        throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(attachmentPartIndices.get(partIndex));
        // get the attachment file name
        String fileName = part.getFileName();
        if (fileName == null) {
            throw new PackageException("Could not determine file name for attachment at position " + partIndex);
        }
        return fileName;
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 12 with MimePart

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

the class AttachmentDataSource method getName.

public String getName() {
    MimePart mp = null;
    String name = null;
    try {
        mp = getMimePart();
        if (mp != null) {
            name = mp.getFileName();
        }
    } catch (Exception e) {
        ZimbraLog.mailbox.error("Unable to determine the filename for contact %d, part %s.", mContact.getId(), mPartName, e);
    }
    return name;
}
Also used : MimePart(javax.mail.internet.MimePart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException)

Example 13 with MimePart

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

the class Mime method listParts.

private static List<MPartInfo> listParts(MimePart root, String defaultCharset) throws MessagingException, IOException {
    List<MPartInfo> parts = new ArrayList<MPartInfo>();
    LinkedList<MPartInfo> queue = new LinkedList<MPartInfo>();
    queue.add(generateMPartInfo(root, null, "", 0));
    MimeMultipart emptyMultipart = null;
    while (!queue.isEmpty()) {
        MPartInfo mpart = queue.removeFirst();
        MimePart mp = mpart.getMimePart();
        parts.add(mpart);
        String cts = mpart.mContentType;
        boolean isMultipart = cts.startsWith(MimeConstants.CT_MULTIPART_PREFIX);
        boolean isMessage = !isMultipart && cts.equals(MimeConstants.CT_MESSAGE_RFC822);
        if (isMultipart) {
            // IMAP part numbering is screwy: top-level multipart doesn't get a number
            String prefix = mpart.mPartName.length() > 0 ? (mpart.mPartName + '.') : "";
            if (mp instanceof MimeMessage) {
                mpart.mPartName = prefix + "TEXT";
            }
            MimeMultipart multi = getMultipartContent(mp, cts);
            if (multi != null) {
                if (multi.getCount() == 0 && LC.mime_promote_empty_multipart.booleanValue()) {
                    if (emptyMultipart == null) {
                        emptyMultipart = multi;
                    }
                    if (MimeConstants.CT_MULTIPART_APPLEDOUBLE.equalsIgnoreCase(getContentType(mp))) {
                        ZimbraLog.misc.debug("appledouble with no children; assuming it is malformed and really applefile");
                        mpart.mContentType = mpart.mContentType.replace(MimeConstants.CT_MULTIPART_APPLEDOUBLE, MimeConstants.CT_APPLEFILE);
                    }
                }
                mpart.mChildren = new ArrayList<MPartInfo>(multi.getCount());
                for (int i = 1; i <= multi.getCount(); i++) {
                    mpart.mChildren.add(generateMPartInfo((MimePart) multi.getBodyPart(i - 1), mpart, prefix + i, i));
                }
                queue.addAll(0, mpart.mChildren);
            }
        } else if (isMessage) {
            MimeMessage mm = getMessageContent(mp);
            if (mm != null) {
                MPartInfo child = generateMPartInfo(mm, mpart, mpart.mPartName, 0);
                queue.addFirst(child);
                mpart.mChildren = Arrays.asList(child);
            }
        } else {
        // nothing to do at this stage
        }
    }
    if (emptyMultipart != null && parts.size() == 1) {
        String text = emptyMultipart.getPreamble();
        if (!StringUtil.isNullOrEmpty(text)) {
            ZimbraLog.misc.debug("single multipart with no children. promoting the preamble into a single text part");
            parts.remove(0);
            MPartInfo mpart = new MPartInfo();
            ZMimeBodyPart mp = new ZMimeBodyPart();
            mp.setText(text, defaultCharset);
            mpart.mPart = mp;
            mpart.mContentType = mp.getContentType();
            mpart.mDisposition = "";
            mpart.mPartName = "1";
            parts.add(mpart);
        }
    }
    return parts;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) 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 14 with MimePart

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

the class SyncFormatter method handleMessagePart.

private void handleMessagePart(UserServletContext context, MailItem item) throws IOException, ServiceException, MessagingException, UserServletException {
    if (!(item instanceof Message))
        throw UserServletException.notImplemented("can only handle messages");
    Message message = (Message) item;
    MimePart mp = getMimePart(message, context.getPart());
    if (mp != null) {
        String contentType = mp.getContentType();
        if (contentType == null)
            contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
        sendbackOriginalDoc(mp, contentType, context.req, context.resp);
        return;
    }
    context.resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "part not found");
}
Also used : Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MimePart(javax.mail.internet.MimePart)

Example 15 with MimePart

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

the class TestMain method doConversion.

private static boolean doConversion(MimeMessage mm, Writer out, TnefToICalendar converter, File tnefFile, boolean debug) throws ServiceException, IOException, MessagingException {
    boolean successful = false;
    // Find the TNEF part.
    TNEFPartFinder finder = new TNEFPartFinder();
    finder.accept(mm);
    MimePart tnefPart = finder.getTNEFPart();
    if (tnefPart == null)
        throw ServiceException.FAILURE("No TNEF part in the message!", null);
    if (tnefFile != null) {
        FileOutputStream wout = null;
        try {
            byte[] bytes = ByteUtil.getContent(tnefPart.getInputStream(), tnefPart.getSize());
            wout = new FileOutputStream(tnefFile);
            wout.write(bytes);
            wout.flush();
        } finally {
            if (wout != null) {
                wout.close();
            }
        }
    }
    if (debug) {
        // Test text part extraction.
        String desc = getPlainText(mm);
        if (desc != null) {
            System.out.println("<<< TEXT PART BEGIN >>>");
            System.out.println(desc);
            System.out.println("<<< TEXT PART END >>>");
        }
    }
    if (converter == null)
        throw ServiceException.FAILURE("No converter!", null);
    TestContentHandler icalGen = new TestContentHandler(debug);
    successful = converter.convert(mm, tnefPart.getInputStream(), icalGen);
    if (successful) {
        for (ZVCalendar cal : icalGen.getCals()) {
            cal.toICalendar(out);
        }
    }
    out.flush();
    return successful;
}
Also used : ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) FileOutputStream(java.io.FileOutputStream) 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