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 LIST command
32 */
33 public class ListCmdHandler implements CommandHandler {
34
35 /**
36 * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
37 */
38 public void onCommand(POP3Session session) {
39 doLIST(session,session.getCommandArgument());
40 }
41
42 /**
43 * Handler method called upon receipt of a LIST command.
44 * Returns the number of messages in the mailbox and its
45 * aggregate size, or optionally, the number and size of
46 * a single message.
47 *
48 * @param argument the first argument parsed by the parseCommand method
49 */
50 private void doLIST(POP3Session session,String argument) {
51 if (session.getHandlerState() == POP3Handler.TRANSACTION) {
52 if (argument == null) {
53 long size = 0;
54 int count = 0;
55 try {
56 for (Iterator i = session.getUserMailbox().iterator(); i.hasNext(); ) {
57 Mail mc = (Mail) i.next();
58 if (mc != POP3Handler.DELETED) {
59 size += mc.getMessageSize();
60 count++;
61 }
62 }
63 StringBuffer responseBuffer =
64 new StringBuffer(32)
65 .append(POP3Handler.OK_RESPONSE)
66 .append(" ")
67 .append(count)
68 .append(" ")
69 .append(size);
70 session.writeResponse(responseBuffer.toString());
71 count = 0;
72 for (Iterator i = session.getUserMailbox().iterator(); i.hasNext(); count++) {
73 Mail mc = (Mail) i.next();
74
75 if (mc != POP3Handler.DELETED) {
76 responseBuffer =
77 new StringBuffer(16)
78 .append(count)
79 .append(" ")
80 .append(mc.getMessageSize());
81 session.writeResponse(responseBuffer.toString());
82 }
83 }
84 session.writeResponse(".");
85 } catch (MessagingException me) {
86 session.writeResponse(POP3Handler.ERR_RESPONSE);
87 }
88 } else {
89 int num = 0;
90 try {
91 num = Integer.parseInt(argument);
92 Mail mc = (Mail) session.getUserMailbox().get(num);
93 if (mc != POP3Handler.DELETED) {
94 StringBuffer responseBuffer =
95 new StringBuffer(64)
96 .append(POP3Handler.OK_RESPONSE)
97 .append(" ")
98 .append(num)
99 .append(" ")
100 .append(mc.getMessageSize());
101 session.writeResponse(responseBuffer.toString());
102 } else {
103 StringBuffer responseBuffer =
104 new StringBuffer(64)
105 .append(POP3Handler.ERR_RESPONSE)
106 .append(" Message (")
107 .append(num)
108 .append(") already deleted.");
109 session.writeResponse(responseBuffer.toString());
110 }
111 } catch (IndexOutOfBoundsException npe) {
112 StringBuffer responseBuffer =
113 new StringBuffer(64)
114 .append(POP3Handler.ERR_RESPONSE)
115 .append(" Message (")
116 .append(num)
117 .append(") does not exist.");
118 session.writeResponse(responseBuffer.toString());
119 } catch (NumberFormatException nfe) {
120 StringBuffer responseBuffer =
121 new StringBuffer(64)
122 .append(POP3Handler.ERR_RESPONSE)
123 .append(" ")
124 .append(argument)
125 .append(" is not a valid number");
126 session.writeResponse(responseBuffer.toString());
127 } catch (MessagingException me) {
128 session.writeResponse(POP3Handler.ERR_RESPONSE);
129 }
130 }
131 } else {
132 session.writeResponse(POP3Handler.ERR_RESPONSE);
133 }
134 }
135
136 }