Search in sources :

Example 1 with ConnectionString

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;
}
Also used : ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 2 with 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());
}
Also used : ConnectionString(com.mongodb.ConnectionString)

Example 3 with ConnectionString

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());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoCredential(com.mongodb.MongoCredential) Builder(com.mongodb.async.client.MongoClientSettings.Builder) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 4 with ConnectionString

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);
    }
}
Also used : ConnectionString(com.mongodb.ConnectionString)

Example 5 with ConnectionString

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);
    }
}
Also used : MongoCredential(com.mongodb.MongoCredential) AuthenticationMechanism(com.mongodb.AuthenticationMechanism) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Aggregations

ConnectionString (com.mongodb.ConnectionString)6 MongoCredential (com.mongodb.MongoCredential)2 AuthenticationMechanism (com.mongodb.AuthenticationMechanism)1 ServerAddress (com.mongodb.ServerAddress)1 Builder (com.mongodb.async.client.MongoClientSettings.Builder)1 ClusterSettings (com.mongodb.connection.ClusterSettings)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BsonValue (org.bson.BsonValue)1