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

EWS: fix single message in folder with Exchange 2010 bug

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1369 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-20 09:12:29 +00:00
parent ac22c555bc
commit 64a377c43d
4 changed files with 71 additions and 36 deletions

View File

@ -511,7 +511,7 @@ public abstract class ExchangeSession {
* @param mimeMessage MIME message
* @throws IOException when unable to create message
*/
public abstract void createMessage(String folderPath, String messageName, HashMap<String, String> properties, MimeMessage mimeMessage) throws IOException, MessagingException;
public abstract void createMessage(String folderPath, String messageName, HashMap<String, String> properties, MimeMessage mimeMessage) throws IOException;
/**
* Update given properties on message.
@ -537,7 +537,7 @@ public abstract class ExchangeSession {
* @param messageBody MIME message body
* @throws IOException on error
*/
public abstract void sendMessage(byte[] messageBody) throws IOException, MessagingException;
public abstract void sendMessage(byte[] messageBody) throws IOException;
/**
* Get raw MIME message content
@ -1077,7 +1077,12 @@ public abstract class ExchangeSession {
}
}
public abstract void sendMessage(MimeMessage mimeMessage) throws IOException, MessagingException;
/**
* Send Mime message.
* @param mimeMessage MIME message
* @throws IOException on error
*/
public abstract void sendMessage(MimeMessage mimeMessage) throws IOException;
/**
* Get folder object.
@ -1286,16 +1291,16 @@ public abstract class ExchangeSession {
protected void fixUids(MessageList messages) {
boolean sortNeeded = false;
for (Message message : messages) {
if (permanentUrlToImapUidMap.containsKey(message.permanentUrl)) {
long previousUid = permanentUrlToImapUidMap.get(message.permanentUrl);
if (permanentUrlToImapUidMap.containsKey(message.getPermanentId())) {
long previousUid = permanentUrlToImapUidMap.get(message.getPermanentId());
if (message.getImapUid() != previousUid) {
LOGGER.debug("Restoring IMAP uid " + message.getImapUid() + " -> " + previousUid + " for message " + message.permanentUrl);
LOGGER.debug("Restoring IMAP uid " + message.getImapUid() + " -> " + previousUid + " for message " + message.getPermanentId());
message.setImapUid(previousUid);
sortNeeded = true;
}
} else {
// add message to uid map
permanentUrlToImapUidMap.put(message.permanentUrl, message.getImapUid());
permanentUrlToImapUidMap.put(message.getPermanentId(), message.getImapUid());
}
}
if (sortNeeded) {
@ -1375,7 +1380,7 @@ public abstract class ExchangeSession {
/**
* Exchange message.
*/
public class Message implements Comparable<Message> {
public abstract class Message implements Comparable<Message> {
/**
* enclosing message list
*/
@ -1444,6 +1449,13 @@ public abstract class ExchangeSession {
*/
protected MimeMessage mimeMessage;
/**
* Get permanent message id.
* permanentUrl over WebDav or IitemId over EWS
* @return permanent id
*/
public abstract String getPermanentId();
/**
* IMAP uid , unique in folder (x0e230003)
*

View File

@ -921,6 +921,18 @@ public class DavExchangeSession extends ExchangeSession {
return new MonoCondition(attributeName, Operator.IsFalse);
}
/**
* @inheritDoc
*/
public class Message extends ExchangeSession.Message {
@Override
public String getPermanentId() {
return permanentUrl;
}
}
/**
* @inheritDoc
*/
@ -2019,8 +2031,8 @@ public class DavExchangeSession extends ExchangeSession {
try {
// use same encoding as client socket reader
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
baos.close();
mimeMessage.writeTo(baos);
baos.close();
putmethod.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray()));
int code = httpClient.executeMethod(putmethod);
@ -2060,7 +2072,7 @@ public class DavExchangeSession extends ExchangeSession {
* @inheritDoc
*/
@Override
public void updateMessage(Message message, Map<String, String> properties) throws IOException {
public void updateMessage(ExchangeSession.Message message, Map<String, String> properties) throws IOException {
PropPatchMethod patchMethod = new PropPatchMethod(message.permanentUrl, buildProperties(properties)) {
@Override
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
@ -2082,7 +2094,7 @@ public class DavExchangeSession extends ExchangeSession {
* @inheritDoc
*/
@Override
public void deleteMessage(Message message) throws IOException {
public void deleteMessage(ExchangeSession.Message message) throws IOException {
LOGGER.debug("Delete " + message.permanentUrl + " (" + message.messageUrl + ')');
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, message.permanentUrl);
}
@ -2102,20 +2114,23 @@ public class DavExchangeSession extends ExchangeSession {
}
@Override
public void sendMessage(MimeMessage mimeMessage) throws IOException, MessagingException {
if (mimeMessage.getHeader("Bcc") != null) {
// need to create draft first
String itemName = UUID.randomUUID().toString() + ".EML";
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("draft", "9");
createMessage(DRAFTS, itemName, properties, mimeMessage);
moveMessage(DRAFTS + '/' + itemName, SENDMSG);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
sendMessage(baos.toByteArray());
public void sendMessage(MimeMessage mimeMessage) throws IOException {
try {
if (mimeMessage.getHeader("Bcc") != null) {
// need to create draft first
String itemName = UUID.randomUUID().toString() + ".EML";
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("draft", "9");
createMessage(DRAFTS, itemName, properties, mimeMessage);
moveMessage(DRAFTS + '/' + itemName, SENDMSG);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
sendMessage(baos.toByteArray());
}
} catch (MessagingException e) {
throw new IOException(e.getMessage());
}
}
protected boolean isGzipEncoded(HttpMethod method) {
@ -2134,7 +2149,7 @@ public class DavExchangeSession extends ExchangeSession {
* @inheritDoc
*/
@Override
protected byte[] getContent(Message message) throws IOException {
protected byte[] getContent(ExchangeSession.Message message) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream contentInputStream;
try {
@ -2203,7 +2218,7 @@ public class DavExchangeSession extends ExchangeSession {
* @inheritDoc
*/
@Override
public void copyMessage(Message message, String targetFolder) throws IOException {
public void copyMessage(ExchangeSession.Message message, String targetFolder) throws IOException {
String targetPath = URIUtil.encodePath(getFolderPath(targetFolder)) + '/' + UUID.randomUUID().toString();
CopyMethod method = new CopyMethod(message.permanentUrl, targetPath, false);
// allow rename if a message with the same name exists
@ -2221,7 +2236,7 @@ public class DavExchangeSession extends ExchangeSession {
}
@Override
protected void moveToTrash(Message message) throws IOException {
protected void moveToTrash(ExchangeSession.Message message) throws IOException {
String destination = URIUtil.encodePath(deleteditemsUrl) + '/' + UUID.randomUUID().toString();
LOGGER.debug("Deleting : " + message.permanentUrl + " to " + destination);
MoveMethod method = new MoveMethod(message.permanentUrl, destination, false);

View File

@ -125,6 +125,11 @@ public class EwsExchangeSession extends ExchangeSession {
class Message extends ExchangeSession.Message {
// message item id
ItemId itemId;
@Override
public String getPermanentId() {
return itemId.id;
}
}
/**
@ -219,9 +224,13 @@ public class EwsExchangeSession extends ExchangeSession {
}
@Override
public void sendMessage(MimeMessage mimeMessage) throws IOException, MessagingException {
public void sendMessage(MimeMessage mimeMessage) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
try {
mimeMessage.writeTo(baos);
} catch (MessagingException e) {
throw new IOException(e.getMessage());
}
sendMessage(baos.toByteArray());
}

View File

@ -29,11 +29,13 @@ import davmail.exception.HttpNotFoundException;
import davmail.exchange.ExchangeSession;
import davmail.exchange.ExchangeSessionFactory;
import davmail.ui.tray.DavGatewayTray;
import davmail.util.IOUtil;
import davmail.util.StringUtil;
import org.apache.commons.httpclient.HttpException;
import javax.mail.MessagingException;
import javax.mail.internet.*;
import javax.mail.util.SharedByteArrayInputStream;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
@ -436,9 +438,10 @@ public class ImapConnection extends AbstractConnection {
byte[] buffer = in.readContent(size);
// empty line
readClient();
MimeMessage mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(buffer));
String messageName = UUID.randomUUID().toString() + ".EML";
session.createMessage(folderName, messageName, properties, buffer);
session.createMessage(folderName, messageName, properties, mimeMessage);
sendClient(commandId + " OK APPEND completed");
} else if ("idle".equalsIgnoreCase(command) && imapIdleDelay > 0) {
sendClient("+ idling ");
@ -690,11 +693,7 @@ public class ImapConnection extends AbstractConnection {
}
// copy selected content to baos
byte[] bytes = new byte[8192];
int length;
while ((length = partInputStream.read(bytes)) > 0) {
partOutputStream.write(bytes, 0, length);
}
IOUtil.write(partInputStream, partOutputStream);
partInputStream.close();
partOutputStream.close();
baos.close();