1
1
package net .swofty .redisapi .api ;
2
2
3
+ import lombok .AccessLevel ;
3
4
import lombok .NonNull ;
5
+ import lombok .experimental .FieldDefaults ;
4
6
import net .swofty .redisapi .events .EventRegistry ;
5
7
import net .swofty .redisapi .events .RedisMessagingReceiveEvent ;
6
8
import net .swofty .redisapi .events .RedisMessagingReceiveInterface ;
16
18
17
19
import java .util .function .Consumer ;
18
20
21
+ @ Getter
22
+ @ Setter
23
+ @ FieldDefaults (level = AccessLevel .PRIVATE )
19
24
public class RedisAPI {
20
25
26
+ private static final String REDIS_FULL_URI_PATTERN = "rediss?:\\ /\\ /\\ w+:[\\ w-]+@[\\ w.-]+:\\ d+" ;
27
+ private static final String REDIS_URI_PATTERN = "rediss?:\\ /\\ /[\\ w.-]+:\\ d+" ;
28
+
21
29
@ Getter
22
- public static RedisAPI instance = null ;
23
- @ Getter
30
+ private static RedisAPI instance = null ;
31
+
24
32
@ Setter
25
- private JedisPool pool ;
26
- @ Getter
33
+ JedisPool pool ;
34
+
27
35
@ Setter
28
- private String filterId ;
36
+ String filterId ;
29
37
30
38
/**
31
39
* Creates a new main Redis pool instance, there will only ever be one at a time so #getInstance should be used after generation
32
- * @param uri the URI used to connect to the Redis server running
33
- * @param password the password used to connect to the Redis server running
40
+ * @param credentials the credentials used to connect to the Redis server running, instanceof api.RedisCredentials
34
41
* @exception CouldNotConnectToRedisException signifies either an issue with the password passed through or the URI that is passed through the method
35
42
* @return main instance of the api.RedisAPI
36
43
*/
37
- public static RedisAPI generateInstance (String uri , String password ) {
44
+ public static RedisAPI generateInstance (@ NonNull RedisCredentials credentials ) {
38
45
RedisAPI api = new RedisAPI ();
39
46
40
47
if (instance != null ) {
41
48
instance .getPool ().close ();
42
49
}
43
50
44
- String host = uri .split ("//" )[1 ].split (":" )[0 ];
45
- int port = Integer .parseInt (uri .split ("//" )[1 ].split (":" )[1 ]);
51
+ String host = credentials .getHost ();
52
+ int port = credentials .getPort ();
53
+
54
+ String password = credentials .getPassword ();
55
+ String user = credentials .getUser ();
56
+
57
+ boolean ssl = credentials .isSsl ();
46
58
47
59
try {
48
- if (password == null ) {
49
- api .setPool (new JedisPool (new JedisPoolConfig (), host , port , 20 ));
60
+ JedisPool pool ;
61
+
62
+ if (user != null ) {
63
+ pool = new JedisPool (new JedisPoolConfig (), host , port , 2000 , user , password , ssl );
64
+ } else if (password != null ) {
65
+ pool = new JedisPool (new JedisPoolConfig (), host , port , 2000 , password , ssl );
50
66
} else {
51
- api . setPool ( new JedisPool (new JedisPoolConfig (), host , port , 20 , password ) );
67
+ pool = new JedisPool (new JedisPoolConfig (), host , port , 2000 , ssl );
52
68
}
69
+
70
+ api .setPool (pool );
53
71
} catch (Exception e ) {
54
- throw new CouldNotConnectToRedisException ("Either invalid Redis URI passed through; '" + uri + "' OR invalid Redis Password passed through; '" + password + "'" );
72
+ throw new CouldNotConnectToRedisException ("Either invalid Redis Credentials passed through; '" + credentials + "' OR invalid Redis Password passed through; '" + password + "'" );
55
73
}
56
74
57
75
instance = api ;
58
76
return api ;
59
77
}
60
78
79
+ /**
80
+ * Creates a new main Redis pool instance, there will only ever be one at a time so #getInstance should be used after generation
81
+ * @param uri the URI used to connect to the Redis server running (e.g. redis://username:password@localhost:6379 or redis://localhost:6379)
82
+ * @exception CouldNotConnectToRedisException signifies either an issue with the password passed through or the URI that is passed through the method
83
+ * @return main instance of the api.RedisAPI
84
+ */
85
+ 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 ("@" );
90
+
91
+ String credentials = parts [0 ];
92
+ target = parts [1 ];
93
+
94
+ user = credentials .split (":" )[0 ];
95
+ password = credentials .split (":" )[1 ];
96
+ } else if (uri .matches (REDIS_URI_PATTERN )) {
97
+ target = uri .split ("//" )[1 ];
98
+ } else {
99
+ throw new CouldNotConnectToRedisException ("Invalid Redis URI passed through; '" + uri + "'" );
100
+ }
101
+
102
+ String host = target .split (":" )[0 ];
103
+ int port = Integer .parseInt (target .split (":" )[1 ]);
104
+
105
+ boolean ssl = uri .startsWith ("rediss" );
106
+
107
+ return generateInstance (new RedisCredentials (host , port , user , password , ssl ));
108
+ }
109
+
110
+ /**
111
+ * Creates a new main Redis pool instance, there will only ever be one at a time so #getInstance should be used after generation
112
+ * @param uri the URI used to connect to the Redis server running (e.g. redis://localhost:6379)
113
+ * @param password the password used to connect to the Redis server running
114
+ * @exception CouldNotConnectToRedisException signifies either an issue with the password passed through or the URI that is passed through the method
115
+ * @return main instance of the api.RedisAPI
116
+ */
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
+ }
121
+
122
+ String target = uri .split ("//" )[1 ];
123
+
124
+ String host = target .split (":" )[0 ];
125
+ int port = Integer .parseInt (target .split (":" )[1 ]);
126
+
127
+ boolean ssl = uri .startsWith ("rediss" );
128
+
129
+ return generateInstance (new RedisCredentials (host , port , password , ssl ));
130
+ }
131
+
61
132
/**
62
133
* Starts listeners for the Redis Pub/Sub channels
63
134
*/
@@ -78,16 +149,6 @@ public void onMessage(String channel, String message) {
78
149
}).start ();
79
150
}
80
151
81
- /**
82
- * Creates a new main Redis pool instance, there will only ever be one at a time so #getInstance should be used after generation
83
- * @param uri the URI used to connect to the Redis server running
84
- * @exception CouldNotConnectToRedisException signifies either an issue with the password passed through or the URI that is passed through the method
85
- * @return main instance of the api.RedisAPI
86
- */
87
- public static RedisAPI generateInstance (String uri ) {
88
- return generateInstance (uri , null );
89
- }
90
-
91
152
/**
92
153
* This method is used to set a filter ID onto your Redis Pool, you can then use this when publishing messages to
93
154
* a channel to ensure that a specific RedisAPI instance receives your message
0 commit comments