View Javadoc

1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace.mina;
20  
21  import javax.mail.MessagingException;
22  
23  import net.sf.wallace.ClientMessage;
24  import net.sf.wallace.Command;
25  import net.sf.wallace.InvalidStateException;
26  import net.sf.wallace.ServerResponse;
27  import net.sf.wallace.SessionHandler;
28  import net.sf.wallace.WallaceSession;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.mina.common.IdleStatus;
33  import org.apache.mina.protocol.ProtocolHandlerAdapter;
34  import org.apache.mina.protocol.ProtocolSession;
35  
36  /***
37   * IMAP4 protocol handler.
38   * 
39   * @author rnewson
40   */
41  public final class IMAP4ProtocolHandler extends ProtocolHandlerAdapter {
42  
43      private static final String COPYRIGHT_NOTICE = "OK Wallace IMAP Server ready. (Copyright Robert Newson 2004)";
44  
45      // TODO inject this setting.
46      // private static final int IDLE_TIMEOUT = 30;
47  
48      private Log log = LogFactory.getLog(getClass());
49  
50      private final IMAP4ProtocolProvider provider;
51  
52      public IMAP4ProtocolHandler(final IMAP4ProtocolProvider newProvider) {
53          provider = newProvider;
54      }
55  
56      public void sessionOpened(final ProtocolSession protocolSession) {
57          if (log.isInfoEnabled()) {
58              log.info("Connection established from " + protocolSession.getRemoteAddress());
59          }
60  
61          final SessionHandler sessionHandler = new MinaSessionHandler(protocolSession);
62          final WallaceSession wallaceSession = new WallaceSession();
63  
64          wallaceSession.setSessionHandler(sessionHandler);
65          wallaceSession.setServerConfig(provider.getWallaceServerConfig());
66  
67          protocolSession.setAttachment(wallaceSession);
68          // protocolSession.getConfig().setIdleTime(IdleStatus.BOTH_IDLE,
69          // IDLE_TIMEOUT);
70          // protocolSession.getConfig().setWriteTimeout(IDLE_TIMEOUT);
71  
72          protocolSession.write(new ServerResponse(ServerResponse.Type.UNTAGGED, COPYRIGHT_NOTICE));
73      }
74  
75      public void sessionClosed(final ProtocolSession protocolSession) {
76          if (log.isInfoEnabled()) {
77              log.info(protocolSession.getRemoteAddress() + " closed.");
78          }
79      }
80  
81      public void sessionIdle(final ProtocolSession protocolSession, final IdleStatus idleStatus) {
82          assert idleStatus != null;
83          if (log.isInfoEnabled()) {
84              log.info(protocolSession.getRemoteAddress() + " is idle, closing session.");
85          }
86          protocolSession.close();
87      }
88  
89      public void exceptionCaught(final ProtocolSession protocolSession, final Throwable throwable) {
90          if (log.isErrorEnabled()) {
91              log.error(protocolSession.getRemoteAddress() + " threw uncaught exception, closing session.", throwable);
92          }
93          protocolSession.close();
94      }
95  
96      public void messageReceived(final ProtocolSession protocolSession, final Object object) {
97          if (log.isDebugEnabled()) {
98              log.debug(protocolSession.getRemoteAddress() + " Received " + object);
99          }
100 
101         assert object instanceof ClientMessage : "Only ClientMessage objects are supported.";
102         final ClientMessage clientMessage = (ClientMessage) object;
103         final Command command = clientMessage.getCommand();
104         final WallaceSession wallaceSession = (WallaceSession) protocolSession.getAttachment();
105 
106         try {
107             command.execute(wallaceSession);
108         } catch (InvalidStateException e) {
109             log.error(clientMessage + " received in invalid state.", e);
110             protocolSession.write(new ServerResponse(clientMessage, "BAD " + clientMessage));
111         } catch (MessagingException e) {
112             log.error(e.getMessage(), e);
113             protocolSession.write(new ServerResponse(clientMessage, "BAD " + clientMessage));
114         }
115     }
116 
117 }