Commit 9a988a0d authored by Mickaël Guessant's avatar Mickaël Guessant

IMAP: Fix #633 Compatibility with javamail 1.5.6, store byte array instead of...

IMAP: Fix #633 Compatibility with javamail 1.5.6, store byte array instead of SharedByteArrayInputStream

git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@2441 3d1905a2-6b24-0410-a738-b14d5a86fcbd
parent 950c82d0
......@@ -1887,7 +1887,7 @@ public abstract class ExchangeSession {
/**
* Unparsed message content.
*/
protected SharedByteArrayInputStream mimeBody;
protected byte[] mimeBody;
/**
* Message content parsed in a MIME message.
......@@ -1983,16 +1983,20 @@ public abstract class ExchangeSession {
LOGGER.debug("Got message content for " + imapUid + " from cache");
} else {
// load and parse message
mimeBody = new SharedByteArrayInputStream(getContent(this));
mimeMessage = new MimeMessage(null, mimeBody);
mimeBody.reset();
byte[] mimeBody = getContent(this);
mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(mimeBody));
// workaround for Exchange 2003 ActiveSync bug
if (mimeMessage.getHeader("MAIL FROM") != null) {
mimeBody = (SharedByteArrayInputStream) mimeMessage.getRawInputStream();
mimeMessage = new MimeMessage(null, mimeBody);
mimeBody.reset();
// find start of actual message
byte[] mimeBodyCopy = new byte[((SharedByteArrayInputStream) mimeMessage.getRawInputStream()).available()];
int offset = mimeBody.length - mimeBodyCopy.length;
// remove unwanted header
System.arraycopy(mimeBody, offset, mimeBodyCopy, 0, mimeBodyCopy.length);
mimeBody = mimeBodyCopy;
mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(mimeBody));
System.out.println(new String(mimeBody));
}
LOGGER.debug("Downloaded full message content for IMAP UID " + imapUid + " (" + mimeBody.available() + " bytes)");
LOGGER.debug("Downloaded full message content for IMAP UID " + imapUid + " (" + mimeBody.length + " bytes)");
}
}
}
......@@ -2054,8 +2058,7 @@ public abstract class ExchangeSession {
*/
public int getMimeMessageSize() throws IOException, MessagingException {
loadMimeMessage();
mimeBody.reset();
return mimeBody.available();
return mimeBody.length;
}
/**
......@@ -2067,8 +2070,7 @@ public abstract class ExchangeSession {
*/
public InputStream getRawInputStream() throws IOException, MessagingException {
loadMimeMessage();
mimeBody.reset();
return mimeBody;
return new SharedByteArrayInputStream(mimeBody);
}
......@@ -2222,7 +2224,7 @@ public abstract class ExchangeSession {
/**
* Cached unparsed message
*/
protected transient SharedByteArrayInputStream cachedMimeBody;
protected transient byte[] cachedMimeBody;
}
......
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package davmail.exchange;
import davmail.Settings;
import junit.framework.TestCase;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.util.SharedByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
/**
* Test Exchange 2003 ActiveSync bug.
*/
public class TestExchange2003ActiveSyncBug extends TestCase {
public byte[] buildContent() throws MessagingException, IOException {
MimeMessage mimeMessage = new MimeMessage((Session) null);
mimeMessage.addHeader("to", "testto <" + Settings.getProperty("davmail.to") + ">");
mimeMessage.addHeader("cc", "testcc <" + Settings.getProperty("davmail.to") + ">");
mimeMessage.setText("Test message ");
mimeMessage.setSubject("Test subject ");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
byte[] content = baos.toByteArray();
return content;
}
public void testMailFrom() throws IOException, MessagingException {
byte[] mimeBody = buildContent();
mimeBody = ("MAIL FROM: test \r\n\r\n"+new String(mimeBody)).getBytes("UTF-8");
System.out.println(new String(mimeBody));
System.out.println("******************************");
System.out.println("length: " + mimeBody.length);
MimeMessage mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(mimeBody));
// workaround for Exchange 2003 ActiveSync bug
if (mimeMessage.getHeader("MAIL FROM") != null) {
byte[] mimeBodyCopy = new byte[((SharedByteArrayInputStream) mimeMessage.getRawInputStream()).available()];
int offset = mimeBody.length - mimeBodyCopy.length;
System.arraycopy(mimeBody, offset, mimeBodyCopy, 0, mimeBodyCopy.length);
mimeBody = mimeBodyCopy;
mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(mimeBody));
System.out.println(new String(mimeBody));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment