package org.openas2.lib.util;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.mail.MessagingException;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

import org.openas2.Session;
import org.openas2.lib.util.javamail.ByteArrayDataSource;


public class MimeUtil {
    public static String HEADER_VALUE_SEPARATOR = ", ";

    public static int getContentLength(InternetHeaders headers)
        throws IOException {
        // get the content length
        String contentLengthStr = getHeader(headers, "Content-Length");

        // content-length is required
        if (contentLengthStr == null) {
            throw new IOException("Content-Length missing");
        }

        // convert the content length to an int
        try {
            return Integer.parseInt(contentLengthStr);
        } catch (NumberFormatException nfe) {
            throw new IOException("Invalid Content-Length: " + contentLengthStr);
        }
    }

    public static String getHeader(InternetHeaders headers, String key) {
        return getHeader(headers, key, HEADER_VALUE_SEPARATOR);
    }

    public static String getHeader(InternetHeaders headers, String key, String delimiter) {
        // TODO test this to make sure it returns null if no header values exist
        // - I remember something about it returning a blank string instead
        String value = headers.getHeader(key, delimiter);

        if (value == null) {
            return null;
        } else if (value.equalsIgnoreCase("null")) {
            return null;
        } else {
            return value;
        }
    }

    public static MimeBodyPart createMimeBodyPart(byte[] data, String contentType, String contentTransferEncoding)
        throws MessagingException {
        // create a MimeBodyPart and set up it's content and content headers
        MimeBodyPart part = new MimeBodyPart();
        part.setDataHandler(new DataHandler(new ByteArrayDataSource(data, contentType, null)));
        part.setHeader("Content-Type", contentType);
        part.setHeader("Content-Transfer-Encoding", contentTransferEncoding);

        return part;
    }

    public static MimeBodyPart createMimeBodyPart(MimeMultipart multipart)
        throws MessagingException {
        MimeBodyPart part = new MimeBodyPart();
        part.setContent(multipart);
        part.setHeader("Content-Type", multipart.getContentType());

        return part;
    }

    public static MimeMultipart createMimeMultipart(MimeBodyPart bodypart)
        throws MessagingException {
        return new MimeMultipart(bodypart.getDataHandler().getDataSource());
    }

    public static InternetHeaders readHeaders(InputStream source)
        throws MessagingException {
        // read in the MIME headers
        return new InternetHeaders(source);
    }

    public static MimeBodyPart readMimeBodyPart(InputStream source, InternetHeaders headers)
        throws IOException, MessagingException {
        // get the length of the Mime body's data
        int contentLength = getContentLength(headers);

        // read the data into a byte array
        DataInputStream dataIn = new DataInputStream(source);
        byte[] data = new byte[contentLength];
        dataIn.readFully(data);

        // convert the byte array to a MimeBodyPart
        String contentTransferEncoding = getHeader(headers, "Content-Transfer-Encoding");
        if (contentTransferEncoding == null) contentTransferEncoding = Session.DEFAULT_CONTENT_TRANSFER_ENCODING;
        return createMimeBodyPart(data, getHeader(headers, "Content-Type"), contentTransferEncoding);
    }
}

@@ -22,24 +22,63 @@
 ib.util;%0D%0A%0D%0A
+import java.io.ByteArrayOutputStream;%0D%0A
 import java.
@@ -156,18 +156,16 @@
 tream;%0D%0A
-%0D%0A
 import j
@@ -3885,11 +3885,418 @@
 %0A    %7D%0D%0A
+    %0D%0A%09public static String toString(MimeBodyPart mbp, boolean addDelimiterText) throws IOException, MessagingException%0D%0A%09%7B%0D%0A%09%09ByteArrayOutputStream os = new ByteArrayOutputStream();%0D%0A%09%09if (addDelimiterText) os.write(%22========BEGIN MIMEBODYPART=========%5Cn%22.getBytes());%0D%0A%09%09mbp.writeTo(os);%0D%0A%09%09if (addDelimiterText) os.write(%22%5Cn========END MIMEBODYPART=========%22.getBytes());%0D%0A%09%09return os.toString();%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

Add helper method to print MimeBodyPart