001: package org.claros.commons.mail.protocols;
002:
003: import java.util.ArrayList;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.claros.commons.mail.models.ConnectionProfile;
008:
009: import com.sun.mail.iap.Argument;
010: import com.sun.mail.iap.ProtocolException;
011: import com.sun.mail.iap.Response;
012: import com.sun.mail.imap.IMAPFolder;
013: import com.sun.mail.imap.protocol.IMAPProtocol;
014: import com.sun.mail.imap.protocol.IMAPResponse;
015:
016: public class ImapSortProtocolCommand implements
017: IMAPFolder.ProtocolCommand {
018: private ArrayList sortedList = new ArrayList();
019:
020: public static final String SORT_DATE = "DATE";
021: public static final String SORT_SUBJECT = "SUBJECT";
022: public static final String SORT_SIZE = "SIZE";
023: public static final String SORT_FROM = "FROM";
024: public static final String SORT_TO = "TO";
025: private static final String REVERSE_STR = "REVERSE";
026:
027: private String sortCriteria = SORT_DATE;
028: private boolean ascending = false;
029: private ConnectionProfile profile;
030:
031: private static Log log = LogFactory
032: .getLog(ImapSortProtocolCommand.class);
033:
034: public ImapSortProtocolCommand() {
035: super ();
036: }
037:
038: public ImapSortProtocolCommand(String sortCriteria,
039: boolean ascending, ConnectionProfile profile) {
040: this .sortCriteria = sortCriteria;
041: this .ascending = ascending;
042: this .profile = profile;
043: }
044:
045: public Object doCommand(IMAPProtocol p) throws ProtocolException {
046: sortedList = new ArrayList();
047: Argument args = new Argument();
048: Argument list = new Argument();
049: if (!ascending) {
050: list.writeString(REVERSE_STR);
051: }
052: list.writeString(sortCriteria);
053: args.writeArgument(list);
054: args.writeString("UTF-8");
055: args.writeString("ALL");
056: Response[] r = p.command("SORT", args);
057: Response response = r[r.length - 1];
058:
059: // Grab response
060: if (response.isOK()) { // command succesful
061: // the server supports imap sort.
062: log
063: .info("IMAP Server supports server side sorting. This is good news!!! So performance will be much better!!!");
064: profile.setSupportSort(true);
065:
066: for (int i = 0, len = r.length; i < len; i++) {
067: if (!(r[i] instanceof IMAPResponse)) {
068: continue;
069: }
070:
071: IMAPResponse ir = (IMAPResponse) r[i];
072: if (ir.keyEquals("SORT")) {
073: String num;
074: while ((num = ir.readAtomString()) != null) {
075: sortedList.add(new Integer(num));
076: log.debug("The message with number: " + num
077: + " added to the sorted fetch list");
078: }
079: r[i] = null;
080: }
081: }
082: } else {
083: log
084: .info("IMAP Server doesn't support server side sorting. This is bad news :( So performance will be not as good as it could be!!!");
085: profile.setSupportSort(false);
086: throw new ProtocolException();
087: }
088: // dispatch remaining untagged responses
089: p.notifyResponseHandlers(r);
090: p.handleResult(response);
091: return null;
092: }
093:
094: public ArrayList getSortedList() {
095: return sortedList;
096: }
097:
098: public void setSortedList(ArrayList sortedList) {
099: this .sortedList = sortedList;
100: }
101:
102: public ConnectionProfile getProfile() {
103: return profile;
104: }
105:
106: }
|