@Override public SockJSHandler bridge(BridgeOptions bridgeOptions, Handler<BridgeEvent> bridgeEventHandler) { socketHandler(new EventBusBridgeImpl(vertx, bridgeOptions, bridgeEventHandler)); return this; }
private void internalHandleRegister(SockJSSocket sock, JsonObject rawMsg, Map<String, MessageConsumer> registrations) { final SockInfo info = sockInfos.get(sock); if (!checkMaxHandlers(sock, info)) { return; checkCallHook(() -> new BridgeEventImpl(BridgeEventType.REGISTER, rawMsg, sock), () -> { final boolean debug = log.isDebugEnabled(); final String address = rawMsg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } else if (address.length() > maxAddressLength) { log.warn("Refusing to register as address length > max_address_length"); replyError(sock, "max_address_length_reached"); return; Match match = checkMatches(false, address, null); if (match.doesMatch) { Handler<Message<Object>> handler = msg -> { Match curMatch = checkMatches(false, address, msg.body()); if (curMatch.doesMatch) { if (curMatch.requiredAuthority != null) { authorise(curMatch, sock.webUser(), res -> { if (res.succeeded()) { if (res.result()) { checkAddAccceptedReplyAddress(msg); deliverMessage(sock, address, msg); } else { if (debug) {
replyError(sock, "invalid_reply_address"); return; curMatch = new Match(true); } else { curMatch = checkMatches(true, address, body); User webUser = sock.webUser(); if (webUser != null) { authorise(curMatch, webUser, res -> { if (res.succeeded()) { if (res.result()) { checkAndSend(send, address, body, headers, sock, replyAddress, null); } else { replyError(sock, "access_denied"); if (debug) { log.debug("Inbound message for address " + address + " rejected because is not authorised"); replyError(sock, "auth_error"); log.error("Error in performing authorisation", res.cause()); replyError(sock, "not_logged_in"); if (debug) { log.debug("Inbound message for address " + address + checkAndSend(send, address, body, headers, sock, replyAddress, awaitingReply); replyError(sock, "access_denied"); if (debug) {
private void internalHandleSendOrPub(SockJSSocket sock, boolean send, JsonObject msg) { checkCallHook(() -> new BridgeEventImpl(send ? BridgeEventType.SEND : BridgeEventType.PUBLISH, msg, sock), () -> { String address = msg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } doSendOrPub(send, sock, address, msg); }, () -> replyError(sock, "rejected")); }
private void internalHandleUnregister(SockJSSocket sock, JsonObject rawMsg, Map<String, MessageConsumer> registrations) { checkCallHook(() -> new BridgeEventImpl(BridgeEventType.UNREGISTER, rawMsg, sock), () -> { String address = rawMsg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } Match match = checkMatches(false, address, null); if (match.doesMatch) { MessageConsumer reg = registrations.remove(address); if (reg != null) { reg.unregister(); SockInfo info = sockInfos.get(sock); info.handlerCount--; } } else { if (log.isDebugEnabled()) { log.debug("Cannot unregister handler for address " + address + " because there is no inbound match"); } replyError(sock, "access_denied"); } }, () -> replyError(sock, "rejected")); }
public void handle(final SockJSSocket sock) { checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_CREATED, null, sock), () -> { Map<String, MessageConsumer> registrations = new HashMap<>(); sock.endHandler(v -> handleSocketClosed(sock, registrations)); sock.handler(data -> handleSocketData(sock, data, registrations)); // Start a checker to check for pings PingInfo pingInfo = new PingInfo(); pingInfo.timerID = vertx.setPeriodic(pingTimeout, id -> { if (System.currentTimeMillis() - pingInfo.lastPing >= pingTimeout) { // Trigger an event to allow custom behavior before disconnecting client. checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_IDLE, null, sock), // We didn't receive a ping in time so close the socket ((SockJSSocketBase) sock)::closeAfterSessionExpired, () -> replyError(sock, "rejected")); } }); SockInfo sockInfo = new SockInfo(); sockInfo.pingInfo = pingInfo; sockInfos.put(sock, sockInfo); }, sock::close); }
Message awaitingReply) { SockInfo info = sockInfos.get(sock); if (replyAddress != null && !checkMaxHandlers(sock, info)) { return; checkAddAccceptedReplyAddress(message); deliverMessage(sock, replyAddress, message); } else { ReplyException cause = (ReplyException) result.cause();
private void handleSocketClosed(SockJSSocket sock, Map<String, MessageConsumer> registrations) { // On close unregister any handlers that haven't been unregistered registrations.forEach((key, value) -> { value.unregister(); checkCallHook(() -> new BridgeEventImpl(BridgeEventType.UNREGISTER, new JsonObject().put("type", "unregister").put("address", value.address()), sock), null, null); }); SockInfo info = sockInfos.remove(sock); if (info != null) { PingInfo pingInfo = info.pingInfo; if (pingInfo != null) { vertx.cancelTimer(pingInfo.timerID); } } checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_CLOSED, null, sock), null, null); }
public void handle(final SockJSSocket sock) { checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_CREATED, null, sock), () -> { Map<String, MessageConsumer> registrations = new HashMap<>(); sock.endHandler(v -> handleSocketClosed(sock, registrations)); sock.handler(data -> handleSocketData(sock, data, registrations)); // Start a checker to check for pings PingInfo pingInfo = new PingInfo(); pingInfo.timerID = vertx.setPeriodic(pingTimeout, id -> { if (System.currentTimeMillis() - pingInfo.lastPing >= pingTimeout) { // Trigger an event to allow custom behavior before disconnecting client. checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_IDLE, null, sock), // We didn't receive a ping in time so close the socket ((SockJSSocketBase) sock)::closeAfterSessionExpired, () -> replyError(sock, "rejected")); } }); SockInfo sockInfo = new SockInfo(); sockInfo.pingInfo = pingInfo; sockInfos.put(sock, sockInfo); }, sock::close); }
private void internalHandleSendOrPub(SockJSSocket sock, boolean send, JsonObject msg) { checkCallHook(() -> new BridgeEventImpl(send ? BridgeEventType.SEND : BridgeEventType.PUBLISH, msg, sock), () -> { String address = msg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } doSendOrPub(send, sock, address, msg); }, () -> replyError(sock, "rejected")); }
private void internalHandleUnregister(SockJSSocket sock, JsonObject rawMsg, Map<String, MessageConsumer> registrations) { checkCallHook(() -> new BridgeEventImpl(BridgeEventType.UNREGISTER, rawMsg, sock), () -> { String address = rawMsg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } Match match = checkMatches(false, address, null); if (match.doesMatch) { MessageConsumer reg = registrations.remove(address); if (reg != null) { reg.unregister(); SockInfo info = sockInfos.get(sock); info.handlerCount--; } } else { if (log.isDebugEnabled()) { log.debug("Cannot unregister handler for address " + address + " because there is no inbound match"); } replyError(sock, "access_denied"); } }, () -> replyError(sock, "rejected")); }
Message awaitingReply) { SockInfo info = sockInfos.get(sock); if (replyAddress != null && !checkMaxHandlers(sock, info)) { return; checkAddAccceptedReplyAddress(message); deliverMessage(sock, replyAddress, message); } else { ReplyException cause = (ReplyException) result.cause();
private void internalHandlePing(final SockJSSocket sock) { Session webSession = sock.webSession(); if (webSession != null) { webSession.setAccessed(); } SockInfo info = sockInfos.get(sock); if (info != null) { info.pingInfo.lastPing = System.currentTimeMillis(); // Trigger an event to allow custom behavior after updating lastPing checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_PING, null, sock), null, null); } }
private void internalHandleRegister(SockJSSocket sock, JsonObject rawMsg, Map<String, MessageConsumer> registrations) { final SockInfo info = sockInfos.get(sock); if (!checkMaxHandlers(sock, info)) { return; checkCallHook(() -> new BridgeEventImpl(BridgeEventType.REGISTER, rawMsg, sock), () -> { final boolean debug = log.isDebugEnabled(); final String address = rawMsg.getString("address"); if (address == null) { replyError(sock, "missing_address"); return; } else if (address.length() > maxAddressLength) { log.warn("Refusing to register as address length > max_address_length"); replyError(sock, "max_address_length_reached"); return; Match match = checkMatches(false, address, null); if (match.doesMatch) { Handler<Message<Object>> handler = msg -> { Match curMatch = checkMatches(false, address, msg.body()); if (curMatch.doesMatch) { if (curMatch.requiredAuthority != null) { authorise(curMatch, sock.webUser(), res -> { if (res.succeeded()) { if (res.result()) { checkAddAccceptedReplyAddress(msg); deliverMessage(sock, address, msg); } else { if (debug) {
replyError(sock, "invalid_reply_address"); return; curMatch = new Match(true); } else { curMatch = checkMatches(true, address, body); User webUser = sock.webUser(); if (webUser != null) { authorise(curMatch, webUser, res -> { if (res.succeeded()) { if (res.result()) { checkAndSend(send, address, body, headers, sock, replyAddress, null); } else { replyError(sock, "access_denied"); if (debug) { log.debug("Inbound message for address " + address + " rejected because is not authorised"); replyError(sock, "auth_error"); log.error("Error in performing authorisation", res.cause()); replyError(sock, "not_logged_in"); if (debug) { log.debug("Inbound message for address " + address + checkAndSend(send, address, body, headers, sock, replyAddress, awaitingReply); replyError(sock, "access_denied"); if (debug) {
private void deliverMessage(SockJSSocket sock, String address, Message message) { JsonObject envelope = new JsonObject().put("type", "rec").put("address", address).put("body", message.body()); if (message.replyAddress() != null) { envelope.put("replyAddress", message.replyAddress()); } if (message.headers() != null && !message.headers().isEmpty()) { JsonObject headersCopy = new JsonObject(); for (String name : message.headers().names()) { List<String> values = message.headers().getAll(name); if (values.size() == 1) { headersCopy.put(name, values.get(0)); } else { headersCopy.put(name, values); } } envelope.put("headers", headersCopy); } checkCallHook(() -> new BridgeEventImpl(BridgeEventType.RECEIVE, envelope, sock), () -> sock.write(buffer(envelope.encode())), () -> log.debug("outbound message rejected by bridge event handler")); }
@Override public SockJSHandler bridge(BridgeOptions bridgeOptions, Handler<BridgeEvent> bridgeEventHandler) { socketHandler(new EventBusBridgeImpl(vertx, bridgeOptions, bridgeEventHandler)); return this; }
private void internalHandlePing(final SockJSSocket sock) { Session webSession = sock.webSession(); if (webSession != null) { webSession.setAccessed(); } SockInfo info = sockInfos.get(sock); if (info != null) { info.pingInfo.lastPing = System.currentTimeMillis(); // Trigger an event to allow custom behavior after updating lastPing checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_PING, null, sock), null, null); } }
private void handleSocketClosed(SockJSSocket sock, Map<String, MessageConsumer> registrations) { // On close unregister any handlers that haven't been unregistered registrations.forEach((key, value) -> { value.unregister(); checkCallHook(() -> new BridgeEventImpl(BridgeEventType.UNREGISTER, new JsonObject().put("type", "unregister").put("address", value.address()), sock), null, null); }); SockInfo info = sockInfos.remove(sock); if (info != null) { PingInfo pingInfo = info.pingInfo; if (pingInfo != null) { vertx.cancelTimer(pingInfo.timerID); } } checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_CLOSED, null, sock), null, null); }
private void deliverMessage(SockJSSocket sock, String address, Message message) { JsonObject envelope = new JsonObject().put("type", "rec").put("address", address).put("body", message.body()); if (message.replyAddress() != null) { envelope.put("replyAddress", message.replyAddress()); } if (message.headers() != null && !message.headers().isEmpty()) { JsonObject headersCopy = new JsonObject(); for (String name : message.headers().names()) { List<String> values = message.headers().getAll(name); if (values.size() == 1) { headersCopy.put(name, values.get(0)); } else { headersCopy.put(name, values); } } envelope.put("headers", headersCopy); } checkCallHook(() -> new BridgeEventImpl(BridgeEventType.RECEIVE, envelope, sock), () -> sock.write(buffer(envelope.encode())), () -> log.debug("outbound message rejected by bridge event handler")); }