io.vertx.ext.bridge.BridgeEventType Java Examples

The following examples show how to use io.vertx.ext.bridge.BridgeEventType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TcpEventBusBridgeImpl.java    From vertx-tcp-eventbus-bridge with Apache License 2.0 6 votes vote down vote up
private static BridgeEventType parseType(String typeStr) {
  switch (typeStr) {
    case "ping":
      return BridgeEventType.SOCKET_PING;
    case "register":
      return BridgeEventType.REGISTER;
    case "unregister":
        return BridgeEventType.UNREGISTER;
    case "publish":
      return BridgeEventType.PUBLISH;
    case "send":
      return BridgeEventType.SEND;
    default:
      throw new IllegalArgumentException("Invalid frame type " + typeStr);
  }
}
 
Example #2
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookUnregister() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.UNREGISTER) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testUnregister(addr);
  await();
}
 
Example #3
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookReceive() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.RECEIVE) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      assertEquals("foobar", raw.getString("body"));
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testReceive("foobar");
  await();
}
 
Example #4
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookRegister() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.REGISTER) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testReceive("foobar");
  await();
}
 
Example #5
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookPublishHeaders() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.PUBLISH) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      assertEquals("foobar", raw.getString("body"));
      raw.put("headers", new JsonObject().put("hdr1", "val1").put("hdr2", "val2"));
      be.setRawMessage(raw);
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testPublish(addr, "foobar", true);
  await();
}
 
Example #6
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookPublish() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.PUBLISH) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      assertEquals("foobar", raw.getString("body"));
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testPublish("foobar");
  await();
}
 
Example #7
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookSendHeaders() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SEND) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      assertEquals("foobar", raw.getString("body"));
      raw.put("headers", new JsonObject().put("hdr1", "val1").put("hdr2", "val2"));
      be.setRawMessage(raw);
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testSend(addr, "foobar", true);
  await();
}
 
Example #8
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookSend() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SEND) {
      assertNotNull(be.socket());
      JsonObject raw = be.getRawMessage();
      assertEquals(addr, raw.getString("address"));
      assertEquals("foobar", raw.getString("body"));
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testSend("foobar");
  await();
}
 
Example #9
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookSocketClosed() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SOCKET_CLOSED) {
      assertNotNull(be.socket());
      assertNull(be.getRawMessage());
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  client.webSocket(websocketURI, onSuccess(WebSocketBase::close));
  await();
}
 
Example #10
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookCreateSocketRejected() throws Exception {

  CountDownLatch latch = new CountDownLatch(2);

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SOCKET_CREATED) {
      be.complete(false);
      latch.countDown();
    } else {
      be.complete(true);
    }
  });

  client.webSocket(websocketURI, onSuccess(ws -> {
    JsonObject msg = new JsonObject().put("type", "send").put("address", addr).put("body", "foobar");
    ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
    ws.closeHandler(v -> latch.countDown());
  }));

  awaitLatch(latch);
}
 
Example #11
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookCreateSocket() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SOCKET_CREATED) {
      assertNotNull(be.socket());
      assertNull(be.getRawMessage());
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testSend("foobar");
  await();
}
 
Example #12
Source File: EventBusBridgeImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example #13
Source File: EventBusBridgeImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #14
Source File: EventBusBridgeImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example #15
Source File: EventBusBridgeImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #16
Source File: WebExamples.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
public void handleSocketIdle(Vertx vertx, PermittedOptions inboundPermitted) {
  Router router = Router.router(vertx);

  // Initialize SockJS handler
  SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
  SockJSBridgeOptions options = new SockJSBridgeOptions()
    .addInboundPermitted(inboundPermitted)
    .setPingTimeout(5000);

  // mount the bridge on the router
  router
    .mountSubRouter("/eventbus", sockJSHandler.bridge(options, be -> {
      if (be.type() == BridgeEventType.SOCKET_IDLE) {
        // Do some custom handling...
      }

      be.complete(true);
    }));
}
 
Example #17
Source File: TestEventBusBridge.java    From nubes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendRelatedEvents() throws Exception {
  CountDownLatch latch = new CountDownLatch(3);
  String msg = "Happiness is a warm puppy";
  vertx.eventBus().consumer(TEST_EB_ADDRESS, ebMsg -> {
    assertEquals(msg, ebMsg.body());
    latch.countDown();
  });

  vertx.eventBus().consumer(BridgeEventType.SOCKET_CREATED.toString(), ebMsg -> {
    assertEquals(BridgeEventType.SOCKET_CREATED.toString(), ebMsg.body());
    latch.countDown();
  });
  vertx.eventBus().consumer(BridgeEventType.SEND.toString(), ebMsg -> {
    assertEquals(BridgeEventType.SEND.toString(), ebMsg.body());
    latch.countDown();
  });
  client().websocket("/eventbus/default/websocket", ws -> {
    sendThroughBridge(ws, TEST_EB_ADDRESS, msg);
  });
  assertTrue(latch.await(2, TimeUnit.SECONDS));
}
 
