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.collections.CollectionUtils;
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.request.ExpungeRequest;
027    import com.hs.mail.imap.message.request.ImapRequest;
028    import com.hs.mail.imap.message.responder.Responder;
029    import com.hs.mail.imap.message.response.HumanReadableText;
030    
031    /**
032     * 
033     * @author Won Chul Doh
034     * @since Feb 1, 2010
035     *
036     */
037    public class ExpungeProcessor extends AbstractExpungeProcessor {
038    
039            @Override
040            protected void doProcess(ImapSession session, ImapRequest message,
041                            Responder responder) {
042                    ExpungeRequest request = (ExpungeRequest) message;
043                    SelectedMailbox selected = session.getSelectedMailbox();
044                    if (selected.isReadOnly()) {
045                            responder.taggedNo(request, "[READ-ONLY]",
046                                            HumanReadableText.MAILBOX_IS_READ_ONLY);
047                            return;
048                    }
049                    MailboxManager manager = getMailboxManager();
050                    // Permanently removes all messages that have the \Deleted flag set.
051                    List<Long> expungedUids = manager.expunge(selected.getMailboxID());
052                    if (CollectionUtils.isNotEmpty(expungedUids)) {
053                            UidToMsnMapper map = new UidToMsnMapper(selected, false);
054                            // Propagate event before delete message.
055                            fireExpunged(session, expungedUids);
056                            for (Long uid : expungedUids) {
057                                    int msgnum = map.getMessageNumber(uid);
058                                    if (msgnum != -1) {
059                                            manager.deleteMessage(uid);
060                                            responder.untagged(msgnum + " " + request.getCommand()
061                                                            + "\r\n");
062                                    } else {
063                                            // This case is impossible.
064                                            logger.error("Failed to convert UID " + uid
065                                                            + " to message number.");
066                                    }
067                            }
068                            selected.resetEvents();
069                    }
070                    responder.okCompleted(request);
071            }
072    
073    }