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.james.util.stream.ExtraDotOutputStream;
25  import org.apache.james.util.watchdog.BytesWrittenResetOutputStream;
26  import org.apache.mailet.Mail;
27  
28  import javax.mail.MessagingException;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  
33  /**
34    * Handles RETR command
35    */
36  public class RetrCmdHandler implements CommandHandler {
37  
38      /**
39       * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
40       */
41      public void onCommand(POP3Session session) {
42          doRETR(session,session.getCommandArgument());
43      }
44  
45      /**
46       * Handler method called upon receipt of a RETR command.
47       * This command retrieves a particular mail message from the
48       * mailbox.
49       *
50       * @param argument the first argument parsed by the parseCommand method
51       */
52      private void doRETR(POP3Session session,String argument) {
53          String responseString = null;
54          if (session.getHandlerState() == POP3Handler.TRANSACTION) {
55              int num = 0;
56              try {
57                  num = Integer.parseInt(argument.trim());
58              } catch (Exception e) {
59                  responseString = POP3Handler.ERR_RESPONSE + " Usage: RETR [mail number]";
60                  session.writeResponse(responseString);
61                  return;
62              }
63              try {
64                  Mail mc = (Mail) session.getUserMailbox().get(num);
65                  if (mc != POP3Handler.DELETED) {
66                      responseString = POP3Handler.OK_RESPONSE + " Message follows";
67                      session.writeResponse(responseString);
68                      try {
69                          ExtraDotOutputStream edouts =
70                                  new ExtraDotOutputStream(session.getOutputStream());
71                          OutputStream nouts = new BytesWrittenResetOutputStream(edouts,
72                                                                    session.getWatchdog(),
73                                                                    session.getConfigurationData().getResetLength());
74                          mc.getMessage().writeTo(nouts);
75                          nouts.flush();
76                          edouts.checkCRLFTerminator();
77                          edouts.flush();
78                      } finally {
79                          session.writeResponse(".");
80                      }
81                  } else {
82                      StringBuffer responseBuffer =
83                          new StringBuffer(64)
84                                  .append(POP3Handler.ERR_RESPONSE)
85                                  .append(" Message (")
86                                  .append(num)
87                                  .append(") already deleted.");
88                      responseString = responseBuffer.toString();
89                      session.writeResponse(responseString);
90                  }
91              } catch (IOException ioe) {
92                  responseString = POP3Handler.ERR_RESPONSE + " Error while retrieving message.";
93                  session.writeResponse(responseString);
94              } catch (MessagingException me) {
95                  responseString = POP3Handler.ERR_RESPONSE + " Error while retrieving message.";
96                  session.writeResponse(responseString);
97              } catch (IndexOutOfBoundsException iob) {
98                  StringBuffer responseBuffer =
99                      new StringBuffer(64)
100                             .append(POP3Handler.ERR_RESPONSE)
101                             .append(" Message (")
102                             .append(num)
103                             .append(") does not exist.");
104                 responseString = responseBuffer.toString();
105                 session.writeResponse(responseString);
106             }
107         } else {
108             responseString = POP3Handler.ERR_RESPONSE;
109             session.writeResponse(responseString);
110         }
111     }
112 
113 
114 }