From 12246cbdf97f17b5c4fb6300adc14bf64d635dc9 Mon Sep 17 00:00:00 2001 From: mguessan Date: Sat, 25 Apr 2009 10:31:33 +0000 Subject: [PATCH] Workaround for post logon script redirect git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@541 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index b1f898c8..bd5858dc 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -156,26 +156,18 @@ public class ExchangeSession { // manually follow redirect HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, poolKey.url); - if (!isBasicAuthentication) { - method = formLogin(httpClient, method, poolKey.userName, poolKey.password); - } - int status = method.getStatusCode(); + if (isBasicAuthentication) { + int status = method.getStatusCode(); - if (status == HttpStatus.SC_UNAUTHORIZED) { - throw new AuthenticationException("Authentication failed: invalid user or password"); - } else if (status != HttpStatus.SC_OK) { - throw DavGatewayHttpClientFacade.buildHttpException(method); - } - // test form based authentication - String queryString = method.getQueryString(); - if (queryString != null && queryString.contains("reason=2")) { - method.releaseConnection(); - if (poolKey.userName != null && poolKey.userName.contains("\\")) { + if (status == HttpStatus.SC_UNAUTHORIZED) { + method.releaseConnection(); throw new AuthenticationException("Authentication failed: invalid user or password"); - } else { - throw new AuthenticationException("Authentication failed: invalid user or password, " + - "retry with domain\\user"); + } else if (status != HttpStatus.SC_OK) { + method.releaseConnection(); + throw DavGatewayHttpClientFacade.buildHttpException(method); } + } else { + method = formLogin(httpClient, method, poolKey.userName, poolKey.password); } buildMailPath(method); @@ -374,9 +366,33 @@ public class ExchangeSession { ((PostMethod) logonMethod).addParameter("username", userName); ((PostMethod) logonMethod).addParameter("password", password); logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, logonMethod); + + // test form based authentication + checkFormLoginQueryString(logonMethod); + + // workaround for post logon script redirect + if (httpClient.getState().getCookies().length == 0) { + logonMethod = buildLogonMethod(httpClient, logonMethod); + logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, logonMethod); + checkFormLoginQueryString(logonMethod); + } + return logonMethod; } + protected void checkFormLoginQueryString(HttpMethod logonMethod) throws AuthenticationException { + String queryString = logonMethod.getQueryString(); + if (queryString != null && queryString.contains("reason=2")) { + logonMethod.releaseConnection(); + if (poolKey.userName != null && poolKey.userName.contains("\\")) { + throw new AuthenticationException("Authentication failed: invalid user or password"); + } else { + throw new AuthenticationException("Authentication failed: invalid user or password, " + + "retry with domain\\user"); + } + } + } + protected void buildMailPath(HttpMethod method) throws HttpException { // get user mail URL from html body (multi frame) BufferedReader mainPageReader = null;