View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.imap.cmd;
14  
15  import java.io.IOException;
16  import javax.mail.Folder;
17  import javax.mail.MessagingException;
18  import javax.mail.Store;
19  import org.abstracthorizon.mercury.imap.IMAPSession;
20  import org.abstracthorizon.mercury.imap.response.ListResponse;
21  import org.abstracthorizon.mercury.imap.util.ParserException;
22  import org.abstracthorizon.mercury.imap.util.IMAPScanner;
23  
24  /**
25   * List IMAP command
26   *
27   * @author Daniel Sendula
28   */
29  public class List extends IMAPCommand {
30  
31      /**
32       * Constructor
33       * @param mnemonic mnemonic
34       */
35      public List(String mnemonic) {
36          super(mnemonic);
37      }
38  
39      /**
40       * Executes the command
41       * @param session
42       * @throws ParserException
43       * @throws MessagingException
44       * @throws CommandException
45       * @throws IOException
46       */
47      protected void execute(IMAPSession session) throws ParserException, MessagingException, IOException {
48          Store store = session.getStore();
49          StringBuffer name = new StringBuffer();
50          StringBuffer expr = new StringBuffer();
51          IMAPScanner scanner = session.getScanner();
52  
53          if (!scanner.mailbox(name)) {
54              throw new ParserException("<mailbox_name>");
55          }
56          if (!scanner.is_char(' ')) {
57              throw new ParserException("<SP>");
58          }
59          if (!scanner.list_mailbox(expr)) {
60              throw new ParserException("<expr>");
61          }
62          checkEOL(session);
63  
64          Folder root = store.getFolder(name.toString());
65  
66          Folder[] res = root.list(expr.toString());
67          if (res.length > 0) {
68              for (int i=0; i<res.length; i++) {
69                  new ListResponse(session, res[i]).submit();
70              }
71          }
72          sendOK(session);
73      }
74  }