Search in sources :

Example 1 with ParseException

use of jakarta.mail.internet.ParseException in project spring-integration-samples by spring-projects.

the class EmailParserUtils method handleMultipart.

/**
 * Parses any {@link Multipart} instances that contain text or Html attachments,
 * {@link InputStream} instances, additional instances of {@link Multipart}
 * or other attached instances of {@link jakarta.mail.Message}.
 *
 * Will create the respective {@link EmailFragment}s representing those attachments.
 *
 * Instances of {@link jakarta.mail.Message} are delegated to
 * {@link #handleMessage(File, jakarta.mail.Message, List)}. Further instances
 * of {@link Multipart} are delegated to
 * {@link #handleMultipart(File, Multipart, jakarta.mail.Message, List)}.
 *
 * @param directory Must not be null
 * @param multipart Must not be null
 * @param mailMessage Must not be null
 * @param emailFragments Must not be null
 */
public static void handleMultipart(File directory, Multipart multipart, jakarta.mail.Message mailMessage, List<EmailFragment> emailFragments) {
    Assert.notNull(directory, "The directory must not be null.");
    Assert.notNull(multipart, "The multipart object to be parsed must not be null.");
    Assert.notNull(mailMessage, "The mail message to be parsed must not be null.");
    Assert.notNull(emailFragments, "The collection of emailfragments must not be null.");
    final int count;
    try {
        count = multipart.getCount();
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Number of enclosed BodyPart objects: %s.", count));
        }
    } catch (MessagingException e) {
        throw new IllegalStateException("Error while retrieving the number of enclosed BodyPart objects.", e);
    }
    for (int i = 0; i < count; i++) {
        final BodyPart bp;
        try {
            bp = multipart.getBodyPart(i);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving body part.", e);
        }
        final String contentType;
        String filename;
        final String disposition;
        final String subject;
        try {
            contentType = bp.getContentType();
            filename = bp.getFileName();
            disposition = bp.getDisposition();
            subject = mailMessage.getSubject();
            if (filename == null && bp instanceof MimeBodyPart) {
                filename = ((MimeBodyPart) bp).getContentID();
            }
        } catch (MessagingException e) {
            throw new IllegalStateException("Unable to retrieve body part meta data.", e);
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("BodyPart - Content Type: '%s', filename: '%s', disposition: '%s', subject: '%s'", new Object[] { contentType, filename, disposition, subject }));
        }
        if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
            LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
        }
        final Object content;
        try {
            content = bp.getContent();
        } catch (IOException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        }
        if (content instanceof String) {
            if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                emailFragments.add(new EmailFragment(directory, i + "-" + filename, content));
                LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
            } else {
                final String textFilename;
                final ContentType ct;
                try {
                    ct = new ContentType(contentType);
                } catch (ParseException e) {
                    throw new IllegalStateException("Error while parsing content type '" + contentType + "'.", e);
                }
                if ("text/plain".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.txt";
                } else if ("text/html".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.html";
                } else {
                    textFilename = "message.other";
                }
                emailFragments.add(new EmailFragment(directory, textFilename, content));
            }
        } else if (content instanceof InputStream) {
            final InputStream inputStream = (InputStream) content;
            final ByteArrayOutputStream bis = new ByteArrayOutputStream();
            try {
                IOUtils.copy(inputStream, bis);
            } catch (IOException e) {
                throw new IllegalStateException("Error while copying input stream to the ByteArrayOutputStream.", e);
            }
            emailFragments.add(new EmailFragment(directory, filename, bis.toByteArray()));
        } else if (content instanceof jakarta.mail.Message) {
            handleMessage(directory, (jakarta.mail.Message) content, emailFragments);
        } else if (content instanceof Multipart) {
            final Multipart mp2 = (Multipart) content;
            handleMultipart(directory, mp2, mailMessage, emailFragments);
        } else {
            throw new IllegalStateException("Content type not handled: " + content.getClass().getSimpleName());
        }
    }
}
Also used : MimeBodyPart(jakarta.mail.internet.MimeBodyPart) BodyPart(jakarta.mail.BodyPart) Multipart(jakarta.mail.Multipart) ContentType(jakarta.mail.internet.ContentType) MessagingException(jakarta.mail.MessagingException) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(jakarta.mail.internet.ParseException) MimeBodyPart(jakarta.mail.internet.MimeBodyPart)

Aggregations

BodyPart (jakarta.mail.BodyPart)1 MessagingException (jakarta.mail.MessagingException)1 Multipart (jakarta.mail.Multipart)1 ContentType (jakarta.mail.internet.ContentType)1 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)1 ParseException (jakarta.mail.internet.ParseException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1