use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.
the class VertxHandler method channelInactive.
@Override
public void channelInactive(ChannelHandlerContext chctx) throws Exception {
C connection = removeConnection();
if (connection != null) {
ContextImpl context = getContext(connection);
context.executeFromIO(connection::handleClosed);
}
}
use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.
the class VertxHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext chctx, final Throwable t) throws Exception {
Channel ch = chctx.channel();
// Don't remove the connection at this point, or the handleClosed won't be called when channelInactive is called!
C connection = getConnection();
if (connection != null) {
ContextImpl context = getContext(connection);
context.executeFromIO(() -> {
try {
if (ch.isOpen()) {
ch.close();
}
} catch (Throwable ignore) {
}
connection.handleException(t);
});
} else {
ch.close();
}
}
use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.
the class HttpServerResponseImpl method doSendFile.
private void doSendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
if (headWritten) {
throw new IllegalStateException("Head already written");
}
checkWritten();
File file = vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
ContextImpl ctx = vertx.getOrCreateContext();
ctx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
log.error("File not found: " + filename);
}
return;
}
long contentLength = Math.min(length, file.length() - offset);
bytesWritten = contentLength;
if (!contentLengthSet()) {
putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (!contentTypeSet()) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaders.CONTENT_TYPE, contentType);
}
}
prepareHeaders();
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, "r");
conn.queueForWrite(response);
conn.sendFile(raf, Math.min(offset, file.length()), contentLength);
} catch (IOException e) {
try {
if (raf != null) {
raf.close();
}
} catch (IOException ignore) {
}
if (resultHandler != null) {
ContextImpl ctx = vertx.getOrCreateContext();
ctx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
log.error("Failed to send file", e);
}
return;
}
// write an empty last content to let the http encoder know the response is complete
channelFuture = conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
written = true;
if (resultHandler != null) {
ContextImpl ctx = vertx.getOrCreateContext();
channelFuture.addListener(future -> {
AsyncResult<Void> res;
if (future.isSuccess()) {
res = Future.succeededFuture();
} else {
res = Future.failedFuture(future.cause());
}
ctx.runOnContext((v) -> resultHandler.handle(res));
});
}
if (!keepAlive) {
closeConnAfterWrite();
}
conn.responseComplete();
if (bodyEndHandler != null) {
bodyEndHandler.handle(null);
}
}
}
Aggregations