Use MongoOptions and MongoClientURI to build MongoClient
The way that Spring Data Mongo conctructs a MongoDb has changed in Spring Data 1.8.0 to align with the recommended way from 10gen. This involves leaving the user credentials embedded in the URI if they are there, which they always are in Spring Cloud Connectors. Fixes gh-129
This commit is contained in:
@@ -4,28 +4,23 @@ import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.mongodb.MongoClientOptions.Builder;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.MongoException;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.WriteConcern;
|
||||
|
||||
import org.springframework.cloud.service.AbstractServiceConnectorCreator;
|
||||
import org.springframework.cloud.service.ServiceConnectorConfig;
|
||||
import org.springframework.cloud.service.ServiceConnectorCreationException;
|
||||
import org.springframework.cloud.service.common.MongoServiceInfo;
|
||||
import org.springframework.data.authentication.UserCredentials;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.MongoClientOptions.Builder;
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.mongodb.MongoException;
|
||||
import com.mongodb.WriteConcern;
|
||||
|
||||
/**
|
||||
* Simplified access to creating MongoDB service objects.
|
||||
*
|
||||
@@ -37,7 +32,7 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<Mongo
|
||||
@Override
|
||||
public MongoDbFactory create(MongoServiceInfo serviceInfo, ServiceConnectorConfig config) {
|
||||
try {
|
||||
MongoClientOptions mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
|
||||
MongoClientOptions.Builder mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
|
||||
|
||||
SimpleMongoDbFactory mongoDbFactory = createMongoDbFactory(serviceInfo, mongoOptionsToUse);
|
||||
|
||||
@@ -49,33 +44,13 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<Mongo
|
||||
}
|
||||
}
|
||||
|
||||
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions mongoOptionsToUse) throws UnknownHostException {
|
||||
MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri());
|
||||
List<ServerAddress> serverAddressList = getServerAddresses(mongoClientURI);
|
||||
|
||||
MongoClient mongo = new MongoClient(serverAddressList, mongoOptionsToUse);
|
||||
MongoCredential mongoCredential = mongoClientURI.getCredentials();
|
||||
|
||||
if (mongoCredential.getUserName() != null && mongoCredential.getPassword() != null) {
|
||||
UserCredentials credentials = new UserCredentials(mongoCredential.getUserName(), new String(mongoCredential.getPassword()));
|
||||
return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase(), credentials, mongoCredential.getSource());
|
||||
}
|
||||
|
||||
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
|
||||
MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
|
||||
MongoClient mongo = new MongoClient(mongoClientURI);
|
||||
return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
|
||||
}
|
||||
|
||||
private List<ServerAddress> getServerAddresses(MongoClientURI mongoClientURI) throws UnknownHostException {
|
||||
List<String> servers = mongoClientURI.getHosts();
|
||||
List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();
|
||||
|
||||
for(String server : servers) {
|
||||
serverAddressList.add(new ServerAddress(server));
|
||||
}
|
||||
|
||||
return serverAddressList;
|
||||
}
|
||||
|
||||
private MongoClientOptions getMongoOptions(MongoDbFactoryConfig config) {
|
||||
private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) {
|
||||
MongoClientOptions.Builder builder;
|
||||
|
||||
Method builderMethod = ClassUtils.getMethodIfAvailable(MongoClientOptions.class, "builder");
|
||||
@@ -108,7 +83,7 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<Mongo
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public SimpleMongoDbFactory configure(SimpleMongoDbFactory mongoDbFactory, MongoDbFactoryConfig config) {
|
||||
|
||||
@@ -8,14 +8,14 @@ import java.util.List;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.service.common.MongoServiceInfo;
|
||||
import org.springframework.cloud.service.document.MongoDbFactoryCreator;
|
||||
import org.springframework.data.authentication.UserCredentials;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.ServerAddress;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
|
||||
/**
|
||||
* Test cases for Mongo service connector creators.
|
||||
*
|
||||
@@ -44,10 +44,11 @@ public class MongoServiceConnectorCreatorTest {
|
||||
|
||||
assertNotNull(mongoDbFactory);
|
||||
|
||||
Mongo mongo = (Mongo) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
|
||||
UserCredentials credentials = (UserCredentials) ReflectionTestUtils.getField(mongoDbFactory, "credentials");
|
||||
MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
|
||||
assertNotNull(mongo);
|
||||
|
||||
MongoCredential credentials = mongo.getCredentialsList().get(0);
|
||||
|
||||
List<ServerAddress> addresses = mongo.getAllAddress();
|
||||
assertEquals(1, addresses.size());
|
||||
|
||||
@@ -55,8 +56,8 @@ public class MongoServiceConnectorCreatorTest {
|
||||
|
||||
assertEquals(serviceInfo.getHost(), address.getHost());
|
||||
assertEquals(serviceInfo.getPort(), address.getPort());
|
||||
assertEquals(serviceInfo.getUserName(), ReflectionTestUtils.getField(credentials, "username"));
|
||||
assertEquals(serviceInfo.getPassword(), ReflectionTestUtils.getField(credentials, "password"));
|
||||
assertEquals(serviceInfo.getUserName(), credentials.getUserName());
|
||||
assertNotNull(credentials.getPassword());
|
||||
|
||||
// Don't do connector.getDatabase().getName() as that will try to initiate the connection
|
||||
assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
|
||||
@@ -73,15 +74,15 @@ public class MongoServiceConnectorCreatorTest {
|
||||
|
||||
assertNotNull(mongoDbFactory);
|
||||
|
||||
Mongo mongo = (Mongo) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
|
||||
UserCredentials credentials = (UserCredentials) ReflectionTestUtils.getField(mongoDbFactory, "credentials");
|
||||
MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
|
||||
assertNotNull(mongo);
|
||||
|
||||
List<ServerAddress> addresses = mongo.getAllAddress();
|
||||
assertEquals(3, addresses.size());
|
||||
|
||||
assertEquals(TEST_USERNAME, ReflectionTestUtils.getField(credentials, "username"));
|
||||
assertEquals(TEST_PASSWORD, ReflectionTestUtils.getField(credentials, "password"));
|
||||
MongoCredential credentials = mongo.getCredentialsList().get(0);
|
||||
assertEquals(TEST_USERNAME, credentials.getUserName());
|
||||
assertNotNull(credentials.getPassword());
|
||||
|
||||
// Don't do connector.getDatabase().getName() as that will try to initiate the connection
|
||||
assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
|
||||
|
||||
Reference in New Issue
Block a user