1
1
package redis .clients .jedis ;
2
2
3
+ import static redis .clients .jedis .util .SafeEncoder .encode ;
4
+
3
5
import java .io .Closeable ;
4
6
import java .io .IOException ;
5
7
import java .net .Socket ;
9
11
import java .util .ArrayList ;
10
12
import java .util .Arrays ;
11
13
import java .util .List ;
12
- import java .util .Map ;
13
14
import java .util .function .Supplier ;
14
15
15
16
import redis .clients .jedis .Protocol .Command ;
25
26
import redis .clients .jedis .util .JedisMetaInfo ;
26
27
import redis .clients .jedis .util .RedisInputStream ;
27
28
import redis .clients .jedis .util .RedisOutputStream ;
28
- import redis .clients .jedis .util .SafeEncoder ;
29
29
30
30
public class Connection implements Closeable {
31
31
@@ -270,14 +270,14 @@ public String getStatusCodeReply() {
270
270
if (null == resp ) {
271
271
return null ;
272
272
} else {
273
- return SafeEncoder . encode (resp );
273
+ return encode (resp );
274
274
}
275
275
}
276
276
277
277
public String getBulkReply () {
278
278
final byte [] result = getBinaryBulkReply ();
279
279
if (null != result ) {
280
- return SafeEncoder . encode (result );
280
+ return encode (result );
281
281
} else {
282
282
return null ;
283
283
}
@@ -391,44 +391,27 @@ private static boolean validateClientInfo(String info) {
391
391
private void initializeFromClientConfig (JedisClientConfig config ) {
392
392
try {
393
393
connect ();
394
- protocol = config .getRedisProtocol ();
395
-
396
- boolean doClientName = true ;
397
-
398
- /// HELLO and AUTH -->
399
- if (protocol == RedisProtocol .RESP3 && config .getUser () != null ) {
400
-
401
- hello (protocol , config .getUser (), config .getPassword (), config .getClientName ());
402
- doClientName = false ;
403
394
404
- } else {
405
-
406
- Supplier <RedisCredentials > credentialsProvider = config .getCredentialsProvider ();
407
- if (credentialsProvider instanceof RedisCredentialsProvider ) {
408
- try {
409
- ((RedisCredentialsProvider ) credentialsProvider ).prepare ();
410
- auth (credentialsProvider );
411
- } finally {
412
- ((RedisCredentialsProvider ) credentialsProvider ).cleanUp ();
413
- }
414
- } else {
415
- auth (credentialsProvider );
416
- }
395
+ protocol = config .getRedisProtocol ();
417
396
418
- if (protocol != null ) {
419
- hello (protocol );
397
+ final Supplier <RedisCredentials > credentialsProvider = config .getCredentialsProvider ();
398
+ if (credentialsProvider instanceof RedisCredentialsProvider ) {
399
+ final RedisCredentialsProvider redisCredentialsProvider = (RedisCredentialsProvider ) credentialsProvider ;
400
+ try {
401
+ redisCredentialsProvider .prepare ();
402
+ helloOrAuth (protocol , redisCredentialsProvider .get ());
403
+ } finally {
404
+ redisCredentialsProvider .cleanUp ();
420
405
}
421
- }
422
-
423
- int dbIndex = config .getDatabase ();
424
- if (dbIndex > 0 ) {
425
- select (dbIndex );
406
+ } else {
407
+ helloOrAuth (protocol , credentialsProvider != null ? credentialsProvider .get ()
408
+ : new DefaultRedisCredentials (config .getUser (), config .getPassword ()));
426
409
}
427
410
428
411
List <CommandArguments > fireAndForgetMsg = new ArrayList <>();
429
412
430
413
String clientName = config .getClientName ();
431
- if (doClientName && clientName != null && validateClientInfo (clientName )) {
414
+ if (clientName != null && validateClientInfo (clientName )) {
432
415
fireAndForgetMsg .add (new CommandArguments (Command .CLIENT ).add (Keyword .SETNAME ).add (clientName ));
433
416
}
434
417
@@ -448,6 +431,12 @@ private void initializeFromClientConfig(JedisClientConfig config) {
448
431
sendCommand (arg );
449
432
}
450
433
getMany (fireAndForgetMsg .size ());
434
+
435
+ int dbIndex = config .getDatabase ();
436
+ if (dbIndex > 0 ) {
437
+ select (dbIndex );
438
+ }
439
+
451
440
} catch (JedisException je ) {
452
441
try {
453
442
disconnect ();
@@ -458,58 +447,59 @@ private void initializeFromClientConfig(JedisClientConfig config) {
458
447
}
459
448
}
460
449
461
- private Map hello (final RedisProtocol protocol ) {
462
- sendCommand (Protocol .Command .HELLO , String .valueOf (protocol .version ()));
463
- Map reply = BuilderFactory .ENCODED_OBJECT_MAP .build (getOne ());
464
- // LoggerFactory.getLogger(Connection.class).info("HELLO reply: {}", reply);
465
- return reply ;
466
- }
450
+ private void helloOrAuth (final RedisProtocol protocol , final RedisCredentials credentials ) {
467
451
468
- private Map hello (final RedisProtocol protocol , final String user , final String password ,
469
- final String clientName ) {
470
- if (clientName == null ) {
471
- sendCommand (Protocol .Command .HELLO , String .valueOf (protocol .version ()),
472
- Protocol .Keyword .AUTH .name (), user , password );
473
- } else {
474
- sendCommand (Protocol .Command .HELLO , String .valueOf (protocol .version ()),
475
- Protocol .Keyword .AUTH .name (), user , password ,
476
- Protocol .Keyword .SETNAME .name (), clientName );
452
+ if (credentials == null || credentials .getPassword () == null ) {
453
+ if (protocol != null ) {
454
+ sendCommand (Command .HELLO , encode (protocol .version ()));
455
+ getOne ();
456
+ }
457
+ return ;
477
458
}
478
- Map reply = BuilderFactory .ENCODED_OBJECT_MAP .build (getOne ());
479
- // LoggerFactory.getLogger(Connection.class).info("HELLO reply: {}", reply);
480
- return reply ;
481
- }
482
-
483
- private void auth (final Supplier <RedisCredentials > credentialsProvider ) {
484
- RedisCredentials credentials = credentialsProvider .get ();
485
- if (credentials == null || credentials .getPassword () == null ) return ;
486
459
487
460
// Source: https://stackoverflow.com/a/9670279/4021802
488
461
ByteBuffer passBuf = Protocol .CHARSET .encode (CharBuffer .wrap (credentials .getPassword ()));
489
462
byte [] rawPass = Arrays .copyOfRange (passBuf .array (), passBuf .position (), passBuf .limit ());
490
463
Arrays .fill (passBuf .array (), (byte ) 0 ); // clear sensitive data
491
464
492
- if (credentials .getUser () != null ) {
493
- sendCommand (Protocol .Command .AUTH , SafeEncoder .encode (credentials .getUser ()), rawPass );
494
- } else {
495
- sendCommand (Protocol .Command .AUTH , rawPass );
496
- }
465
+ try {
466
+ /// actual HELLO or AUTH -->
467
+ if (protocol != null ) {
468
+ if (credentials .getUser () != null ) {
469
+ sendCommand (Command .HELLO , encode (protocol .version ()),
470
+ Keyword .AUTH .getRaw (), encode (credentials .getUser ()), rawPass );
471
+ getOne (); // Map
472
+ } else {
473
+ sendCommand (Command .AUTH , rawPass );
474
+ getStatusCodeReply (); // OK
475
+ sendCommand (Command .HELLO , encode (protocol .version ()));
476
+ getOne (); // Map
477
+ }
478
+ } else { // protocol == null
479
+ if (credentials .getUser () != null ) {
480
+ sendCommand (Command .AUTH , encode (credentials .getUser ()), rawPass );
481
+ } else {
482
+ sendCommand (Command .AUTH , rawPass );
483
+ }
484
+ getStatusCodeReply (); // OK
485
+ }
486
+ /// <-- actual HELLO or AUTH
487
+ } finally {
497
488
498
- Arrays .fill (rawPass , (byte ) 0 ); // clear sensitive data
489
+ Arrays .fill (rawPass , (byte ) 0 ); // clear sensitive data
490
+ }
499
491
500
492
// clearing 'char[] credentials.getPassword()' should be
501
493
// handled in RedisCredentialsProvider.cleanUp()
502
-
503
- getStatusCodeReply (); // OK
504
494
}
505
495
506
496
public String select (final int index ) {
507
- sendCommand (Protocol . Command .SELECT , Protocol .toByteArray (index ));
497
+ sendCommand (Command .SELECT , Protocol .toByteArray (index ));
508
498
return getStatusCodeReply ();
509
499
}
510
500
511
501
public boolean ping () {
512
- sendCommand (Protocol . Command .PING );
502
+ sendCommand (Command .PING );
513
503
String status = getStatusCodeReply ();
514
504
if (!"PONG" .equals (status )) {
515
505
throw new JedisException (status );
0 commit comments