Search in sources :

Example 6 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow outbound traffic to the news-feed address
    BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("news-feed"));
    router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options, event -> {
        if (event.type() == BridgeEventType.SOCKET_CREATED) {
            System.out.println("A socket was created");
        }
        // This signals that it's ok to process the event
        event.complete(true);
    }));
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    // Publish a message to the address "news-feed" every second
    vertx.setPeriodic(1000, t -> vertx.eventBus().publish("news-feed", "news from the server!"));
}
Also used : PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler) StaticHandler(io.vertx.ext.web.handler.StaticHandler) AbstractVerticle(io.vertx.core.AbstractVerticle) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) Router(io.vertx.ext.web.Router) Runner(io.vertx.example.util.Runner) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) Router(io.vertx.ext.web.Router) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions)

Example 7 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    // the load function just populates some data on the storage
    loadData(mongo);
    // the app works 100% realtime
    vertx.eventBus().consumer("vtoons.listAlbums", this::listAlbums);
    vertx.eventBus().consumer("vtoons.placeOrder", this::placeOrder);
    Router router = Router.router(vertx);
    // We need cookies and sessions
    router.route().handler(CookieHandler.create());
    router.route().handler(BodyHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
    // Simple auth service which uses a properties file for user/role info
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, new JsonObject());
    // We need a user session handler too to make sure the user is stored in the session between requests
    router.route().handler(UserSessionHandler.create(authProvider));
    router.post("/login").handler(ctx -> {
        JsonObject credentials = ctx.getBodyAsJson();
        if (credentials == null) {
            // bad request
            ctx.fail(400);
            return;
        }
        // use the auth handler to perform the authentication for us
        authProvider.authenticate(credentials, login -> {
            // error handling
            if (login.failed()) {
                // forbidden
                ctx.fail(403);
                return;
            }
            ctx.setUser(login.result());
            ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json").end("{}");
        });
    });
    router.route("/eventbus/*").handler(ctx -> {
        // we need to be logged in
        if (ctx.user() == null) {
            ctx.fail(403);
        } else {
            ctx.next();
        }
    });
    // Allow outbound traffic to the vtoons addresses
    BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("vtoons.listAlbums")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.login")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.placeOrder").setRequiredAuthority("place_order")).addOutboundPermitted(new PermittedOptions());
    router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) AuthProvider(io.vertx.ext.auth.AuthProvider) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions)

Example 8 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class ProcessorServiceVerticle method start.

@Override
public void start() throws Exception {
    // Create the client object
    service = new ProcessorServiceImpl();
    // Register the handler
    ProxyHelper.registerService(ProcessorService.class, vertx, service, "vertx.processor");
    // 
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("vertx.processor")).addOutboundPermitted(new PermittedOptions().setAddress("vertx.processor"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // 
    router.route().handler(StaticHandler.create());
    // 
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : ProcessorServiceImpl(io.vertx.examples.service.impl.ProcessorServiceImpl) Router(io.vertx.ext.web.Router) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 9 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("feed"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // Create a router endpoint for the static content.
    router.route().handler(StaticHandler.create());
    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    EventBus eb = vertx.eventBus();
    vertx.setPeriodic(1000l, t -> {
        // Create a timestamp string
        String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
        eb.send("feed", new JsonObject().put("now", timestamp));
    });
}
Also used : Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) EventBus(io.vertx.core.eventbus.EventBus) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 10 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("feed"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // Create a router endpoint for the static content.
    router.route().handler(StaticHandler.create());
    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    EventBus eb = vertx.eventBus();
    vertx.setPeriodic(1000l, t -> {
        // Create a timestamp string
        String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
        eb.send("feed", new JsonObject().put("now", timestamp));
    });
}
Also used : Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) EventBus(io.vertx.core.eventbus.EventBus) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Aggregations

BridgeOptions (io.vertx.ext.web.handler.sockjs.BridgeOptions)12 PermittedOptions (io.vertx.ext.web.handler.sockjs.PermittedOptions)12 Router (io.vertx.ext.web.Router)11 SockJSHandler (io.vertx.ext.web.handler.sockjs.SockJSHandler)8 JsonObject (io.vertx.core.json.JsonObject)7 EventBus (io.vertx.core.eventbus.EventBus)5 HttpServer (io.vertx.core.http.HttpServer)2 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Runner (io.vertx.example.util.Runner)1 ProcessorServiceImpl (io.vertx.examples.service.impl.ProcessorServiceImpl)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)1 MetricsService (io.vertx.ext.dropwizard.MetricsService)1 StaticHandler (io.vertx.ext.web.handler.StaticHandler)1 SockJSHandler (io.vertx.rxjava.ext.web.handler.sockjs.SockJSHandler)1 Random (java.util.Random)1