@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); }
private Match checkMatches(boolean inbound, String address, Object body) { List<PermittedOptions> matches = inbound ? inboundPermitted : outboundPermitted; for (PermittedOptions matchHolder : matches) { String matchAddress = matchHolder.getAddress(); String matchRegex; if (matchAddress == null) { matchRegex = matchHolder.getAddressRegex(); } else { matchRegex = null; } boolean addressOK; if (matchAddress == null) { addressOK = matchRegex == null || regexMatches(matchRegex, address); } else { addressOK = matchAddress.equals(address); } if (addressOK) { boolean matched = structureMatches(matchHolder.getMatch(), body); if (matched) { String requiredAuthority = matchHolder.getRequiredAuthority(); return new Match(true, requiredAuthority); } } } return new Match(false); }
.setRequiredAuthority("place_order"))
@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 testSendPermittedMultipleAddressRe() throws Exception { String addr1 = "allo.+"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(addr1)). addInboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testSend("allow1", "foobar"); testSend("allow2", "foobar"); testSend("ballow1", "foobar"); testSend("ballow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testSendRequiresAuthorityNotLoggedIn() throws Exception { sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("admin"))); testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "not_logged_in"); }
@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 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"); }
@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 testPermittedOptionsJson() { String address = TestUtils.randomAlphaString(10); String addressRegex = TestUtils.randomAlphaString(10); String requiredAuthority = TestUtils.randomAlphaString(10); JsonObject match = new JsonObject().put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(10)); JsonObject json = new JsonObject(). put("address", address). put("addressRegex", addressRegex). put("requiredAuthority", requiredAuthority). put("match", match); PermittedOptions options = new PermittedOptions(json); assertEquals(address, options.getAddress()); assertEquals(addressRegex, options.getAddressRegex()); assertEquals(requiredAuthority, options.getRequiredAuthority()); assertEquals(match, options.getMatch()); }
@Test public void testSendPermittedMixedAddressRe() throws Exception { String addr1 = "allow1"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr1)). addInboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testSend("allow1", "foobar"); testSend("ballow1", "foobar"); testSend("ballow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"), "access_denied"); testError(new JsonObject().put("type", "send").put("address", "allow2").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 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 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 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 testSendPermittedAllowAddressRe() throws Exception { String addr = "allo.+"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(addr))); testSend("allow1", "foobar"); testSend("allow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testSendPermittedMultipleAddresses() throws Exception { String addr1 = "allow1"; String addr2 = "allow2"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr1)). addInboundPermitted(new PermittedOptions().setAddress(addr2))); testSend("allow1", "foobar"); testSend("allow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "allow3").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 testSendPermittedAllowAddress() throws Exception { String addr = "allow1"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr))); testSend(addr, "foobar"); testError(new JsonObject().put("type", "send").put("address", "allow2").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"); }