001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.processor.fetch;
017    
018    import javax.mail.FetchProfile;
019    
020    import org.jboss.netty.channel.Channel;
021    
022    import com.hs.mail.imap.ImapSession;
023    import com.hs.mail.imap.mailbox.MailboxManager;
024    import com.hs.mail.imap.mailbox.SelectedMailbox;
025    import com.hs.mail.imap.mailbox.UidToMsnMapper;
026    import com.hs.mail.imap.message.FetchData;
027    import com.hs.mail.imap.message.SequenceRange;
028    import com.hs.mail.imap.message.request.FetchRequest;
029    import com.hs.mail.imap.message.request.ImapRequest;
030    import com.hs.mail.imap.message.responder.FetchResponder;
031    import com.hs.mail.imap.message.responder.Responder;
032    import com.hs.mail.imap.message.response.FetchResponse;
033    import com.hs.mail.imap.message.response.FetchResponseBuilder;
034    import com.hs.mail.imap.processor.AbstractImapProcessor;
035    
036    /**
037     * Handler for FETCH command.
038     * 
039     * @author Won Chul Doh
040     * @since Feb 1, 2010
041     *
042     */
043    public class FetchProcessor extends AbstractImapProcessor {
044    
045            @Override
046            protected void doProcess(ImapSession session, ImapRequest message,
047                            Responder responder) {
048                    doProcess(session, (FetchRequest) message, (FetchResponder) responder);
049            }
050            
051            private void doProcess(ImapSession session, FetchRequest request,
052                            FetchResponder responder) {
053                    SelectedMailbox selected = session.getSelectedMailbox();
054                    UidToMsnMapper map = new UidToMsnMapper(selected, request.isUseUID());
055                    MailboxManager manager = getMailboxManager();
056                    FetchResponseBuilder builder = new FetchResponseBuilder(manager);
057                    SequenceRange[] sequenceSet = request.getSequenceSet();
058                    FetchProfile fp = request.getFetchProfile();
059                    for (int i = 0; i < sequenceSet.length; i++) {
060                            long min = map.getMinMessageNumber(sequenceSet[i].getMin());
061                            long max = map.getMaxMessageNumber(sequenceSet[i].getMax());
062                            for (long j = min; j <= max && j >= 0; j++) {
063                                    long uid = map.getUID((int) j);
064                                    if (uid != -1) {
065                                            FetchData fd = manager.getMessageFetchData(uid);
066                                            if (fd != null) {
067                                                    FetchResponse response = builder.build(j, fp, fd);
068                                                    if (response != null) {
069                                                            responder.respond(response);
070                                                    }
071                                            }
072                                    } else {
073                                            break; // Out of index
074                                    }
075                            }
076                    }
077                    responder.okCompleted(request);
078            }
079            
080            @Override
081            protected Responder createResponder(Channel channel, ImapRequest request) {
082                    return new FetchResponder(channel, request);
083            }
084    
085    }