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.mailet.Mail;
25
26 import javax.mail.MessagingException;
27
28 import java.util.Iterator;
29
30 /**
31 * Handles STAT command
32 */
33 public class StatCmdHandler implements CommandHandler {
34
35 /**
36 * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
37 */
38 public void onCommand(POP3Session session) {
39 doSTAT(session,session.getCommandArgument());
40 }
41
42 /**
43 * Handler method called upon receipt of a STAT command.
44 * Returns the number of messages in the mailbox and its
45 * aggregate size.
46 *
47 * @param argument the first argument parsed by the parseCommand method
48 */
49 private void doSTAT(POP3Session session,String argument) {
50 String responseString = null;
51 if (session.getHandlerState() == POP3Handler.TRANSACTION) {
52 long size = 0;
53 int count = 0;
54 try {
55 for (Iterator i = session.getUserMailbox().iterator(); i.hasNext(); ) {
56 Mail mc = (Mail) i.next();
57 if (mc != POP3Handler.DELETED) {
58 size += mc.getMessageSize();
59 count++;
60 }
61 }
62 StringBuffer responseBuffer =
63 new StringBuffer(32)
64 .append(POP3Handler.OK_RESPONSE)
65 .append(" ")
66 .append(count)
67 .append(" ")
68 .append(size);
69 responseString = responseBuffer.toString();
70 session.writeResponse(responseString);
71 } catch (MessagingException me) {
72 responseString = POP3Handler.ERR_RESPONSE;
73 session.writeResponse(responseString);
74 }
75 } else {
76 responseString = POP3Handler.ERR_RESPONSE;
77 session.writeResponse(responseString);
78 }
79 }
80
81
82 }