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.avalon.framework.container.ContainerUtil;
25 import org.apache.mailet.Mail;
26
27 /**
28 * Handles DELE command
29 */
30 public class DeleCmdHandler implements CommandHandler {
31
32 /**
33 * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
34 */
35 public void onCommand(POP3Session session) {
36 doDELE(session,session.getCommandArgument());
37 }
38
39 /**
40 * Handler method called upon receipt of a DELE command.
41 * This command deletes a particular mail message from the
42 * mailbox.
43 *
44 * @param argument the first argument parsed by the parseCommand method
45 */
46 private void doDELE(POP3Session session,String argument) {
47 String responseString = null;
48 if (session.getHandlerState() == POP3Handler.TRANSACTION) {
49 int num = 0;
50 try {
51 num = Integer.parseInt(argument);
52 } catch (Exception e) {
53 responseString = POP3Handler.ERR_RESPONSE + " Usage: DELE [mail number]";
54 session.writeResponse(responseString);
55 return;
56 }
57 try {
58 Mail mc = (Mail) session.getUserMailbox().get(num);
59 if (mc == POP3Handler.DELETED) {
60 StringBuffer responseBuffer =
61 new StringBuffer(64)
62 .append(POP3Handler.ERR_RESPONSE)
63 .append(" Message (")
64 .append(num)
65 .append(") already deleted.");
66 responseString = responseBuffer.toString();
67 session.writeResponse(responseString);
68 } else {
69 session.getUserMailbox().set(num, POP3Handler.DELETED);
70 // we are replacing our reference with "DELETED", so we have
71 // to dispose the no-more-referenced mail object.
72 ContainerUtil.dispose(mc);
73 session.writeResponse(POP3Handler.OK_RESPONSE + " Message deleted");
74 }
75 } catch (IndexOutOfBoundsException iob) {
76 StringBuffer responseBuffer =
77 new StringBuffer(64)
78 .append(POP3Handler.ERR_RESPONSE)
79 .append(" Message (")
80 .append(num)
81 .append(") does not exist.");
82 responseString = responseBuffer.toString();
83 session.writeResponse(responseString);
84 }
85 } else {
86 responseString = POP3Handler.ERR_RESPONSE;
87 session.writeResponse(responseString);
88 }
89 }
90
91
92 }