From 5c07267f552ecfa1aa73a201fcf7b4d1d65d2d41 Mon Sep 17 00:00:00 2001 From: Vinay Date: Sat, 11 Nov 2017 23:36:56 +0000 Subject: [PATCH] Refactored BridgeEventHandler --- .../sockjs/BridgeEventHandler.properties | 5 +- ...ntHandler.java => BridgeEventHandler.java} | 51 +++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) rename src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/{DefaultBridgeEventHandler.java => BridgeEventHandler.java} (58%) diff --git a/config-layers/common/io/vertx/ext/web/handler/sockjs/BridgeEventHandler.properties b/config-layers/common/io/vertx/ext/web/handler/sockjs/BridgeEventHandler.properties index 28f9223..fa51ff2 100644 --- a/config-layers/common/io/vertx/ext/web/handler/sockjs/BridgeEventHandler.properties +++ b/config-layers/common/io/vertx/ext/web/handler/sockjs/BridgeEventHandler.properties @@ -1,2 +1,5 @@ #/io/vertx/ext/web/handler/sockjs/BridgeEventHandler -$class=io.vertx.reactivex.ext.web.handler.sockjs.DefaultBridgeEventHandler +$class=io.vertx.reactivex.ext.web.handler.sockjs.BridgeEventHandler + +addressAllowedToRegister= +addressAllowedToRegisterRegex= diff --git a/src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/DefaultBridgeEventHandler.java b/src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/BridgeEventHandler.java similarity index 58% rename from src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/DefaultBridgeEventHandler.java rename to src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/BridgeEventHandler.java index 2da1c89..76688ad 100644 --- a/src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/DefaultBridgeEventHandler.java +++ b/src/main/java/io/vertx/reactivex/ext/web/handler/sockjs/BridgeEventHandler.java @@ -3,12 +3,16 @@ import in.erail.common.FramworkConstants; import io.vertx.core.Handler; import io.vertx.core.json.JsonObject; +import java.util.List; /** * * @author vinay */ -public class DefaultBridgeEventHandler implements Handler { +public class BridgeEventHandler implements Handler { + + private List mAddressAllowedToRegister; + private List mAddressAllowedToRegisterRegex; @Override public void handle(BridgeEvent pEvent) { @@ -52,15 +56,52 @@ public void handle(BridgeEvent pEvent) { } - public void handlePublish(String pAddress, BridgeEvent pEvent) { - pEvent.complete(true); + public void handleRegister(String pAddress, BridgeEvent pEvent) { + if (mAddressAllowedToRegister.isEmpty() && mAddressAllowedToRegisterRegex.isEmpty()) { + pEvent.complete(true); + return; + } + + if (!(matchAddress(pAddress) || matchAddressRegex(pAddress))) { + pEvent.fail("Can't subscribe to topic : " + pAddress); + } else { + pEvent.complete(true); + } } - public void handleRecieve(String pAddress, BridgeEvent pEvent) { + private boolean matchAddress(String pAddress) { + return mAddressAllowedToRegister + .stream() + .anyMatch((allowedAddress) -> (pAddress.equals(allowedAddress))); + } + + private boolean matchAddressRegex(String pAddress) { + return mAddressAllowedToRegisterRegex + .stream() + .anyMatch((allowedAddress) -> (pAddress.matches(allowedAddress))); + } + + public List getAddressAllowedToRegister() { + return mAddressAllowedToRegister; + } + + public void setAddressAllowedToRegister(List pAddressAllowedToRegister) { + this.mAddressAllowedToRegister = pAddressAllowedToRegister; + } + + public List getAddressAllowedToRegisterRegex() { + return mAddressAllowedToRegisterRegex; + } + + public void setAddressAllowedToRegisterRegex(List pAddressAllowedToRegisterRegex) { + this.mAddressAllowedToRegisterRegex = pAddressAllowedToRegisterRegex; + } + + public void handlePublish(String pAddress, BridgeEvent pEvent) { pEvent.complete(true); } - public void handleRegister(String pAddress, BridgeEvent pEvent) { + public void handleRecieve(String pAddress, BridgeEvent pEvent) { pEvent.complete(true); }