Example usage for javax.mail.internet MimeUtility decodeText

List of usage examples for javax.mail.internet MimeUtility decodeText

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility decodeText.

Prototype

public static String decodeText(String etext) throws UnsupportedEncodingException 

Source Link

Document

Decode "unstructured" headers, that is, headers that are defined as '*text' as per RFC 822.

Usage

From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java

private String getFileName(Part part) throws MessagingException {
    String fileName = part.getFileName();
    if (!StringUtil.isDefined(fileName)) {
        try {//from   ww  w .j  av  a  2s  .  c  o  m
            ContentType type = new ContentType(part.getContentType());
            fileName = type.getParameter("name");
        } catch (ParseException e) {
            SilverLogger.getLogger(this).error(e);
        }
    }
    if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) {
        try {
            fileName = MimeUtility.decodeText(part.getFileName());
        } catch (UnsupportedEncodingException e) {
            SilverLogger.getLogger(this).error(e);
        }
    }
    return fileName;
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private static String getFileName(Part part) throws MessagingException {
    String fileName = part.getFileName();
    if (!StringUtil.isDefined(fileName)) {
        try {//from  w w  w .  j a v  a 2 s  . co m
            ContentType type = new ContentType(part.getContentType());
            fileName = type.getParameter("name");
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) {
        try {
            fileName = MimeUtility.decodeText(part.getFileName());
        } catch (UnsupportedEncodingException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    return fileName;
}

From source file:org.simplejavamail.internal.util.MimeMessageParser.java

/**
 * Determines the name of the data source if it is not already set.
 *
 * @param part       the mail part/*from  ww  w.  java 2s.c  o m*/
 * @param dataSource the data source
 * @return the name of the data source or {@code null} if no name can be determined
 * @throws MessagingException           accessing the part failed
 * @throws UnsupportedEncodingException decoding the text failed
 */
private static String getDataSourceName(final Part part, final DataSource dataSource)
        throws MessagingException, UnsupportedEncodingException {
    String result = dataSource.getName();

    if (result == null || result.length() == 0) {
        result = part.getFileName();
    }

    if (result != null && result.length() > 0) {
        result = MimeUtility.decodeText(result);
    } else {
        result = null;
    }

    return result;
}

From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java

/**
 * Gets a unique value from a mail header. If header is present more than once, only first value is returned. Value
 * is Mime decoded, if needed. If header is not found, an empty string is returned.
 * /*  www  . ja va2 s .co  m*/
 * @param part
 * @param name Header identifier.
 * @return First header value, or empty string if not found.
 * @throws MessagingException
 */
public static String extractSingleHeader(final Part part, final String name) throws MessagingException {
    String[] values = part.getHeader(name);
    if (values != null && values.length > 0) {
        try {
            return MimeUtility.decodeText(values[0]);
        } catch (UnsupportedEncodingException e) {
            return values[0];
        }
    }
    return "";
}

From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java

/**
 * Recursively extracts content of an email. Every Part that has a file name, or is neither multipart, plain text or
 * html, is considered an attachment./* w  ww.j a v a  2 s . c om*/
 * 
 * @param part
 * @return
 * @throws MessagingException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public MailContent extractPartsContent(Part part) throws MessagingException, IOException {
    MailContent mailContent = new MailContent();

    String contentType = part.getContentType().toLowerCase();

    if (!StringUtils.isBlank(part.getFileName()) || (!contentType.startsWith("multipart/")
            && !part.isMimeType("text/plain") && !part.isMimeType("text/html"))) {
        mailContent.addAttachment((MimeBodyPart) part);
    } else if (part.isMimeType("text/plain")) {
        logger.debug("Extracting part PLAIN TEXT");
        mailContent.appendText(MimeUtility.decodeText((String) part.getContent()));
    } else if (part.isMimeType("text/html")) {
        logger.debug("Extracting part HTML");
        mailContent.appendHtml(MimeUtility.decodeText((String) part.getContent()));
    } else if (part.isMimeType("message/rfc822")) {
        logger.debug("Extracting part message/rfc822");
        Message innerMessage = (Message) part.getContent();
        mailContent.addAttachedMail(innerMessage);
        // FIXME attached mails should be loaded previously to their container
    } else if (contentType.startsWith("multipart/")) {
        logger.debug("Extracting MULTIPART");
        Multipart multipart = (Multipart) part.getContent();
        if (contentType.startsWith("multipart/signed")) {
            // Signed multiparts contain 2 parts: first is the content, second is the control information
            // We just ignore the control information
            logger.debug("Extracting SIGNED MULTIPART");
            mailContent.append(extractPartsContent(multipart.getBodyPart(0)));
        } else if (part.isMimeType("multipart/related") || part.isMimeType("multipart/mixed")
                || part.isMimeType("multipart/alternative")) {
            logger.debug("Extracting multipart / related or mixed or alternative");
            // FIXME multipart/alternative should be treated differently than other parts, though the same treatment
            // should be ok most of the time
            // (multipart/alternative is usually one part text/plain and the alternative text/html, so as text and
            // html
            // are always considered alternates by this algorithm, it's ok)
            int i = 0;
            int mcount = multipart.getCount();
            while (i < mcount) {
                logger.debug("Adding MULTIPART #{}", i);
                try {
                    final MailContent innerMailContent = extractPartsContent(multipart.getBodyPart(i));
                    mailContent.append(innerMailContent);
                } catch (Exception e) {
                    logger.warn("Could not add MULTIPART #{} because of {}", i, ExceptionUtils.getRootCause(e));
                }
                i++;
            }
        } else {
            logger.info("Multipart subtype {} not managed", contentType.substring(0, contentType.indexOf(' ')));
        }
    } else {
        logger.info("Message Type {} not managed", contentType.substring(0, contentType.indexOf('/')));
    }

    return mailContent;

}