From 0d7dd47db26ce5439ec051e6e466e69826ea7d86 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 24 Aug 2010 09:28:40 +0000 Subject: [PATCH] IMAP: handle 507 InsufficientStorage error git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1377 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../InsufficientStorageException.java | 35 ++++ .../exchange/dav/DavExchangeSession.java | 15 +- src/java/davmail/imap/ImapConnection.java | 9 +- src/test/davmail/imap/TestImap.java | 189 +++++++++++------- 4 files changed, 168 insertions(+), 80 deletions(-) create mode 100644 src/java/davmail/exception/InsufficientStorageException.java diff --git a/src/java/davmail/exception/InsufficientStorageException.java b/src/java/davmail/exception/InsufficientStorageException.java new file mode 100644 index 00000000..34180872 --- /dev/null +++ b/src/java/davmail/exception/InsufficientStorageException.java @@ -0,0 +1,35 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 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.exception; + +import org.apache.commons.httpclient.HttpException; + +/** + * HttpException with 507 Insufficient Storage status. + */ +public class InsufficientStorageException extends HttpException { + /** + * HttpException with 507 Insufficient Storage status. + * + * @param message exception message + */ + public InsufficientStorageException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 949b67ca..21df1efb 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -23,6 +23,7 @@ import davmail.Settings; import davmail.exception.DavMailAuthenticationException; import davmail.exception.DavMailException; import davmail.exception.HttpNotFoundException; +import davmail.exception.InsufficientStorageException; import davmail.exchange.ExchangeSession; import davmail.exchange.VObject; import davmail.exchange.XMLStreamUtil; @@ -2055,7 +2056,19 @@ public class DavExchangeSession extends ExchangeSession { int code = httpClient.executeMethod(putmethod); if (code != HttpStatus.SC_OK && code != HttpStatus.SC_CREATED) { - throw new DavMailException("EXCEPTION_UNABLE_TO_CREATE_MESSAGE", messageUrl, code, ' ', putmethod.getStatusLine()); + // first delete draft message + if (!davProperties.isEmpty()) { + try { + DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, messageUrl); + } catch (IOException e) { + LOGGER.warn("Unable to delete draft message"); + } + } + if (code == HttpStatus.SC_INSUFFICIENT_STORAGE) { + throw new InsufficientStorageException(putmethod.getStatusText()); + } else { + throw new DavMailException("EXCEPTION_UNABLE_TO_CREATE_MESSAGE", messageUrl, code, ' ', putmethod.getStatusLine()); + } } } catch (MessagingException e) { throw new IOException(e.getMessage()); diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index a482d71d..2ae004cc 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -26,6 +26,7 @@ import davmail.Settings; import davmail.exception.DavMailException; import davmail.exception.HttpForbiddenException; import davmail.exception.HttpNotFoundException; +import davmail.exception.InsufficientStorageException; import davmail.exchange.ExchangeSession; import davmail.exchange.ExchangeSessionFactory; import davmail.ui.tray.DavGatewayTray; @@ -446,8 +447,12 @@ public class ImapConnection extends AbstractConnection { MimeMessage mimeMessage = new MimeMessage(null, new SharedByteArrayInputStream(buffer)); String messageName = UUID.randomUUID().toString() + ".EML"; - session.createMessage(folderName, messageName, properties, mimeMessage); - sendClient(commandId + " OK APPEND completed"); + try { + session.createMessage(folderName, messageName, properties, mimeMessage); + sendClient(commandId + " OK APPEND completed"); + } catch (InsufficientStorageException e) { + sendClient(commandId + " NO " + e.getMessage()); + } } else if ("idle".equalsIgnoreCase(command) && imapIdleDelay > 0) { sendClient("+ idling "); // clear cache before going to idle mode diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index 6c8ac734..68470586 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -27,6 +27,7 @@ import javax.mail.Session; import javax.mail.internet.MimeMessage; import java.io.*; import java.net.Socket; +import java.util.Random; /** * IMAP tests, an instance of DavMail Gateway must be available @@ -70,19 +71,15 @@ public class TestImap extends AbstractDavMailTestCase { clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.imapPort")); socketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + + String banner = socketReader.readLine(); + assertNotNull(banner); + + writeLine(". LOGIN " + Settings.getProperty("davmail.username").replaceAll("\\\\", "\\\\\\\\") + ' ' + Settings.getProperty("davmail.password")); + assertEquals(". OK Authenticated", socketReader.readLine()); } } - public void testBanner() throws IOException { - String banner = socketReader.readLine(); - assertNotNull(banner); - } - - public void testLogin() throws IOException { - writeLine(". LOGIN " + Settings.getProperty("davmail.username").replaceAll("\\\\", "\\\\\\\\") + ' ' + Settings.getProperty("davmail.password")); - assertEquals(". OK Authenticated", socketReader.readLine()); - } - public void testListFolders() throws IOException { writeLine(". LSUB \"\" \"*\""); assertEquals(". OK LSUB completed", readFullAnswer(".")); @@ -98,11 +95,6 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } - public void testStoreDelete() throws IOException { - writeLine(". UID STORE 10 +FLAGS (\\Deleted)"); - readFullAnswer("."); - } - public void testUidSearchUnDeleted() throws IOException { writeLine(". UID SEARCH UNDELETED"); assertEquals(". OK SEARCH completed", readFullAnswer(".")); @@ -125,15 +117,49 @@ public class TestImap extends AbstractDavMailTestCase { readFullAnswer("."); writeLine(". CREATE testfolder"); assertEquals(". OK folder created", readFullAnswer(".")); + writeLine(". SELECT testfolder"); + assertEquals(". OK [READ-WRITE] SELECT completed", readFullAnswer(".")); } + public void testFetchBigMessage() throws IOException, MessagingException { + testCreateFolder(); + // create 10MB message + MimeMessage mimeMessage = new MimeMessage((Session) null); + mimeMessage.addHeader("to", Settings.getProperty("davmail.to")); + mimeMessage.addHeader("bcc", Settings.getProperty("davmail.bcc")); + Random random = new Random(); + StringBuilder randomText = new StringBuilder(); + for (int i=0;i<10*1024*1024;i++) { + randomText.append((char)('a'+random.nextInt(26))); + } + mimeMessage.setText(randomText.toString()); + mimeMessage.setSubject("Big subject"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mimeMessage.writeTo(baos); + byte[] content = baos.toByteArray(); + long start = System.currentTimeMillis(); + writeLine(". APPEND testfolder (\\Seen \\Draft) {" + content.length + '}'); + assertEquals("+ send literal data", readLine()); + writeLine(new String(content)); + assertEquals(". OK APPEND completed", readFullAnswer(".")); + System.out.println("Create time: "+(System.currentTimeMillis()-start)+" ms"); + writeLine(". NOOP"); + assertEquals(". OK NOOP completed", readFullAnswer(".")); + start = System.currentTimeMillis(); + writeLine(". UID FETCH 1:* (RFC822.SIZE BODY.TEXT)"); + readFullAnswer("."); + System.out.println("Fetch time: "+(System.currentTimeMillis()-start)+" ms"); + + } + + public void testSelectFolder() throws IOException { writeLine(". SELECT testfolder"); assertEquals(". OK [READ-WRITE] SELECT completed", readFullAnswer(".")); } public void testCreateMessage() throws IOException, MessagingException { - + MimeMessage mimeMessage = new MimeMessage((Session) null); mimeMessage.addHeader("to", Settings.getProperty("davmail.to")); mimeMessage.addHeader("bcc", Settings.getProperty("davmail.bcc")); @@ -154,119 +180,128 @@ public class TestImap extends AbstractDavMailTestCase { String messageLine = readLine(); int uidIndex = messageLine.indexOf("UID ") + 4; messageUid = messageLine.substring(uidIndex, messageLine.indexOf(' ', uidIndex)); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); assertNotNull(messageUid); } public void testUidStoreDeletedFlag() throws IOException { // test deleted flag - writeLine(". UID STORE "+messageUid+" +FLAGS (\\Deleted)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Seen \\Deleted \\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " +FLAGS (\\Deleted)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Seen \\Deleted \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); // remove deleted flag - writeLine(". UID STORE "+messageUid+" -FLAGS (\\Deleted)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Seen \\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " -FLAGS (\\Deleted)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Seen \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testUidRemoveSeenFlag() throws IOException { // remove seen flag - writeLine(". UID STORE "+messageUid+" FLAGS (\\Draft)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " FLAGS (\\Draft)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testUidStoreForwardedFlag() throws IOException { // add forwarded flag - writeLine(". UID STORE "+messageUid+" +FLAGS ($Forwarded)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft $Forwarded))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " +FLAGS ($Forwarded)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft $Forwarded))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); // remove forwarded flag - writeLine(". UID STORE "+messageUid+" -FLAGS ($Forwarded)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " -FLAGS ($Forwarded)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testUidStoreAnsweredFlag() throws IOException { // add answered flag - writeLine(". UID STORE "+messageUid+" +FLAGS (\\Answered)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft \\Answered))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " +FLAGS (\\Answered)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft \\Answered))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); // remove answered flag - writeLine(". UID STORE "+messageUid+" -FLAGS (\\Answered)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " -FLAGS (\\Answered)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testUidStoreJunkFlag() throws IOException { // add Junk flag - writeLine(". UID STORE "+messageUid+" +FLAGS (Junk)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (Junk \\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " +FLAGS (Junk)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (Junk \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); // remove Junk flag - writeLine(". UID STORE "+messageUid+" -FLAGS (Junk)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " -FLAGS (Junk)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testUidStoreSeenFlag() throws IOException { // add Junk flag - writeLine(". UID STORE "+messageUid+" +FLAGS (\\Seen)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Seen \\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " +FLAGS (\\Seen)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Seen \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); // remove Junk flag - writeLine(". UID STORE "+messageUid+" -FLAGS (\\Seen)"); - assertEquals(". OK STORE completed",readFullAnswer(".")); - writeLine(". UID FETCH "+messageUid+" (FLAGS)"); - assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID STORE " + messageUid + " -FLAGS (\\Seen)"); + assertEquals(". OK STORE completed", readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (FLAGS)"); + assertEquals("* 1 FETCH (UID " + messageUid + " FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testPartialFetch() throws IOException { - writeLine(". UID FETCH "+messageUid+" (BODY.PEEK[1.MIME])"); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (BODY.PEEK[1.MIME])"); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testFetchInternalDate() throws IOException { - writeLine(". UID FETCH "+messageUid+" (INTERNALDATE)"); - assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + writeLine(". UID FETCH " + messageUid + " (INTERNALDATE)"); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } public void testDeleteFolder() throws IOException { writeLine(". DELETE testfolder"); - assertEquals(". OK folder deleted",readFullAnswer(".")); + assertEquals(". OK folder deleted", readFullAnswer(".")); } public void testLogout() throws IOException { writeLine(". LOGOUT"); assertEquals("* BYE Closing connection", socketReader.readLine()); } + + public void testBrokenPipe() throws IOException, InterruptedException { + testSelectInbox(); + writeLine(". UID FETCH 1:* (RFC822.SIZE BODY.TEXT)"); + socketReader.readLine(); + // force close connection + clientSocket.close(); + Thread.sleep(5000); + } }