31
31
* Parse a {@link String} to a Collection of {@link MongoCredential}.
32
32
*
33
33
* @author Christoph Strobl
34
+ * @author Oliver Gierke
34
35
* @since 1.7
35
36
*/
36
37
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -95,16 +96,16 @@ public void setAsText(String text) throws IllegalArgumentException {
95
96
credentials .add (MongoCredential .createScramSha1Credential (userNameAndPassword [0 ], database ,
96
97
userNameAndPassword [1 ].toCharArray ()));
97
98
} else {
98
- throw new IllegalArgumentException (String . format (
99
- "Cannot create MongoCredentials for unknown auth mechanism '%s'!" , authMechanism ));
99
+ throw new IllegalArgumentException (
100
+ String . format ( "Cannot create MongoCredentials for unknown auth mechanism '%s'!" , authMechanism ));
100
101
}
101
102
}
102
103
} else {
103
104
104
105
verifyUsernameAndPasswordPresent (userNameAndPassword );
105
106
verifyDatabasePresent (database );
106
- credentials .add (MongoCredential . createCredential ( userNameAndPassword [ 0 ], database ,
107
- userNameAndPassword [1 ].toCharArray ()));
107
+ credentials .add (
108
+ MongoCredential . createCredential ( userNameAndPassword [ 0 ], database , userNameAndPassword [1 ].toCharArray ()));
108
109
}
109
110
}
110
111
@@ -114,8 +115,8 @@ public void setAsText(String text) throws IllegalArgumentException {
114
115
private List <String > extractCredentialsString (String source ) {
115
116
116
117
Matcher matcher = GROUP_PATTERN .matcher (source );
117
-
118
118
List <String > list = new ArrayList <String >();
119
+
119
120
while (matcher .find ()) {
120
121
121
122
String value = StringUtils .trimLeadingCharacter (matcher .group (), '\'' );
@@ -125,21 +126,17 @@ private List<String> extractCredentialsString(String source) {
125
126
if (!list .isEmpty ()) {
126
127
return list ;
127
128
}
129
+
128
130
return Arrays .asList (source .split ("," ));
129
131
}
130
132
131
133
private static String [] extractUserNameAndPassword (String text ) {
132
134
133
135
int index = text .lastIndexOf (DATABASE_DELIMINATOR );
134
136
135
- if (index == -1 ) {
136
- index = text .lastIndexOf (OPTIONS_DELIMINATOR );
137
- }
138
- if (index == -1 ) {
139
- return new String [] {};
140
- }
141
- String userNameAndPassword = text .substring (0 , index );
142
- return userNameAndPassword .split (USERNAME_PASSWORD_DELIMINATOR );
137
+ index = index != -1 ? index : text .lastIndexOf (OPTIONS_DELIMINATOR );
138
+
139
+ return index == -1 ? new String [] {} : text .substring (0 , index ).split (USERNAME_PASSWORD_DELIMINATOR );
143
140
}
144
141
145
142
private static String extractDB (String text ) {
@@ -175,26 +172,27 @@ private static Properties extractOptions(String text) {
175
172
return properties ;
176
173
}
177
174
178
- private void verifyUserNamePresent (String [] source ) {
179
-
180
- if (source .length == 0 || !StringUtils .hasText (source [0 ])) {
181
- throw new IllegalArgumentException ("Credentials need to specify username!" );
182
- }
183
- }
184
-
185
- private void verifyUsernameAndPasswordPresent (String [] source ) {
175
+ private static void verifyUsernameAndPasswordPresent (String [] source ) {
186
176
187
177
verifyUserNamePresent (source );
178
+
188
179
if (source .length != 2 ) {
189
180
throw new IllegalArgumentException (
190
181
"Credentials need to specify username and password like in 'username:password@database'!" );
191
182
}
192
183
}
193
184
194
- private void verifyDatabasePresent (String source ) {
185
+ private static void verifyDatabasePresent (String source ) {
195
186
196
187
if (!StringUtils .hasText (source )) {
197
188
throw new IllegalArgumentException ("Credentials need to specify database like in 'username:password@database'!" );
198
189
}
199
190
}
191
+
192
+ private static void verifyUserNamePresent (String [] source ) {
193
+
194
+ if (source .length == 0 || !StringUtils .hasText (source [0 ])) {
195
+ throw new IllegalArgumentException ("Credentials need to specify username!" );
196
+ }
197
+ }
200
198
}
0 commit comments