From 393b283324c23b85b7d2aa46f6dab207179bac07 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 22 Sep 2009 15:55:31 +0000 Subject: [PATCH] IMAP: test session expiration on each command, get a new session on expiration git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@741 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exchange/ExchangeSessionFactory.java | 34 +++++++++++++++++++ src/java/davmail/imap/ImapConnection.java | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 18e4358c..6f795bd9 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -124,6 +124,40 @@ public final class ExchangeSessionFactory { return session; } + /** + * Get a non expired session. + * If the current session is not expired, return current session, else try to create a new session + * + * @param currentSession current session + * @param userName user login + * @param password user password + * @return authenticated session + * @throws IOException on error + */ + public static ExchangeSession getInstance(ExchangeSession currentSession, String userName, String password) + throws IOException { + ExchangeSession session = currentSession; + try { + if (session.isExpired()) { + session = null; + String baseUrl = Settings.getProperty("davmail.url"); + PoolKey poolKey = new PoolKey(baseUrl, userName, password); + // expired session, remove from cache + synchronized (LOCK) { + POOL_MAP.remove(poolKey); + } + session = getInstance(userName, password); + } + } catch (DavMailAuthenticationException exc) { + throw exc; + } catch (DavMailException exc) { + throw exc; + } catch (Exception exc) { + handleNetworkDown(exc); + } + return session; + } + /** * Send a request to Exchange server to check current settings. * diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index c6f5163d..85960f44 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -126,6 +126,8 @@ public class ImapConnection extends AbstractConnection { if (state != State.AUTHENTICATED) { sendClient(commandId + " BAD command authentication required"); } else { + // check for expired session + session = ExchangeSessionFactory.getInstance(session, userName, password); if ("lsub".equalsIgnoreCase(command) || "list".equalsIgnoreCase(command)) { if (tokens.hasMoreTokens()) { String folderContext = BASE64MailboxDecoder.decode(tokens.nextToken());