use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerResponseImpl method sendFile.
@Override
public HttpServerResponse sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
checkEnded();
Context resultCtx = resultHandler != null ? stream.vertx.getOrCreateContext() : null;
File file = stream.vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
log.error("File not found: " + filename);
}
return this;
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (IOException e) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
log.error("Failed to send file", e);
}
return this;
}
long contentLength = Math.min(length, file.length() - offset);
if (headers.get(HttpHeaderNames.CONTENT_LENGTH) == null) {
putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
}
}
checkSendHeaders(false);
FileStreamChannel fileChannel = new FileStreamChannel(ar -> {
if (ar.succeeded()) {
bytesWritten += ar.result();
end();
}
if (resultHandler != null) {
resultCtx.runOnContext(v -> {
resultHandler.handle(Future.succeededFuture());
});
}
}, stream, offset, contentLength);
drainHandler(fileChannel.drainHandler);
ctx.channel().eventLoop().register(fileChannel);
fileChannel.pipeline().fireUserEventTriggered(raf);
}
return this;
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class HTTP2Examples method example6.
public void example6(HttpServerRequest request) {
HttpServerResponse response = request.response();
// Push main.js to the client
response.push(HttpMethod.GET, "/main.js", ar -> {
if (ar.succeeded()) {
HttpServerResponse pushedResponse = ar.result();
pushedResponse.putHeader("content-type", "application/json").end("alert(\"Push response hello\")");
} else {
System.out.println("Could not push client resource " + ar.cause());
}
});
// Send the requested resource
response.sendFile("<html><head><script src=\"/main.js\"></script></head><body></body></html>");
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class CoreExamples method example4.
public void example4(HttpServerRequest request) {
HttpServerResponse response = request.response();
response.putHeader("Content-Type", "text/plain");
response.write("some text");
response.end();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ClientTest method testPushPromise.
@Test
public void testPushPromise() throws Exception {
waitFor(2);
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/wibble?a=b", ar -> {
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
}).end();
});
startServer();
AtomicReference<Context> ctx = new AtomicReference<>();
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
Context current = Vertx.currentContext();
if (ctx.get() == null) {
ctx.set(current);
} else {
assertEquals(ctx.get(), current);
}
resp.endHandler(v -> {
complete();
});
});
req.pushHandler(pushedReq -> {
Context current = Vertx.currentContext();
if (ctx.get() == null) {
ctx.set(current);
} else {
assertEquals(ctx.get(), current);
}
assertOnIOContext(current);
assertEquals(HttpMethod.GET, pushedReq.method());
assertEquals("/wibble?a=b", pushedReq.uri());
assertEquals("/wibble", pushedReq.path());
assertEquals("a=b", pushedReq.query());
pushedReq.handler(resp -> {
assertEquals(200, resp.statusCode());
Buffer content = Buffer.buffer();
resp.handler(content::appendBuffer);
resp.endHandler(v -> {
complete();
});
});
});
req.end();
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ClientTest method testResponseBody.
private void testResponseBody(String expected) throws Exception {
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.end(expected);
});
startServer();
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
AtomicInteger count = new AtomicInteger();
Buffer content = Buffer.buffer();
resp.handler(buff -> {
content.appendBuffer(buff);
count.incrementAndGet();
});
resp.endHandler(v -> {
assertTrue(count.get() > 0);
assertEquals(expected, content.toString());
testComplete();
});
}).exceptionHandler(err -> fail()).end();
await();
}
Aggregations