1
2
3
4
5
6
7
8
9
10
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
31
32
33
34
35 public class SMTPConnectionHandler extends ServerConnectionHandler {
36
37
38 protected static final Logger logger = LoggerFactory.getLogger(SMTPConnectionHandler.class);
39
40
41 public static final SMTPResponse READY_RESPONSE = new SMTPResponse(220, "Service ready");
42
43
44 protected StorageManager manager;
45
46
47 protected ConnectionHandler connectionHandler;
48
49
50
51
52 public SMTPConnectionHandler() {
53 }
54
55
56
57
58
59 public StorageManager getStorageManager() {
60 return manager;
61 }
62
63
64
65
66
67 public void setStorageManager(StorageManager manager) {
68 this.manager = manager;
69 }
70
71
72
73
74
75
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
103
104
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
118
119
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 }