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 org.jboss.netty.channel.Channel; 019 020 import com.hs.mail.imap.ImapSession; 021 import com.hs.mail.imap.mailbox.Mailbox; 022 import com.hs.mail.imap.mailbox.MailboxManager; 023 import com.hs.mail.imap.mailbox.SelectedMailbox; 024 import com.hs.mail.imap.mailbox.UidToMsnMapper; 025 import com.hs.mail.imap.message.request.AbstractMailboxRequest; 026 import com.hs.mail.imap.message.request.ImapRequest; 027 import com.hs.mail.imap.message.responder.Responder; 028 import com.hs.mail.imap.message.responder.SelectResponder; 029 import com.hs.mail.imap.message.response.HumanReadableText; 030 import com.hs.mail.imap.message.response.SelectResponse; 031 import com.hs.mail.imap.message.response.SelectResponseBuilder; 032 033 /** 034 * 035 * @author Won Chul Doh 036 * @since Mar 24, 2010 037 * 038 */ 039 public abstract class AbstractSelectProcessor extends AbstractImapProcessor { 040 041 @Override 042 protected void doProcess(ImapSession session, ImapRequest message, 043 Responder responder) { 044 doProcess(session, (AbstractMailboxRequest) message, 045 (SelectResponder) responder); 046 } 047 048 private void doProcess(ImapSession session, AbstractMailboxRequest request, 049 SelectResponder responder) { 050 String mailboxName = request.getMailbox(); 051 MailboxManager manager = getMailboxManager(); 052 Mailbox mailbox = manager.getMailbox(session.getUserID(), mailboxName); 053 if (mailbox == null) { 054 responder.taggedNo(request, HumanReadableText.MAILBOX_NOT_FOUND); 055 } else if (mailbox.isNoSelect()) { 056 responder.taggedNo(request, 057 HumanReadableText.MAILBOX_NOT_SELECTABLE); 058 } else { 059 SelectedMailbox selected = session.getSelectedMailbox(); 060 if (selected != null && !selected.isReadOnly() 061 && selected.isRecent()) { 062 // If the session is read-write, subsequent sessions will not see 063 // \Recent set for the messages in this mailbox. 064 manager.resetRecent(selected.getMailboxID()); 065 } 066 long sessionID = session.getSessionID(); 067 long mailboxID = mailbox.getMailboxID(); 068 selected = new SelectedMailbox(sessionID, mailboxID, isReadOnly()); 069 UidToMsnMapper map = new UidToMsnMapper(selected, false); 070 SelectResponse response = new SelectResponseBuilder().build(map, mailbox); 071 responder.respond(response); 072 073 selected.setRecent(response.getRecentMessageCount() > 0); 074 session.selected(selected); 075 manager.addEventListener(selected); 076 077 responder.okCompleted(request, "[" + getResponseCode() + "]"); 078 } 079 } 080 081 private String getResponseCode() { 082 return isReadOnly() ? "READ-ONLY" : "READ-WRITE"; 083 } 084 085 @Override 086 protected Responder createResponder(Channel channel, ImapRequest request) { 087 return new SelectResponder(channel, request); 088 } 089 090 protected abstract boolean isReadOnly(); 091 092 }