From dc9659fe3dcaf8530212b74954340fe02f94687b Mon Sep 17 00:00:00 2001 From: mguessan Date: Thu, 2 Sep 2010 20:19:47 +0000 Subject: [PATCH] EWS: improve autodiscover implementation git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1412 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exchange/ews/EwsExchangeSession.java | 38 +++++++++++++++++-- .../http/DavGatewayHttpClientFacade.java | 15 +++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 9c6ffa2d..7a5411b6 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -148,8 +148,17 @@ public class EwsExchangeSession extends ExchangeSession { } protected static class AutoDiscoverMethod extends PostMethod { + AutoDiscoverMethod(String autodiscoverHost, String userEmail) { + super("https://"+autodiscoverHost+"/autodiscover/autodiscover.xml"); + setAutoDiscoverRequestEntity(userEmail); + } + AutoDiscoverMethod(String userEmail) { super("/autodiscover/autodiscover.xml"); + setAutoDiscoverRequestEntity(userEmail); + } + + void setAutoDiscoverRequestEntity(String userEmail) { String body = "" + "" + "" + userEmail + "" + @@ -197,19 +206,40 @@ public class EwsExchangeSession extends ExchangeSession { protected String getEwsUrlFromAutoDiscover() throws DavMailAuthenticationException { String ewsUrl; - AutoDiscoverMethod autoDiscoverMethod = new AutoDiscoverMethod(email); + try { + ewsUrl = getEwsUrlFromAutoDiscover(null); + } catch (IOException e) { + try { + ewsUrl = getEwsUrlFromAutoDiscover("autodiscover." + email.substring(email.indexOf('@') + 1)); + } catch (IOException e2) { + LOGGER.error(e2.getMessage()); + throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE"); + } + } + return ewsUrl; + } + + protected String getEwsUrlFromAutoDiscover(String autodiscoverHostname) throws IOException { + String ewsUrl; + AutoDiscoverMethod autoDiscoverMethod; + if (autodiscoverHostname != null) { + autoDiscoverMethod = new AutoDiscoverMethod(autodiscoverHostname, email); + } else { + autoDiscoverMethod = new AutoDiscoverMethod(email); + } try { int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, autoDiscoverMethod); if (status != HttpStatus.SC_OK) { throw DavGatewayHttpClientFacade.buildHttpException(autoDiscoverMethod); } ewsUrl = autoDiscoverMethod.ewsUrl; + + // update host name + DavGatewayHttpClientFacade.setClientHost(httpClient, ewsUrl); + if (ewsUrl == null) { throw new IOException("Ews url not found"); } - } catch (IOException e) { - LOGGER.error(e.getMessage()); - throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE"); } finally { autoDiscoverMethod.releaseConnection(); } diff --git a/src/java/davmail/http/DavGatewayHttpClientFacade.java b/src/java/davmail/http/DavGatewayHttpClientFacade.java index ea79ab84..4954abc4 100644 --- a/src/java/davmail/http/DavGatewayHttpClientFacade.java +++ b/src/java/davmail/http/DavGatewayHttpClientFacade.java @@ -125,13 +125,13 @@ public final class DavGatewayHttpClientFacade { } /** - * Update http client configuration (proxy) + * Set http client current host configuration. * * @param httpClient current Http client * @param url target url * @throws DavMailException on error */ - public static void configureClient(HttpClient httpClient, String url) throws DavMailException { + public static void setClientHost(HttpClient httpClient, String url) throws DavMailException { try { HostConfiguration hostConfig = httpClient.getHostConfiguration(); URI httpURI = new URI(url, true); @@ -139,6 +139,17 @@ public final class DavGatewayHttpClientFacade { } catch (URIException e) { throw new DavMailException("LOG_INVALID_URL", url); } + } + + /** + * Update http client configuration (proxy) + * + * @param httpClient current Http client + * @param url target url + * @throws DavMailException on error + */ + public static void configureClient(HttpClient httpClient, String url) throws DavMailException { + setClientHost(httpClient, url); synchronized (LOCK) { httpClient.setHttpConnectionManager(multiThreadedHttpConnectionManager);