use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectAddsSaslPlainForNonEmptyUsernameAndPassword.
/**
* Verifies that the factory enables SASL_PLAIN if the username and password are non-empty
* strings.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testConnectAddsSaslPlainForNonEmptyUsernameAndPassword(final TestContext ctx) {
// GIVEN a factory configured to connect to a server
final ProtonClientOptions options = new ProtonClientOptions();
final ProtonClient client = mock(ProtonClient.class);
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
factory.setProtonClient(client);
// WHEN connecting to the server using non-empty strings for username and password
factory.connect(options, "user", "pw", null, null, c -> {
});
// THEN the factory uses SASL_PLAIN when establishing the connection
ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), anyString(), anyInt(), eq("user"), eq("pw"), any(Handler.class));
assertTrue(optionsCaptor.getValue().getEnabledSaslMechanisms().contains("PLAIN"));
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImpl method connect.
private void connect(final ProtonClientOptions options, final Handler<AsyncResult<HonoClient>> connectionHandler, final Handler<ProtonConnection> disconnectHandler) {
context.runOnContext(connect -> {
if (isConnectedInternal()) {
LOG.debug("already connected to server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort());
connectionHandler.handle(Future.succeededFuture(this));
} else if (connecting.compareAndSet(false, true)) {
if (options == null) {
// by default, try to re-connect forever
clientOptions = new ProtonClientOptions().setConnectTimeout(200).setReconnectAttempts(-1).setReconnectInterval(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS);
} else {
clientOptions = options;
}
connectionFactory.connect(clientOptions, remoteClose -> onRemoteClose(remoteClose, disconnectHandler), failedConnection -> onRemoteDisconnect(failedConnection, disconnectHandler), conAttempt -> {
connecting.compareAndSet(true, false);
if (conAttempt.failed()) {
if (conAttempt.cause() instanceof SecurityException) {
// SASL handshake has failed
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "failed to authenticate with server")));
} else {
reconnect(conAttempt.cause(), connectionHandler, disconnectHandler);
}
} else {
// make sure we try to re-connect as often as we tried to connect initially
reconnectAttempts = new AtomicInteger(0);
final ProtonConnection newConnection = conAttempt.result();
if (shuttingDown.get()) {
// if client was shut down in the meantime, we need to immediately
// close again the newly created connection
newConnection.closeHandler(null);
newConnection.disconnectHandler(null);
newConnection.close();
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "client is already shut down")));
} else {
setConnection(newConnection);
connectionHandler.handle(Future.succeededFuture(this));
}
}
});
} else {
LOG.debug("already trying to connect to server ...");
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "already connecting to server")));
}
});
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class StandaloneTelemetryApiTest method prepareHonoServer.
/**
* Sets up the fixture common to all test cases.
*
* @param ctx The vert.x test context.
*/
@BeforeClass
public static void prepareHonoServer(final TestContext ctx) {
vertx = Vertx.vertx();
downstreamAdapter = new MessageDiscardingDownstreamAdapter(vertx);
final HonoMessagingConfigProperties configProperties = new HonoMessagingConfigProperties();
configProperties.setInsecurePort(0);
final TelemetryEndpoint telemetryEndpoint = new TelemetryEndpoint(vertx);
telemetryEndpoint.setMetrics(mock(MessagingMetrics.class));
telemetryEndpoint.setTelemetryAdapter(downstreamAdapter);
telemetryEndpoint.setRegistrationAssertionValidator(assertionHelper);
telemetryEndpoint.setConfiguration(configProperties);
server = new HonoMessaging();
server.setSaslAuthenticatorFactory(new HonoSaslAuthenticatorFactory(TestSupport.createAuthenticationService(createUser())));
server.setConfig(configProperties);
server.addEndpoint(telemetryEndpoint);
final Future<String> serverTracker = Future.future();
vertx.deployVerticle(server, serverTracker.completer());
serverTracker.compose(s -> {
final ClientConfigProperties clientProps = new ClientConfigProperties();
clientProps.setName("test");
clientProps.setHost(server.getInsecurePortBindAddress());
clientProps.setPort(server.getInsecurePort());
clientProps.setUsername(USER);
clientProps.setPassword(PWD);
client = new HonoClientImpl(vertx, clientProps);
return client.connect(new ProtonClientOptions());
}).setHandler(ctx.asyncAssertSuccess());
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class CredentialsAmqpIT method prepareDeviceRegistry.
/**
* Starts the device registry and connects a client.
*
* @param ctx The vert.x test context.
*/
@BeforeClass
public static void prepareDeviceRegistry(final TestContext ctx) {
client = DeviceRegistryAmqpTestSupport.prepareDeviceRegistryClient(vertx);
client.connect(new ProtonClientOptions()).compose(c -> c.getOrCreateCredentialsClient(Constants.DEFAULT_TENANT)).setHandler(ctx.asyncAssertSuccess(r -> {
credentialsClient = r;
}));
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ForwardingDownstreamAdapter method reconnect.
private void reconnect(final Handler<AsyncResult<ProtonConnection>> resultHandler) {
if (!running) {
logger.info("adapter is stopped, will not re-connect to downstream container");
} else {
final ProtonClientOptions clientOptions = createClientOptions();
if (clientOptions.getReconnectAttempts() != 0) {
vertx.setTimer(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS, reconnect -> {
logger.info("attempting to re-connect to downstream container");
connectToDownstream(clientOptions, resultHandler);
});
}
}
}
Aggregations