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.common.command.CommandException;
20  import org.abstracthorizon.mercury.imap.IMAPSession;
21  import org.abstracthorizon.mercury.imap.response.LsubResponse;
22  import org.abstracthorizon.mercury.imap.util.ParserException;
23  import org.abstracthorizon.mercury.imap.util.IMAPScanner;
24  
25  /**
26   * LSUB IMAP command
27   *
28   * @author Daniel Sendula
29   */
30  public class LSub extends IMAPCommand {
31  
32      /**
33       * Constructor
34       * @param mnemonic mnemonic
35       */
36      public LSub(String mnemonic) {
37          super(mnemonic);
38      }
39  
40      /**
41       * Executes the command
42       * @param session
43       * @throws ParserException
44       * @throws MessagingException
45       * @throws CommandException
46       * @throws IOException
47       */
48      protected void execute(IMAPSession session) throws ParserException, MessagingException, CommandException, IOException {
49          Store store = session.getStore();
50          StringBuffer name = new StringBuffer();
51          StringBuffer expr = new StringBuffer();
52          IMAPScanner scanner = session.getScanner();
53  
54  
55          if (!scanner.mailbox(name)) {
56              throw new ParserException("<mailbox_name>");
57          }
58          if (!scanner.is_char(' ')) {
59              throw new ParserException("<SP>");
60          }
61          if (!scanner.list_mailbox(expr)) {
62              throw new ParserException("<expr>");
63          }
64          checkEOL(session);
65  
66          Folder root = store.getFolder(name.toString());
67  
68          Folder[] res = root.list(expr.toString());
69          if (res.length > 0) {
70              for (int i=0; i<res.length; i++) {
71                  new LsubResponse(session, res[i]).submit();
72              }
73          }
74          sendOK(session);
75      }
76  }