Example #18
Source File: EventBusBridgeVisitor.java    From nubes with Apache License 2.0 6 votes vote down vote up
public void visit() {
  sockJSHandler = SockJSHandler.create(config.getVertx(), config.getSockJSOptions());
  try {
    instance = clazz.newInstance();
    injectServices();
  } catch (Exception e) {
    throw new VertxException("Could not instanciate socket controller : " + clazz.getName(), e);
  }
  EventBusBridge annot = clazz.getAnnotation(EventBusBridge.class);
  path = annot.value();
  BridgeOptions bridge = createBridgeOptions(clazz);
  Map<BridgeEventType, Method> handlers = BridgeEventFactory.createFromController(clazz);
  sockJSHandler.bridge(bridge, be -> {
    Method method = handlers.get(be.type());
    if (method != null) {
      tryToInvoke(instance, method, be);
    } else {
      be.complete(true);
    }
  });
  normalizePath();
  router.route(path).handler(sockJSHandler);
}
 
Example #19
Source File: BridgeEventFactory.java    From nubes with Apache License 2.0 5 votes vote down vote up
static Map<BridgeEventType, Method> createFromController(Class<?> controller) {
  Map<BridgeEventType, Method> map = new EnumMap<>(BridgeEventType.class);
  for (Method method : controller.getMethods()) {
    createFromMethod(map, method);
  }
  return map;
}
 
Example #20
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookUnregisterMissingAddress() throws Exception {
  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.UNREGISTER) {
      be.getRawMessage().remove("address");
      testComplete();
    }
    be.complete(true);
  });
  testError(new JsonObject().put("type", "unregister").put("address", addr).put("body", "foobar"),
    "missing_address");
  await();
}
 
Example #21
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookUnregisterRejected() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.UNREGISTER) {
      be.complete(false);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testError(new JsonObject().put("type", "unregister").put("address", addr),
    "rejected");
  await();
}
 
Example #22
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookReceiveRejected() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.RECEIVE) {
      be.complete(false);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testReceiveFail(addr, "foobar");
  await();
}
 
Example #23
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookRegisterMissingAddress() throws Exception {
  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.REGISTER) {
      be.getRawMessage().remove("address");
      testComplete();
    }
    be.complete(true);
  });
  testError(new JsonObject().put("type", "register").put("address", addr).put("body", "foobar"),
    "missing_address");
  await();
}
 
Example #24
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookRegisterRejected() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.REGISTER) {
      be.complete(false);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testError(new JsonObject().put("type", "register").put("address", addr),
    "rejected");
  await();
}
 
Example #25
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookPublishMissingAddress() throws Exception {
  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.PUBLISH) {
      be.getRawMessage().remove("address");
      testComplete();
    }
    be.complete(true);
  });
  testError(new JsonObject().put("type", "publish").put("address", addr).put("body", "foobar"),
    "missing_address");
  await();
}
 
Example #26
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookPubRejected() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.PUBLISH) {
      be.complete(false);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testError(new JsonObject().put("type", "publish").put("address", addr).put("body", "foobar"),
    "rejected");
  await();
}
 
Example #27
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookSendMissingAddress() throws Exception {
  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SEND) {
      be.getRawMessage().remove("address");
      testComplete();
    }
    be.complete(true);
  });
  testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foobar"),
    "missing_address");
  await();
}
 
Example #28
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testHookSendRejected() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SEND) {
      be.complete(false);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foobar"),
    "rejected");
  await();
}
 
Example #29
Source File: WebExamples.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public void example49(Vertx vertx) {

    Router router = Router.router(vertx);

    // Let through any messages sent to 'demo.orderMgr' from the client
    PermittedOptions inboundPermitted = new PermittedOptions()
      .setAddress("demo.someService");

    SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
    SockJSBridgeOptions options = new SockJSBridgeOptions()
      .addInboundPermitted(inboundPermitted);

    // mount the bridge on the router
    router
      .mountSubRouter("/eventbus", sockJSHandler
        .bridge(options, be -> {
          if (be.type() == BridgeEventType.PUBLISH ||
            be.type() == BridgeEventType.RECEIVE) {

            if (be.getRawMessage().getString("body").equals("armadillos")) {
              // Reject it
              be.complete(false);
              return;
            }
          }
          be.complete(true);
        }));
  }
 
Example #30
Source File: BridgeEventFactory.java    From nubes with Apache License 2.0 5 votes vote down vote up
private static void createFromAnnotation(Map<BridgeEventType, Method> map, Method method, Annotation annot) {
  BridgeEventType type = types.get(annot.annotationType());
  if (type != null && map.get(type) != null) {
    throw new IllegalArgumentException("You cannot register many methods on the same BridgeEvent.Type");
  } else if (type != null ){
    map.put(type, method);
  }
}