17
17
import redis .clients .jedis .JedisPubSub ;
18
18
19
19
import java .util .function .Consumer ;
20
+ import java .util .regex .Matcher ;
21
+ import java .util .regex .Pattern ;
20
22
21
23
@ Getter
22
24
@ Setter
23
25
@ FieldDefaults (level = AccessLevel .PRIVATE )
24
26
public class RedisAPI {
25
-
26
- private static final String REDIS_FULL_URI_PATTERN = "rediss?:\\ /\\ /\\ w+:[\\ w-]+@[\\ w.-]+:\\ d+" ;
27
+ private static final String REDIS_FULL_URI_PATTERN = "rediss?:\\ /\\ /(?:(?<user>\\ w+)?:(?<password>[\\ w-]+)@)?(?<host>[\\ w.-]+):(?<port>\\ d+)" ;
27
28
private static final String REDIS_URI_PATTERN = "rediss?:\\ /\\ /[\\ w.-]+:\\ d+" ;
28
29
29
30
@ Getter
@@ -83,27 +84,26 @@ public static RedisAPI generateInstance(@NonNull RedisCredentials credentials) {
83
84
* @return main instance of the api.RedisAPI
84
85
*/
85
86
public static RedisAPI generateInstance (@ NonNull String uri ) {
86
- String user = null , password = null , target ;
87
-
88
- if (uri .matches (REDIS_FULL_URI_PATTERN )) {
89
- String [] parts = uri .split ("//" )[1 ].split ("@" );
87
+ String user = null , password = null , host , target ;
88
+ int port ;
90
89
91
- String credentials = parts [ 0 ] ;
92
- target = parts [ 1 ] ;
90
+ Pattern pattern = java . util . regex . Pattern . compile ( REDIS_FULL_URI_PATTERN ) ;
91
+ Matcher matcher = pattern . matcher ( uri ) ;
93
92
94
- user = credentials .split (":" )[0 ];
95
- password = credentials .split (":" )[1 ];
93
+ if (matcher .matches ()) {
94
+ user = matcher .group ("user" );
95
+ password = matcher .group ("password" );
96
+ host = matcher .group ("host" );
97
+ port = Integer .parseInt (matcher .group ("port" ));
96
98
} else if (uri .matches (REDIS_URI_PATTERN )) {
97
99
target = uri .split ("//" )[1 ];
100
+ host = target .split (":" )[0 ];
101
+ port = Integer .parseInt (target .split (":" )[1 ]);
98
102
} else {
99
103
throw new CouldNotConnectToRedisException ("Invalid Redis URI passed through; '" + uri + "'" );
100
104
}
101
105
102
- String host = target .split (":" )[0 ];
103
- int port = Integer .parseInt (target .split (":" )[1 ]);
104
-
105
106
boolean ssl = uri .startsWith ("rediss" );
106
-
107
107
return generateInstance (new RedisCredentials (host , port , user , password , ssl ));
108
108
}
109
109
@@ -115,18 +115,22 @@ public static RedisAPI generateInstance(@NonNull String uri) {
115
115
* @return main instance of the api.RedisAPI
116
116
*/
117
117
public static RedisAPI generateInstance (@ NonNull String uri , String password ) {
118
- if (!uri .matches (REDIS_URI_PATTERN )) {
119
- throw new CouldNotConnectToRedisException ("Invalid Redis URI passed through; '" + uri + "'" );
120
- }
118
+ String user = null , host , target ;
119
+ int port ;
121
120
122
- String target = uri .split ("//" )[1 ];
121
+ java .util .regex .Pattern pattern = java .util .regex .Pattern .compile (REDIS_URI_PATTERN );
122
+ java .util .regex .Matcher matcher = pattern .matcher (uri );
123
123
124
- String host = target .split (":" )[0 ];
125
- int port = Integer .parseInt (target .split (":" )[1 ]);
124
+ if (matcher .matches ()) {
125
+ host = matcher .group ("host" );
126
+ port = Integer .parseInt (matcher .group ("port" ));
127
+ } else {
128
+ throw new CouldNotConnectToRedisException ("Invalid Redis URI passed through; '" + uri + "'" );
129
+ }
126
130
127
131
boolean ssl = uri .startsWith ("rediss" );
128
132
129
- return generateInstance (new RedisCredentials (host , port , password , ssl ));
133
+ return generateInstance (new RedisCredentials (host , port , user , password , ssl ));
130
134
}
131
135
132
136
/**
0 commit comments