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.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.jboss.netty.channel.Channel;
022    
023    import com.hs.mail.imap.ImapConstants;
024    import com.hs.mail.imap.ImapSession;
025    import com.hs.mail.imap.mailbox.Mailbox;
026    import com.hs.mail.imap.mailbox.MailboxQuery;
027    import com.hs.mail.imap.message.request.AbstractListRequest;
028    import com.hs.mail.imap.message.request.ImapRequest;
029    import com.hs.mail.imap.message.responder.ListResponder;
030    import com.hs.mail.imap.message.responder.Responder;
031    import com.hs.mail.imap.message.response.ListResponse;
032    
033    /**
034     * 
035     * @author Won Chul Doh
036     * @since Apr 16, 2010
037     *
038     */
039    public abstract class AbstractListProcessor extends AbstractImapProcessor {
040    
041            @Override
042            protected void doProcess(ImapSession session, ImapRequest message,
043                            Responder responder) {
044                    doProcess(session, (AbstractListRequest) message,
045                                    (ListResponder) responder);
046            }
047    
048            private void doProcess(ImapSession session, AbstractListRequest request,
049                            ListResponder responder) {
050                    String referenceName = request.getMailbox();
051                    String mailboxName = request.getPattern();
052                    if (StringUtils.isEmpty(mailboxName)) {
053                            // An empty mailbox name argument is a special request
054                            // to return the hierarchy delimiter and the root name of the name
055                            // given in the reference.
056                            String referenceRoot = "";
057                            if (referenceName.startsWith(ImapConstants.NAMESPACE_PREFIX)) {
058                                    // A qualified reference name - get the first element.
059                                    int i = referenceName.indexOf(Mailbox.folderSeparator);
060                                    if (i != -1) {
061                                            referenceRoot = referenceName.substring(0, i + 1);
062                                    }
063                            }
064                            responder.untagged(request.getCommand() + " (\\Noselect) \""
065                                            + Mailbox.folderSeparator + "\" \"" + referenceRoot
066                                            + "\"\r\n");
067                    } else {
068                            if (mailboxName.startsWith(ImapConstants.NAMESPACE_PREFIX)) {
069                                    // If the mailboxName if fully qualified, ignore the reference
070                                    // name.
071                                    referenceName = "";
072                            } else {
073                                    // Remove separator from the end of reference name.
074                                    referenceName = StringUtils.chomp(referenceName,
075                                                    Mailbox.folderSeparator);
076                            }
077                            doList(session, responder, referenceName, mailboxName);
078                    }
079                    responder.okCompleted(request);
080            }
081    
082            @Override
083            protected Responder createResponder(Channel channel, ImapRequest request) {
084                    return new ListResponder(channel, request);
085            }
086    
087            protected abstract List<Mailbox> listMailbox(long userID, long ownerID,
088                            String mailboxName, MailboxQuery query);
089            
090            protected abstract Mailbox getMailbox(long ownerID, String mailboxName);
091    
092            protected void doList(ImapSession session, ListResponder responder,
093                            String referenceName, String mailboxName) {
094                    MailboxQuery query = new MailboxQuery(referenceName, mailboxName);
095                    if (query.containsWildcard()) {
096                            List<Mailbox> mailboxes = listMailbox(session.getUserID(),
097                                            session.getUserID(), referenceName, query);
098                            for (Mailbox mailbox : mailboxes) {
099                                    responder.respond(new ListResponse(mailbox));
100                            }
101                    } else {
102                            // Expression is an absolute mailbox name.
103                            Mailbox mailbox = getMailbox(session.getUserID(),
104                                            query.getExpression());
105                            if (mailbox != null) {
106                                    responder.respond(new ListResponse(mailbox));
107                            }
108                    }
109            }
110            
111    }