Skip to content

Commit 127887a

Browse files
committed
JAVA-2656: Deprecate ConnectionString#getCredentialList and replace with ConnectionString#getCredential
1 parent c4222c6 commit 127887a

File tree

6 files changed

+53
-42
lines changed

6 files changed

+53
-42
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClients.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,17 @@ public static MongoClient create(final ConnectionString connectionString, final
145145
.serverSettings(ServerSettings.builder()
146146
.applyConnectionString(connectionString)
147147
.build())
148-
.credentialList(connectionString.getCredentialList())
149148
.sslSettings(SslSettings.builder()
150149
.applyConnectionString(connectionString)
151150
.build())
152151
.socketSettings(SocketSettings.builder()
153152
.applyConnectionString(connectionString)
154153
.build());
155154

155+
if (connectionString.getCredential() != null) {
156+
builder.credential(connectionString.getCredential());
157+
}
158+
156159
if (connectionString.getReadPreference() != null) {
157160
builder.readPreference(connectionString.getReadPreference());
158161
}
@@ -183,6 +186,7 @@ private static MongoClient create(final MongoClientSettings settings, final Mong
183186
}
184187
}
185188

189+
@SuppressWarnings("deprecation")
186190
static MongoClient createMongoClient(final MongoClientSettings settings, final MongoDriverInformation mongoDriverInformation,
187191
final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory,
188192
final Closeable externalResourceCloser) {

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class ConnectionString {
201201

202202
private static final Logger LOGGER = Loggers.getLogger("uri");
203203

204-
private final MongoCredential credentials;
204+
private final MongoCredential credential;
205205
private final List<String> hosts;
206206
private final String database;
207207
private final String collection;
@@ -314,7 +314,7 @@ public ConnectionString(final String connectionString) {
314314

315315
Map<String, List<String>> optionsMap = parseOptions(unprocessedConnectionString);
316316
translateOptions(optionsMap);
317-
credentials = createCredentials(optionsMap, userName, password);
317+
credential = createCredentials(optionsMap, userName, password);
318318
warnOnUnsupportedOptions(optionsMap);
319319
}
320320

@@ -830,7 +830,7 @@ private String urldecode(final String input, final boolean password) {
830830
* @return the username
831831
*/
832832
public String getUsername() {
833-
return credentials != null ? credentials.getUserName() : null;
833+
return credential != null ? credential.getUserName() : null;
834834
}
835835

836836
/**
@@ -839,7 +839,7 @@ public String getUsername() {
839839
* @return the password
840840
*/
841841
public char[] getPassword() {
842-
return credentials != null ? credentials.getPassword() : null;
842+
return credential != null ? credential.getPassword() : null;
843843
}
844844

845845
/**
@@ -896,9 +896,21 @@ public String getConnectionString() {
896896
* Gets the credentials in an immutable list. The list will be empty if no credentials were specified in the connection string.
897897
*
898898
* @return the credentials in an immutable list
899+
* @deprecated Prefer {@link #getCredential()}
899900
*/
901+
@Deprecated
900902
public List<MongoCredential> getCredentialList() {
901-
return credentials != null ? singletonList(credentials) : Collections.<MongoCredential>emptyList();
903+
return credential != null ? singletonList(credential) : Collections.<MongoCredential>emptyList();
904+
}
905+
906+
/**
907+
* Gets the credentials in an immutable list. The list will be empty if no credentials were specified in the connection string.
908+
*
909+
* @return the credentials in an immutable list
910+
* @since 3.6
911+
*/
912+
public MongoCredential getCredential() {
913+
return credential;
902914
}
903915

904916
/**
@@ -1108,7 +1120,7 @@ public boolean equals(final Object o) {
11081120
if (connectTimeout != null ? !connectTimeout.equals(that.connectTimeout) : that.connectTimeout != null) {
11091121
return false;
11101122
}
1111-
if (credentials != null ? !credentials.equals(that.credentials) : that.credentials != null) {
1123+
if (credential != null ? !credential.equals(that.credential) : that.credential != null) {
11121124
return false;
11131125
}
11141126
if (database != null ? !database.equals(that.database) : that.database != null) {
@@ -1169,7 +1181,7 @@ public boolean equals(final Object o) {
11691181

11701182
@Override
11711183
public int hashCode() {
1172-
int result = credentials != null ? credentials.hashCode() : 0;
1184+
int result = credential != null ? credential.hashCode() : 0;
11731185
result = 31 * result + hosts.hashCode();
11741186
result = 31 * result + (database != null ? database.hashCode() : 0);
11751187
result = 31 * result + (collection != null ? collection.hashCode() : 0);

driver-core/src/test/functional/com/mongodb/ClusterFixture.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public static synchronized Cluster getAsyncCluster() {
259259
return asyncCluster;
260260
}
261261

262+
@SuppressWarnings("deprecation")
262263
public static Cluster createCluster(final StreamFactory streamFactory) {
263264
return new DefaultClusterFactory().createCluster(ClusterSettings.builder().applyConnectionString(getConnectionString()).build(),
264265
ServerSettings.builder().build(),
@@ -303,6 +304,7 @@ public static ServerAddress getPrimary() throws InterruptedException {
303304
return serverDescriptions.get(0).getAddress();
304305
}
305306

307+
@SuppressWarnings("deprecation")
306308
public static List<MongoCredential> getCredentialList() {
307309
return getConnectionString().getCredentialList();
308310
}
@@ -321,7 +323,7 @@ public static boolean isStandalone() {
321323
}
322324

323325
public static boolean isAuthenticated() {
324-
return !getConnectionString().getCredentialList().isEmpty();
326+
return getConnectionString().getCredential() != null;
325327
}
326328

327329
public static void enableMaxTimeFailPoint() {

driver-core/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,54 +224,55 @@ class ConnectionStringSpecification extends Specification {
224224
@Unroll
225225
def 'should support all credential types'() {
226226
expect:
227-
uri.credentialList == credentialList
227+
uri.credential == credential
228+
uri.credentialList == [credential]
228229

229230
where:
230-
uri | credentialList
231-
new ConnectionString('mongodb://jeff:123@localhost') | asList(createCredential('jeff', 'admin', '123'.toCharArray()))
231+
uri | credential
232+
new ConnectionString('mongodb://jeff:123@localhost') | createCredential('jeff', 'admin', '123'.toCharArray())
232233
new ConnectionString('mongodb://jeff:123@localhost/?' +
233-
'&authSource=test') | asList(createCredential('jeff', 'test', '123'.toCharArray()))
234+
'&authSource=test') | createCredential('jeff', 'test', '123'.toCharArray())
234235
new ConnectionString('mongodb://jeff:123@localhost/?' +
235-
'authMechanism=MONGODB-CR') | asList(createMongoCRCredential('jeff', 'admin', '123'.toCharArray()))
236+
'authMechanism=MONGODB-CR') | createMongoCRCredential('jeff', 'admin', '123'.toCharArray())
236237
new ConnectionString('mongodb://jeff:123@localhost/?' +
237238
'authMechanism=MONGODB-CR' +
238-
'&authSource=test') | asList(createMongoCRCredential('jeff', 'test', '123'.toCharArray()))
239+
'&authSource=test') | createMongoCRCredential('jeff', 'test', '123'.toCharArray())
239240
new ConnectionString('mongodb://jeff:123@localhost/?' +
240-
'authMechanism=SCRAM-SHA-1') | asList(createScramSha1Credential('jeff', 'admin', '123'.toCharArray()))
241+
'authMechanism=SCRAM-SHA-1') | createScramSha1Credential('jeff', 'admin', '123'.toCharArray())
241242
new ConnectionString('mongodb://jeff:123@localhost/?' +
242243
'authMechanism=SCRAM-SHA-1' +
243-
'&authSource=test') | asList(createScramSha1Credential('jeff', 'test', '123'.toCharArray()))
244+
'&authSource=test') | createScramSha1Credential('jeff', 'test', '123'.toCharArray())
244245
new ConnectionString('mongodb://jeff@localhost/?' +
245-
'authMechanism=GSSAPI') | asList(createGSSAPICredential('jeff'))
246+
'authMechanism=GSSAPI') | createGSSAPICredential('jeff')
246247
new ConnectionString('mongodb://jeff:123@localhost/?' +
247-
'authMechanism=PLAIN') | asList(createPlainCredential('jeff', 'admin', '123'.toCharArray()))
248+
'authMechanism=PLAIN') | createPlainCredential('jeff', 'admin', '123'.toCharArray())
248249
new ConnectionString('mongodb://jeff@localhost/?' +
249-
'authMechanism=MONGODB-X509') | asList(createMongoX509Credential('jeff'))
250+
'authMechanism=MONGODB-X509') | createMongoX509Credential('jeff')
250251
new ConnectionString('mongodb://localhost/?' +
251-
'authMechanism=MONGODB-X509') | asList(createMongoX509Credential())
252+
'authMechanism=MONGODB-X509') | createMongoX509Credential()
252253
new ConnectionString('mongodb://jeff@localhost/?' +
253254
'authMechanism=GSSAPI' +
254-
'&gssapiServiceName=foo') | asList(createGSSAPICredential('jeff')
255-
.withMechanismProperty('SERVICE_NAME', 'foo'))
255+
'&gssapiServiceName=foo') | createGSSAPICredential('jeff')
256+
.withMechanismProperty('SERVICE_NAME', 'foo')
256257
new ConnectionString('mongodb://jeff@localhost/?' +
257258
'authMechanism=GSSAPI' +
258259
'&authMechanismProperties=' +
259-
'SERVICE_NAME:foo') | asList(createGSSAPICredential('jeff')
260-
.withMechanismProperty('SERVICE_NAME', 'foo'))
260+
'SERVICE_NAME:foo') | createGSSAPICredential('jeff')
261+
.withMechanismProperty('SERVICE_NAME', 'foo')
261262
new ConnectionString('mongodb://jeff@localhost/?' +
262263
'authMechanism=GSSAPI' +
263264
'&authMechanismProperties=' +
264-
'SERVICE_NAME :foo') | asList(createGSSAPICredential('jeff')
265-
.withMechanismProperty('SERVICE_NAME', 'foo'))
265+
'SERVICE_NAME :foo') | createGSSAPICredential('jeff')
266+
.withMechanismProperty('SERVICE_NAME', 'foo')
266267
new ConnectionString('mongodb://jeff@localhost/?' +
267268
'authMechanism=GSSAPI' +
268269
'&authMechanismProperties=' +
269270
'SERVICE_NAME:foo,' +
270271
'CANONICALIZE_HOST_NAME:true,' +
271-
'SERVICE_REALM:AWESOME') | asList(createGSSAPICredential('jeff')
272+
'SERVICE_REALM:AWESOME') | createGSSAPICredential('jeff')
272273
.withMechanismProperty('SERVICE_NAME', 'foo')
273274
.withMechanismProperty('CANONICALIZE_HOST_NAME', true)
274-
.withMechanismProperty('SERVICE_REALM', 'AWESOME'))
275+
.withMechanismProperty('SERVICE_REALM', 'AWESOME')
275276
}
276277

277278
def 'should ignore authSource if there is no credential'() {

driver-core/src/test/unit/com/mongodb/ConnectionStringTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.client;
17+
package com.mongodb;
1818

19-
import com.mongodb.AuthenticationMechanism;
20-
import com.mongodb.ConnectionString;
21-
import com.mongodb.MongoCredential;
2219
import junit.framework.TestCase;
2320
import org.bson.BsonDocument;
2421
import org.bson.BsonValue;
@@ -125,7 +122,7 @@ private void testValidOptions() {
125122
for (Map.Entry<String, BsonValue> option : definition.getDocument("options").entrySet()) {
126123
if (option.getKey().equals("authmechanism")) {
127124
String expected = option.getValue().asString().getValue();
128-
String actual = connectionString.getCredentialList().get(0).getAuthenticationMechanism().getMechanismName();
125+
String actual = connectionString.getCredential().getAuthenticationMechanism().getMechanismName();
129126
assertEquals(expected, actual);
130127
} else if (option.getKey().equals("replicaset")) {
131128
String expected = option.getValue().asString().getValue();
@@ -161,9 +158,8 @@ private void testValidAuth() {
161158
if (connectionString.getPassword() != null) {
162159
password = new String(connectionString.getPassword());
163160
}
164-
List<MongoCredential> credentials = connectionString.getCredentialList();
165-
if (credentials.size() > 0) {
166-
AuthenticationMechanism mechanism = credentials.get(0).getAuthenticationMechanism();
161+
if (connectionString.getCredential() != null) {
162+
AuthenticationMechanism mechanism = connectionString.getCredential().getAuthenticationMechanism();
167163
if (mechanism == null) {
168164
assertString("auth.password", password);
169165
} else {

driver/src/main/com/mongodb/MongoClientURI.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,7 @@ public String getURI() {
275275
* @return the credentials
276276
*/
277277
public MongoCredential getCredentials() {
278-
if (proxied.getCredentialList().isEmpty()) {
279-
return null;
280-
} else {
281-
return proxied.getCredentialList().get(0);
282-
}
278+
return proxied.getCredential();
283279
}
284280

285281
/**

0 commit comments

Comments
 (0)