Browse Source

DATAMONGO-1257 - Polishing.

Made internal helper methods in MongoCredentialPropertyEditor static where possible.

Original pull request: #310.
pull/311/head
Oliver Gierke 11 years ago
parent
commit
df9a9f5fb6
  1. 42
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

42
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

@ -31,6 +31,7 @@ import com.mongodb.MongoCredential;
* Parse a {@link String} to a Collection of {@link MongoCredential}. * Parse a {@link String} to a Collection of {@link MongoCredential}.
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
* @since 1.7 * @since 1.7
*/ */
public class MongoCredentialPropertyEditor extends PropertyEditorSupport { public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@ -95,16 +96,16 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database, credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database,
userNameAndPassword[1].toCharArray())); userNameAndPassword[1].toCharArray()));
} else { } else {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(
"Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism)); String.format("Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));
} }
} }
} else { } else {
verifyUsernameAndPasswordPresent(userNameAndPassword); verifyUsernameAndPasswordPresent(userNameAndPassword);
verifyDatabasePresent(database); verifyDatabasePresent(database);
credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database, credentials.add(
userNameAndPassword[1].toCharArray())); MongoCredential.createCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray()));
} }
} }
@ -114,8 +115,8 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
private List<String> extractCredentialsString(String source) { private List<String> extractCredentialsString(String source) {
Matcher matcher = GROUP_PATTERN.matcher(source); Matcher matcher = GROUP_PATTERN.matcher(source);
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
while (matcher.find()) { while (matcher.find()) {
String value = StringUtils.trimLeadingCharacter(matcher.group(), '\''); String value = StringUtils.trimLeadingCharacter(matcher.group(), '\'');
@ -125,6 +126,7 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
if (!list.isEmpty()) { if (!list.isEmpty()) {
return list; return list;
} }
return Arrays.asList(source.split(",")); return Arrays.asList(source.split(","));
} }
@ -132,14 +134,9 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
int index = text.lastIndexOf(DATABASE_DELIMINATOR); int index = text.lastIndexOf(DATABASE_DELIMINATOR);
if (index == -1) { index = index != -1 ? index : text.lastIndexOf(OPTIONS_DELIMINATOR);
index = text.lastIndexOf(OPTIONS_DELIMINATOR);
} return index == -1 ? new String[] {} : text.substring(0, index).split(USERNAME_PASSWORD_DELIMINATOR);
if (index == -1) {
return new String[] {};
}
String userNameAndPassword = text.substring(0, index);
return userNameAndPassword.split(USERNAME_PASSWORD_DELIMINATOR);
} }
private static String extractDB(String text) { private static String extractDB(String text) {
@ -175,26 +172,27 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
return properties; return properties;
} }
private void verifyUserNamePresent(String[] source) { private static void verifyUsernameAndPasswordPresent(String[] source) {
if (source.length == 0 || !StringUtils.hasText(source[0])) {
throw new IllegalArgumentException("Credentials need to specify username!");
}
}
private void verifyUsernameAndPasswordPresent(String[] source) {
verifyUserNamePresent(source); verifyUserNamePresent(source);
if (source.length != 2) { if (source.length != 2) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Credentials need to specify username and password like in 'username:password@database'!"); "Credentials need to specify username and password like in 'username:password@database'!");
} }
} }
private void verifyDatabasePresent(String source) { private static void verifyDatabasePresent(String source) {
if (!StringUtils.hasText(source)) { if (!StringUtils.hasText(source)) {
throw new IllegalArgumentException("Credentials need to specify database like in 'username:password@database'!"); throw new IllegalArgumentException("Credentials need to specify database like in 'username:password@database'!");
} }
} }
private static void verifyUserNamePresent(String[] source) {
if (source.length == 0 || !StringUtils.hasText(source[0])) {
throw new IllegalArgumentException("Credentials need to specify username!");
}
}
} }

Loading…
Cancel
Save