com.szmslab.quickjavamail.receive.MessageLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.szmslab.quickjavamail.receive.MessageLoader.java

Source

/*
 * Copyright (c) 2014 szmslab
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
package com.szmslab.quickjavamail.receive;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.mail.Header;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeUtility;

import org.apache.commons.lang3.StringUtils;

import com.szmslab.quickjavamail.utils.AttachmentFile;
import com.szmslab.quickjavamail.utils.InlineImageFile;
import com.szmslab.quickjavamail.utils.MailAddress;

/**
 * ??????
 *
 * @author szmslab
 */
public class MessageLoader {

    /**
     * ??????????
     */
    private boolean isDeleted;

    /**
     * ???
     */
    private Message message;

    /**
     * ?????
     */
    private MessageContent contentCashe;

    /**
     * ??
     *
     * @param message
     *            ???
     */
    public MessageLoader(Message message) {
        this.message = message;
        this.isDeleted = false;
    }

    /**
     * ??
     *
     * @param message
     *            ???
     * @param isDeleted
     *            ??????????
     */
    public MessageLoader(Message message, boolean isDeleted) {
        this.message = message;
        this.isDeleted = isDeleted;
    }

    /**
     * ??????????????
     *
     * @return ??????????
     */
    public boolean isDeleted() {
        return isDeleted;
    }

