diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 5be1d26f..9e040d7e 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -503,7 +503,7 @@ public abstract class ExchangeSession { public abstract void sendMessage(HashMap properties, String messageBody) throws IOException; /** - * Create message MIME body reader; + * Create message MIME body reader. * * @param message Exchange message * @return message body reader @@ -511,6 +511,15 @@ public abstract class ExchangeSession { */ protected abstract BufferedReader getContentReader(Message message) throws IOException; + /** + * Get raw MIME message content + * + * @param message Exchange message + * @return message body + * @throws IOException on error + */ + protected abstract byte[] getContent(Message message) throws IOException; + protected static final Set POP_MESSAGE_ATTRIBUTES = new HashSet(); static { @@ -653,7 +662,7 @@ public abstract class ExchangeSession { public boolean isEmpty() { boolean isEmpty = true; - for (Condition condition: conditions) { + for (Condition condition : conditions) { if (!condition.isEmpty()) { isEmpty = false; break; @@ -1378,6 +1387,7 @@ public abstract class ExchangeSession { * @param os output stream * @param doubleDot replace '.' lines with '..' (POP protocol) * @throws IOException on error + * @deprecated move to byte array handling instead */ public void write(OutputStream os, boolean doubleDot) throws IOException { BufferedReader reader = getContentReader(this); @@ -1432,13 +1442,10 @@ public abstract class ExchangeSession { mimeMessage = messageList.cachedMimeMessage; LOGGER.debug("Got message content for " + imapUid + " from cache"); } else { - // load message - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - write(baos, false); // load and parse message - mimeBody = new SharedByteArrayInputStream(baos.toByteArray()); + mimeBody = new SharedByteArrayInputStream(getContent(this)); mimeMessage = new MimeMessage(null, mimeBody); - LOGGER.debug("Downloaded message content for " + imapUid + " (" + baos.size() + ')'); + LOGGER.debug("Downloaded message content for " + imapUid + " (" + mimeBody.available() + ')'); } } } diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index a680da7c..698b3b0c 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -1529,6 +1529,32 @@ public class DavExchangeSession extends ExchangeSession { return false; } + /** + * @inheritDoc + */ + @Override + protected byte[] getContent(Message message) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BufferedReader reader = getContentReader(message); + try { + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos); + String line; + while ((line = reader.readLine()) != null) { + outputStreamWriter.write(line); + outputStreamWriter.write((char) 13); + outputStreamWriter.write((char) 10); + } + outputStreamWriter.flush(); + } finally { + try { + reader.close(); + } catch (IOException e) { + LOGGER.warn("Error closing message input stream", e); + } + } + return baos.toByteArray(); + } + /** * @inheritDoc */ diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 6134a2ce..a6e329c6 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -18,22 +18,30 @@ */ package davmail.exchange.ews; +import davmail.Settings; import davmail.exception.DavMailAuthenticationException; import davmail.exception.DavMailException; +import davmail.exception.HttpNotFoundException; import davmail.exchange.ExchangeSession; import davmail.http.DavGatewayHttpClientFacade; import davmail.util.StringUtil; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.URIException; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.HeadMethod; +import org.apache.commons.httpclient.util.URIUtil; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.net.NoRouteToHostException; import java.net.UnknownHostException; import java.util.*; +import java.util.zip.GZIPInputStream; /** * EWS Exchange adapter. @@ -173,7 +181,7 @@ public class EwsExchangeSession extends ExchangeSession { @Override public void updateMessage(ExchangeSession.Message message, Map properties) throws IOException { - UpdateItemMethod updateItemMethod = new UpdateItemMethod(ConflictResolution.AlwaysOverwrite, ((EwsExchangeSession.Message)message).itemId, buildProperties(properties)); + UpdateItemMethod updateItemMethod = new UpdateItemMethod(ConflictResolution.AlwaysOverwrite, ((EwsExchangeSession.Message) message).itemId, buildProperties(properties)); updateItemMethod.messageDisposition = MessageDisposition.SaveOnly; executeMethod(updateItemMethod); } @@ -190,9 +198,23 @@ public class EwsExchangeSession extends ExchangeSession { } + /** + * @inheritDoc + */ @Override protected BufferedReader getContentReader(ExchangeSession.Message message) throws IOException { - throw new UnsupportedOperationException(); + byte[] content = getContent(message); + return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content))); + } + + /** + * @inheritDoc + */ + @Override + protected byte[] getContent(ExchangeSession.Message message) throws IOException { + GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, ((EwsExchangeSession.Message) message).itemId, true); + executeMethod(getItemMethod); + return getItemMethod.getMimeContent(); } protected Message buildMessage(EWSMethod.Item response) throws URIException {