From bb4fb345820bff99bd87f9c744ec8562c332c3ac Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 24 Sep 2012 10:53:39 +0000 Subject: [PATCH] EWS: Get primary smtp email address with ResolveNames in direct EWS mode git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2032 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exchange/ExchangeSessionFactory.java | 2 ++ .../exchange/ews/EwsExchangeSession.java | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 6094f71d..1b2be21d 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -169,6 +169,8 @@ public final class ExchangeSessionFactory { throw exc; } catch (IllegalStateException exc) { throw exc; + } catch (NullPointerException exc) { + throw exc; } catch (Exception exc) { handleNetworkDown(exc); } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 2e685d10..5cd960ec 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -186,7 +186,12 @@ public class EwsExchangeSession extends ExchangeSession { // no need to check logon method body if (method != null) { method.releaseConnection(); - // need to retrieve email and alias + } + boolean directEws = method == null || "/ews/services.wsdl".equalsIgnoreCase(method.getPath()); + + // options page is not available in direct EWS mode + if (!directEws) { + // retrieve email and alias from options page getEmailAndAliasFromOptions(); } @@ -240,6 +245,23 @@ public class EwsExchangeSession extends ExchangeSession { httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true); } + // direct EWS: get primary smtp email address with ResolveNames + if (directEws) { + try { + ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod(alias); + executeMethod(resolveNamesMethod); + List responses = resolveNamesMethod.getResponseItems(); + for (EWSMethod.Item response : responses) { + if (alias.equalsIgnoreCase(response.get("Name"))) { + email = response.get("EmailAddress"); + currentMailboxPath = "/users/" + email.toLowerCase(); + } + } + } catch (IOException e) { + LOGGER.warn("Unable to get primary email address with ResolveNames", e); + } + } + try { folderIdMap = new HashMap(); // load actual well known folder ids @@ -2097,7 +2119,7 @@ public class EwsExchangeSession extends ExchangeSession { protected FolderId getFolderIdIfExists(String folderPath) throws IOException { String lowerCaseFolderPath = folderPath.toLowerCase(); - if (currentMailboxPath.equals(lowerCaseFolderPath)) { + if (lowerCaseFolderPath.equals(currentMailboxPath)) { return getSubFolderIdIfExists(null, ""); } else if (lowerCaseFolderPath.startsWith(currentMailboxPath + '/')) { return getSubFolderIdIfExists(null, folderPath.substring(currentMailboxPath.length() + 1));