@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)); }); } }
@Test public void testInvalidClientReplyAddress() throws Exception { sockJSHandler.bridge(allAccessOptions); testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo") .put("replyAddress", "thishasmorethan36characters__________"), "invalid_reply_address"); }
private SockJSHandler configureSockJsHandler(VertxContext vertxContext, BridgeOptions bridgeOptions, JsonObject config) { SockJSHandler sockJSHandler = SockJSHandler.create(vertxContext.vertx()); sockJSHandler.bridge(bridgeOptions, event -> event.complete(true)); JsonArray jsonArray=config.getJsonArray(ALLOWED_ORIGINS); sockJSHandler.socketHandler(event -> jsonArray.forEach(o -> event.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, (String)o))); return sockJSHandler; }
private void setupSockJsServer(String serverPath, BiConsumer<SockJSSocket, Buffer> serverBufferHandler) { String path = serverPath + "/*"; router.route(path).handler(SockJSHandler.create(vertx) .socketHandler(sock -> { sock.handler(buffer -> serverBufferHandler.accept(sock, buffer)); sock.exceptionHandler(this::fail); })); }
@Override public void setUp() throws Exception { super.setUp(); sockJSHandler = SockJSHandler.create(vertx); router.route("/eventbus/*").handler(sockJSHandler); }
@Test public void testSendNotPermittedDefaultOptions() throws Exception { sockJSHandler.bridge(defaultOptions); testError(new JsonObject().put("type", "send").put("address", addr).put("body", "hello world"), "access_denied"); }
@Override public SockJSHandler configure(VertxContext vertxContext, ServerConfiguration<JsonObject, JsonObject[]> config) { BridgeOptions bridgeOptions=new BridgeOptions(); bridgeOptions.addOutboundPermitted(new PermittedOptions().setAddress(VertxBusContext.DEFAULT_SOCKJS_ADDRESS)); SockJSHandler sockJSHandler = SockJSHandler.create(vertxContext.vertx()); sockJSHandler.bridge(bridgeOptions, event -> event.complete(true)); sockJSHandler.socketHandler(event -> event.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, ALLOW_ALL)); return sockJSHandler; } }
router.route("/echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> sock.handler(sock::write))); router.route("/close/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> { router.route("/disabled_websocket_echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions() .setMaxBytesStreaming(4096).addDisabledTransport("WEBSOCKET")).socketHandler(sock -> sock.handler(sock::write))); router.route("/ticker/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> { long timerID = vertx.setPeriodic(1000, tid -> sock.write(buffer("tick!"))); sock.endHandler(v -> vertx.cancelTimer(timerID)); })); router.route("/amplify/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> sock.handler(data -> { String str = data.toString(); int n = Integer.valueOf(str); router.route("/broadcast/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(new Handler<SockJSSocket>() { Set<String> connections = new HashSet<>(); router.route("/cookie_needed_echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions(). setMaxBytesStreaming(4096).setInsertJSESSIONID(true)).socketHandler(sock -> sock.handler(sock::write)));
@Override public void setUp() throws Exception { super.setUp(); sockJSHandler = SockJSHandler.create(vertx); router.route("/eventbus/*").handler(sockJSHandler); }
@Test public void testInvalidType() throws Exception { sockJSHandler.bridge(allAccessOptions); testError(new JsonObject().put("type", "wibble").put("address", "addr"), "invalid_type"); }
@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 testCookiesRemoved() throws Exception { router.route("/cookiesremoved/*").handler(SockJSHandler.create(vertx) .socketHandler(sock -> { MultiMap headers = sock.headers(); String cookieHeader = headers.get("cookie"); assertNotNull(cookieHeader); assertEquals("JSESSIONID=wibble", cookieHeader); testComplete(); })); MultiMap headers = new CaseInsensitiveHeaders(); headers.add("cookie", "JSESSIONID=wibble"); headers.add("cookie", "flibble=floob"); client.websocket("/cookiesremoved/websocket", headers, ws -> { String frame = "foo"; ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(frame, true)); }); await(); }
@Test public void testReceiveJsonObjectAllAccess() throws Exception { sockJSHandler.bridge(allAccessOptions); testReceive(new JsonObject().put("foo", "bar").put("blah", 123)); }
@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)); }); } }
private void setupSockJsServer(String serverPath, BiConsumer<SockJSSocket, Buffer> serverBufferHandler) { String path = serverPath + "/*"; router.route(path).handler(SockJSHandler.create(vertx) .socketHandler(sock -> { sock.handler(buffer -> serverBufferHandler.accept(sock, buffer)); sock.exceptionHandler(this::fail); })); }
@Test public void testMissingAddress() throws Exception { sockJSHandler.bridge(allAccessOptions); testError(new JsonObject().put("type", "send").put("body", "hello world"), "missing_address"); }
@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)); }); } }
@Test public void testRegisterNotPermittedDefaultOptions() throws Exception { sockJSHandler.bridge(defaultOptions); testError(new JsonObject().put("type", "register").put("address", addr), "access_denied"); }
@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); } }
@Test public void testUnregisterNotPermittedDefaultOptions() throws Exception { sockJSHandler.bridge(defaultOptions); testError(new JsonObject().put("type", "unregister").put("address", addr), "access_denied"); }