diff --git a/src/java/davmail/exchange/FolderLoadThread.java b/src/java/davmail/exchange/FolderLoadThread.java new file mode 100644 index 00000000..c154a426 --- /dev/null +++ b/src/java/davmail/exchange/FolderLoadThread.java @@ -0,0 +1,84 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2013 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exchange; + +import davmail.Settings; +import org.apache.log4j.Logger; + +import javax.mail.MessagingException; +import java.io.IOException; +import java.io.OutputStream; +import java.net.SocketException; + +/** + * Load folder messages in a separate thread. + */ +public class FolderLoadThread extends Thread { + private static final Logger LOGGER = Logger.getLogger(FolderLoadThread.class); + + boolean isComplete = false; + ExchangeSession.Folder folder; + IOException exception; + + FolderLoadThread(String threadName, ExchangeSession.Folder folder) { + super(threadName + "-LoadFolder"); + setDaemon(true); + this.folder = folder; + } + + public void run() { + try { + folder.loadMessages(); + } catch (IOException e) { + exception = e; + } finally { + isComplete = true; + } + } + + /** + * Load folder in a separate thread. + * + * @param folder current folder + * @param outputStream client connection + * @throws InterruptedException on error + * @throws IOException on error + */ + public static void loadFolder(ExchangeSession.Folder folder, OutputStream outputStream) throws InterruptedException, IOException { + FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), folder); + folderLoadThread.start(); + while (!folderLoadThread.isComplete) { + folderLoadThread.join(20000); + LOGGER.debug("Still loading " + folder.folderPath + " (" + folder.count() + " messages)"); + if (Settings.getBooleanProperty("davmail.imapEnableKeepalive", false)) { + try { + outputStream.write(' '); + outputStream.flush(); + } catch (SocketException e) { + folderLoadThread.interrupt(); + throw e; + } + } + } + if (folderLoadThread.exception != null) { + throw folderLoadThread.exception; + } + + } +} \ No newline at end of file diff --git a/src/java/davmail/exchange/MessageLoadThread.java b/src/java/davmail/exchange/MessageLoadThread.java index 6c29718b..22fd37ff 100644 --- a/src/java/davmail/exchange/MessageLoadThread.java +++ b/src/java/davmail/exchange/MessageLoadThread.java @@ -1,5 +1,6 @@ package davmail.exchange; +import davmail.Settings; import org.apache.log4j.Logger; import javax.mail.MessagingException; @@ -57,14 +58,16 @@ public class MessageLoadThread extends Thread { while (!messageLoadThread.isComplete) { messageLoadThread.join(10000); LOGGER.debug("Still loading uid " + message.getUid() + " imapUid " + message.getImapUid()); - try { - outputStream.write(' '); - outputStream.flush(); - } catch (SocketException e) { - // client closed connection, stop thread - message.dropMimeMessage(); - messageLoadThread.interrupt(); - throw e; + if (Settings.getBooleanProperty("davmail.imapEnableKeepalive", false)) { + try { + outputStream.write(' '); + outputStream.flush(); + } catch (SocketException e) { + // client closed connection, stop thread + message.dropMimeMessage(); + messageLoadThread.interrupt(); + throw e; + } } } if (messageLoadThread.ioException != null) { diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 64fa3548..1e350e84 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -30,6 +30,7 @@ import davmail.exception.HttpNotFoundException; import davmail.exception.InsufficientStorageException; import davmail.exchange.ExchangeSession; import davmail.exchange.ExchangeSessionFactory; +import davmail.exchange.FolderLoadThread; import davmail.exchange.MessageLoadThread; import davmail.ui.tray.DavGatewayTray; import davmail.util.IOUtil; @@ -57,28 +58,6 @@ 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 + "-LoadFolder"); - 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. * @@ -251,25 +230,10 @@ public class ImapConnection extends AbstractConnection { sendClient("* " + currentFolder.count() + " EXISTS"); } else { // load folder in a separate thread - FolderLoadThread folderLoadThread = new FolderLoadThread(currentThread().getName(), currentFolder); - folderLoadThread.start(); LOGGER.debug("*"); os.write('*'); - while (!folderLoadThread.isComplete) { - folderLoadThread.join(20000); - LOGGER.debug("Still loading " + currentFolder.folderPath+" ("+currentFolder.count()+" messages)"); - try { - os.write(' '); - os.flush(); - } catch (SocketException e) { - folderLoadThread.interrupt(); - throw e; - } - } + FolderLoadThread.loadFolder(currentFolder, os); sendClient(" " + currentFolder.count() + " EXISTS"); - if (folderLoadThread.exception != null) { - throw folderLoadThread.exception; - } } sendClient("* " + currentFolder.recent + " RECENT");