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 org.abstracthorizon.danube.connection.Connection;
16  import org.abstracthorizon.danube.connection.ConnectionHandler;
17  import org.abstracthorizon.danube.support.RuntimeIOException;
18  import org.abstracthorizon.mercury.common.command.CommandException;
19  import org.abstracthorizon.mercury.imap.BADCommandException;
20  import org.abstracthorizon.mercury.imap.IMAPSession;
21  import org.abstracthorizon.mercury.imap.NOCommandException;
22  import org.abstracthorizon.mercury.imap.response.ExistsResponse;
23  import org.abstracthorizon.mercury.imap.response.NOResponse;
24  import org.abstracthorizon.mercury.imap.response.OKResponse;
25  import org.abstracthorizon.mercury.imap.response.RecentResponse;
26  import org.abstracthorizon.mercury.imap.util.ParserException;
27  
28  import java.io.IOException;
29  
30  import javax.mail.Folder;
31  import javax.mail.MessagingException;
32  
33  /**
34   * A class that represents IMAP command
35   *
36   * @author Daniel Sendula
37   */
38  public class IMAPCommand implements ConnectionHandler {
39  
40      public static final short SEND_WHEN_HAVE_NEW = 0;
41      public static final short ALWAYS_SEND_UNILATERAL_DATA = 1;
42      public static final short ALWAYS_SUPRESS_UNILATERAL_DATA = 2;
43  
44      protected short unilateral = SEND_WHEN_HAVE_NEW;
45  
46      /** Mnemonic */
47      protected String mnemonic;
48  
49      /**
50       * Constructor
51       * @param mnemonic mnemonic
52       */
53      public IMAPCommand(String mnemonic) {
54          this.mnemonic = mnemonic;
55      }
56  
57      /**
58       * Returns mnemonic
59       * @return mnemonic
60       */
61      public String getMnemonic() {
62          return mnemonic;
63      }
64  
65      /**
66       * Handles connection
67       * @param connection connection
68       */
69      public void handleConnection(Connection connection) {
70          IMAPSession session = (IMAPSession)connection;
71          try {
72              session.markCommandStarted();
73              execute(session);
74          } catch (IOException e) {
75              throw new RuntimeIOException(e);
76          } catch (ParserException e) {
77              throw new BADCommandException(e);
78          } catch (MessagingException e) {
79              throw new NOCommandException(e);
80          }
81      }
82  
83      /**
84       * Executes the command. This implemnetation returns {@link NOResponse}.
85       * @param session session
86       * @throws ParserException
87       * @throws MessagingException
88       * @throws CommandException
89       * @throws IOException
90       */
91      protected void execute(IMAPSession session) throws ParserException, MessagingException, CommandException, IOException {
92          checkEOL(session);
93          new NOResponse(session, getMnemonic()+" unimplemented").submit();
94      }
95  
96      /**
97       * Sets {@link OKResponse}
98       * @param session session
99       * @throws IOException
100      */
101     protected void sendOK(IMAPSession session) throws IOException {
102         new OKResponse(session, mnemonic+" Completed in "+session.getCommandLasted()+"ms").submit();
103     }
104 
105     /**
106      * Checks if command is read fully
107      * @param session session
108      * @throws IOException
109      * @throws ParserException
110      */
111     protected void checkEOL(IMAPSession session) throws IOException, ParserException {
112         session.getScanner().check_eol();
113         if (unilateral != ALWAYS_SUPRESS_UNILATERAL_DATA) {
114             try {
115                 Folder folder = session.getSelectedFolder();
116                 if (folder != null) {
117                     if ((unilateral == ALWAYS_SEND_UNILATERAL_DATA) || (folder.getNewMessageCount() > 0)) {
118                         new ExistsResponse(session, folder).submit();
119                         new RecentResponse(session, folder).submit();
120                     }
121                 }
122             } catch (MessagingException ignore) {
123                 // We can't do much or even want to fix or report this...
124             }
125         }
126     }
127 }