mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-14 03:32:22 -05:00
IMAP: major FETCH implementation refactoring, make code simpler and more efficient
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@980 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
56cbeda23b
commit
e952f0d4be
@ -538,6 +538,13 @@ public class ImapConnection extends AbstractConnection {
|
|||||||
String param = paramTokens.nextToken();
|
String param = paramTokens.nextToken();
|
||||||
if ("FLAGS".equals(param)) {
|
if ("FLAGS".equals(param)) {
|
||||||
buffer.append(" FLAGS (").append(message.getImapFlags()).append(')');
|
buffer.append(" FLAGS (").append(message.getImapFlags()).append(')');
|
||||||
|
} else if ("RFC822.SIZE".equals(param)) {
|
||||||
|
MimeMessage mimeMessage = message.getMimeMessage();
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
PartialOutputStream partOutputStream = new PartialOutputStream(baos, 0, 0);
|
||||||
|
mimeMessage.writeTo(partOutputStream);
|
||||||
|
baos.close();
|
||||||
|
buffer.append(" RFC822.SIZE ").append(partOutputStream.size);
|
||||||
} else if ("ENVELOPE".equals(param)) {
|
} else if ("ENVELOPE".equals(param)) {
|
||||||
appendEnvelope(buffer, message);
|
appendEnvelope(buffer, message);
|
||||||
} else if ("BODYSTRUCTURE".equals(param)) {
|
} else if ("BODYSTRUCTURE".equals(param)) {
|
||||||
@ -552,26 +559,13 @@ public class ImapConnection extends AbstractConnection {
|
|||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new DavMailException("EXCEPTION_INVALID_DATE", message.date);
|
throw new DavMailException("EXCEPTION_INVALID_DATE", message.date);
|
||||||
}
|
}
|
||||||
} else if ("BODY.PEEK[HEADER]".equals(param) || param.startsWith("BODY.PEEK[HEADER") || "RFC822.HEADER".equals(param)) {
|
} else if (param.startsWith("BODY[") || param.startsWith("BODY.PEEK[") || "RFC822.HEADER".equals(param)) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
// get full param
|
||||||
PartOutputStream partOutputStream = new PartOutputStream(baos, true, false, 0, Integer.MAX_VALUE);
|
if (param.indexOf('[') >= 0) {
|
||||||
message.write(partOutputStream);
|
while (paramTokens.hasMoreTokens() && param.indexOf(']') < 0) {
|
||||||
baos.close();
|
param += ' ' + paramTokens.nextToken();
|
||||||
|
}
|
||||||
buffer.append(" RFC822.SIZE ").append(partOutputStream.size);
|
|
||||||
if ("BODY.PEEK[HEADER]".equals(param)) {
|
|
||||||
buffer.append(" BODY[HEADER] {");
|
|
||||||
} else if ("RFC822.HEADER".equals(param)) {
|
|
||||||
buffer.append(" RFC822.HEADER {");
|
|
||||||
} else {
|
|
||||||
buffer.append(" BODY[HEADER.FIELDS ()] {");
|
|
||||||
}
|
}
|
||||||
buffer.append(baos.size()).append('}');
|
|
||||||
sendClient(buffer.toString());
|
|
||||||
os.write(baos.toByteArray());
|
|
||||||
os.flush();
|
|
||||||
buffer.setLength(0);
|
|
||||||
} else if (param.startsWith("BODY[]") || param.startsWith("BODY.PEEK[]") || param.startsWith("BODY[TEXT]") || "BODY.PEEK[TEXT]".equals(param)) {
|
|
||||||
// parse buffer size
|
// parse buffer size
|
||||||
int startIndex = 0;
|
int startIndex = 0;
|
||||||
int maxSize = Integer.MAX_VALUE;
|
int maxSize = Integer.MAX_VALUE;
|
||||||
@ -580,81 +574,59 @@ public class ImapConnection extends AbstractConnection {
|
|||||||
int dotIndex = param.indexOf('.', ltIndex);
|
int dotIndex = param.indexOf('.', ltIndex);
|
||||||
if (dotIndex >= 0) {
|
if (dotIndex >= 0) {
|
||||||
startIndex = Integer.parseInt(param.substring(ltIndex + 1, dotIndex));
|
startIndex = Integer.parseInt(param.substring(ltIndex + 1, dotIndex));
|
||||||
maxSize = Integer.parseInt(param.substring(dotIndex+1, param.indexOf('>')));
|
// do not implement maxSize to avoid downloading message multiple times
|
||||||
|
//maxSize = Integer.parseInt(param.substring(dotIndex + 1, param.indexOf('>')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
boolean writeHeaders = true;
|
|
||||||
int rfc822size;
|
|
||||||
if ("BODY.PEEK[TEXT]".equals(param)) {
|
|
||||||
writeHeaders = false;
|
|
||||||
}
|
|
||||||
PartOutputStream bodyOutputStream = new PartOutputStream(baos, writeHeaders, true, startIndex, maxSize);
|
|
||||||
message.write(bodyOutputStream);
|
|
||||||
rfc822size = bodyOutputStream.size;
|
|
||||||
baos.close();
|
|
||||||
|
|
||||||
buffer.append(" RFC822.SIZE ").append(rfc822size).append(' ');
|
// load message
|
||||||
if ("BODY.PEEK[TEXT]".equals(param)) {
|
MimeMessage mimeMessage = message.getMimeMessage();
|
||||||
buffer.append("BODY[TEXT]");
|
|
||||||
} else {
|
|
||||||
buffer.append("BODY[]");
|
|
||||||
}
|
|
||||||
// partial
|
|
||||||
if (startIndex > 0 || maxSize != Integer.MAX_VALUE) {
|
|
||||||
buffer.append('<').append(startIndex).append('>');
|
|
||||||
}
|
|
||||||
buffer.append(" {").append(baos.size()).append('}');
|
|
||||||
sendClient(buffer.toString());
|
|
||||||
os.write(baos.toByteArray());
|
|
||||||
os.flush();
|
|
||||||
buffer.setLength(0);
|
|
||||||
} else if (param.startsWith("BODY[") || param.startsWith("BODY.PEEK[")) {
|
|
||||||
int partIndex = 0;
|
int partIndex = 0;
|
||||||
// try to parse message part index
|
// try to parse message part index
|
||||||
String partIndexString = StringUtil.getToken(param, "[", "]");
|
String partIndexString = StringUtil.getToken(param, "[", "]");
|
||||||
if (partIndexString != null) {
|
if ("".equals(partIndexString)) {
|
||||||
|
// write message with headers
|
||||||
|
mimeMessage.writeTo(new PartialOutputStream(baos, startIndex, maxSize));
|
||||||
|
} else if ("TEXT".equals(partIndexString)) {
|
||||||
|
// write message without headers
|
||||||
|
mimeMessage.getDataHandler().writeTo(new PartialOutputStream(baos, startIndex, maxSize));
|
||||||
|
} else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) {
|
||||||
|
// write headers only
|
||||||
|
mimeMessage.writeTo(new PartOutputStream(baos, true, false, startIndex, maxSize));
|
||||||
|
} else {
|
||||||
|
// try to parse part index
|
||||||
try {
|
try {
|
||||||
partIndex = Integer.parseInt(partIndexString);
|
partIndex = Integer.parseInt(partIndexString);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new DavMailException("EXCEPTION_UNSUPPORTED_PARAMETER", param);
|
throw new DavMailException("EXCEPTION_UNSUPPORTED_PARAMETER", param);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (partIndex == 0) {
|
|
||||||
throw new DavMailException("EXCEPTION_INVALID_PARAMETER", param);
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse buffer size
|
Object mimeBody = mimeMessage.getContent();
|
||||||
int startIndex = 0;
|
MimePart bodyPart;
|
||||||
int maxSize = Integer.MAX_VALUE;
|
if (mimeBody instanceof MimeMultipart) {
|
||||||
int ltIndex = param.indexOf('<');
|
MimeMultipart multiPart = (MimeMultipart) mimeBody;
|
||||||
if (ltIndex >= 0) {
|
bodyPart = (MimePart) multiPart.getBodyPart(partIndex - 1);
|
||||||
int dotIndex = param.indexOf('.', ltIndex);
|
} else if (partIndex == 1) {
|
||||||
if (dotIndex >= 0) {
|
// no multipart, single body
|
||||||
startIndex = Integer.parseInt(param.substring(ltIndex + 1, dotIndex));
|
bodyPart = mimeMessage;
|
||||||
maxSize = Integer.parseInt(param.substring(dotIndex+1, param.indexOf('>')));
|
} else {
|
||||||
|
throw new DavMailException("EXCEPTION_INVALID_PARAMETER", param);
|
||||||
}
|
}
|
||||||
|
// write selected part
|
||||||
|
bodyPart.getDataHandler().writeTo(new PartialOutputStream(baos, startIndex, maxSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
MimeMessage mimeMessage = message.getMimeMessage();
|
|
||||||
Object mimeBody = mimeMessage.getContent();
|
|
||||||
MimePart bodyPart;
|
|
||||||
if (mimeBody instanceof MimeMultipart) {
|
|
||||||
MimeMultipart multiPart = (MimeMultipart) mimeBody;
|
|
||||||
bodyPart = (MimePart) multiPart.getBodyPart(partIndex - 1);
|
|
||||||
} else if (partIndex == 1) {
|
|
||||||
// no multipart, single body
|
|
||||||
bodyPart = mimeMessage;
|
|
||||||
} else {
|
|
||||||
throw new DavMailException("EXCEPTION_INVALID_PARAMETER", param);
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
bodyPart.getDataHandler().writeTo(new PartialOutputStream(baos, startIndex, maxSize));
|
|
||||||
baos.close();
|
baos.close();
|
||||||
|
|
||||||
buffer.append(" BODY[").append(partIndexString).append(']');
|
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(']');
|
||||||
|
}
|
||||||
// partial
|
// partial
|
||||||
if (startIndex > 0 || maxSize != Integer.MAX_VALUE) {
|
if (startIndex > 0 || maxSize != Integer.MAX_VALUE) {
|
||||||
buffer.append('<').append(startIndex).append('>');
|
buffer.append('<').append(startIndex).append('>');
|
||||||
|
Loading…
Reference in New Issue
Block a user