views:

204

answers:

1

Hi, I have a Requirement to make an IMAP client as a Web application

I achieved the functionality of Sorting as:

   //userFolder is an Object of IMAPFolder
   Message[] messages = userFolder.getMessages();

   Arrays.sort(messages, new Comparator<Message>()
    {
        public int compare(Message message1, Message message2)
        {
            int returnValue = 0;
            try
            {
                if (sortCriteria == SORT_SENT_DATE)
                {
                    returnValue = message1.getSentDate().compareTo(message2.getSentDate());
                }
            } catch (Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }

            if (sortType == SORT_TYPE_DESCENDING)
            {
                returnValue = -returnValue;
            }

            return returnValue;
        }
    });

The code snippet is not complete , its just brief SORT_SENT_DATE,SORT_TYPE_DESCENDING are my own constants.

Actually This solution is working fine, but it fails in logic for paging Being a Web based application, i cant expect server to load all messages for every user and sort them (We do have situations >1000 Simultaneous users with mail boxes having > 1000 messages each )

It also does not make sense for the web server to load all, sort them, return just a small part (say 1-20), and on the next request, again load all sort them and return (21-40). Caching possible, but whts the gaurantee user would actually make a request ?

I heard there is a class called FetchProfile, can that help me here ? (I guess it would still load all messages but just the information thats required) Is there any other way to achieve this ?

I need a solution that could also work in Search operation (searching with paging), I have built an archietecture to create a SearchTerm but here too i would require paging.

for ref, i have asked this same Question at : http://www.coderanch.com/t/461408/Other-JSE-JEE-APIs/java/it-possible-use-IMAP-paging

+1  A: 

You would need a server with the SORT extension and even that may not be enough. Then you issue SORT on the specific mailbox and FETCH only those message numbers that fall into your view.

Update based on comments:

For servers where the SORT extension is not available the next best thing is to FETCH header field representing the sort key for all items (eg. FETCH 1:* BODY[HEADER.FIELDS(SUBJECT)] for subject or FETCH 1:* BODY[HEADER.FIELDS(DATA)] for sent date), then sort based on the key. You will get a list of sorted message number this way, which should be equivalent to what the SORT command would return.

If server side cache is allowed then the best way is to keep cache of envelopes (in the IMAP ENVELOPE sense) and then update it using the techniques described in RFC 4549. It's easy to sort and page given this cache.

There are two IMAP APIs on Java - the official JavaMail API and Risoretto. Risoretto is more low-level and should allow to implement anything described above, JavaMail may be able to do so as well, but I don't have much experience with it.

Filip Navara
BTW, I have never tried to implement this using JavaMail API, but the open source Risoretto (http://columba.sourceforge.net/ristretto-1.0-docs/) libraries should support it.
Filip Navara
+1 for the suggestion,My server does not support the sort Extension (i am using GMAIL currently for testing)
Salvin Francis
Speaking purely in the IMAP terms, the second best way after the SORT extension is to do FETCH 1:* BODY.PEEK[HEADER.FIELDS(SUBJECT)], which will return you the sort keys. Then you can sort it and get the message number list equivalent to that received from SORT. Unfortunately this may not be the best way to implement it in practice. -- What we do in out application is to FETCH all envelopes (in the IMAP definition of ENVELOPE) and cache that. Then we synchronize the cache (RFC4549 - http://www.isi.edu/in-notes/rfc4549.txt) and use that for sorting and displaying.
Filip Navara
(where SUBJECT is the current sort key, of course)
Filip Navara
oh yes, sorry not to mention, i am sorting on Subject,date (sent/recieved), conversation etc.
Salvin Francis
@Filip Navara,Is that possible in java mail ?all over the net i just find the same example on fetchprofile: Message[] msgs = folder.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add("X-mailer"); folder.fetch(msgs, fp);Can it be used for (like in your example) fetch only subject/To/From etc... ??
Salvin Francis
Apparently the FetchProfile class supports fetching individual header fields - http://www.ermalaev.spb.ru/products/javamail/javadocs/javax/mail/FetchProfile.html#add(java.lang.String) - but I have never used it myself.
Filip Navara
Damn, the URL in the last comment should have pointed to the "add(String headerName)" method in FetchProfile.
Filip Navara
its ok, i understood that, however, the way i see it is that fetch still loads all content (or only those required).There is an optimisation here @ batch loading, but it still isnt solving my problem. Thanks for the suggestion though
Salvin Francis
Fetch, in IMAP terms, shouldn't load all content and I believe JavaMail follows IMAP here.
Filip Navara
by all i meant : if i am sorting by subject, it would be used to load all subjects, client would now pick up the first 20 subjects and discard the rest. so, it is still not optimized.
Salvin Francis
and this would happen for every paging request.
Salvin Francis
@Salvin Francis, Yes, but without the SORT extension there's no better way in terms of IMAP. You could do server-side cache though.
Filip Navara
Server side cache is too costly @ too many simultaneous user load still, +1 for that suggestion.
Salvin Francis
@Salvin Francis, Well, if you can choose the IMAP server for final solution then you can go with Dovecot (http://www.dovecot.org/) or Archivopteryx (http://www.archiveopteryx.org/) which both support the SORT extension.
Filip Navara
Its a good suggestion, but i am working on enhancing an existing application that is already using a mail server, so i dont think that option is feasible for me.
Salvin Francis
@Filip Navara is there no option for paging in search too ?
Salvin Francis
@Salvin Francis, it's all practically the same when search is involved. With SORT extension available the SORT command accepts search criteria. Without the SORT extension you have to call regular search, which will give you a list of message numbers and the you can perform FETCH w/ sort key just on those messages and the rest is the same as if search was not applied...
Filip Navara