IMAP : various enhancements from audit, switch to enum and avoid NullPointerException

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2247 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2014-03-10 21:13:33 +00:00
parent 0440d25b84
commit 33c80402b6
1 changed files with 30 additions and 36 deletions

View File

@ -126,14 +126,14 @@ public class ImapConnection extends AbstractConnection {
String authenticationMethod = tokens.nextToken(); String authenticationMethod = tokens.nextToken();
if ("LOGIN".equalsIgnoreCase(authenticationMethod)) { if ("LOGIN".equalsIgnoreCase(authenticationMethod)) {
try { try {
sendClient("+ " + base64Encode("Username:")); sendClient("+ " + IOUtil.encodeBase64("Username:"));
state = State.LOGIN; state = State.LOGIN;
userName = base64Decode(readClient()); userName = IOUtil.decodeBase64AsString(readClient());
// detect shared mailbox access // detect shared mailbox access
splitUserName(); splitUserName();
sendClient("+ " + base64Encode("Password:")); sendClient("+ " + IOUtil.encodeBase64("Password:"));
state = State.PASSWORD; state = State.PASSWORD;
password = base64Decode(readClient()); password = IOUtil.decodeBase64AsString(readClient());
session = ExchangeSessionFactory.getInstance(userName, password); session = ExchangeSessionFactory.getInstance(userName, password);
sendClient(commandId + " OK Authenticated"); sendClient(commandId + " OK Authenticated");
state = State.AUTHENTICATED; state = State.AUTHENTICATED;
@ -700,8 +700,8 @@ public class ImapConnection extends AbstractConnection {
/** /**
* Send expunge untagged response for removed IMAP message uids. * Send expunge untagged response for removed IMAP message uids.
* *
* @param previousImapUidList uid list before refresh * @param previousImapFlagMap uid map before refresh
* @param imapUidList uid list after refresh * @param imapFlagMap uid map after refresh
* @throws IOException on error * @throws IOException on error
*/ */
private void handleRefresh(TreeMap<Long, String> previousImapFlagMap, TreeMap<Long, String> imapFlagMap) throws IOException { private void handleRefresh(TreeMap<Long, String> previousImapFlagMap, TreeMap<Long, String> imapFlagMap) throws IOException {
@ -741,17 +741,13 @@ public class ImapConnection extends AbstractConnection {
/** /**
* Monitor full message download * Monitor full message download
*
* @param os client socket output stream
* @param buffer current output buffer
* @param message message
*/ */
protected void loadMessage() throws IOException, MessagingException { protected void loadMessage() throws IOException, MessagingException {
if (!message.isLoaded()) { if (!message.isLoaded()) {
// flush current buffer // flush current buffer
String flushString = buffer.toString(); String flushString = buffer.toString();
LOGGER.debug(flushString); LOGGER.debug(flushString);
os.write(flushString.getBytes()); os.write(flushString.getBytes("UTF-8"));
buffer.setLength(0); buffer.setLength(0);
MessageLoadThread.loadMimeMessage(message, os); MessageLoadThread.loadMimeMessage(message, os);
} }
@ -791,9 +787,9 @@ public class ImapConnection extends AbstractConnection {
buffer.append(" FLAGS (").append(message.getImapFlags()).append(')'); buffer.append(" FLAGS (").append(message.getImapFlags()).append(')');
} else if ("RFC822.SIZE".equals(param)) { } else if ("RFC822.SIZE".equals(param)) {
int size; int size;
if (parameters.indexOf("BODY.PEEK[HEADER.FIELDS (") >= 0 if (parameters.contains("BODY.PEEK[HEADER.FIELDS (")
// exclude mutt header request // exclude mutt header request
&& parameters.indexOf("X-LABEL") < 0) { && !parameters.contains("X-LABEL")) {
// Header request, send approximate size // Header request, send approximate size
size = message.size; size = message.size;
} else { } else {
@ -814,7 +810,7 @@ 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 (param.equals("RFC822") || param.startsWith("BODY[") || param.startsWith("BODY.PEEK[") || "RFC822.HEADER".equals(param)) { } else if ("RFC822".equals(param) || param.startsWith("BODY[") || param.startsWith("BODY.PEEK[") || "RFC822.HEADER".equals(param)) {
// get full param // get full param
if (param.indexOf('[') >= 0) { if (param.indexOf('[') >= 0) {
StringBuilder paramBuffer = new StringBuilder(param); StringBuilder paramBuffer = new StringBuilder(param);
@ -841,7 +837,7 @@ public class ImapConnection extends AbstractConnection {
// try to parse message part index // try to parse message part index
String partIndexString = StringUtil.getToken(param, "[", "]"); String partIndexString = StringUtil.getToken(param, "[", "]");
if (("".equals(partIndexString) || partIndexString == null) && !"RFC822.HEADER".equals(param)) { if ((partIndexString == null || partIndexString.length() == 0) && !"RFC822.HEADER".equals(param)) {
// write message with headers // write message with headers
partOutputStream = new PartialOutputStream(baos, startIndex, maxSize); partOutputStream = new PartialOutputStream(baos, startIndex, maxSize);
partInputStream = messageWrapper.getRawInputStream(); partInputStream = messageWrapper.getRawInputStream();
@ -849,7 +845,7 @@ public class ImapConnection extends AbstractConnection {
// write message without headers // write message without headers
partOutputStream = new PartOutputStream(baos, false, true, startIndex, maxSize); partOutputStream = new PartOutputStream(baos, false, true, startIndex, maxSize);
partInputStream = messageWrapper.getRawInputStream(); partInputStream = messageWrapper.getRawInputStream();
} else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) { } else if ("RFC822.HEADER".equals(param) || (partIndexString != null && partIndexString.startsWith("HEADER"))) {
// Header requested fetch headers // Header requested fetch headers
String[] requestedHeaders = getRequestedHeaders(partIndexString); String[] requestedHeaders = getRequestedHeaders(partIndexString);
// OSX Lion special flags request // OSX Lion special flags request
@ -870,7 +866,7 @@ public class ImapConnection extends AbstractConnection {
baos.write(10); baos.write(10);
} }
} }
} else { } else if (partIndexString != null) {
MimePart bodyPart = messageWrapper.getMimeMessage(); MimePart bodyPart = messageWrapper.getMimeMessage();
String[] partIndexStrings = partIndexString.split("\\."); String[] partIndexStrings = partIndexString.split("\\.");
for (String subPartIndexString : partIndexStrings) { for (String subPartIndexString : partIndexStrings) {
@ -1660,13 +1656,11 @@ public class ImapConnection extends AbstractConnection {
* Filter to output only headers, also count full size * Filter to output only headers, also count full size
*/ */
private static final class PartOutputStream extends FilterOutputStream { private static final class PartOutputStream extends FilterOutputStream {
private static final int START = 0; protected static enum State {
private static final int CR = 1; START, CR, CRLF, CRLFCR, BODY
private static final int CRLF = 2; }
private static final int CRLFCR = 3;
private static final int BODY = 4;
private int state = START; private State state = State.START;
private int size; private int size;
private int bufferSize; private int bufferSize;
private final boolean writeHeaders; private final boolean writeHeaders;
@ -1686,33 +1680,33 @@ public class ImapConnection extends AbstractConnection {
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
size++; size++;
if (((state != BODY && writeHeaders) || (state == BODY && writeBody)) && if (((state != State.BODY && writeHeaders) || (state == State.BODY && writeBody)) &&
(size > startIndex) && (bufferSize < maxSize) (size > startIndex) && (bufferSize < maxSize)
) { ) {
super.write(b); super.write(b);
bufferSize++; bufferSize++;
} }
if (state == START) { if (state == State.START) {
if (b == '\r') { if (b == '\r') {
state = CR; state = State.CR;
} }
} else if (state == CR) { } else if (state == State.CR) {
if (b == '\n') { if (b == '\n') {
state = CRLF; state = State.CRLF;
} else { } else {
state = START; state = State.START;
} }
} else if (state == CRLF) { } else if (state == State.CRLF) {
if (b == '\r') { if (b == '\r') {
state = CRLFCR; state = State.CRLFCR;
} else { } else {
state = START; state = State.START;
} }
} else if (state == CRLFCR) { } else if (state == State.CRLFCR) {
if (b == '\n') { if (b == '\n') {
state = BODY; state = State.BODY;
} else { } else {
state = START; state = State.START;
} }
} }
} }
@ -1913,7 +1907,7 @@ public class ImapConnection extends AbstractConnection {
} }
} }
class IMAPTokenizer extends StringTokenizer { static class IMAPTokenizer extends StringTokenizer {
IMAPTokenizer(String value) { IMAPTokenizer(String value) {
super(value); super(value);
} }