@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); // Start the web server and tell it to use the router to handle requests. vertx.createHttpServer().requestHandler(router).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)); }); } }
@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).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)); }); } }
@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).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)); }); } }
@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() .addInboundPermitted(new PermittedOptions().setAddress("com.example:cmd:poke-server")) .addOutboundPermitted(new PermittedOptions().setAddress("com.example:stat:server-info")); // 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).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("com.example:stat:server-info", new JsonObject().put("systemTime", timestamp)); }); } }
@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() .addInboundPermitted(new PermittedOptions().setAddress("chat.message")) .addOutboundPermitted(new PermittedOptions().setAddress("chat.message")); // 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).listen(8080); } }
.addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));
BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("news-feed"));
@Override public void start() throws Exception { // Create the client object ProcessorService service = new ProcessorServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("vertx.processor") .register(ProcessorService.class, service); 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).listen(8080); }
addOutboundPermitted( new PermittedOptions(). setAddress("metrics")
.addOutboundPermitted(new PermittedOptions());
@Test public void testRegisterPermittedMixedAddressRe() throws Exception { String addr1 = "allow1"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr1)). addOutboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testReceive("allow1", "foobar"); testReceive("ballow1", "foobar"); testReceive("ballow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"), "access_denied"); testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedMultipleAddresses() throws Exception { String addr1 = "allow1"; String addr2 = "allow2"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr1)). addOutboundPermitted(new PermittedOptions().setAddress(addr2))); testReceive("allow1", "foobar"); testReceive("allow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "allow3").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedAllowAddress() throws Exception { String addr = "allow1"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr))); testReceive(addr, "foobar"); testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedAllowAddressRe() throws Exception { String addr = "allo.+"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddressRegex(addr))); testReceive("allow1", "foobar"); testReceive("allow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedMultipleAddressRe() throws Exception { String addr1 = "allo.+"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddressRegex(addr1)). addOutboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testReceive("allow1", "foobar"); testReceive("allow2", "foobar"); testReceive("ballow1", "foobar"); testReceive("ballow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedStructureMatch() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setMatch(match))); testReceive(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testReceive(addr, json1); JsonObject json2 = json1.copy(); json2.remove("fib"); testReceiveFail(addr, json2); }
@Test public void testRegisterPermittedStructureMatchWithAddress() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setMatch(match).setAddress(addr))); testReceive(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testReceive(addr, json1); JsonObject json2 = json1.copy(); json2.remove("fib"); testReceiveFail(addr, json2); }
@Test public void testReplyMessagesOutbound() throws Exception { // Only allow outbound address, reply message should still get through though sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr))); CountDownLatch latch = new CountDownLatch(1); client.websocket(websocketURI, ws -> { JsonObject reg = new JsonObject().put("type", "register").put("address", addr); ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(reg.encode(), true)); ws.handler(buff -> { String str = buff.toString(); JsonObject received = new JsonObject(str); Object rec = received.getValue("body"); assertEquals("foobar", rec); // Now send back reply JsonObject reply = new JsonObject().put("type", "send").put("address", received.getString("replyAddress")).put("body", "barfoo"); ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(reply.encode(), true)); }); vertx.setTimer(500, tid -> vertx.eventBus().send(addr, "foobar", res -> { if (res.succeeded()) { assertEquals("barfoo", res.result().body()); ws.closeHandler(v2 -> latch.countDown()); ws.close(); } })); }); awaitLatch(latch); }
@Override public void start() throws Exception { /* test service */ final TestService testService = new TestService(); /* address */ final String address = "testservice"; /* service builder */ final ServiceBuilder serviceBuilder = ServiceBuilder.serviceBuilder(); serviceBuilder.setServiceObject(testService); final ServiceQueue serviceQueue = serviceBuilder.build(); /* vertx event bus bridge to qbit. */ final VertxEventBusBridgeBuilder vertxEventBusBridgeBuilder = VertxEventBusBridgeBuilder .vertxEventBusBridgeBuilder() .setVertx(vertx); vertxEventBusBridgeBuilder.addBridgeAddress(address, TestService.class); final Router router = Router.router(vertx); router.route("/health/").handler(routingContext -> routingContext.response().end("\"ok\"")); /* Configure bridge at this HTTP/WebSocket URI. */ router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge( new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress(address)) .addOutboundPermitted(new PermittedOptions().setAddress(address)) )); vertxEventBusBridgeBuilder.setServiceQueue(serviceQueue); serviceQueue.startAll(); //startall not supported yet for bridge. vertxEventBusBridgeBuilder.build(); vertx.createHttpServer().requestHandler(router::accept).listen(8080); System.out.println("Bound to 8080"); }
@Test public void testRegisterPermittedAllowAddress() throws Exception { String addr = "allow1"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr))); testReceive(addr, "foobar"); testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"), "access_denied"); }