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;
/**
* 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<String> POP_MESSAGE_ATTRIBUTES = new HashSet<String>();
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() + ')');
}
}
}

View File

@ -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
*/

View File

@ -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<String, String> 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 {