    /**
     * ?????????????
     *
     * @param isDeleted
     *            ??????????
     */
    public void deleted(boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    /**
     * ???????
     *
     * @return ???
     */
    public Message getOriginalMessage() {
        return message;
    }

    /**
     * Message-ID????
     *
     * @return Message-ID
     * @throws MessagingException
     */
    public String getMessageId() throws MessagingException {
        return StringUtils.defaultString(StringUtils.join(message.getHeader("Message-ID"), ","));
    }

    /**
     * MUA????
     *
     * @return MUA
     * @throws MessagingException
     */
    public String getMessageUserAgent() throws MessagingException {
        String mua = StringUtils.join(message.getHeader("User-Agent"), ",");
        if (StringUtils.isBlank(mua)) {
            mua = StringUtils.defaultString(StringUtils.join(message.getHeader("X-Mailer"), ","));
        }
        return mua;
    }

    /**
     * ?????
     *
     * @return ?
     * @throws MessagingException
     */
    public Date getSentDate() throws MessagingException {
        return message.getSentDate();
    }

    /**
     * ?????
     *
     * @return ?
     * @throws MessagingException
     */
    public int getSize() throws MessagingException {
        return message.getSize();
    }

    /**
     * From????
     *
     * @return From
     * @throws MessagingException
     */
    public List<MailAddress> getFromAddressList() throws MessagingException {
        return toMailAddressList((InternetAddress[]) message.getFrom());
    }

    /**
     * ReplyTo????
     *
     * @return ReplyTo
     * @throws MessagingException
     */
    public List<MailAddress> getReplyToAddressList() throws MessagingException {
        return toMailAddressList((InternetAddress[]) message.getReplyTo());
    }

    /**
     * To????
     *
     * @return To
     * @throws MessagingException
     */
    public List<MailAddress> getToAddressList() throws MessagingException {
        return toMailAddressList((InternetAddress[]) message.getRecipients(RecipientType.TO));
    }

    /**
     * Cc????
     *
     * @return Cc
     * @throws MessagingException
     */
    public List<MailAddress> getCcAddressList() throws MessagingException {
        return toMailAddressList((InternetAddress[]) message.getRecipients(RecipientType.CC));
    }

    /**
     * ?????
     *
     * @return ?
     * @throws MessagingException
     */
    @SuppressWarnings("unchecked")
    public Properties getHeaders() throws MessagingException {
        Properties p = new Properties();
        for (Enumeration<Header> headers = message.getAllHeaders(); headers.hasMoreElements();) {
            Header header = headers.nextElement();
            p.setProperty(header.getName(), header.getValue());
        }
        return p;
    }

    /**
     * ??????
     *
     * @return ??
     * @throws MessagingException
     */
    public String getSubject() throws MessagingException {
        return message.getSubject();
    }

    /**
     * TEXT????
     *
     * @return TEXT
     * @throws MessagingException
     * @throws IOException
     */
    public String getText() throws MessagingException, IOException {
        return getContent().text;
    }

    /**
     * HTML????
     *
     * @return HTML
     * @throws MessagingException
     * @throws IOException
     */
    public String getHtml() throws MessagingException, IOException {
        return getContent().html;
    }

    /**
     * ????
     *
     * @return 
     * @throws MessagingException
     * @throws IOException
     */
    public List<AttachmentFile> getAttachmentFileList() throws MessagingException, IOException {
        return getContent().attachmentFileList;
    }

    /**
     * ?????
     *
     * @return ?
     * @throws MessagingException
     * @throws IOException
     */
    public List<InlineImageFile> getInlineImageFileList() throws MessagingException, IOException {
        return getContent().inlineImageFileList;
    }

    /**
     * ????????
     *
     * @return ????
     * @throws MessagingException
     */
    public boolean isPartial() throws MessagingException {
        return message.getContentType().indexOf("message/partial") >= 0;
    }

    /**
     * ?????
     *
     * @return ?
     * @throws MessagingException
     * @throws IOException
     */
    public ByteArrayInputStream getPartialContent() throws MessagingException, IOException {
        return getContent().partialContent;
    }

    /**
     * InternetAddress??MailAddress??????
     *
     * @param addresses
     *            InternetAddress??
     * @return MailAddress?
     */
    private List<MailAddress> toMailAddressList(InternetAddress[] addresses) {
        List<MailAddress> list = new ArrayList<MailAddress>();
        if (addresses != null) {
            for (InternetAddress address : addresses) {
                list.add(new MailAddress(address.getAddress(), address.getPersonal()));
            }
        }
        return list;
    }

    /**
     * ????????
     *
     * @return ????
     * @throws MessagingException
     * @throws IOException
     */
    private MessageContent getContent() throws MessagingException, IOException {
        if (contentCashe == null) {
            MessageContent msgContent = new MessageContent();
            Object c = message.getContent();
            if (c instanceof Multipart) {
                setMultipartContent((Multipart) c, msgContent);
            } else if (c instanceof ByteArrayInputStream) {
                msgContent.partialContent = (ByteArrayInputStream) c;
            } else {
                msgContent.text = c.toString();
            }
            contentCashe = msgContent;
        }
        return contentCashe;
    }

    /**
     * ?????MessageContent????
     *
     * @param multiPart
     *            ?
     * @param msgContent
     *            ????
     * @throws MessagingException
     * @throws IOException
     */
    private void setMultipartContent(Multipart multiPart, MessageContent msgContent)
            throws MessagingException, IOException {
        for (int i = 0; i < multiPart.getCount(); i++) {
            Part part = multiPart.getBodyPart(i);
            if (part.getContentType().indexOf("multipart") >= 0) {
                setMultipartContent((Multipart) part.getContent(), msgContent);
            } else {
                String disposition = part.getDisposition();
                if (Part.ATTACHMENT.equals(disposition)) {
                    // Disposition?"attachment"???ContentType????
                    msgContent.attachmentFileList.add(new AttachmentFile(MimeUtility.decodeText(part.getFileName()),
                            part.getDataHandler().getDataSource()));
                } else {
                    if (part.isMimeType("text/html")) {
                        msgContent.html = part.getContent().toString();
                    } else if (part.isMimeType("text/plain")) {
                        msgContent.text = part.getContent().toString();
                    } else {
                        // Disposition?"inline"???ContentType??
                        if (Part.INLINE.equals(disposition)) {
                            String cid = "";
                            if (part instanceof MimeBodyPart) {
                                MimeBodyPart mimePart = (MimeBodyPart) part;
                                cid = mimePart.getContentID();
                            }
                            msgContent.inlineImageFileList
                                    .add(new InlineImageFile(cid, MimeUtility.decodeText(part.getFileName()),
                                            part.getDataHandler().getDataSource()));
                        }
                    }
                }
            }
        }
    }

    /**
     * ????????
     *
     * @author szmslab
     */
    class MessageContent {

        /**
         * (TEXT)
         */
        public String text = "";

        /**
         * (HTML)
         */
        public String html = "";

        /**
         * 
         */
        public List<AttachmentFile> attachmentFileList = new ArrayList<AttachmentFile>();

        /**
         * ?
         */
        public List<InlineImageFile> inlineImageFileList = new ArrayList<InlineImageFile>();

        /**
         * ?
         */
        public ByteArrayInputStream partialContent = null;

    }

}