mirror of
https://github.com/moparisthebest/davmail
synced 2025-02-28 17:31:52 -05:00
EWS: workaround for missing urlcompname on Exchange 2010, use encoded ItemId instead
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1301 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
fddab4b812
commit
da4468ec50
@ -665,6 +665,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
etag = response.get(Field.get("etag").getResponseName());
|
||||
displayName = response.get(Field.get("displayname").getResponseName());
|
||||
itemName = response.get(Field.get("urlcompname").getResponseName());
|
||||
// workaround for missing urlcompname in Exchange 2010
|
||||
if (itemName == null) {
|
||||
itemName = StringUtil.base64ToUrl(itemId.id) + ".EML";
|
||||
}
|
||||
for (String attributeName : CONTACT_ATTRIBUTES) {
|
||||
String value = response.get(Field.get(attributeName).getResponseName());
|
||||
if (value != null) {
|
||||
@ -692,8 +696,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
list.add(Field.createFieldUpdate(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
// force urlcompname
|
||||
list.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
|
||||
// force urlcompname, only for DavMail created items
|
||||
if (!isItemId(itemName)) {
|
||||
list.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@ -715,11 +721,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
String currentEtag = null;
|
||||
ItemId currentItemId = null;
|
||||
FileAttachment currentFileAttachment = null;
|
||||
List<EWSMethod.Item> responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, EwsExchangeSession.this.isEqualTo("urlcompname", urlcompname), FolderQueryTraversal.SHALLOW);
|
||||
if (!responses.isEmpty()) {
|
||||
EWSMethod.Item response = responses.get(0);
|
||||
currentItemId = new ItemId(response);
|
||||
currentEtag = response.get(Field.get("etag").getResponseName());
|
||||
EWSMethod.Item currentItem = getEwsItem(folderPath, itemName);
|
||||
if (currentItem != null) {
|
||||
currentItemId = new ItemId(currentItem);
|
||||
currentEtag = currentItem.get(Field.get("etag").getResponseName());
|
||||
|
||||
// load current picture
|
||||
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, currentItemId, false);
|
||||
@ -813,6 +818,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
etag = response.get(Field.get("etag").getResponseName());
|
||||
displayName = response.get(Field.get("displayname").getResponseName());
|
||||
itemName = response.get(Field.get("urlcompname").getResponseName());
|
||||
// workaround for missing urlcompname in Exchange 2010
|
||||
if (itemName == null) {
|
||||
itemName = StringUtil.base64ToUrl(itemId.id) + ".EML";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -949,28 +958,56 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
EVENT_REQUEST_PROPERTIES.add("urlcompname");
|
||||
}
|
||||
|
||||
protected EWSMethod.Item getEwsItem(String folderPath, String itemName) throws IOException {
|
||||
EWSMethod.Item item = null;
|
||||
String urlcompname = convertItemNameToEML(itemName);
|
||||
// workaround for missing urlcompname in Exchange 2010
|
||||
if (isItemId(urlcompname)) {
|
||||
ItemId itemId = new ItemId(StringUtil.urlToBase64(urlcompname.substring(0, 152)));
|
||||
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false);
|
||||
for (String attribute : EVENT_REQUEST_PROPERTIES) {
|
||||
getItemMethod.addAdditionalProperty(Field.get(attribute));
|
||||
}
|
||||
executeMethod(getItemMethod);
|
||||
item = getItemMethod.getResponseItem();
|
||||
} else {
|
||||
List<EWSMethod.Item> responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, isEqualTo("urlcompname", urlcompname), FolderQueryTraversal.SHALLOW);
|
||||
if (!responses.isEmpty()) {
|
||||
item = responses.get(0);
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Item getItem(String folderPath, String itemName) throws IOException {
|
||||
String urlcompname = convertItemNameToEML(itemName);
|
||||
List<EWSMethod.Item> responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, isEqualTo("urlcompname", urlcompname), FolderQueryTraversal.SHALLOW);
|
||||
if (responses.isEmpty()) {
|
||||
EWSMethod.Item item = getEwsItem(folderPath, itemName);
|
||||
if (item == null) {
|
||||
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
||||
}
|
||||
String itemType = responses.get(0).type;
|
||||
|
||||
String itemType = item.type;
|
||||
if ("Contact".equals(itemType)) {
|
||||
// retrieve Contact properties
|
||||
List<ExchangeSession.Contact> contacts = searchContacts(folderPath, CONTACT_ATTRIBUTES, isEqualTo("urlcompname", urlcompname));
|
||||
if (contacts.isEmpty()) {
|
||||
ItemId itemId = new ItemId(item);
|
||||
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false);
|
||||
for (String attribute : CONTACT_ATTRIBUTES) {
|
||||
getItemMethod.addAdditionalProperty(Field.get(attribute));
|
||||
}
|
||||
executeMethod(getItemMethod);
|
||||
item = getItemMethod.getResponseItem();
|
||||
if (item == null) {
|
||||
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
||||
}
|
||||
return contacts.get(0);
|
||||
return new Contact(item);
|
||||
} else if ("CalendarItem".equals(itemType)
|
||||
|| "MeetingRequest".equals(itemType)) {
|
||||
return new Event(responses.get(0));
|
||||
return new Event(item);
|
||||
} else {
|
||||
throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -1067,11 +1104,14 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
private FolderId getFolderIdIfExists(String folderPath) throws IOException {
|
||||
String[] folderNames;
|
||||
FolderId currentFolderId;
|
||||
String currentMailboxPath = "/users/" + email;
|
||||
if (currentMailboxPath.equals(folderPath)) {
|
||||
return DistinguishedFolderId.MSGFOLDERROOT;
|
||||
} else if (folderPath.startsWith(currentMailboxPath + '/')) {
|
||||
return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length() + 1));
|
||||
String lowerCaseFolderPath = folderPath.toLowerCase();
|
||||
if (email != null) {
|
||||
String currentMailboxPath = "/users/" + email.toLowerCase();
|
||||
if (currentMailboxPath.equals(lowerCaseFolderPath)) {
|
||||
return DistinguishedFolderId.MSGFOLDERROOT;
|
||||
} else if (lowerCaseFolderPath.startsWith(currentMailboxPath + '/')) {
|
||||
return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length() + 1));
|
||||
}
|
||||
}
|
||||
if (folderPath.startsWith(PUBLIC_ROOT)) {
|
||||
currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT;
|
||||
@ -1171,6 +1211,8 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
return dateFormatter.format(date);
|
||||
}
|
||||
|
||||
|
||||
protected static boolean isItemId(String itemName) {
|
||||
return itemName.length() == 156;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,6 +38,11 @@ public class ItemId {
|
||||
this.changeKey = item.get("ChangeKey");
|
||||
}
|
||||
|
||||
public ItemId(String itemId) {
|
||||
this.id = itemId;
|
||||
this.changeKey = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write item id as XML.
|
||||
*
|
||||
|
@ -137,15 +137,9 @@ public final class StringUtil {
|
||||
|
||||
private static final Pattern F8FF_PATTERN = Pattern.compile("_xF8FF_");
|
||||
private static final Pattern PLUS_PATTERN = Pattern.compile("\\+");
|
||||
|
||||
|
||||
public static String urlEncodeSlash(String name) {
|
||||
String result = name;
|
||||
if (name.indexOf("_xF8FF_") >= 0) {
|
||||
result = F8FF_PATTERN.matcher(result).replaceAll(String.valueOf((char) 0xF8FF));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private static final Pattern SLASH_PATTERN = Pattern.compile("/");
|
||||
private static final Pattern UNDERSCORE_PATTERN = Pattern.compile("_");
|
||||
private static final Pattern DASH_PATTERN = Pattern.compile("-");
|
||||
|
||||
/**
|
||||
* Encode & to %26 for urlcompname.
|
||||
@ -279,4 +273,25 @@ public final class StringUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String base64ToUrl(String value) {
|
||||
String result = value;
|
||||
if (result.indexOf('+') >= 0) {
|
||||
result = PLUS_PATTERN.matcher(result).replaceAll("-");
|
||||
}
|
||||
if (result.indexOf('/') >= 0) {
|
||||
result = SLASH_PATTERN.matcher(result).replaceAll("_");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String urlToBase64(String value) {
|
||||
String result = value;
|
||||
if (result.indexOf('-') >= 0) {
|
||||
result = DASH_PATTERN.matcher(result).replaceAll("+");
|
||||
}
|
||||
if (result.indexOf('_') >= 0) {
|
||||
result = UNDERSCORE_PATTERN.matcher(result).replaceAll("/");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user