1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.smtp;
14
15 import java.io.BufferedInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.io.PrintStream;
20 import java.text.SimpleDateFormat;
21
22 import org.abstracthorizon.danube.connection.Connection;
23 import org.abstracthorizon.danube.connection.ConnectionException;
24 import org.abstracthorizon.danube.connection.ConnectionWrapper;
25 import org.abstracthorizon.danube.support.logging.LoggingConnection;
26 import org.abstracthorizon.mercury.common.io.TempStorage;
27 import org.abstracthorizon.mercury.smtp.command.SMTPCommandFactory;
28 import org.abstracthorizon.mercury.smtp.filter.MailSessionData;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37 public class SMTPSession extends ConnectionWrapper {
38
39
40 public static final SimpleDateFormat USER_READABLE_FORMAT = new SimpleDateFormat("HH:mm:ss dd:MM:yyyy");
41
42
43 public static Logger logger = LoggerFactory.getLogger(SMTPConnectionHandler.class);
44
45
46 public static final int STATE_UNKNOWN = 0;
47
48
49 public static final int STATE_CONNECTED = 1;
50
51
52 public static final int STATE_READY = 2;
53
54
55 public static final int STATE_MAIL = 3;
56
57
58 protected TempStorage mail = new TempStorage();
59
60
61 protected SMTPScanner scanner;
62
63
64 protected int state = STATE_UNKNOWN;
65
66
67 protected MailSessionData data = new MailSessionData();
68
69
70 protected SMTPConnectionHandler parent;
71
72
73 protected InputStream inputStream;
74
75
76 protected SMTPCommandFactory factory;
77
78
79 protected long created;
80
81
82 protected long lastAccessed;
83
84
85
86
87
88
89
90 public SMTPSession(Connection connection, SMTPConnectionHandler parent) throws ConnectionException {
91 super(connection);
92 this.parent = parent;
93
94 inputStream = new BufferedInputStream((InputStream)connection.adapt(InputStream.class));
95 scanner = new SMTPScanner(inputStream);
96
97 data.setAttribute("session", this);
98 data.setAttribute("manager", parent.getStorageManager());
99
100 created = System.currentTimeMillis();
101 lastAccessed = created;
102 }
103
104
105
106
107
108
109 public void setKeepLog(boolean keepLog) {
110 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
111 if (loggingConnection != null) {
112 loggingConnection.setTemporaryLog(!keepLog);
113 }
114 }
115
116
117
118
119
120 public boolean isKeepLog() {
121 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
122 if (loggingConnection != null) {
123 return !loggingConnection.isTermporaryLog();
124 } else {
125 return false;
126 }
127 }
128
129
130
131
132
133
134 public void writeLogMessage(String msg) {
135 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
136 if (loggingConnection != null) {
137 PrintStream out = new PrintStream(loggingConnection.getDebugOutputStream());
138 out.println(msg);
139 out.flush();
140 }
141 }
142
143
144
145
146
147
148 public void setStreamDebug(boolean debug) {
149 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
150 if (loggingConnection != null) {
151 loggingConnection.setLogging(debug);
152 }
153 }
154
155
156
157
158
159 public boolean isStreamDebug() {
160 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
161 if (loggingConnection != null) {
162 return loggingConnection.isLogging();
163 } else {
164 return false;
165 }
166 }
167
168
169
170
171
172 public OutputStream getDebugStream() {
173 LoggingConnection loggingConnection = (LoggingConnection)connection.adapt(LoggingConnection.class);
174 if (loggingConnection != null) {
175 return loggingConnection.getDebugOutputStream();
176 } else {
177 return null;
178 }
179 }
180
181
182
183
184
185 public InputStream getInputStream() {
186 return inputStream;
187 }
188
189
190
191
192
193 public void setState(int state) {
194 this.state = state;
195 }
196
197
198
199
200
201 public int getState() {
202 return state;
203 }
204
205
206
207
208
209 public SMTPScanner getScanner() {
210 return scanner;
211 }
212
213
214
215
216
217 public MailSessionData getMailSessionData() {
218 return data;
219 }
220
221
222
223
224
225 public SMTPConnectionHandler getConnectionHandler() {
226 return parent;
227 }
228
229
230
231
232 public void close() {
233 super.close();
234 }
235
236
237
238
239
240
241 public void sendResponse(SMTPResponse response) throws IOException {
242 data.setReturnCode(response.getCode());
243 response.submit((OutputStream)connection.adapt(OutputStream.class));
244 }
245
246
247
248
249
250 public void setCommandFactory(SMTPCommandFactory factory) {
251 this.factory = factory;
252 }
253
254
255
256
257
258 public SMTPCommandFactory getCommandFactory() {
259 return factory;
260 }
261
262
263
264
265
266 public long getSessionCreated() {
267 return created;
268 }
269
270
271
272
273
274 public String getSessionCreatedString() {
275 return USER_READABLE_FORMAT.format(created);
276 }
277
278
279
280
281
282 public long getSessionAccessed() {
283 return lastAccessed;
284 }
285
286
287
288
289
290 public String getSessionAccessedString() {
291 return USER_READABLE_FORMAT.format(lastAccessed);
292 }
293
294
295
296
297 public void resetLastAccessed() {
298 lastAccessed = System.currentTimeMillis();
299 }
300 }