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

📄 defaultinboxmsgbuilder.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if( contentType.indexOf( InboxHelper.CONTENT_TYPE_HTML ) > -1 ) {            return getMessageBody( messagePart, InboxHelper.CONTENT_TYPE_HTML );        } else if( contentType.indexOf( InboxHelper.CONTENT_TYPE_PLAIN ) > -1 ) {            return getMessageBody( messagePart, InboxHelper.CONTENT_TYPE_PLAIN );        } else if( contentType.indexOf( InboxHelper.CONTENT_TYPE_TEXT ) > -1 ) {            return getMessageBody( messagePart, InboxHelper.CONTENT_TYPE_TEXT );        } else {            return null;        }    }    /**     * The method gets message body from the email message     * @param messagePart Part     * @param recognizedContentType it can be:     *          InboxHelper.CONTENT_TYPE_HTML or     *          InboxHelper.CONTENT_TYPE_PLAIN or     *          InboxHelper.CONTENT_TYPE_TEXT     * @return String     * @throws IOException     * @throws MessagingException     */    protected String getMessageBody( Part messagePart, String recognizedContentType )        throws IOException, MessagingException {        if( recognizedContentType == null ) {            throw new NullPointerException( "Got NULL recognized content type" );        }        // serialize Part into temp buffer        InputStream is = messagePart.getInputStream();        ByteArrayOutputStream bosAtt = new ByteArrayOutputStream();        byte buf[] = new byte[1024];        int len;        while( ( len = is.read( buf ) ) > 0 ) {            bosAtt.write( buf, 0, len );        }        // get code page from Content Type string        String contentType = getContentType( messagePart );        String charsetValue = getCodePage( contentType );        // build mail body using character set        String mailBody = null;        if( !StringHelper.isEmpty( charsetValue ) ) {            try {                mailBody = new String( bosAtt.toByteArray(), charsetValue );            } catch( UnsupportedEncodingException e ) {                // Log problem with content type!                String msg = "Cant't process the mail body for charset \"" +                    charsetValue + "\". Content type is \"" + contentType + "\"";                INFO( logName + msg );                publisher.INFO( msg );            }        }        if( mailBody == null ) {            // build mail body without character set            mailBody = new String( bosAtt.toByteArray() );        }        // additional check for html body        if( InboxHelper.CONTENT_TYPE_HTML.equals( recognizedContentType ) ) {            if( !StringHelper.isHTML( mailBody ) ) {                if( isHTMLContent( mailBody ) ) {                    mailBody = checkUpHtml( mailBody );                } else {                    mailBody = StringHelper.text2html( mailBody );                }            }            // Build safe HTML.            mailBody = "<html><body>" + StringHelper.clearHtml( mailBody, true ) + "</body></html>";        }        return mailBody;    }    /**     * The method builds <code>Attachment</code> using <code>filePart</code>.     * @param filePart Part     * @return Attachment     * @throws IOException     * @throws MessagingException     */    protected Attachment getMessageAttachment( Part filePart )        throws IOException, MessagingException {        int size = filePart.getSize();        if( size <= 0 ) {            WARN( logName + "Bad attachment found" );            return null;        }        // Get file name.        String fileName = filePart.getFileName();        // Try to find Content-ID.        String contentId = null;        if( filePart instanceof MimeBodyPart ) {            contentId = ( ( MimeBodyPart ) filePart ).getContentID();        }        if( contentId != null ) {            fileName = InboxHelper.CONTENT_ID_CAPTION + contentId;        }        // Check file name.        if( StringHelper.isEmpty( fileName ) ) {            fileName = generateUniqueAttachName();        }        DEBUG( logName + "Attachment " +               "\n\t\t size = " + size +               "\n\t\t file name = " + fileName +               "\n\t\t content-type = " + filePart.getContentType() );        // Setting up I/O streams for attachment data retrieving.        ByteArrayOutputStream out = new ByteArrayOutputStream( size );        InputStream in = filePart.getInputStream();        // Go!        // Writing each byte from the input stream to the buffer.        int i;        byte[] buff = new byte[1024];        while( ( i = in.read( buff ) ) != -1 ) {            out.write( buff, 0, i );        }        // Creating a value object for the attachment...        out.flush();        Attachment attach = new Attachment( fileName, out.toByteArray() );        out.close();        // Set content type.        attach.setFiletype( filePart.getContentType() );        return attach;    }    /**     * The method appends attached message <code>attachMesage</code> to the end of     * body of <code>mainMessage</code>. The result is always in HTML format!     *     * @param mainMsg the main message     * @param attachMsg attached message     * @return true if the message as attachment was saved correctly     * @throws MessagingException     */    protected boolean appendMessage( InboxMessage mainMsg, InboxMessage attachMsg )        throws MessagingException {        // set body        String bodyMessage;        if( mainMsg.getBodyType() == MailMessage.BODYTYPE_TEXT ) {            // .. convert to HTML            bodyMessage = StringHelper.text2html( mainMsg.getBody() );        } else {            bodyMessage = StringHelper.clearHtml( mainMsg.getBody(), false );        }        String bodyAttachMessage;        if( attachMsg.getBodyType() == MailMessage.BODYTYPE_TEXT ) {            // .. convert to HTML            bodyAttachMessage = StringHelper.text2html( attachMsg.getBody() );        } else {            bodyAttachMessage = StringHelper.clearHtml( attachMsg.getBody(), false );        }        bodyMessage += "<br><br>---------- Forwarded message ----------<br>";        // get From addresses        bodyMessage += "From: ";        if( attachMsg.getFrom() != null ) {            bodyMessage += StringHelper.escape( attachMsg.getFrom().toRfcString() );        }        bodyMessage += "<br>";        // get To addresses        bodyMessage += "To: ";        if( attachMsg.getTo() != null ) {            bodyMessage += StringHelper.escape( MailAddress.toRfcString( attachMsg.getTo() ) );        }        bodyMessage += "<br>";        // get CC addresses        bodyMessage += "CC: ";        if( attachMsg.getCc() != null ) {            bodyMessage += StringHelper.escape( MailAddress.toRfcString( attachMsg.getCc() ) );        }        bodyMessage += "<br>";        // calculate message date        Date receiveDate = attachMsg.getReceiveTime();        Date messageDate = ( receiveDate == null ) ? attachMsg.getSentTime() : receiveDate;        if( messageDate != null ) {            // get date as string            long defaultTime = DateHelper.toUser( messageDate.getTime(), SystemHelper.DEFAULT_TIMEZONE );            String dateAsString = DateHelper.formatDate( new Date( defaultTime ) );            bodyMessage += "Date: " + StringHelper.escape( dateAsString ) + "<br>";        } else {            bodyMessage += "Date: <br>";        }        // get subject        String subject = attachMsg.getSubject();        if( subject == null ) {            subject = StringHelper.EMPTY_VALUE;        }        bodyMessage += "Subject: " + StringHelper.escape( subject ) + "<br><br>";        // add message body secondary message        bodyMessage += bodyAttachMessage;        // set <html> tag        bodyMessage = checkUpHtml( bodyMessage );        // set body into main message        mainMsg.setBody( bodyMessage );        // save attchments into main message        List attachments = attachMsg.getAttachments();        int size = ( attachments == null ) ? 0 : attachments.size();        for( int i = 0; i < size; i++ ) {            Attachment attach = ( Attachment ) attachments.get( i );            mainMsg.addAttachments( attach );        }        return true;    }    /**     * The method gets the name of code page from Content Type string     *     * @param contentType formatted content type string     * @return String or NULL     * @see #getContentType     */    protected String getCodePage( String contentType ) {        // position of the first symbol charset= string        String charsetParamName = "CHARSET=";        int startCharset = contentType.indexOf( charsetParamName );        // position of the last symbol charset= string        int startCharsetLength = startCharset + charsetParamName.length();        if( startCharsetLength + 1 >= contentType.length() ) {            // if Content Type string doesn't contain code page            return null;        }        // check up the first symbol of code page - it shouldn't be comma        String charsetValue;        if( contentType.substring( startCharsetLength, startCharsetLength + 1 ).equals( "\"" ) ) {            startCharset = startCharsetLength + 1;        } else {            startCharset = startCharsetLength;        }        // get position of the last symbol of code page        int endCharset = 0;        for( int i = startCharset; i < contentType.length(); i++ ) {            if( contentType.substring( i, i + 1 ).equals( " " ) ||                contentType.substring( i, i + 1 ).equals( "\"" ) ) {                break;            } else {                endCharset++;            }        }        endCharset += startCharset;        charsetValue = contentType.substring( startCharset, endCharset );        return charsetValue;    }    /**     * The method checks - is given string .     * It needs for additional HTML check by <br> tag     *     * @param s the string to check     * @return <b>true</b> if <br> presents     */    protected boolean isHTMLContent( String s ) {        if( StringHelper.isEmpty( s ) ) {            return false;        }        try {            RE re = new RE( "<br((\\s+[^>]*)|(>))", RE.MATCH_CASEINDEPENDENT );            return re.match( s );        } catch( RESyntaxException rex ) {            throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex );        }    }    /**     * The method deletes \r\n symbols from HTML     *     * @param s the string to check     * @return HTML string     */    protected String checkUpHtml( String s ) {        String result = s;        if( !StringHelper.isEmpty( result ) ) {            try {                RE re = new RE( "\\r\\n|\\r|\\n", RE.REPLACE_ALL );                result = re.subst( result, "" );            } catch( RESyntaxException rex ) {                throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex );            }        }        return result;    }    /**     * Gets content type in appropriate format.     * @param part Part     * @return String     * @throws MessagingException     */    protected String getContentType( Part part )        throws MessagingException {        String contentType = part.getContentType();        if( StringHelper.isEmpty( contentType ) ) {            // .. if empty take "text/plain"            contentType = InboxHelper.CONTENT_TYPE_PLAIN;        }        return contentType.toUpperCase();    }    /**     * Returns new unique name for attachment.     * @return String     * @see InboxHelper#generateUniqueAttachName     */    protected String generateUniqueAttachName() {        return InboxHelper.generateUniqueAttachName( ++uniqueID );    }}

⌨️ 快捷键说明

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