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:
parent
50fc71c7f1
commit
131a51cd55
@ -675,9 +675,9 @@ public class ImapConnection extends AbstractConnection {
|
||||
}
|
||||
}
|
||||
|
||||
InputStream partInputStream;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
OutputStream partOutputStream;
|
||||
InputStream partInputStream = null;
|
||||
OutputStream partOutputStream = null;
|
||||
|
||||
// load message
|
||||
MimeMessage mimeMessage = message.getMimeMessage();
|
||||
@ -692,9 +692,20 @@ public class ImapConnection extends AbstractConnection {
|
||||
partOutputStream = new PartialOutputStream(baos, startIndex, maxSize);
|
||||
partInputStream = mimeMessage.getRawInputStream();
|
||||
} else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) {
|
||||
// write headers only
|
||||
partOutputStream = new PartOutputStream(baos, true, false, startIndex, maxSize);
|
||||
partInputStream = message.getRawInputStream();
|
||||
// Header requested fetch headers
|
||||
String[] requestedHeaders = getRequestedHeaders(partIndexString);
|
||||
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 {
|
||||
MimePart bodyPart = mimeMessage;
|
||||
String[] partIndexStrings = partIndexString.split("\\.");
|
||||
@ -734,15 +745,15 @@ public class ImapConnection extends AbstractConnection {
|
||||
}
|
||||
|
||||
// copy selected content to baos
|
||||
IOUtil.write(partInputStream, partOutputStream);
|
||||
partInputStream.close();
|
||||
partOutputStream.close();
|
||||
if (partInputStream != null && partOutputStream != null) {
|
||||
IOUtil.write(partInputStream, partOutputStream);
|
||||
partInputStream.close();
|
||||
partOutputStream.close();
|
||||
}
|
||||
baos.close();
|
||||
|
||||
if ("RFC822.HEADER".equals(param)) {
|
||||
buffer.append(" RFC822.HEADER ");
|
||||
} else if (partIndexString.startsWith("HEADER.FIELDS")) {
|
||||
buffer.append(" BODY[HEADER.FIELDS ()]");
|
||||
} else {
|
||||
buffer.append(" BODY[").append(partIndexString).append(']');
|
||||
}
|
||||
@ -764,6 +775,16 @@ public class ImapConnection extends AbstractConnection {
|
||||
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 {
|
||||
while (rangeIterator.hasNext()) {
|
||||
DavGatewayTray.switchIcon();
|
||||
|
@ -283,6 +283,11 @@ public class TestImap extends AbstractDavMailTestCase {
|
||||
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 {
|
||||
writeLine(". UID FETCH " + messageUid + " (INTERNALDATE)");
|
||||
assertEquals(". OK UID FETCH completed", readFullAnswer("."));
|
||||
|
Loading…
Reference in New Issue
Block a user