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 checkCallHook(Supplier<BridgeEventImpl> eventSupplier, Runnable okAction, Runnable rejectAction) { if (bridgeEventHandler == null) { if (okAction != null) { okAction.run(); } } else { BridgeEventImpl event = eventSupplier.get(); Future<Boolean> fut = Future.future(); event.setFuture(fut); bridgeEventHandler.handle(event); fut.setHandler(res -> { if (res.succeeded()) { if (res.result()) { if (okAction != null) { okAction.run(); } } else { if (rejectAction != null) { rejectAction.run(); } else { log.debug("Bridge handler prevented send or pub"); } } } else { log.error("Failure in bridge event handler", res.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 checkCallHook(Supplier<BridgeEventImpl> eventSupplier, Runnable okAction, Runnable rejectAction) { if (bridgeEventHandler == null) { if (okAction != null) { okAction.run(); } } else { BridgeEventImpl event = eventSupplier.get(); Future<Boolean> fut = Future.future(); event.setFuture(fut); bridgeEventHandler.handle(event); fut.setHandler(res -> { if (res.succeeded()) { if (res.result()) { if (okAction != null) { okAction.run(); } } else { if (rejectAction != null) { rejectAction.run(); } else { log.debug("Bridge handler prevented send or pub"); } } } else { log.error("Failure in bridge event handler", res.cause()); } }); } }
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 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")); }
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 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")); }
return; checkCallHook(() -> new BridgeEventImpl(BridgeEventType.REGISTER, rawMsg, sock), () -> { final boolean debug = log.isDebugEnabled();
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 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 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 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")); }
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 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")); }
return; checkCallHook(() -> new BridgeEventImpl(BridgeEventType.REGISTER, rawMsg, sock), () -> { final boolean debug = log.isDebugEnabled();