From 0d29cee7025624bb09eaa028724a10b7815d7067 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 14 Sep 2010 13:42:48 +0000 Subject: [PATCH] IMAP: improve BODYSTRUCTURE implementation, make it recursive git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1444 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/imap/ImapConnection.java | 43 ++++++++++++++--------- src/test/davmail/imap/TestImap.java | 5 +++ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index c04ea857..d31f86a4 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -938,23 +938,7 @@ public class ImapConnection extends AbstractConnection { MimeMessage mimeMessage = message.getMimeMessage(); Object mimeBody = mimeMessage.getContent(); if (mimeBody instanceof MimeMultipart) { - buffer.append('('); - MimeMultipart multiPart = (MimeMultipart) mimeBody; - - for (int i = 0; i < multiPart.getCount(); i++) { - MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i); - appendBodyStructure(buffer, bodyPart); - } - int slashIndex = multiPart.getContentType().indexOf('/'); - if (slashIndex < 0) { - throw new DavMailException("EXCEPTION_INVALID_CONTENT_TYPE", multiPart.getContentType()); - } - int semiColonIndex = multiPart.getContentType().indexOf(';'); - if (semiColonIndex < 0) { - buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1).toUpperCase()).append("\")"); - } else { - buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append("\")"); - } + appendBodyStructure(buffer, (MimeMultipart) mimeBody); } else { // no multipart, single body appendBodyStructure(buffer, mimeMessage); @@ -970,6 +954,31 @@ public class ImapConnection extends AbstractConnection { } } + protected void appendBodyStructure(StringBuilder buffer, MimeMultipart multiPart) throws IOException, MessagingException { + buffer.append('('); + + for (int i = 0; i < multiPart.getCount(); i++) { + MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i); + Object mimeBody = bodyPart.getContent(); + if (mimeBody instanceof MimeMultipart) { + appendBodyStructure(buffer, (MimeMultipart) mimeBody); + } else { + // no multipart, single body + appendBodyStructure(buffer, bodyPart); + } + } + int slashIndex = multiPart.getContentType().indexOf('/'); + if (slashIndex < 0) { + throw new DavMailException("EXCEPTION_INVALID_CONTENT_TYPE", multiPart.getContentType()); + } + int semiColonIndex = multiPart.getContentType().indexOf(';'); + if (semiColonIndex < 0) { + buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1).toUpperCase()).append("\")"); + } else { + buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append("\")"); + } + } + protected void appendBodyStructure(StringBuilder buffer, MimePart bodyPart) throws IOException, MessagingException { String contentType = bodyPart.getContentType(); int slashIndex = contentType.indexOf('/'); diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index 01bbf78b..27c2f729 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -98,6 +98,11 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } + public void testFetchBodyStructure() throws IOException { + writeLine(". UID FETCH 1:* (FLAGS BODYSTRUCTURE)"); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); + } + public void testUidSearchUnDeleted() throws IOException { writeLine(". UID SEARCH UNDELETED"); assertEquals(". OK SEARCH completed", readFullAnswer("."));