1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36 public class RetrCmdHandler implements CommandHandler {
37
38
39
40
41 public void onCommand(POP3Session session) {
42 doRETR(session,session.getCommandArgument());
43 }
44
45
46
47
48
49
50
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 }