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.smtp;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.io.PrintStream;
18  import java.net.Socket;
19  
20  import org.abstracthorizon.danube.connection.Connection;
21  import org.abstracthorizon.danube.connection.ConnectionHandler;
22  import org.abstracthorizon.danube.service.server.ServerConnectionHandler;
23  import org.abstracthorizon.danube.support.RuntimeIOException;
24  import org.abstracthorizon.mercury.common.StorageManager;
25  import org.abstracthorizon.mercury.smtp.filter.MailSessionData;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * A class that handles connection as SMTP connections. It creates {@link SMTPSession} wrapper
31   * over session..
32   *
33   * @author Daniel Sendula
34   */
35  public class SMTPConnectionHandler extends ServerConnectionHandler {
36  
37      /** Logger */
38      protected static final Logger logger = LoggerFactory.getLogger(SMTPConnectionHandler.class);
39  
40      /** Starting response */
41      public static final SMTPResponse READY_RESPONSE = new SMTPResponse(220, "Service ready");
42  
43      /** Cached value */
44      protected StorageManager manager;
45  
46      /** Cached value */
47      protected ConnectionHandler connectionHandler;
48  
49      /**
50       * Constructor
51       */
52      public SMTPConnectionHandler() {
53      }
54  
55      /**
56       * Sets storage manager
57       * @return storage manager
58       */
59      public StorageManager getStorageManager() {
60          return manager;
61      }
62  
63      /**
64       * Sets storage manager
65       * @param manager storage manager
66       */
67      public void setStorageManager(StorageManager manager) {
68          this.manager = manager;
69      }
70  
71      /**
72       * This method creates {@link SMTPSession}, sends initial response and sets
73       * state of session to {@link SMTPSession#STATE_CONNECTED}
74       *
75       * @param connection connection
76       */
77      protected Connection decorateConnection(Connection connection) {
78          SMTPSession smtpConnection = new SMTPSession(connection, this);
79          SMTPResponse ready = new SMTPResponse(220, getStorageManager().getMainDomain() + " Ready");
80          try {
81              smtpConnection.sendResponse(ready);
82          } catch (IOException e) {
83              smtpConnection.setKeepLog(true);
84              OutputStream debugStream = smtpConnection.getDebugStream();
85              if (debugStream != null) {
86                  PrintStream ps = new PrintStream(debugStream);
87                  ps.println("Unexpected IO problem");
88                  e.printStackTrace(ps);
89              }
90              try {
91                  smtpConnection.sendResponse(SMTPResponses.SHUTTING_DOWN_RESPONSE);
92              } catch (IOException ignore) {
93              }
94              smtpConnection.close();
95              throw new RuntimeIOException(e);
96          }
97          smtpConnection.setState(SMTPSession.STATE_CONNECTED);
98          return smtpConnection;
99      }
100 
101     /**
102      * Resets smtp session
103      * @param connection connection
104      * @return persistConnection unchanged
105      */
106     protected boolean postProcessing(Connection connection) {
107         boolean persistConnection = super.postProcessing(connection);
108         SMTPSession smtpConnection = (SMTPSession)connection.adapt(SMTPSession.class);
109         MailSessionData data = smtpConnection.getMailSessionData();
110         boolean dropConnection = "true".equals(data.getAttribute("dropconnection"));
111         persistConnection = !dropConnection & persistConnection;
112         data.setTotalBytes(0);
113         return persistConnection;
114     }
115 
116     /**
117      * Sends {@link SMTPResponses#SHUTTING_DOWN_RESPONSE} if possible
118      * and closes the session
119      * @param connection connection
120      */
121     protected void finishConnection(Connection connection) {
122         SMTPSession smtpConnection = (SMTPSession)connection.adapt(SMTPSession.class);
123         try {
124             Socket socket = (Socket)smtpConnection.adapt(Socket.class);
125             if ((socket == null) || (!socket.isClosed() && !socket.isOutputShutdown())) {
126                 smtpConnection.sendResponse(SMTPResponses.SHUTTING_DOWN_RESPONSE);
127             }
128         } catch (IOException ignore) {
129         }
130         smtpConnection.close();
131     }
132 
133 }