Example usage for javax.mail.internet MimePart getDataHandler

List of usage examples for javax.mail.internet MimePart getDataHandler

Introduction

In this page you can find the example usage for javax.mail.internet MimePart getDataHandler.

Prototype

public DataHandler getDataHandler() throws MessagingException;

Source Link

Document

Return a DataHandler for the content within this part.

Usage

From source file:com.cubusmail.mail.MessageHandler.java

/**
 * @param msg//from   w w w  . j  av  a2 s.  c om
 * @throws MessagingException
 * @throws IOException
 */
public void createForwardMessage(Message msg) throws MessagingException, IOException {

    init();
    setSubject("Fwd: " + msg.getSubject());
    setHtmlMessage(MessageUtils.isHtmlMessage(msg));
    List<MimePart> attachments = MessageUtils.attachmentsFromPart(msg);
    if (attachments != null) {
        for (MimePart part : attachments) {
            DataSource source = part.getDataHandler().getDataSource();

            ByteArrayDataSource newSource = new ByteArrayDataSource(source.getInputStream(),
                    source.getContentType());

            if (StringUtils.isEmpty(source.getName())) {
                newSource.setName(this.applicationContext.getMessage("message.unknown.attachment", null,
                        SessionManager.get().getLocale()));
            } else {
                newSource.setName(source.getName());
            }

            addComposeAttachment(newSource);
        }
    }
    Preferences prefs = SessionManager.get().getPreferences();
    MessageTextUtil.messageTextFromPart(msg, this, true, MessageTextMode.REPLY, prefs, 0);
}

From source file:com.stimulus.archiva.store.MessageStore.java

public void writeTo(MimePart part, OutputStream os, String[] includeList)
        throws IOException, MessagingException {
    LineOutputStream los = null;//from   w ww . ja  v  a2  s .c o  m
    if (os instanceof LineOutputStream) {
        los = (LineOutputStream) os;
    } else {
        los = new LineOutputStream(os);
    }
    Enumeration hdrLines = part.getMatchingHeaderLines(includeList);
    while (hdrLines.hasMoreElements()) {
        String line = (String) hdrLines.nextElement();
        los.writeln(line);
    }
    los.writeln();
    os = MimeUtility.encode(os, part.getEncoding());
    part.getDataHandler().writeTo(os);
    os.flush();
}

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

/**
 * Checks whether the MimePart contains an object of the given mime type.
 *
 * @param part     the current MimePart//from  w w  w.j a va 2s . c o  m
 * @param mimeType the mime type to check
 * @return {@code true} if the MimePart matches the given mime type, {@code false} otherwise
 * @throws MessagingException parsing the MimeMessage failed
 */
private static boolean isMimeType(final MimePart part, final String mimeType) throws MessagingException {
    // Do not use part.isMimeType(String) as it is broken for MimeBodyPart
    // and does not really check the actual content type.

    try {
        final ContentType ct = new ContentType(part.getDataHandler().getContentType());
        return ct.match(mimeType);
    } catch (final ParseException ex) {
        return part.getContentType().equalsIgnoreCase(mimeType);
    }
}

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

/**
 * Parses the MimePart to create a DataSource.
 *
 * @param part   the current part to be processed
 * @return the DataSource/*  w  ww.j a v  a 2 s  .c o  m*/
 * @throws MessagingException creating the DataSource failed
 * @throws IOException        creating the DataSource failed
 */
private static DataSource createDataSource(final MimePart part) throws MessagingException, IOException {
    final DataHandler dataHandler = part.getDataHandler();
    final DataSource dataSource = dataHandler.getDataSource();
    final String contentType = getBaseMimeType(dataSource.getContentType());
    final byte[] content = MimeMessageParser.getContent(dataSource.getInputStream());
    final ByteArrayDataSource result = new ByteArrayDataSource(content, contentType);
    final String dataSourceName = getDataSourceName(part, dataSource);

    result.setName(dataSourceName);
    return result;
}