View Javadoc

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 java.util.Iterator;
27  
28  /**
29    * Handles UIDL command
30    */
31  public class UidlCmdHandler implements CommandHandler {
32  
33      /**
34       * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
35       */
36      public void onCommand(POP3Session session) {
37          doUIDL(session,session.getCommandArgument());
38      }
39  
40      /**
41       * Handler method called upon receipt of a UIDL command.
42       * Returns a listing of message ids to the client.
43       *
44       * @param argument the first argument parsed by the parseCommand method
45       */
46      private void doUIDL(POP3Session session,String argument) {
47          if (session.getHandlerState() == POP3Handler.TRANSACTION) {
48              if (argument == null) {
49                  String responseString = POP3Handler.OK_RESPONSE + " unique-id listing follows";
50                  session.writeResponse(responseString);
51                  int count = 0;
52                  for (Iterator i = session.getUserMailbox().iterator(); i.hasNext(); count++) {
53                      Mail mc = (Mail) i.next();
54                      if (mc != POP3Handler.DELETED) {
55                          StringBuffer responseBuffer =
56                              new StringBuffer(64)
57                                      .append(count)
58                                      .append(" ")
59                                      .append(mc.getName());
60                          session.writeResponse(responseBuffer.toString());
61                      }
62                  }
63                  session.writeResponse(".");
64              } else {
65                  int num = 0;
66                  try {
67                      num = Integer.parseInt(argument);
68                      Mail mc = (Mail) session.getUserMailbox().get(num);
69                      if (mc != POP3Handler.DELETED) {
70                          StringBuffer responseBuffer =
71                              new StringBuffer(64)
72                                      .append(POP3Handler.OK_RESPONSE)
73                                      .append(" ")
74                                      .append(num)
75                                      .append(" ")
76                                      .append(mc.getName());
77                          session.writeResponse(responseBuffer.toString());
78                      } else {
79                          StringBuffer responseBuffer =
80                              new StringBuffer(64)
81                                      .append(POP3Handler.ERR_RESPONSE)
82                                      .append(" Message (")
83                                      .append(num)
84                                      .append(") already deleted.");
85                          session.writeResponse(responseBuffer.toString());
86                      }
87                  } catch (IndexOutOfBoundsException npe) {
88                      StringBuffer responseBuffer =
89                          new StringBuffer(64)
90                                  .append(POP3Handler.ERR_RESPONSE)
91                                  .append(" Message (")
92                                  .append(num)
93                                  .append(") does not exist.");
94                      session.writeResponse(responseBuffer.toString());
95                  } catch (NumberFormatException nfe) {
96                      StringBuffer responseBuffer =
97                          new StringBuffer(64)
98                                  .append(POP3Handler.ERR_RESPONSE)
99                                  .append(" ")
100                                 .append(argument)
101                                 .append(" is not a valid number");
102                     session.writeResponse(responseBuffer.toString());
103                 }
104             }
105         } else {
106             session.writeResponse(POP3Handler.ERR_RESPONSE);
107         }
108     }
109 
110 
111 }