Example usage for io.vertx.core.http HttpMethod OPTIONS

List of usage examples for io.vertx.core.http HttpMethod OPTIONS

Introduction

In this page you can find the example usage for io.vertx.core.http HttpMethod OPTIONS.

Prototype

HttpMethod OPTIONS

To view the source code for io.vertx.core.http HttpMethod OPTIONS.

Click Source Link

Usage

From source file:spring.vertxtest.verticle.MyTestVerticle.java

@Override
public void start(Future<Void> fut) throws Exception {
    log.info("start() -- starting Vertx Verticle with eventbus, API handler, and static file handler");

    // grab the router
    router = getRouter();// w  w  w .  ja  va 2  s  .  c om

    // enable CORS for the router 
    CorsHandler corsHandler = CorsHandler.create("*"); //Wildcard(*) not allowed if allowCredentials is true
    corsHandler.allowedMethod(HttpMethod.OPTIONS);
    corsHandler.allowedMethod(HttpMethod.GET);
    corsHandler.allowedMethod(HttpMethod.POST);
    corsHandler.allowedMethod(HttpMethod.PUT);
    corsHandler.allowedMethod(HttpMethod.DELETE);
    corsHandler.allowCredentials(false);
    corsHandler.allowedHeader("Access-Control-Request-Method");
    corsHandler.allowedHeader("Access-Control-Allow-Method");
    corsHandler.allowedHeader("Access-Control-Allow-Credentials");
    corsHandler.allowedHeader("Access-Control-Allow-Origin");
    corsHandler.allowedHeader("Access-Control-Allow-Headers");
    corsHandler.allowedHeader("Content-Type");

    // enable handling of body
    router.route().handler(BodyHandler.create());
    router.route().handler(corsHandler);
    router.route().handler(this::handleAccessLogging);

    // publish a payload to provided eventbus destination
    router.post("/api/eventbus/publish/:destination").handler(this::publish);

    // open up all for outbound and inbound traffic
    bridgeOptions = new BridgeOptions();
    bridgeOptions.addOutboundPermitted(new PermittedOptions().setAddressRegex(".*"));
    bridgeOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(".*"));
    //        sockJsHandler = SockJSHandler.create(vertx).bridge(bridgeOptions);   
    sockJsHandler = SockJSHandler.create(vertx);
    sockJsHandler.bridge(bridgeOptions, be -> {
        try {
            if (be.type() == BridgeEventType.SOCKET_CREATED) {
                handleSocketOpenEvent(be);
            } else if (be.type() == BridgeEventType.REGISTER) {
                handleRegisterEvent(be);
            } else if (be.type() == BridgeEventType.UNREGISTER) {
                handleUnregisterEvent(be);
            } else if (be.type() == BridgeEventType.SOCKET_CLOSED) {
                handleSocketCloseEvent(be);
            }
        } catch (Exception e) {

        } finally {
            be.complete(true);
        }
    });
    router.route("/eventbus/*").handler(sockJsHandler);

    if (testPathEnabled) {
        router.route("/" + testUrlPath + "/*")
                .handler(StaticHandler.create(testFilePath).setCachingEnabled(cachingEnabled));
    }

    // create periodic task, pushing all current EventBusRegistrations
    vertx.setPeriodic(1000, handler -> {
        JsonObject obj = new JsonObject();
        obj.put("testMessage", "Periodic test message from server...");
        vertx.eventBus().publish("heartbeat-test", Json.encodePrettily(obj));
    });

    EventBus eb = vertx.eventBus();
    eb.consumer("client-test", message -> {
        log.info("Received message from client: " + Json.encodePrettily(message.body()) + " at "
                + System.currentTimeMillis());
    });

    HttpServerOptions httpOptions = new HttpServerOptions();
    if (sslEnabled) {
        httpOptions.setSsl(true);
        httpOptions.setKeyStoreOptions(sslKeyStoreOptions);
    }

    log.info("starting web server on port: " + port);
    vertx.createHttpServer(httpOptions).requestHandler(router::accept).listen(port, result -> {
        if (result.succeeded()) {
            setStarted(true);
            log.info("Server started and ready to accept requests");
            fut.complete();
        } else {
            setStarted(false);
            fut.fail(result.cause());
        }
    });
}