/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * 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 org.apache.james.core;

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

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.james.util.stream.CombinedInputStream;

/**
 * Provide an {@link InputStream} over an {@link MimeMessage}
 * 
 */
public class MimeMessageInputStream extends InputStream {
    private InputStream in;

    @SuppressWarnings("unchecked")
    public MimeMessageInputStream(MimeMessage message) throws MessagingException {
        MimeMessage m = message;
       
        // check if we need to use the wrapped message
        if (m instanceof MimeMessageCopyOnWriteProxy) {
            m = ((MimeMessageCopyOnWriteProxy) message).getWrappedMessage();
        }

        // check if we can use optimized operations
        if (m instanceof MimeMessageWrapper) {
            in = ((MimeMessageWrapper) m).getMessageInputStream();
        } else {
            in = new CombinedInputStream(new InputStream[] { new InternetHeadersInputStream(message.getAllHeaderLines()), message.getRawInputStream() });
        }

    }

    @Override
    public int read() throws IOException {
        return in.read();
    }

    @Override
    public void close() throws IOException {
       in.close();
    }

    @Override
    public int available() throws IOException {
        return in.available();
    }

    @Override
    public void mark(int readlimit) {
        in.mark(readlimit);
    }

    @Override
    public boolean markSupported() {
        return in.markSupported();
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return in.read(b, off, len);
    }

    @Override
    public int read(byte[] b) throws IOException {
        return in.read(b);
    }

    @Override
    public void reset() throws IOException {
        in.reset();
    }

    @Override
    public long skip(long n) throws IOException {
        return in.skip(n);
    }

}

@@ -1232,24 +1232,101 @@
 es.core;%0D%0A%0D%0A
+import java.io.ByteArrayInputStream;%0D%0Aimport java.io.ByteArrayOutputStream;%0D%0A
 import java.
@@ -1334,32 +1334,32 @@
 o.IOException;%0D%0A
-
 import java.io.I
@@ -2007,22 +2007,16 @@
 Proxy) m
-essage
 ).getWra
@@ -2227,24 +2227,47 @@
   %7D else %7B%0D%0A
+            try %7B%0D%0A    
             
@@ -2405,24 +2405,670 @@
 ream() %7D);%0D%0A
+            %7D catch (MessagingException e) %7B%0D%0A                // its possible that MimeMessage.getRawInputStream throws an exception when try to access the method on a self constructed MimeMessage.%0D%0A                // so try to read it in memory %0D%0A                ByteArrayOutputStream out = new ByteArrayOutputStream();%0D%0A                try %7B%0D%0A                    message.writeTo(out);%0D%0A                    in = new ByteArrayInputStream(out.toByteArray());%0D%0A                %7D catch (IOException e1) %7B%0D%0A                    throw new MessagingException(%22Unable to read message %22 + message, e);%0D%0A                %7D%0D%0A                %0D%0A            %7D%0D%0A
         %7D%0D%0A%0D

Fix parsing of MimeMessage instances which were not constructed via an InputStream (JAMES-1106)