1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.pop3server;
23
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.mailet.Mail;
27
28 import javax.mail.MessagingException;
29
30 import java.util.ArrayList;
31 import java.util.Iterator;
32
33
34 /**
35 * Handles RSET command
36 */
37 public class RsetCmdHandler extends AbstractLogEnabled implements CommandHandler {
38
39 /**
40 * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
41 */
42 public void onCommand(POP3Session session) {
43 doRSET(session,session.getCommandArgument());
44 }
45
46 /**
47 * Handler method called upon receipt of a RSET command.
48 * Calls stat() to reset the mailbox.
49 *
50 * @param argument the first argument parsed by the parseCommand method
51 */
52 private void doRSET(POP3Session session,String argument) {
53 String responseString = null;
54 if (session.getHandlerState() == POP3Handler.TRANSACTION) {
55 stat(session, getLogger());
56 responseString = POP3Handler.OK_RESPONSE;
57 } else {
58 responseString = POP3Handler.ERR_RESPONSE;
59 }
60 session.writeResponse(responseString);
61 }
62
63
64 /**
65 * Implements a "stat". If the handler is currently in
66 * a transaction state, this amounts to a rollback of the
67 * mailbox contents to the beginning of the transaction.
68 * This method is also called when first entering the
69 * transaction state to initialize the handler copies of the
70 * user inbox.
71 *
72 */
73 public static void stat(POP3Session session, Logger logger) {
74 ArrayList userMailbox = new ArrayList();
75 userMailbox.add(POP3Handler.DELETED);
76 try {
77 for (Iterator it = session.getUserInbox().list(); it.hasNext(); ) {
78 String key = (String) it.next();
79 Mail mc = session.getUserInbox().retrieve(key);
80 // Retrieve can return null if the mail is no longer in the store.
81 // In this case we simply continue to the next key
82 if (mc == null) {
83 continue;
84 }
85 userMailbox.add(mc);
86 }
87 } catch(MessagingException e) {
88 // In the event of an exception being thrown there may or may not be anything in userMailbox
89 logger.error("Unable to STAT mail box ", e);
90 }
91 finally {
92 session.setUserMailbox(userMailbox);
93 session.setBackupUserMailbox((ArrayList) userMailbox.clone());
94 }
95 }
96
97 }