⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultinboxmsgbuilder.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.inbox.utils;import com.queplix.core.error.GenericSystemException;import com.queplix.core.modules.inbox.InboxHelper;import com.queplix.core.modules.inbox.InboxMessage;import com.queplix.core.modules.mail.Attachment;import com.queplix.core.modules.mail.MailAddress;import com.queplix.core.modules.mail.MailMessage;import com.queplix.core.utils.DateHelper;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.SystemHelper;import org.apache.regexp.RE;import org.apache.regexp.RESyntaxException;import javax.mail.Address;import javax.mail.Header;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Part;import javax.mail.Session;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Enumeration;import java.util.List;import java.util.TimeZone;/** * The class realizes default message builder. * @author Konstantin Mironov * @since 8 Dec 2006 */public class DefaultInboxMsgBuilder    extends AbstractInboxPluggableModule implements InboxMsgBuilder {    // ----------------------------------------------------- variables    protected InboxProvider provider;    protected int uniqueID = 0;    // ----------------------------------------------------- public methods    /*     * No javadoc.     * @see InboxMsgBuilder#setInboxProvider     */    public void setInboxProvider( InboxProvider provider ) {        this.provider = provider;    }    /*     * No javadoc.     * @see InboxMsgBuilder#build     */    public InboxMessage build( Message message )        throws IOException, MessagingException {        return build( message, false );    }    // ----------------------------------------------------- protected methods    /**     * Builds <code>InboxMessage</code> from <code>message</code>.     * @param message Message     * @param isAttachedMail true -- message is attachmed e-mail     * @return InboxMessage     * @throws IOException     * @throws MessagingException     */    protected InboxMessage build( Message message, boolean isAttachedMail )        throws IOException, MessagingException {        //        // Create InboxMessage.        //        // 1. Dates.        Date sentDate = message.getSentDate();        if( sentDate != null ) {            sentDate = new Date( DateHelper.toSystem( sentDate.getTime(), TimeZone.getDefault() ) );        }        Date receiveDate = provider.getServerDate( message );        // 2. 'From' address.        MailAddress fromMa = null;        Address[] froms = message.getFrom();        if( froms != null ) {            InternetAddress from = ( InternetAddress ) froms[0];            DEBUG( logName + "'From' - " + from.getAddress() );            fromMa = new MailAddress( from );        }        // 3. 'To' address.        MailAddress[] toMas = null;        Address[] tos = message.getRecipients( Message.RecipientType.TO );        if( tos != null ) {            toMas = new MailAddress[tos.length];            for( int i = 0; i < tos.length; i++ ) {                InternetAddress to = ( InternetAddress ) tos[i];                DEBUG( logName + "'To' [" + i + "]: " + to.getAddress() );                toMas[i] = new MailAddress( to );            }        }        // 4. 'Cc' address.        Address[] ccs = message.getRecipients( Message.RecipientType.CC );        MailAddress[] ccMas = null;        if( ccs != null ) {            ccMas = new MailAddress[ccs.length];            for( int i = 0; i < ccs.length; i++ ) {                InternetAddress cc = ( InternetAddress ) ccs[i];                DEBUG( logName + "'Cc' [" + i + "]: " + cc.getAddress() );                ccMas[i] = new MailAddress( cc );            }        }        // 5. Subject.        String subject = message.getSubject();        DEBUG( logName + "Subject: " + subject );        // Do create.        InboxMessage msg = new InboxMessage( toMas,                                             fromMa,                                             subject,                                             null );        msg.setSentTime( sentDate );        msg.setReceiveTime( receiveDate );        msg.setCc( ccMas );        // Add message headers.        for( Enumeration en = message.getAllHeaders(); en.hasMoreElements(); ) {            Header header = ( Header ) en.nextElement();            String name = header.getName();            String value = header.getValue();            msg.addHeader( name, value );        }        // Add UID (only for non-attached mails).        if( !isAttachedMail ) {            if( provider == null ) {                throw new NullPointerException( "Inbox provider not set!" );            }            String uid = provider.getUID( message );            if( uid != null ) {                msg.setMessageUid( uid );            }            DEBUG( logName + "UID: " + uid );        }        // Parse message body.        parseMessage( msg, message, 0 );        // set message digest        String messageDigest = InboxHelper.getMessageDigest(msg);        if (messageDigest.length() > 0)            msg.setMessageDigest(messageDigest);        return msg;    }    /**     * Parse mail part <code>messagePart</code> and build structure.     *     * Algorithm:     *     *      0. begin     *      1. if multipart/alternative -> in cycle check content-type for     *              sub parts and set text using rules described #6     *              (if content-type = null, then treat it as text/plain)     *      2. if multipart/+ -> in cycle call #0 for every sub part     *      3. if message/rfc822 -> call #0 and pass     *              new InboxMessage instance, merge two instances of InboxMessage     *      4. if Part.getFileName != null && Part.getContentID != null -> treat it as attachment     *      5. if Part.getFileName != null -> treat it as attachment     *      6. if text not initalized yet && if text/+ -> treat it as text     *      7. end     *     * @param msg InboxMessage     * @param messagePart mail part     * @param depth current depth (for multipart parts)     * @throws MessagingException     * @throws IOException     */    protected void parseMessage( InboxMessage msg, Part messagePart, int depth )        throws MessagingException, IOException {        // Get content        Object content = messagePart.getContent();        // Get content type.        String contentType = getContentType( messagePart );        DEBUG( logName + "Message content type - " + contentType );        DEBUG( logName + "Message content size - " + messagePart.getSize() );        DEBUG( logName + "Message content is part - " + ( content instanceof Part ) );        DEBUG( logName + "Message content is multi part - " + ( content instanceof Multipart ) );        DEBUG( logName + "Depth - " + depth );        // If multipart message ->        if( content != null && content instanceof Multipart ) {            Multipart messageMultiPart = ( Multipart ) content;            int countParts = messageMultiPart.getCount();            DEBUG( logName + "Found multipart message. Parts: " + countParts );            // 1. Check "multipart/alternative".            //      .. for "depth" > 0 only            if( depth > 0 && contentType.indexOf( InboxHelper.CONTENT_TYPE_ALTERNATIVE ) > -1 ) {                DEBUG( logName + "Found alternative part. Content type: " + contentType );                // 1.1 Looking for HTML part                for( int i = 0; i < countParts; i++ ) {                    Part partBodyPart = messageMultiPart.getBodyPart( i );                    String partContentType = partBodyPart.getContentType();                    if( partContentType != null &&                        partContentType.toUpperCase().indexOf( InboxHelper.CONTENT_TYPE_HTML ) > -1 ) {                        // HTML found!                        String body = getMessageBody( partBodyPart, InboxHelper.CONTENT_TYPE_HTML );                        msg.setBody( body );                        DEBUG( logName + "Found HTML alternative sub part. Content type: " + partContentType );                        return;                    }                }                // 1.2 Looking for Text part                for( int i = 0; i < countParts; i++ ) {                    Part partBodyPart = messageMultiPart.getBodyPart( i );                    String partContentType = partBodyPart.getContentType();                    if( partContentType == null ||                        partContentType.toUpperCase().indexOf( InboxHelper.CONTENT_TYPE_PLAIN ) > -1 ) {                        // Text found!                        String body = getMessageBody( partBodyPart, InboxHelper.CONTENT_TYPE_PLAIN );                        msg.setBody( body );                        DEBUG( logName + "Found Text alternative sub part. Content type: " + partContentType );                        return;                    }                }                // Text part not found. Go to #2!            }            // 2. For other Multiparts in cycle call #parseMessage for every sub part            for( int i = 0; i < countParts; i++ ) {                Part nextPart = messageMultiPart.getBodyPart( i );                parseMessage( msg, nextPart, depth + 1 );            }            return;        } //-- end if multipart message        //        // Process non-multipart part.        //        // 3. Check "message/rfc822"        if( contentType.equalsIgnoreCase( InboxHelper.CONTENT_TYPE_MESSAGE ) ) {            DEBUG( logName + "Found 'message/rfc822' part. Content type: " + contentType );            Session session = Session.getDefaultInstance( System.getProperties(), null );            MimeMessage attachMimeMessage = new MimeMessage( session, messagePart.getInputStream() );            InboxMessage attachMsg = build( attachMimeMessage, true );            appendMessage( msg, attachMsg );            return;        }        Part filePart = null;        if( content != null && content instanceof Part ) {            filePart = ( Part ) content;        } else {            filePart = messagePart;        }        String fileName = filePart.getFileName();        if( fileName != null ) {            DEBUG( logName + "Found attachment part. File name: " + fileName );            // 4. Process attachment            Attachment attach = getMessageAttachment( filePart );            if( attach != null ) {                msg.addAttachments( attach );            }        } else {            DEBUG( logName + "Found text part. Content type: " + filePart.getContentType() );            // 5. Process message.            String body = getMessageBody( filePart );            if( body != null ) {                msg.setBody( body );            }        }    }    /**     * The method gets message body from the email message     * @param messagePart Part     * @return String     * @throws IOException     * @throws MessagingException     */    protected String getMessageBody( Part messagePart )        throws IOException, MessagingException {        String contentType = getContentType( messagePart );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -