Search in sources :

Example 26 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class HonoConsumerBase method consumeData.

/**
 * Initiate the connection and set the message handling method to treat data that is received.
 *
 * @throws Exception Thrown if the latch is interrupted during waiting or if the read from System.in throws an IOException.
 */
protected void consumeData() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final Future<MessageConsumer> consumerFuture = Future.future();
    consumerFuture.setHandler(result -> {
        if (!result.succeeded()) {
            System.err.println("honoClient could not create telemetry consumer for " + HonoExampleConstants.HONO_AMQP_CONSUMER_HOST + ":" + HonoExampleConstants.HONO_AMQP_CONSUMER_PORT + " : " + result.cause());
        }
        latch.countDown();
    });
    honoClient.connect(new ProtonClientOptions()).compose(connectedClient -> {
        if (eventMode) {
            return connectedClient.createEventConsumer(HonoExampleConstants.TENANT_ID, this::handleMessage, closeHook -> System.err.println("remotely detached consumer link"));
        } else {
            return connectedClient.createTelemetryConsumer(HonoExampleConstants.TENANT_ID, this::handleMessage, closeHook -> System.err.println("remotely detached consumer link"));
        }
    }).setHandler(consumerFuture.completer());
    latch.await();
    if (consumerFuture.succeeded()) {
        System.in.read();
    }
    vertx.close();
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) CountDownLatch(java.util.concurrent.CountDownLatch) Section(org.apache.qpid.proton.amqp.messaging.Section) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Vertx(io.vertx.core.Vertx) Message(org.apache.qpid.proton.message.Message) MessageConsumer(org.eclipse.hono.client.MessageConsumer) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) MessageHelper(org.eclipse.hono.util.MessageHelper) HonoClient(org.eclipse.hono.client.HonoClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Future(io.vertx.core.Future) MessageConsumer(org.eclipse.hono.client.MessageConsumer) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 27 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class AuthenticationServerClient method verifyPlain.

/**
 * Verifies username/password credentials with a remote authentication server using SASL PLAIN.
 *
 * @param authzid The identity to act as.
 * @param authcid The username.
 * @param password The password.
 * @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication,
 *                                    the result contains a JWT with the authenticated user's claims.
 */
public void verifyPlain(final String authzid, final String authcid, final String password, final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {
    ProtonClientOptions options = new ProtonClientOptions();
    options.setReconnectAttempts(3).setReconnectInterval(50);
    options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN);
    factory.connect(options, authcid, password, null, null, conAttempt -> {
        if (conAttempt.failed()) {
            authenticationResultHandler.handle(Future.failedFuture("cannot connect to Authentication service"));
        } else {
            final ProtonConnection openCon = conAttempt.result();
            final Future<HonoUser> userTracker = Future.future();
            userTracker.setHandler(s -> {
                if (s.succeeded()) {
                    authenticationResultHandler.handle(Future.succeededFuture(s.result()));
                } else {
                    authenticationResultHandler.handle(Future.failedFuture(s.cause()));
                }
                ProtonConnection con = conAttempt.result();
                if (con != null) {
                    LOG.debug("closing connection to Authentication service");
                    con.close();
                }
            });
            vertx.setTimer(5000, tid -> {
                if (!userTracker.isComplete()) {
                    userTracker.fail("time out reached while waiting for token from Authentication service");
                }
            });
            getToken(openCon, userTracker);
        }
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) HonoUser(org.eclipse.hono.auth.HonoUser) ProtonClientOptions(io.vertx.proton.ProtonClientOptions)

Aggregations

ProtonClientOptions (io.vertx.proton.ProtonClientOptions)27 Test (org.junit.Test)20 Vertx (io.vertx.core.Vertx)19 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)19 Handler (io.vertx.core.Handler)17 Future (io.vertx.core.Future)16 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 Constants (org.eclipse.hono.util.Constants)15 RunWith (org.junit.runner.RunWith)15 TimeUnit (java.util.concurrent.TimeUnit)14 MessageSender (org.eclipse.hono.client.MessageSender)14 BeforeClass (org.junit.BeforeClass)14 ProtonConnection (io.vertx.proton.ProtonConnection)13 HttpURLConnection (java.net.HttpURLConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AsyncResult (io.vertx.core.AsyncResult)12 Async (io.vertx.ext.unit.Async)12 ClientErrorException (org.eclipse.hono.client.ClientErrorException)12 RegistrationClient (org.eclipse.hono.client.RegistrationClient)12