1717import redis .clients .jedis .JedisPubSub ;
1818
1919import java .util .function .Consumer ;
20+ import java .util .regex .Matcher ;
21+ import java .util .regex .Pattern ;
2022
2123@ Getter
2224@ Setter
2325@ FieldDefaults (level = AccessLevel .PRIVATE )
2426public 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+)" ;
2728 private static final String REDIS_URI_PATTERN = "rediss?:\\ /\\ /[\\ w.-]+:\\ d+" ;
2829
2930 @ Getter
@@ -83,27 +84,26 @@ public static RedisAPI generateInstance(@NonNull RedisCredentials credentials) {
8384 * @return main instance of the api.RedisAPI
8485 */
8586 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 ;
9089
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 ) ;
9392
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" ));
9698 } else if (uri .matches (REDIS_URI_PATTERN )) {
9799 target = uri .split ("//" )[1 ];
100+ host = target .split (":" )[0 ];
101+ port = Integer .parseInt (target .split (":" )[1 ]);
98102 } else {
99103 throw new CouldNotConnectToRedisException ("Invalid Redis URI passed through; '" + uri + "'" );
100104 }
101105
102- String host = target .split (":" )[0 ];
103- int port = Integer .parseInt (target .split (":" )[1 ]);
104-
105106 boolean ssl = uri .startsWith ("rediss" );
106-
107107 return generateInstance (new RedisCredentials (host , port , user , password , ssl ));
108108 }
109109
@@ -115,18 +115,22 @@ public static RedisAPI generateInstance(@NonNull String uri) {
115115 * @return main instance of the api.RedisAPI
116116 */
117117 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 ;
121120
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 );
123123
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+ }
126130
127131 boolean ssl = uri .startsWith ("rediss" );
128132
129- return generateInstance (new RedisCredentials (host , port , password , ssl ));
133+ return generateInstance (new RedisCredentials (host , port , user , password , ssl ));
130134 }
131135
132136 /**
0 commit comments