* to return]
*/
@SuppressWarnings("unchecked")
@Override
public Response onCommand(POP3Session session, Request request) {
POP3Response response = null;
String parameters = request.getArgument();
if (parameters == null) {
response = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: TOP [mail number] [Line number]");
return response;
}
String argument = "";
String argument1 = "";
int pos = parameters.indexOf(" ");
if (pos > 0) {
argument = parameters.substring(0, pos);
argument1 = parameters.substring(pos + 1);
}
if (session.getHandlerState() == POP3Session.TRANSACTION) {
int num = 0;
int lines = -1;
try {
num = Integer.parseInt(argument);
lines = Integer.parseInt(argument1);
} catch (NumberFormatException nfe) {
response = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: TOP [mail number] [Line number]");
return response;
}
try {
List<MessageMetaData> uidList = (List<MessageMetaData>) session.getState().get(POP3Session.UID_LIST);
List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
Long uid = uidList.get(num - 1).getUid();
if (deletedUidList.contains(uid) == false) {
InputStream body = new CountingBodyInputStream(new ExtraDotInputStream(new CRLFTerminatedInputStream(session.getUserMailbox().getMessageBody(uid))), lines);
InputStream headers = session.getUserMailbox().getMessageHeaders(uid);
if (body != null && headers != null) {
response = new POP3StreamResponse(POP3Response.OK_RESPONSE, "Message follows", new SequenceInputStream(headers, body));
return response;
} else {
StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
}
} else {
StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") already deleted.");
response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
}
} catch (IOException ioe) {
response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
} catch (IndexOutOfBoundsException iob) {
StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
} catch (NoSuchElementException iob) {
StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
}
} else {
response = new POP3Response(POP3Response.ERR_RESPONSE);
}
return response;
}