FileDocCategorySizeDatePackage
MimeMessageUtil.javaAPI DocApache James 2.3.111352Fri Jan 12 12:56:24 GMT 2007org.apache.james.core

MimeMessageUtil

public class MimeMessageUtil extends Object
Utility class to provide optimized write methods for the various MimeMessage implementations.

Fields Summary
Constructors Summary
Methods Summary
public static longcalculateMessageSize(javax.mail.internet.MimeMessage message)

param
message
return
the calculated size
throws
MessagingException

        long size;
        //SK: Should probably eventually store this as a locally
        //  maintained value (so we don't have to load and reparse
        //  messages each time).
        size = message.getSize();
        if (size != -1) {
            Enumeration e = message.getAllHeaderLines();
            if (e.hasMoreElements()) {
                size += 2;
            }
            while (e.hasMoreElements()) {
                // add 2 bytes for the CRLF
                size += ((String) e.nextElement()).length()+2;
            }
        }
        
        
        if (size == -1) {
            SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
            try {
                message.writeTo(out);
            } catch (IOException e) {
                // should never happen as SizeCalculator does not actually throw IOExceptions.
                throw new MessagingException("IOException wrapped by getMessageSize",e);
            }
            size = out.getSize();
        }
        return size;
    
public static voidcopyStream(java.io.InputStream in, java.io.OutputStream out)
Convenience method to copy streams

        // TODO: This is really a bad way to do this sort of thing.  A shared buffer to
        //       allow simultaneous read/writes would be a substantial improvement
        byte[] block = new byte[1024];
        int read = 0;
        while ((read = in.read(block)) > -1) {
            out.write(block, 0, read);
        }
        out.flush();
    
public static java.io.InputStreamgetHeadersInputStream(javax.mail.internet.MimeMessage message, java.lang.String[] ignoreList)

param
message
param
ignoreList
return
throws
MessagingException

        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        writeHeadersTo(message,bo,ignoreList);
        return new ByteArrayInputStream(bo.toByteArray());
    
public static longgetMessageSize(javax.mail.internet.MimeMessage message)

return
size of full message including headers
throws
MessagingException if a problem occours while computing the message size

        //If we have a MimeMessageWrapper, then we can ask it for just the
        //  message size and skip calculating it
        long size = -1;
        
        if (message instanceof MimeMessageWrapper) {
            MimeMessageWrapper wrapper = (MimeMessageWrapper) message;
            size = wrapper.getMessageSize();
        } else if (message instanceof MimeMessageCopyOnWriteProxy) {
            MimeMessageCopyOnWriteProxy wrapper = (MimeMessageCopyOnWriteProxy) message;
            size = wrapper.getMessageSize();
        }
        
        if (size == -1) {
            size = calculateMessageSize(message);
        }
        
        return size;
    
private static voidwriteHeadersTo(javax.mail.internet.MimeMessage message, java.io.OutputStream headerOs, java.lang.String[] ignoreList)
Write the message headers to the given outputstream

param
message
param
headerOs
param
ignoreList
throws
MessagingException

        //Write the headers (minus ignored ones)
        Enumeration headers = message.getNonMatchingHeaderLines(ignoreList);
        writeHeadersTo(headers, headerOs);
    
public static voidwriteHeadersTo(java.util.Enumeration headers, java.io.OutputStream headerOs)
Write the message headers to the given outputstream

param
message
param
headerOs
param
ignoreList
throws
MessagingException

        PrintWriter hos = new InternetPrintWriter(new BufferedWriter(new OutputStreamWriter(headerOs), 512), true);
        while (headers.hasMoreElements()) {
            hos.println((String)headers.nextElement());
        }
        // Print header/data separator
        hos.println();
        hos.flush();
    
public static voidwriteMessageBodyTo(javax.mail.internet.MimeMessage message, java.io.OutputStream bodyOs)

        OutputStream bos;
        InputStream bis;

        try {
            // Get the message as a stream.  This will encode
            // objects as necessary, and we have some input from
            // decoding an re-encoding the stream.  I'd prefer the
            // raw stream, but see
            bos = MimeUtility.encode(bodyOs, message.getEncoding());
            bis = message.getInputStream();
        } catch(UnsupportedDataTypeException udte) {
            /* If we get an UnsupportedDataTypeException try using
             * the raw input stream as a "best attempt" at rendering
             * a message.
             *
             * WARNING: JavaMail v1.3 getRawInputStream() returns
             * INVALID (unchanged) content for a changed message.
             * getInputStream() works properly, but in this case
             * has failed due to a missing DataHandler.
             *
             * MimeMessage.getRawInputStream() may throw a "no
             * content" MessagingException.  In JavaMail v1.3, when
             * you initially create a message using MimeMessage
             * APIs, there is no raw content available.
             * getInputStream() works, but getRawInputStream()
             * throws an exception.  If we catch that exception,
             * throw the UDTE.  It should mean that someone has
             * locally constructed a message part for which JavaMail
             * doesn't have a DataHandler.
            */

            try {
                bis = message.getRawInputStream();
                bos = bodyOs;
            } catch(javax.mail.MessagingException _) {
                throw udte;
            }
        }
        catch(javax.mail.MessagingException me) {
            /* This could be another kind of MessagingException
             * thrown by MimeMessage.getInputStream(), such as a
             * javax.mail.internet.ParseException.
             *
             * The ParseException is precisely one of the reasons
             * why the getRawInputStream() method exists, so that we
             * can continue to stream the content, even if we cannot
             * handle it.  Again, if we get an exception, we throw
             * the one that caused us to call getRawInputStream().
             */
            try {
                bis = message.getRawInputStream();
                bos = bodyOs;
            } catch(javax.mail.MessagingException _) {
                throw me;
            }
        }

        try {
            copyStream(bis, bos);
        }
        finally {
            IOUtil.shutdownStream(bis);
        }
    
public static voidwriteTo(javax.mail.internet.MimeMessage message, java.io.OutputStream headerOs, java.io.OutputStream bodyOs)
Convenience method to take any MimeMessage and write the headers and body to two different output streams

        writeTo(message, headerOs, bodyOs, null);
    
public static voidwriteTo(javax.mail.internet.MimeMessage message, java.io.OutputStream headerOs, java.io.OutputStream bodyOs, java.lang.String[] ignoreList)
Convenience method to take any MimeMessage and write the headers and body to two different output streams, with an ignore list

        MimeMessage testMessage = message;
        if (message instanceof MimeMessageCopyOnWriteProxy) {
            MimeMessageCopyOnWriteProxy wr = (MimeMessageCopyOnWriteProxy) message;
            testMessage = wr.getWrappedMessage();
        }
        if (testMessage instanceof MimeMessageWrapper) {
            MimeMessageWrapper wrapper = (MimeMessageWrapper)testMessage;
            if (!wrapper.isModified()) {
                wrapper.writeTo(headerOs, bodyOs, ignoreList);
                return;
            }
        }
        writeToInternal(message, headerOs, bodyOs, ignoreList);
    
public static voidwriteToInternal(javax.mail.internet.MimeMessage message, java.io.OutputStream headerOs, java.io.OutputStream bodyOs, java.lang.String[] ignoreList)

param
message
param
headerOs
param
bodyOs
param
ignoreList
throws
MessagingException
throws
IOException
throws
UnsupportedDataTypeException

        if(message.getMessageID() == null) {
            message.saveChanges();
        }

        writeHeadersTo(message, headerOs, ignoreList);

        // Write the body to the output stream
        writeMessageBodyTo(message, bodyOs);