Search in sources :

Example 1 with CONTENT_TYPE

use of io.vertx.core.http.HttpHeaders.CONTENT_TYPE in project vertx-openshift-it by cescoffier.

the class MainVerticle method start.

@Override
public void start() {
    vertx.createHttpServer().requestHandler(req -> {
        String path = req.path();
        HttpServerResponse response = req.response();
        switch(path) {
            case "/":
                response.end("Hello Vert.x!");
                break;
            case "/file":
                response.sendFile("/app/config/application.properties");
                break;
            case "/tmp":
                response.sendFile("/tmp/hello.txt");
                break;
            case "/headers":
                json(ok(response)).end(headersToJson(req.headers()).encode());
                break;
            case "/host":
                ok(response).putHeader(CONTENT_TYPE, "text/plain").end(System.getenv("HOSTNAME"));
                break;
            case "/form":
                response.setChunked(true);
                req.setExpectMultipart(true);
                req.endHandler((v) -> {
                    JsonObject json = new JsonObject();
                    for (String attr : req.formAttributes().names()) {
                        json.put(attr, req.formAttributes().get(attr));
                    }
                    ok(json(response)).end(json.encode());
                });
                break;
            case "/write":
                writeSomeFile(response);
                break;
        }
    }).websocketHandler(socket -> {
        if (!socket.path().equals("/ws")) {
            socket.reject();
        } else {
            socket.handler(socket::writeBinaryMessage);
        }
    }).listen(8080);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CONTENT_TYPE(io.vertx.core.http.HttpHeaders.CONTENT_TYPE) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) MultiMap(io.vertx.core.MultiMap) JsonObject(io.vertx.core.json.JsonObject) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

AbstractVerticle (io.vertx.core.AbstractVerticle)1 MultiMap (io.vertx.core.MultiMap)1 Buffer (io.vertx.core.buffer.Buffer)1 CONTENT_TYPE (io.vertx.core.http.HttpHeaders.CONTENT_TYPE)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JsonObject (io.vertx.core.json.JsonObject)1