1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

IMAP: implement partial header fetch

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1442 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-09-13 22:19:25 +00:00
parent 50fc71c7f1
commit 131a51cd55
2 changed files with 36 additions and 10 deletions

View File

@ -675,9 +675,9 @@ public class ImapConnection extends AbstractConnection {
} }
} }
InputStream partInputStream;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream partOutputStream; InputStream partInputStream = null;
OutputStream partOutputStream = null;
// load message // load message
MimeMessage mimeMessage = message.getMimeMessage(); MimeMessage mimeMessage = message.getMimeMessage();
@ -692,9 +692,20 @@ public class ImapConnection extends AbstractConnection {
partOutputStream = new PartialOutputStream(baos, startIndex, maxSize); partOutputStream = new PartialOutputStream(baos, startIndex, maxSize);
partInputStream = mimeMessage.getRawInputStream(); partInputStream = mimeMessage.getRawInputStream();
} else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) { } else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) {
// write headers only // Header requested fetch headers
partOutputStream = new PartOutputStream(baos, true, false, startIndex, maxSize); String[] requestedHeaders = getRequestedHeaders(partIndexString);
partInputStream = message.getRawInputStream(); if (requestedHeaders != null) {
Enumeration headerEnumeration = message.getMimeMessage().getMatchingHeaderLines(requestedHeaders);
while (headerEnumeration.hasMoreElements()) {
baos.write(((String) headerEnumeration.nextElement()).getBytes("UTF-8"));
baos.write(13);
baos.write(10);
}
} else {
// write headers only
partOutputStream = new PartOutputStream(baos, true, false, startIndex, maxSize);
partInputStream = message.getRawInputStream();
}
} else { } else {
MimePart bodyPart = mimeMessage; MimePart bodyPart = mimeMessage;
String[] partIndexStrings = partIndexString.split("\\."); String[] partIndexStrings = partIndexString.split("\\.");
@ -734,15 +745,15 @@ public class ImapConnection extends AbstractConnection {
} }
// copy selected content to baos // copy selected content to baos
IOUtil.write(partInputStream, partOutputStream); if (partInputStream != null && partOutputStream != null) {
partInputStream.close(); IOUtil.write(partInputStream, partOutputStream);
partOutputStream.close(); partInputStream.close();
partOutputStream.close();
}
baos.close(); baos.close();
if ("RFC822.HEADER".equals(param)) { if ("RFC822.HEADER".equals(param)) {
buffer.append(" RFC822.HEADER "); buffer.append(" RFC822.HEADER ");
} else if (partIndexString.startsWith("HEADER.FIELDS")) {
buffer.append(" BODY[HEADER.FIELDS ()]");
} else { } else {
buffer.append(" BODY[").append(partIndexString).append(']'); buffer.append(" BODY[").append(partIndexString).append(']');
} }
@ -764,6 +775,16 @@ public class ImapConnection extends AbstractConnection {
message.dropMimeMessage(); message.dropMimeMessage();
} }
protected String[] getRequestedHeaders(String partIndexString) {
int startIndex = partIndexString.indexOf('(');
int endIndex = partIndexString.indexOf(')');
if (startIndex >= 0 && endIndex >= 0) {
return partIndexString.substring(startIndex + 1, endIndex - 1).split(" ");
} else {
return null;
}
}
protected void handleStore(String commandId, AbstractRangeIterator rangeIterator, String action, String flags) throws IOException { protected void handleStore(String commandId, AbstractRangeIterator rangeIterator, String action, String flags) throws IOException {
while (rangeIterator.hasNext()) { while (rangeIterator.hasNext()) {
DavGatewayTray.switchIcon(); DavGatewayTray.switchIcon();

View File

@ -283,6 +283,11 @@ public class TestImap extends AbstractDavMailTestCase {
assertEquals(". OK UID FETCH completed", readFullAnswer(".")); assertEquals(". OK UID FETCH completed", readFullAnswer("."));
} }
public void testHeaderFetch() throws IOException {
writeLine(". UID FETCH " + messageUid + " (BODY[HEADER.FIELDS (DATE SUBJECT FROM CONTENT-TYPE TO CC BCC MESSAGE-ID IN-REPLY-TO REFERENCES)])");
assertEquals(". OK UID FETCH completed", readFullAnswer("."));
}
public void testFetchInternalDate() throws IOException { public void testFetchInternalDate() throws IOException {
writeLine(". UID FETCH " + messageUid + " (INTERNALDATE)"); writeLine(". UID FETCH " + messageUid + " (INTERNALDATE)");
assertEquals(". OK UID FETCH completed", readFullAnswer(".")); assertEquals(". OK UID FETCH completed", readFullAnswer("."));