From c0abc93ac54e7ff3293bec2f6530c4bf46ef4bbd Mon Sep 17 00:00:00 2001 From: mguessan Date: Sat, 30 Oct 2010 19:13:46 +0000 Subject: [PATCH] EWS: implement failover on OWA authentication failure (e.g. with outlook.com) git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1521 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 24 +++++++----- .../exchange/ews/EwsExchangeSession.java | 38 ++++++++++++++++--- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index da0bc23e..9917dfeb 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -119,7 +119,7 @@ public abstract class ExchangeSession { protected String currentMailboxPath; protected final HttpClient httpClient; - private final String userName; + protected final String userName; protected String serverVersion; @@ -429,15 +429,7 @@ public abstract class ExchangeSession { return logonMethod; } - protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException { - LOGGER.debug("Form based authentication detected"); - - HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod); - if (logonMethod == null) { - LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url"); - logonMethod = new PostMethod("/owa/auth/owaauth.dll"); - } - + protected HttpMethod postLogonMethod(HttpClient httpClient, HttpMethod logonMethod, String userName, String password) throws IOException { // make sure username and password fields are empty ((PostMethod) logonMethod).removeParameter(userNameInput); ((PostMethod) logonMethod).removeParameter(passwordInput); @@ -480,6 +472,18 @@ public abstract class ExchangeSession { // need to submit form logonMethod = submitLanguageSelectionForm(logonMethod); } + return logonMethod; + } + + protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException { + LOGGER.debug("Form based authentication detected"); + + HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod); + if (logonMethod == null) { + LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url"); + logonMethod = new PostMethod("/owa/auth/owaauth.dll"); + } + logonMethod = postLogonMethod(httpClient, logonMethod, userName, password); return logonMethod; } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 69705a39..75a15376 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -79,6 +79,21 @@ public class EwsExchangeSession extends ExchangeSession { super(url, userName, password); } + @Override + protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException { + LOGGER.debug("Form based authentication detected"); + + HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod); + if (logonMethod == null) { + LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", will try direct EWS access"); + } else { + logonMethod = postLogonMethod(httpClient, logonMethod, userName, password); + } + + return logonMethod; + } + + /** * Check endpoint url. * @@ -90,7 +105,9 @@ public class EwsExchangeSession extends ExchangeSession { getMethod.setFollowRedirects(false); try { int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, getMethod); - if (status != HttpStatus.SC_MOVED_TEMPORARILY) { + if (status == HttpStatus.SC_UNAUTHORIZED) { + throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED"); + } else if (status != HttpStatus.SC_MOVED_TEMPORARILY) { throw DavGatewayHttpClientFacade.buildHttpException(getMethod); } // check Location @@ -109,10 +126,19 @@ public class EwsExchangeSession extends ExchangeSession { @Override protected void buildSessionInfo(HttpMethod method) throws DavMailException { // no need to check logon method body - method.releaseConnection(); + if (method != null) { + method.releaseConnection(); + // need to retrieve email and alias + getEmailAndAliasFromOptions(); + } else { + // OWA authentication failed, get email address from login + if (userName.indexOf('@') >= 0) { + // userName is email address + email = userName; + alias = userName.substring(0, userName.indexOf('@')); + } + } - // also need to retrieve email and alias - getEmailAndAliasFromOptions(); if (email == null || alias == null) { throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE"); } @@ -122,6 +148,8 @@ public class EwsExchangeSession extends ExchangeSession { // check EWS access try { checkEndPointUrl("/ews/exchange.asmx"); + } catch (DavMailAuthenticationException e) { + throw e; } catch (IOException e) { try { // failover, try to retrieve EWS url from autodiscover @@ -1273,7 +1301,7 @@ public class EwsExchangeSession extends ExchangeSession { event.getEventContent(); events.add(event); } catch (HttpException e) { - LOGGER.warn("Ignore invalid event "+event.getHref()); + LOGGER.warn("Ignore invalid event " + event.getHref()); } } else { events.add(event);