-
Notifications
You must be signed in to change notification settings - Fork 33
/
SMTPConnection.java
395 lines (355 loc) · 10.5 KB
/
SMTPConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2011-2015 The original author or authors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.mail.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.NoStackTraceThrowable;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.impl.pool.Lease;
import io.vertx.ext.mail.MailConfig;
import java.io.IOException;
import java.util.List;
/**
* SMTP connection to a server.
* <p>
* Encapsulate the NetSocket connection and the data writing/reading
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
class SMTPConnection {
private static final Logger log = LoggerFactory.getLogger(SMTPConnection.class);
private final NetSocket ns;
private final MailConfig config;
private Lease<SMTPConnection> lease;
private MultilineParser nsHandler;
private final Handler<Void> evictionHandler;
private boolean evicted;
private boolean socketClosed;
private boolean shutdown;
private boolean closing;
private boolean inuse;
private boolean quitSent;
private Handler<String> commandReplyHandler;
private Handler<Throwable> errorHandler;
private Handler<AsyncResult<Void>> closeHandler;
private Capabilities capa = new Capabilities();
private final ContextInternal context;
private long expirationTimestamp;
SMTPConnection(MailConfig config, NetSocket ns, ContextInternal context, Handler<Void> evictionHandler) {
this.config = config;
this.ns = ns;
this.context = context;
this.evictionHandler = evictionHandler;
}
/**
* Compute the expiration timeout of the connection, relative to the current time.
*
* @param config the MailConfig
* @return the expiration timestamp
*/
private static long expirationTimestampOf(MailConfig config) {
long timeout = config.getKeepAliveTimeout();
return timeout == 0 ? 0L : System.currentTimeMillis() + config.getKeepAliveTimeoutUnit().toMillis(timeout);
}
SMTPConnection setLease(Lease<SMTPConnection> lease) {
this.lease = lease;
return this;
}
boolean isInitialized() {
return this.nsHandler != null;
}
void init(Handler<String> initialReplyHandler) {
if (nsHandler != null) {
throw new IllegalStateException("SMTPConnection has been initialized.");
}
this.nsHandler = new MultilineParser(buffer -> {
if (commandReplyHandler == null && !quitSent) {
handleError(new IllegalStateException("dropping reply arriving after we stopped processing the buffer."));
} else {
// make sure we only call the handler once
Handler<String> currentHandler = commandReplyHandler;
commandReplyHandler = null;
if (currentHandler != null) {
context.emit(buffer.toString(), currentHandler);
}
}
});
ns.exceptionHandler(this::handleNSException);
ns.closeHandler(this::handleNSClosed);
commandReplyHandler = initialReplyHandler;
this.expirationTimestamp = expirationTimestampOf(config);
ns.handler(this.nsHandler);
}
void handleNSException(Throwable t) {
if (!socketClosed && !shutdown) {
shutdown();
log.debug("got an exception on the netsocket", t);
handleError(t);
} else {
log.debug("not returning follow-up exception", t);
}
}
boolean isAvailable() {
return !socketClosed && !shutdown;
}
boolean isValid() {
return (expirationTimestamp == 0 || System.currentTimeMillis() <= expirationTimestamp) && !quitSent;
}
void handleNSClosed(Void v) {
log.trace("handleNSClosed() - socket has been closed");
socketClosed = true;
if (!shutdown && !quitSent) {
handleError(new IOException("socket was closed unexpected."));
shutdown();
}
handleClosed();
}
private void handleClosed() {
if (closeHandler != null) {
closeHandler.handle(Future.succeededFuture());
closeHandler = null;
}
if (!evicted) {
evicted = true;
evictionHandler.handle(null);
cleanHandlers();
}
}
/**
* @return the capabilities object
*/
Capabilities getCapa() {
return capa;
}
/**
* parse capabilities from the ehlo reply string
*
* @param message the capabilities to set
*/
void parseCapabilities(String message) {
capa = new Capabilities();
capa.parseCapabilities(message);
if (log.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("Supported Auth methods: ");
capa.getCapaAuth().forEach(a -> sb.append(a).append(" "));
sb.append("\n");
if (capa.getSize() > 0) {
sb.append("Max Size: ").append(capa.getSize()).append("\n");
}
sb.append("Support STARTTLS: ").append(capa.isStartTLS()).append(", Current connection TLS: ").append(this.isSsl()).append("\n");
sb.append("Support PIPELINING: ").append(capa.isCapaPipelining()).append("\n");
log.debug(sb);
}
}
void shutdown() {
shutdown = true;
if (!socketClosed) {
socketClosed = true;
ns.close();
}
handleClosed();
}
private void cleanHandlers() {
errorHandler = null;
commandReplyHandler = null;
}
void writeCommands(List<String> commands, Handler<String> resultHandler) {
String cmds = String.join("\r\n", commands);
this.nsHandler.setExpected(commands.size());
this.write(cmds, r -> {
try {
resultHandler.handle(r);
} finally {
this.nsHandler.setExpected(1);
}
});
}
/*
* write command without masking anything
*/
void write(String str, Handler<String> commandResultHandler) {
write(str, -1, commandResultHandler);
}
/*
* write command masking everything after position blank
*/
void write(String str, int blank, Handler<String> commandResultHandler) {
this.commandReplyHandler = commandResultHandler;
checkClosed();
context.emit(roc -> {
if (log.isDebugEnabled()) {
String logStr;
if (blank >= 0) {
StringBuilder sb = new StringBuilder();
for (int i = blank; i < str.length(); i++) {
sb.append('*');
}
logStr = str.substring(0, blank) + sb;
} else {
logStr = str;
}
// avoid logging large mail body
if (logStr.length() < 1000) {
log.debug("command: " + logStr);
} else {
log.debug("command: " + logStr.substring(0, 1000) + "...");
}
}
ns.write(str + "\r\n", r -> {
if (r.failed()) {
handleNSException(r.cause());
}
});
});
}
// write single line not expecting a reply, using drain handler
void writeLineWithDrainPromise(String str, boolean mayLog, Promise<Void> promise) {
if (checkClosed()) {
promise.fail("Connection was closed");
return;
}
if (mayLog) {
log.debug(str);
}
context.emit(roc -> {
if (ns.writeQueueFull()) {
ns.drainHandler(v -> {
// avoid getting confused by being called twice
ns.drainHandler(null);
ns.write(str + "\r\n").onComplete(promise);
});
} else {
ns.write(str + "\r\n").onComplete(promise);
}
});
}
private void handleError(Throwable t) {
context.emit(t, err -> {
Handler<Throwable> handler;
synchronized (SMTPConnection.this) {
handler = errorHandler;
}
if (handler != null) {
handler.handle(err);
} else {
if (log.isDebugEnabled()) {
log.error(t.getMessage(), t);
} else {
log.error(t.getMessage());
}
}
});
}
boolean isSsl() {
return ns.isSsl();
}
void upgradeToSsl(Handler<AsyncResult<Void>> handler) {
ns.upgradeToSsl(handler);
}
Future<SMTPConnection> returnToPool() {
log.trace("return to pool");
Promise<SMTPConnection> promise = context.promise();
try {
if (config.isKeepAlive() && !closing) {
// recycle
log.trace("recycle for next use");
cleanHandlers();
lease.recycle();
inuse = false;
expirationTimestamp = expirationTimestampOf(config);
promise.complete(this);
} else {
Promise<Void> p = Promise.promise();
p.future().onComplete(conn -> {
handleClosed();
promise.complete(this);
});
quitCloseConnection(p);
}
} catch (Exception e) {
promise.fail(e);
}
return promise.future();
}
/**
* send QUIT and close the connection, this operation waits for the success of the quit command but will close the
* connection on exception as well
*/
void quitCloseConnection(Promise<Void> promise) {
quitSent = true;
inuse = false;
log.trace("send QUIT to close");
writeLineWithDrainPromise("QUIT", false, promise);
}
private boolean checkClosed() {
if (isClosed() || shutdown) {
handleError(new NoStackTraceThrowable("Connection was closed."));
return true;
}
return false;
}
void setErrorHandler(Handler<Throwable> newHandler) {
errorHandler = newHandler;
}
void setInUse() {
inuse = true;
expirationTimestamp = expirationTimestampOf(config);
}
/**
* close the connection doing a QUIT command first
*/
void close(Promise<Void> promise) {
closing = true;
if (!inuse) {
log.trace("close by sending quit in close()");
quitCloseConnection(promise);
} else {
this.closeHandler = promise;
if (quitSent) {
shutdown();
}
}
}
/**
* check if a connection is already closed (this is mostly for unit tests)
*/
boolean isClosed() {
return socketClosed;
}
/**
* get the context associated with this connection
*
* @return the context
*/
Context getContext() {
return context;
}
/**
* Gets the underline NetSocket to the email server.
*
* @return the underline NetSocket
*/
NetSocket getSocket() {
return ns;
}
}