1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

EWS: implement getContent

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1126 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-05 10:23:51 +00:00
parent e63ae48216
commit fd419ae661
3 changed files with 64 additions and 9 deletions

View File

@ -503,7 +503,7 @@ public abstract class ExchangeSession {
public abstract void sendMessage(HashMap<String, String> properties, String messageBody) throws IOException; public abstract void sendMessage(HashMap<String, String> properties, String messageBody) throws IOException;
/** /**
* Create message MIME body reader; * Create message MIME body reader.
* *
* @param message Exchange message * @param message Exchange message
* @return message body reader * @return message body reader
@ -511,6 +511,15 @@ public abstract class ExchangeSession {
*/ */
protected abstract BufferedReader getContentReader(Message message) throws IOException; 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<String> POP_MESSAGE_ATTRIBUTES = new HashSet<String>(); protected static final Set<String> POP_MESSAGE_ATTRIBUTES = new HashSet<String>();
static { static {
@ -1378,6 +1387,7 @@ public abstract class ExchangeSession {
* @param os output stream * @param os output stream
* @param doubleDot replace '.' lines with '..' (POP protocol) * @param doubleDot replace '.' lines with '..' (POP protocol)
* @throws IOException on error * @throws IOException on error
* @deprecated move to byte array handling instead
*/ */
public void write(OutputStream os, boolean doubleDot) throws IOException { public void write(OutputStream os, boolean doubleDot) throws IOException {
BufferedReader reader = getContentReader(this); BufferedReader reader = getContentReader(this);
@ -1432,13 +1442,10 @@ public abstract class ExchangeSession {
mimeMessage = messageList.cachedMimeMessage; mimeMessage = messageList.cachedMimeMessage;
LOGGER.debug("Got message content for " + imapUid + " from cache"); LOGGER.debug("Got message content for " + imapUid + " from cache");
} else { } else {
// load message
ByteArrayOutputStream baos = new ByteArrayOutputStream();
write(baos, false);
// load and parse message // load and parse message
mimeBody = new SharedByteArrayInputStream(baos.toByteArray()); mimeBody = new SharedByteArrayInputStream(getContent(this));
mimeMessage = new MimeMessage(null, mimeBody); mimeMessage = new MimeMessage(null, mimeBody);
LOGGER.debug("Downloaded message content for " + imapUid + " (" + baos.size() + ')'); LOGGER.debug("Downloaded message content for " + imapUid + " (" + mimeBody.available() + ')');
} }
} }
} }

View File

@ -1529,6 +1529,32 @@ public class DavExchangeSession extends ExchangeSession {
return false; 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 * @inheritDoc
*/ */

View File

@ -18,22 +18,30 @@
*/ */
package davmail.exchange.ews; package davmail.exchange.ews;
import davmail.Settings;
import davmail.exception.DavMailAuthenticationException; import davmail.exception.DavMailAuthenticationException;
import davmail.exception.DavMailException; import davmail.exception.DavMailException;
import davmail.exception.HttpNotFoundException;
import davmail.exchange.ExchangeSession; import davmail.exchange.ExchangeSession;
import davmail.http.DavGatewayHttpClientFacade; import davmail.http.DavGatewayHttpClientFacade;
import davmail.util.StringUtil; import davmail.util.StringUtil;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException; 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.methods.HeadMethod;
import org.apache.commons.httpclient.util.URIUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.NoRouteToHostException; import java.net.NoRouteToHostException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.*; import java.util.*;
import java.util.zip.GZIPInputStream;
/** /**
* EWS Exchange adapter. * EWS Exchange adapter.
@ -190,9 +198,23 @@ public class EwsExchangeSession extends ExchangeSession {
} }
/**
* @inheritDoc
*/
@Override @Override
protected BufferedReader getContentReader(ExchangeSession.Message message) throws IOException { 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 { protected Message buildMessage(EWSMethod.Item response) throws URIException {