Example usage for javax.mail Part getDataHandler

List of usage examples for javax.mail Part getDataHandler

Introduction

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

Prototype

public DataHandler getDataHandler() throws MessagingException;

Source Link

Document

Return a DataHandler for the content within this part.

Usage

From source file:org.mule.transport.email.MailMuleMessageFactory.java

@Override
protected void addAttachments(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception {
    super.addAttachments(muleMessage, transportMessage);

    Object content = ((Message) transportMessage).getContent();
    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;

        TreeMap<String, Part> attachments = new TreeMap<String, Part>();
        MailUtils.getAttachments(multipart, attachments);

        log.debug("Received Multipart message. Adding attachments");
        for (Map.Entry<String, Part> entry : attachments.entrySet()) {
            Part part = entry.getValue();
            String name = entry.getKey();

            muleMessage.addInboundAttachment(name, part.getDataHandler());
            addAttachmentHeaders(name, part, muleMessage);
        }//  w w  w.  j  a v a2s  .co m
    }
}

From source file:org.nuxeo.ecm.platform.mail.action.TransformMessageAction.java

private void processMultipartMessage(MimeMultipart parts) throws MessagingException, IOException {
    log.debug("processing multipart message.");
    for (int i = 0; i < parts.getCount(); i++) {
        Part part = parts.getBodyPart(i);
        if (part.getDataHandler().getContent() instanceof MimeMultipart) {
            log.debug("** found embedded multipart message");
            processMultipartMessage((MimeMultipart) part.getDataHandler().getContent());
            continue;
        }//  ww w .j  a va  2 s  .  c o m
        log.debug("processing single part message: " + part.getClass());
        processSingleMessagePart(part);
    }
}

From source file:org.nuxeo.ecm.platform.mail.listener.action.ExtractMessageInformationAction.java

/**
 * Interprets the body accordingly to the charset used. It relies on the content type being
 * ****;charset={charset};******//  w  ww  .j a  v  a2  s .  c o m
 *
 * @return the decoded String
 */
protected static String decodeMailBody(Part part) throws MessagingException, IOException {

    String encoding = null;

    // try to get encoding from header rather than from Stream !
    // unfortunately, this does not seem to be reliable ...
    /*
     * String[] cteHeader = part.getHeader("Content-Transfer-Encoding"); if (cteHeader!=null && cteHeader.length>0)
     * { encoding = cteHeader[0].toLowerCase(); }
     */

    // fall back to default sniffing
    // that will actually read the stream from server
    if (encoding == null) {
        encoding = MimeUtility.getEncoding(part.getDataHandler());
    }

    InputStream is = null;
    try {
        is = MimeUtility.decode(part.getInputStream(), encoding);
    } catch (IOException ex) {
        log.error("Unable to read content", ex);
        return "";
    }

    String contType = part.getContentType();
    final String charsetIdentifier = "charset=";
    final String ISO88591 = "iso-8859-1";
    final String WINDOWS1252 = "windows-1252";
    int offset = contType.indexOf(charsetIdentifier);
    String charset = "";
    if (offset >= 0) {
        charset = contType.substring(offset + charsetIdentifier.length());
        offset = charset.indexOf(";");
        if (offset > 0) {
            charset = charset.substring(0, offset);
        }
    }
    // Charset could be like "utf-8" or utf-8
    if (!"".equals(charset)) {
        charset = charset.replaceAll("\"", "");
    }
    log.debug("Content type: " + contType + "; charset: " + charset);
    if (charset.equalsIgnoreCase(ISO88591)) {
        // see
        // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#character1
        // for more details see http://en.wikipedia.org/wiki/ISO_8859-1
        // section "ISO-8859-1 and Windows-1252 confusion"
        charset = WINDOWS1252;
        log.debug("Using replacing charset: " + charset);
    }
    String ret;
    byte[] streamContent = FileUtils.readBytes(is);
    if ("".equals(charset)) {
        ret = new String(streamContent);
    } else {
        try {
            ret = new String(streamContent, charset);
        } catch (UnsupportedEncodingException e) {
            // try without encoding
            ret = new String(streamContent);
        }
    }
    return ret;
}