From c62a982be26b892140c5b028eb5e0f32b2f2c0a2 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 23 Apr 2013 08:36:07 +0000 Subject: [PATCH] IMAP: try to avoid timeout on folder SELECT with a KeepAlive space character git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2096 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/imap/ImapConnection.java | 55 ++++++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 772fba7d..06007434 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -56,6 +56,28 @@ public class ImapConnection extends AbstractConnection { protected String baseMailboxPath; ExchangeSession.Folder currentFolder; + class FolderLoadThread extends Thread { + boolean isComplete = false; + ExchangeSession.Folder folder; + IOException exception; + + FolderLoadThread(String threadName, ExchangeSession.Folder folder) { + super(threadName+"-Load"); + setDaemon(true); + this.folder = folder; + } + + public void run() { + try { + folder.loadMessages(); + } catch (IOException e) { + exception = e; + } finally { + isComplete = true; + } + } + } + /** * Initialize the streams and start the thread. * @@ -115,11 +137,11 @@ public class ImapConnection extends AbstractConnection { state = State.AUTHENTICATED; } catch (Exception e) { DavGatewayTray.error(e); - if (Settings.getBooleanProperty("davmail.enableKerberos")) { - sendClient(commandId + " NO LOGIN Kerberos authentication failed"); - } else { - sendClient(commandId + " NO LOGIN failed"); - } + if (Settings.getBooleanProperty("davmail.enableKerberos")) { + sendClient(commandId + " NO LOGIN Kerberos authentication failed"); + } else { + sendClient(commandId + " NO LOGIN failed"); + } state = State.INITIAL; } } else if ("AUTHENTICATE".equalsIgnoreCase(command)) { @@ -222,8 +244,27 @@ public class ImapConnection extends AbstractConnection { } try { currentFolder = session.getFolder(folderName); - currentFolder.loadMessages(); - sendClient("* " + currentFolder.count() + " EXISTS"); + if (currentFolder.count() <= 500) { + // simple folder load + currentFolder.loadMessages(); + sendClient("* " + currentFolder.count() + " EXISTS"); + } else { + // load folder in a separate thread + FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), currentFolder); + folderLoadThread.start(); + os.write('*'); + while (!folderLoadThread.isComplete) { + folderLoadThread.join(10000); + LOGGER.debug("Still loading "+currentFolder.folderPath); + os.write(' '); + os.flush(); + } + sendClient(" " + currentFolder.count() + " EXISTS"); + if (folderLoadThread.exception != null) { + throw folderLoadThread.exception; + } + } + sendClient("* " + currentFolder.recent + " RECENT"); sendClient("* OK [UIDVALIDITY 1]"); if (currentFolder.count() == 0) {