Java Code Examples for io.vertx.proton.ProtonConnection#open()

The following examples show how to use io.vertx.proton.ProtonConnection#open() . 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: SimpleAuthenticationServer.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Processes the AMQP <em>open</em> frame received from a peer.
 * <p>
 * Checks if the open frame contains a desired <em>ADDRESS_AUTHZ</em> capability and if so,
 * adds the authenticated clients' authorities to the properties of the open frame sent
 * to the peer in response.
 *
 * @param connection The connection opened by the peer.
 */
@Override
protected void processRemoteOpen(final ProtonConnection connection) {
    if (AddressAuthzHelper.isAddressAuthzCapabilitySet(connection)) {
        LOG.debug("client [container: {}] requests transfer of authenticated user's authorities in open frame",
                connection.getRemoteContainer());
        AddressAuthzHelper.processAddressAuthzCapability(connection);
    }
    connection.open();
    vertx.setTimer(5000, closeCon -> {
        if (!connection.isDisconnected()) {
            LOG.debug("connection with client [{}] timed out after 5 seconds, closing connection", connection.getRemoteContainer());
            connection.setCondition(ProtonHelper.condition(Constants.AMQP_ERROR_INACTIVITY,
                    "client must retrieve token within 5 secs after opening connection")).close();
        }
    });
}
 
Example 2
Source File: AmqpAdapterTestBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
private Future<ProtonConnection> handleConnectAttempt(final ProtonConnection unopenedConnection) {

        final Promise<ProtonConnection> result = Promise.promise();

        unopenedConnection.openHandler(result);
        unopenedConnection.closeHandler(remoteClose -> {
            unopenedConnection.close();
        });
        unopenedConnection.open();

        return result.future()
                .map(con -> {
                    assertThat(unopenedConnection.getRemoteOfferedCapabilities()).contains(Constants.CAP_ANONYMOUS_RELAY);
                    this.context = Vertx.currentContext();
                    this.connection = unopenedConnection;
                    return con;
                })
                .recover(t -> {
                    return Optional.ofNullable(unopenedConnection.getRemoteCondition())
                            .map(condition -> Future.<ProtonConnection>failedFuture(StatusCodeMapper.from(condition)))
                            .orElse(Future.failedFuture(t));
                });
    }
 
Example 3
Source File: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Processes a peer's AMQP <em>open</em> frame.
 * <p>
 * This default implementation
 * <ol>
 * <li>adds a unique connection identifier to the connection's attachments
 * under key {@link Constants#KEY_CONNECTION_ID}</li>
 * <li>invokes {@link #processDesiredCapabilities(ProtonConnection, Symbol[])}</li>
 * <li>sets a timer that closes the connection once the client's token
 * has expired</li>
 * <li>sends the AMQP <em>open</em> frame to the peer</li>
 * </ol>
 *
 * @param connection The connection to open.
 */
protected void processRemoteOpen(final ProtonConnection connection) {

    log.debug("processing open frame from client container [{}]", connection.getRemoteContainer());
    final HonoUser clientPrincipal = Constants.getClientPrincipal(connection);
    // attach an ID so that we can later inform downstream components when connection is closed
    connection.attachments().set(Constants.KEY_CONNECTION_ID, String.class, UUID.randomUUID().toString());
    processDesiredCapabilities(connection, connection.getRemoteDesiredCapabilities());
    final Duration delay = Duration.between(Instant.now(), clientPrincipal.getExpirationTime());
    final WeakReference<ProtonConnection> conRef = new WeakReference<>(connection);
    vertx.setTimer(delay.toMillis(), timerId -> {
        if (conRef.get() != null) {
            closeExpiredConnection(conRef.get());
        }
    });
    connection.open();
    log.info("client connected [container: {}, user: {}, token valid until: {}]",
            connection.getRemoteContainer(), clientPrincipal.getName(), clientPrincipal.getExpirationTime().toString());
}
 
Example 4
Source File: AmqpBridge.java    From strimzi-kafka-bridge with Apache License 2.0 6 votes vote down vote up
/**
 * Process a connection request accepted by the Proton server or
 * open the connection if it's working as client
 *
 * @param connection Proton connection accepted instance
 */
private void processConnection(ProtonConnection connection) {

    connection
            .openHandler(this::processOpenConnection)
            .closeHandler(this::processCloseConnection)
            .disconnectHandler(this::processDisconnection)
            .sessionOpenHandler(this::processOpenSession)
            .receiverOpenHandler(receiver -> {
                this.processOpenReceiver(connection, receiver);
            })
            .senderOpenHandler(sender -> {
                this.processOpenSender(connection, sender);
            });

    if (this.bridgeConfig.getAmqpConfig().getMode() == AmqpMode.CLIENT) {
        connection.open();
    }
}
 
Example 5
Source File: AmqpBridge.java    From strimzi-kafka-bridge with Apache License 2.0 6 votes vote down vote up
/**
 * Handler for connection opened by remote
 *
 * @param ar async result with info on related Proton connection
 */
private void processOpenConnection(AsyncResult<ProtonConnection> ar) {

    if (ar.succeeded()) {

        log.info("Connection opened by {} {}", ar.result().getRemoteHostname(), ar.result().getRemoteContainer());

        ProtonConnection connection = ar.result();
        connection.open();

        // new connection, preparing for hosting related sink/source endpoints
        if (!this.endpoints.containsKey(connection)) {
            this.endpoints.put(connection, new ConnectionEndpoint());
        }
    }
}
 
Example 6
Source File: HelloWorld.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
private static void helloWorldSendAndConsumeExample(ProtonConnection connection) {
  connection.open();

  // Receive messages from queue "foo" (using an ActiveMQ style address as example).
  String address = "queue://foo";

  connection.createReceiver(address).handler((delivery, msg) -> {
    Section body = msg.getBody();
    if (body instanceof AmqpValue) {
      String content = (String) ((AmqpValue) body).getValue();
      System.out.println("Received message with content: " + content);
    }
    // By default, the receiver automatically accepts (and settles) the delivery
    // when the handler returns, if no other disposition has been applied.
    // To change this and always manage dispositions yourself, use the
    // setAutoAccept method on the receiver.
  }).open();

  // Create an anonymous (no address) sender, have the message carry its destination
  ProtonSender sender = connection.createSender(null);

  // Create a message to send, have it carry its destination for use with the anonymous sender
  Message message = message(address, "Hello World from client");

  // Can optionally add an openHandler or sendQueueDrainHandler
  // to await remote sender open completing or credit to send being
  // granted. But here we will just buffer the send immediately.
  sender.open();
  System.out.println("Sending message to server");
  sender.send(message, delivery -> {
    System.out.println(String.format("The message was received by the server: remote state=%s, remotely settled=%s",
        delivery.getRemoteState(), delivery.remotelySettled()));
  });
}