use of com.mongodb.ConnectionString in project mongo-java-driver by mongodb.
the class Fixture method getConnectionString.
public static synchronized ConnectionString getConnectionString() {
if (connectionString == null) {
String mongoURIProperty = System.getProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME);
String mongoURIString = mongoURIProperty == null || mongoURIProperty.isEmpty() ? DEFAULT_URI : mongoURIProperty;
connectionString = new ConnectionString(mongoURIString);
}
return connectionString;
}
use of com.mongodb.ConnectionString in project mongo-java-driver by mongodb.
the class ConnectionStringTest method testValidHostIdentifiers.
private void testValidHostIdentifiers() {
ConnectionString connectionString = null;
try {
connectionString = new ConnectionString(input);
} catch (Throwable t) {
assertTrue(String.format("Connection string '%s' should not have throw an exception: %s", input, t.toString()), false);
}
assertExpectedHosts(connectionString.getHosts());
}
use of com.mongodb.ConnectionString in project spring-boot by spring-projects.
the class ReactiveMongoClientFactory method createNetworkMongoClient.
private MongoClient createNetworkMongoClient(MongoClientSettings settings) {
if (hasCustomAddress() || hasCustomCredentials()) {
if (this.properties.getUri() != null) {
throw new IllegalStateException("Invalid mongo configuration, " + "either uri or host/port/credentials must be specified");
}
Builder builder = builder(settings);
if (hasCustomCredentials()) {
List<MongoCredential> credentials = new ArrayList<>();
String database = this.properties.getAuthenticationDatabase() == null ? this.properties.getMongoClientDatabase() : this.properties.getAuthenticationDatabase();
credentials.add(MongoCredential.createCredential(this.properties.getUsername(), database, this.properties.getPassword()));
builder.credentialList(credentials);
}
String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
int port = this.properties.getPort() != null ? this.properties.getPort() : MongoProperties.DEFAULT_PORT;
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(host, port))).build();
builder.clusterSettings(clusterSettings);
return MongoClients.create(builder.build());
}
ConnectionString connectionString = new ConnectionString(this.properties.determineUri());
return MongoClients.create(createBuilder(settings, connectionString).build());
}
use of com.mongodb.ConnectionString in project mongo-java-driver by mongodb.
the class AbstractServerDiscoveryAndMonitoringTest method init.
protected void init(final ServerListenerFactory serverListenerFactory, final ClusterListener clusterListener) {
ConnectionString connectionString = new ConnectionString(definition.getString("uri").getValue());
ClusterConnectionMode mode = getMode(connectionString);
ClusterSettings settings = ClusterSettings.builder().serverSelectionTimeout(1, TimeUnit.SECONDS).hosts(getHosts(connectionString)).mode(mode).requiredReplicaSetName(connectionString.getRequiredReplicaSetName()).addClusterListener(clusterListener).build();
ClusterId clusterId = new ClusterId();
factory = new DefaultTestClusterableServerFactory(clusterId, mode, serverListenerFactory);
if (settings.getMode() == ClusterConnectionMode.SINGLE) {
cluster = new SingleServerCluster(clusterId, settings, factory);
} else {
cluster = new MultiServerCluster(clusterId, settings, factory);
}
}
use of com.mongodb.ConnectionString in project mongo-java-driver by mongodb.
the class ConnectionStringTest method testValidAuth.
private void testValidAuth() {
ConnectionString connectionString = null;
try {
connectionString = new ConnectionString(input);
} catch (Throwable t) {
if (description.contains("without password")) {
// We don't allow null passwords without setting the authentication mechanism.
return;
} else {
assertTrue(String.format("Connection string '%s' should not have throw an exception: %s", input, t.toString()), false);
}
}
assertString("auth.db", getAuthDB(connectionString));
assertString("auth.username", connectionString.getUsername());
// Passwords for certain auth mechanisms are ignored.
String password = null;
if (connectionString.getPassword() != null) {
password = new String(connectionString.getPassword());
}
List<MongoCredential> credentials = connectionString.getCredentialList();
if (credentials.size() > 0) {
AuthenticationMechanism mechanism = credentials.get(0).getAuthenticationMechanism();
if (mechanism == null) {
assertString("auth.password", password);
} else {
switch(mechanism) {
case PLAIN:
case MONGODB_CR:
case SCRAM_SHA_1:
assertString("auth.password", password);
break;
default:
}
}
} else {
assertString("auth.password", password);
}
}
Aggregations