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;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.collections.CollectionUtils;
022    import org.apache.commons.lang.StringUtils;
023    
024    import com.hs.mail.imap.ImapSession;
025    import com.hs.mail.imap.mailbox.MailboxManager;
026    import com.hs.mail.imap.mailbox.SelectedMailbox;
027    import com.hs.mail.imap.mailbox.UidToMsnMapper;
028    import com.hs.mail.imap.message.request.ImapRequest;
029    import com.hs.mail.imap.message.request.SearchRequest;
030    import com.hs.mail.imap.message.responder.Responder;
031    
032    /**
033     * 
034     * @author Won Chul Doh
035     * @since Feb 1, 2010
036     *
037     */
038    public class SearchProcessor extends AbstractImapProcessor {
039    
040            @Override
041            protected void doProcess(ImapSession session, ImapRequest message,
042                            Responder responder) {
043                    SearchRequest request = (SearchRequest) message;
044                    SelectedMailbox selected = session.getSelectedMailbox();
045                    MailboxManager manager = getMailboxManager();
046                    List<Long> uids = selected.getCachedUids();
047                    if (uids == null) {
048                            uids = manager.getMessageIDList(selected.getMailboxID());
049                    }
050                    UidToMsnMapper map = new UidToMsnMapper(selected, uids, request.isUseUID());
051                    List<Long> results = manager.search(map, selected.getMailboxID(),
052                                    request.getSearchKey());
053                    if (CollectionUtils.isNotEmpty(results)) {
054                            List<Long> searched = new ArrayList<Long>();
055                            for (long messageID : results) {
056                                    long msgnum = map.getMessageNumber(messageID);
057                                    if (msgnum != -1) {
058                                            searched.add(msgnum);
059                                    }
060                            }
061                            responder.untagged(request.getCommand() + " "
062                                            + StringUtils.join(searched, ' ') + "\r\n");
063                    }
064                    responder.okCompleted(request);
065            }
066    
067    }