Fix conflict between Draft and bcc PROPATCH

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@325 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-02-03 23:54:48 +00:00
parent d578aa889d
commit db381db85b
2 changed files with 122 additions and 78 deletions

View File

@ -458,41 +458,29 @@ public class ExchangeSession {
* *
* @param folderUrl Exchange folder URL * @param folderUrl Exchange folder URL
* @param messageName message name * @param messageName message name
* @param bcc blind carbon copy header * @param properties message properties (flags)
* @param messageBody mail body * @param messageBody mail body
* @param allowOverwrite allow existing message overwrite * @param allowOverwrite allow existing message overwrite
* @throws java.io.IOException when unable to create message * @throws java.io.IOException when unable to create message
*/ */
public void createMessage(String folderUrl, String messageName, String bcc, String messageBody, boolean allowOverwrite) throws IOException { public void createMessage(String folderUrl, String messageName, HashMap<String, String> properties, String messageBody, boolean allowOverwrite) throws IOException {
String messageUrl = URIUtil.encodePathQuery(folderUrl + "/" + messageName + ".EML"); String messageUrl = URIUtil.encodePathQuery(folderUrl + "/" + messageName + ".EML");
PropPatchMethod patchMethod;
// create the message first as draft // create the message first as draft
PropPatchMethod patchMethod = new PropPatchMethod(messageUrl); if (properties.containsKey("draft")) {
if (!allowOverwrite) { patchMethod = new PropPatchMethod(messageUrl);
patchMethod.setRequestHeader("If-None-Match", "*");
}
try { try {
// update message with blind carbon copy // update message with blind carbon copy and other flags
if (bcc != null && bcc.length() > 0) { addProperties(patchMethod, properties);
patchMethod.addPropertyToSet("bcc", bcc, "b", "urn:schemas:mailheader:");
}
patchMethod.addPropertyToSet("x0E070003", "9", "d", "http://schemas.microsoft.com/mapi/proptag/");
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod); int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
if (statusCode != HttpStatus.SC_MULTI_STATUS) { if (statusCode != HttpStatus.SC_MULTI_STATUS) {
throw new IOException("Unable to add bcc recipients: " + bcc); throw new IOException("Unable to create message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
}
Enumeration responseEntityEnum = patchMethod.getResponses();
if (responseEntityEnum.hasMoreElements()) {
ResponseEntity entity = (ResponseEntity) responseEntityEnum.nextElement();
if (entity.getStatusCode() != HttpStatus.SC_OK) {
throw new IOException("Unable to add bcc recipients: " + bcc);
}
} }
} finally { } finally {
patchMethod.releaseConnection(); patchMethod.releaseConnection();
} }
}
PutMethod putmethod = new PutMethod(messageUrl); PutMethod putmethod = new PutMethod(messageUrl);
putmethod.setRequestHeader("Translate", "f"); putmethod.setRequestHeader("Translate", "f");
@ -504,7 +492,7 @@ public class ExchangeSession {
putmethod.setRequestBody(bodyStream); putmethod.setRequestBody(bodyStream);
int code = wdr.retrieveSessionInstance().executeMethod(putmethod); int code = wdr.retrieveSessionInstance().executeMethod(putmethod);
if (code != HttpURLConnection.HTTP_OK) { if (code != HttpStatus.SC_OK && code != HttpStatus.SC_CREATED) {
throw new IOException("Unable to create message " + messageUrl + ": " + code + " " + putmethod.getStatusLine()); throw new IOException("Unable to create message " + messageUrl + ": " + code + " " + putmethod.getStatusLine());
} }
} finally { } finally {
@ -518,6 +506,19 @@ public class ExchangeSession {
putmethod.releaseConnection(); putmethod.releaseConnection();
} }
// add bcc and other properties
patchMethod = new PropPatchMethod(messageUrl);
try {
// update message with blind carbon copy and other flags
addProperties(patchMethod, properties);
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
throw new IOException("Unable to patch message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
}
} finally {
patchMethod.releaseConnection();
}
} }
protected Message buildMessage(ResponseEntity responseEntity) throws URIException { protected Message buildMessage(ResponseEntity responseEntity) throws URIException {
@ -540,6 +541,8 @@ public class ExchangeSession {
message.flagged = "2".equals(prop.getPropertyAsString()); message.flagged = "2".equals(prop.getPropertyAsString());
} else if ("x0E070003".equals(localName)) { } else if ("x0E070003".equals(localName)) {
message.draft = "9".equals(prop.getPropertyAsString()); message.draft = "9".equals(prop.getPropertyAsString());
} else if ("x10810003".equals(localName)) {
message.answered = prop.getPropertyAsString().length() > 0;
} else if ("message-id".equals(prop.getLocalName())) { } else if ("message-id".equals(prop.getLocalName())) {
message.messageId = prop.getPropertyAsString(); message.messageId = prop.getPropertyAsString();
if (message.messageId.startsWith("<") && message.messageId.endsWith(">")) { if (message.messageId.startsWith("<") && message.messageId.endsWith(">")) {
@ -566,9 +569,7 @@ public class ExchangeSession {
} }
public void updateMessage(Message message, Map<String, String> properties) throws IOException { protected void addProperties(PropPatchMethod patchMethod, Map<String, String> properties) {
PropPatchMethod patchMethod = new PropPatchMethod(URIUtil.encodePathQuery(message.messageUrl));
try {
for (Map.Entry<String, String> entry : properties.entrySet()) { for (Map.Entry<String, String> entry : properties.entrySet()) {
if ("read".equals(entry.getKey())) { if ("read".equals(entry.getKey())) {
patchMethod.addPropertyToSet("read", entry.getValue(), "e", "urn:schemas:httpmail:"); patchMethod.addPropertyToSet("read", entry.getValue(), "e", "urn:schemas:httpmail:");
@ -576,8 +577,20 @@ public class ExchangeSession {
patchMethod.addPropertyToSet("x10830003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/"); patchMethod.addPropertyToSet("x10830003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/");
} else if ("flagged".equals(entry.getKey())) { } else if ("flagged".equals(entry.getKey())) {
patchMethod.addPropertyToSet("x10900003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/"); patchMethod.addPropertyToSet("x10900003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/");
} else if ("answered".equals(entry.getKey())) {
patchMethod.addPropertyToSet("x10810003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/");
} else if ("bcc".equals(entry.getKey())) {
patchMethod.addPropertyToSet("bcc", entry.getValue(), "b", "urn:schemas:mailheader:");
} else if ("draft".equals(entry.getKey())) {
patchMethod.addPropertyToSet("x0E070003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/");
} }
} }
}
public void updateMessage(Message message, Map<String, String> properties) throws IOException {
PropPatchMethod patchMethod = new PropPatchMethod(URIUtil.encodePathQuery(message.messageUrl));
try {
addProperties(patchMethod, properties);
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod); int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
if (statusCode != HttpStatus.SC_MULTI_STATUS) { if (statusCode != HttpStatus.SC_MULTI_STATUS) {
throw new IOException("Unable to update message properties"); throw new IOException("Unable to update message properties");
@ -593,7 +606,7 @@ public class ExchangeSession {
MessageList messages = new MessageList(); MessageList messages = new MessageList();
String searchRequest = "Select \"DAV:uid\", \"http://schemas.microsoft.com/mapi/proptag/x0e080003\"" + String searchRequest = "Select \"DAV:uid\", \"http://schemas.microsoft.com/mapi/proptag/x0e080003\"" +
" ,\"http://schemas.microsoft.com/mapi/proptag/x10830003\", \"http://schemas.microsoft.com/mapi/proptag/x10900003\"" + " ,\"http://schemas.microsoft.com/mapi/proptag/x10830003\", \"http://schemas.microsoft.com/mapi/proptag/x10900003\"" +
" ,\"http://schemas.microsoft.com/mapi/proptag/x0E070003\"" + " ,\"http://schemas.microsoft.com/mapi/proptag/x0E070003\", \"http://schemas.microsoft.com/mapi/proptag/x10810003\"" +
" ,\"urn:schemas:mailheader:message-id\", \"urn:schemas:httpmail:read\"" + " ,\"urn:schemas:mailheader:message-id\", \"urn:schemas:httpmail:read\"" +
" FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" + " FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" +
" WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n" + " WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n" +
@ -770,11 +783,15 @@ public class ExchangeSession {
bccBuffer.append("&gt;"); bccBuffer.append("&gt;");
} }
String bcc = bccBuffer.toString();
HashMap<String, String> properties = new HashMap<String, String>();
if (bcc.length() > 0) {
properties.put("bcc", bcc);
}
String messageName = UUID.randomUUID().toString(); String messageName = UUID.randomUUID().toString();
createMessage(draftsUrl, messageName, createMessage(draftsUrl, messageName, properties, mailBuffer.toString(), false);
bccBuffer.toString()
, mailBuffer.toString(), false);
// warning : slide library expects *unencoded* urls // warning : slide library expects *unencoded* urls
String tempUrl = draftsUrl + "/" + messageName + ".EML"; String tempUrl = draftsUrl + "/" + messageName + ".EML";
@ -925,6 +942,7 @@ public class ExchangeSession {
public boolean junk; public boolean junk;
public boolean flagged; public boolean flagged;
public boolean draft; public boolean draft;
public boolean answered;
public long getUidAsLong() { public long getUidAsLong() {
byte[] decodedValue = Base64.decode(uid.getBytes()); byte[] decodedValue = Base64.decode(uid.getBytes());
@ -955,6 +973,9 @@ public class ExchangeSession {
if (draft) { if (draft) {
buffer.append("\\Draft "); buffer.append("\\Draft ");
} }
if (answered) {
buffer.append("\\Answered ");
}
return buffer.toString().trim(); return buffer.toString().trim();
} }

View File

@ -303,43 +303,7 @@ public class ImapConnection extends AbstractConnection {
ExchangeSession.Message message = messages.getByUid(uid); ExchangeSession.Message message = messages.getByUid(uid);
String action = tokens.nextToken(); String action = tokens.nextToken();
String flags = tokens.nextToken(); String flags = tokens.nextToken();
HashMap<String, String> properties = new HashMap<String, String>(); updateFlags(message, action, flags);
if ("-Flags".equalsIgnoreCase(action)) {
StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) {
properties.put("read", "0");
message.read = false;
} else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "0");
message.flagged = false;
} else if ("Junk".equals(flag)) {
properties.put("junk", "0");
message.junk = false;
}
}
} else if ("+Flags".equalsIgnoreCase(action)) {
StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) {
properties.put("read", "1");
message.read = true;
} else if ("\\Deleted".equals(flag)) {
message.deleted = true;
} else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "2");
message.flagged = true;
} else if ("Junk".equals(flag)) {
properties.put("junk", "1");
message.junk = true;
}
}
}
if (properties.size() > 0) {
session.updateMessage(messages.getByUid(uid), properties);
}
int index = 0; int index = 0;
for (ExchangeSession.Message currentMessage : messages) { for (ExchangeSession.Message currentMessage : messages) {
index++; index++;
@ -387,6 +351,22 @@ public class ImapConnection extends AbstractConnection {
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken()); String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
// TODO handle flags // TODO handle flags
String flags = tokens.nextToken(); String flags = tokens.nextToken();
HashMap<String, String> properties = new HashMap<String, String>();
StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) {
properties.put("read", "1");
} else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "2");
} else if ("\\Answered".equals(flag)) {
properties.put("answered", "103");
} else if ("\\Draft".equals(flag)) {
properties.put("draft", "9");
} else if ("Junk".equals(flag)) {
properties.put("junk", "1");
}
}
// skip optional date // skip optional date
String dateOrSize = tokens.nextToken(); String dateOrSize = tokens.nextToken();
if (tokens.hasMoreTokens()) { if (tokens.hasMoreTokens()) {
@ -407,7 +387,7 @@ public class ImapConnection extends AbstractConnection {
readClient(); readClient();
String messageName = UUID.randomUUID().toString(); String messageName = UUID.randomUUID().toString();
session.createMessage(session.getFolderPath(folderName), messageName, null, new String(buffer), false); session.createMessage(session.getFolderPath(folderName), messageName, properties, new String(buffer), false);
sendClient(commandId + " OK APPEND completed"); sendClient(commandId + " OK APPEND completed");
} else if ("noop".equalsIgnoreCase(command) || "check".equalsIgnoreCase(command)) { } else if ("noop".equalsIgnoreCase(command) || "check".equalsIgnoreCase(command)) {
if (currentFolder != null) { if (currentFolder != null) {
@ -473,6 +453,49 @@ public class ImapConnection extends AbstractConnection {
} }
} }
protected void updateFlags(ExchangeSession.Message message, String action, String flags) throws IOException {
HashMap<String, String> properties = new HashMap<String, String>();
if ("-Flags".equalsIgnoreCase(action)) {
StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) {
properties.put("read", "0");
message.read = false;
} else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "0");
message.flagged = false;
} else if ("Junk".equals(flag)) {
properties.put("junk", "0");
message.junk = false;
}
}
} else if ("+Flags".equalsIgnoreCase(action)) {
StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) {
properties.put("read", "1");
message.read = true;
} else if ("\\Deleted".equals(flag)) {
message.deleted = true;
} else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "2");
message.flagged = true;
} else if ("\\Answered".equals(flag)) {
properties.put("answered", "103");
message.answered = true;
} else if ("Junk".equals(flag)) {
properties.put("junk", "1");
message.junk = true;
}
}
}
if (properties.size() > 0) {
session.updateMessage(message, properties);
}
}
/** /**
* Decode IMAP credentials * Decode IMAP credentials
* *