@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); } }
@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 { // Create the client object MyService service = new MyServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("proxy.example") .register(MyService.class, service); Router router = Router.router(vertx); BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("proxy.example")); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options)); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Test public void testSendPermittedStructureMatch() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setMatch(match))); testSend(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testSend(addr, json1); json1.remove("fib"); testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1), "access_denied"); }
@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"); }
BridgeOptions options = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("news-feed")) .addOutboundPermitted(new PermittedOptions().setAddress("news-feed"));
/** * Configures and binds the SockJS bridge to an HttpServer. * * @param router router */ public void install(Router router) { BridgeOptions bridgeOptions = new BridgeOptions() .addOutboundPermitted(new PermittedOptions().setAddressRegex(addressPrefix + "(.*)")); if (eventbusBridgePingInterval != null) { bridgeOptions = bridgeOptions.setPingTimeout(eventbusBridgePingInterval); } router.route(sockPath).handler(SockJSHandler.create(vertx).bridge(bridgeOptions)); log.info("Installed SockJS endpoint on " + sockPath); log.info("Listening to requests on " + adressPathPattern.pattern()); log.info("Using address prefix " + addressPrefix); }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
@Test public void testMaxAddressLength() throws Exception { CountDownLatch latch = new CountDownLatch(1); sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxAddressLength(10)); client.websocket(websocketURI, ws -> { JsonObject msg = new JsonObject().put("type", "register").put("address", "someaddressyqgyuqwdyudyug"); ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true)); ws.handler(buff -> { String str = buff.toString(); JsonObject received = new JsonObject(str); assertEquals("err", received.getString("type")); assertEquals("max_address_length_reached", received.getString("body")); latch.countDown(); }); }); awaitLatch(latch); }
sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxHandlersPerSocket(maxHandlers));
BridgeOptions options = new BridgeOptions(); options.setOutboundPermitted(Collections.singletonList(new PermittedOptions().setAddress("dashboard"))); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
@Test public void testConnectionClosedAfterPingTimeout() throws Exception { sockJSHandler.bridge(allAccessOptions.setPingTimeout(1000)); CountDownLatch latch = new CountDownLatch(1); long start = System.currentTimeMillis(); client.websocket(websocketURI, ws -> ws.closeHandler(v -> latch.countDown())); awaitLatch(latch); long dur = System.currentTimeMillis() - start; assertTrue(dur > 1000 && dur < 3000); }
@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)); }); } }
@Test public void testSendPermittedStructureMatchWithAddress() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setMatch(match).setAddress(addr))); testSend(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testSend(addr, json1); json1.remove("fib"); testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1), "access_denied"); testError(new JsonObject().put("type", "send").put("address", "otheraddress").put("body", json1), "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 testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }
public static io.vertx.ext.web.handler.sockjs.SockJSHandler bridge(io.vertx.ext.web.handler.sockjs.SockJSHandler j_receiver, java.util.Map<String, Object> bridgeOptions, io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.BridgeEvent> bridgeEventHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.bridge(bridgeOptions != null ? new io.vertx.ext.web.handler.sockjs.BridgeOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(bridgeOptions)) : null, bridgeEventHandler != null ? event -> bridgeEventHandler.handle(io.vertx.core.impl.ConversionHelper.fromObject(event)) : null)); return j_receiver; } }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
@Test public void testMaxAddressLength() throws Exception { CountDownLatch latch = new CountDownLatch(1); sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxAddressLength(10)); client.websocket(websocketURI, ws -> { JsonObject msg = new JsonObject().put("type", "register").put("address", "someaddressyqgyuqwdyudyug"); ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true)); ws.handler(buff -> { String str = buff.toString(); JsonObject received = new JsonObject(str); assertEquals("err", received.getString("type")); assertEquals("max_address_length_reached", received.getString("body")); latch.countDown(); }); }); awaitLatch(latch); }
sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxHandlersPerSocket(maxHandlers));