Example usage for io.vertx.core.http HttpServerResponse sendFile

List of usage examples for io.vertx.core.http HttpServerResponse sendFile

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServerResponse sendFile.

Prototype

default Future<Void> sendFile(String filename) 

Source Link

Document

Same as #sendFile(String,long) using offset @code{0} which means starting from the beginning of the file.

Usage

From source file:examples.HTTP2Examples.java

License:Open Source License

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()) {

            // The server is ready to push the response
            HttpServerResponse pushedResponse = ar.result();

            // Send main.js response
            pushedResponse.putHeader("content-type", "application/json").end("alert(\"Push response hello\")");
        } else {/*from  w w  w. jav a  2 s  . c o m*/
            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>");
}

From source file:io.nitor.api.backend.cache.CacheHandler.java

License:Apache License

private void serveCachedResponse(CacheEntry entry, RoutingContext ctx) {
    logger.debug("Returning cached content for {}", entry.uri);
    HttpServerResponse resp = ctx.response();
    resp.headers().setAll(entry.headers);
    if (ctx.request().method() != HEAD) {
        resp.sendFile(entry.file.toString());
    }//  w  w w . j  a v  a2s .com
}

From source file:vertx_react.verticles.WebApp.java

@Override
public void start(final Future<Void> fut) {

    HttpServer server = vertx.createHttpServer();
    Router router = Router.router(vertx);

    //routes /assets/js to webroot/js
    router.route("/assets/*").handler(StaticHandler.create());

    //routes/*from w w w . j av a 2 s .  c  o  m*/
    SockJSHandlerOptions options = new SockJSHandlerOptions();
    options.setHeartbeatInterval(30000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    BridgeOptions bOptions = new BridgeOptions();
    bOptions.addInboundPermitted(new PermittedOptions().setAddress("incoming"));
    bOptions.addOutboundPermitted(new PermittedOptions().setAddress("incoming"));
    sockJSHandler.bridge(bOptions);

    router.route("/eventbus/*").handler(sockJSHandler);

    //routes root to index.html and not found to custom 404 error.
    router.route("/*").handler(StaticHandler.create().setIndexPage("index.html"))
            .failureHandler(routingContext -> {
                HttpServerResponse response = routingContext.response();
                response.putHeader("content-type", "text/html");
                response.setStatusCode(404);
                response.sendFile("webroot/404.html");
                response.close();
            });

    server.requestHandler(router::accept).listen(config().getInteger("http.port", 8080), result -> {
        if (result.succeeded()) {
            fut.complete();
            log.info("Http Server started on port {0}", config().getInteger("http.port", 8080).toString());
        } else {
            fut.fail(result.cause());
        }
    });
}