Skip to content

Commit d5ea336

Browse files
Merge pull request #6 from Neruxov/master
New URI parsers, refactoring
2 parents 95a7199 + c6dcd83 commit d5ea336

File tree

4 files changed

+133
-32
lines changed

4 files changed

+133
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.swofty.redisapi.api;
22

33
public enum ChannelFunctionType {
4-
CONSUMER(),
5-
CLASS(),
6-
;
4+
CONSUMER,
5+
CLASS
76
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package net.swofty.redisapi.api;
22

3-
import net.swofty.redisapi.events.EventRegistry;
3+
import lombok.experimental.UtilityClass;
44
import net.swofty.redisapi.exceptions.ChannelAlreadyRegisteredException;
55
import net.swofty.redisapi.exceptions.ChannelNotRegisteredException;
66
import lombok.NonNull;
77

88
import java.util.ArrayList;
9-
import java.util.Objects;
109

10+
@UtilityClass
1111
public class ChannelRegistry {
1212

13-
public static ArrayList<RedisChannel> registeredChannels = new ArrayList<>();
13+
public ArrayList<RedisChannel> registeredChannels = new ArrayList<>();
1414

1515
/**
1616
* Used to receive a channel that has already been registered
@@ -19,16 +19,17 @@ public class ChannelRegistry {
1919
* @throws ChannelNotRegisteredException returns channelNotRegistered when you call this method upon a channel that does not exist
2020
*/
2121
@NonNull
22-
public static RedisChannel getFromName(String channelName) {
22+
public RedisChannel getFromName(String channelName) {
2323
return new RedisChannel(channelName, (e) -> {});
2424
// return registeredChannels.stream().filter(channel -> Objects.equals(channel.channelName, channelName)).findFirst().orElseThrow(() -> new ChannelNotRegisteredException("There is no channel registered with the name '" + channelName + "'"));
2525
}
2626

27-
public static void registerChannel(RedisChannel channel) {
27+
public void registerChannel(RedisChannel channel) {
2828
if (registeredChannels.stream().anyMatch(channel2 -> channel2.channelName.equals(channel.channelName)))
2929
throw new ChannelAlreadyRegisteredException("A channel already exists with this name '" + channel.channelName + "'");
3030

3131
registeredChannels.add(channel);
3232
// RedisAPI.getInstance().getPool().getResource().subscribe(EventRegistry.pubSub, channel.channelName);
3333
}
34+
3435
}

src/main/java/net/swofty/redisapi/api/RedisAPI.java

+85-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.swofty.redisapi.api;
22

3+
import lombok.AccessLevel;
34
import lombok.NonNull;
5+
import lombok.experimental.FieldDefaults;
46
import net.swofty.redisapi.events.EventRegistry;
57
import net.swofty.redisapi.events.RedisMessagingReceiveEvent;
68
import net.swofty.redisapi.events.RedisMessagingReceiveInterface;
@@ -16,48 +18,117 @@
1618

1719
import java.util.function.Consumer;
1820

21+
@Getter
22+
@Setter
23+
@FieldDefaults(level = AccessLevel.PRIVATE)
1924
public class RedisAPI {
2025

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+
2129
@Getter
22-
public static RedisAPI instance = null;
23-
@Getter
30+
private static RedisAPI instance = null;
31+
2432
@Setter
25-
private JedisPool pool;
26-
@Getter
33+
JedisPool pool;
34+
2735
@Setter
28-
private String filterId;
36+
String filterId;
2937

3038
/**
3139
* 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
3441
* @exception CouldNotConnectToRedisException signifies either an issue with the password passed through or the URI that is passed through the method
3542
* @return main instance of the api.RedisAPI
3643
*/
37-
public static RedisAPI generateInstance(String uri, String password) {
44+
public static RedisAPI generateInstance(@NonNull RedisCredentials credentials) {
3845
RedisAPI api = new RedisAPI();
3946

4047
if (instance != null) {
4148
instance.getPool().close();
4249
}
4350

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();
4658

4759
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);
5066
} else {
51-
api.setPool(new JedisPool(new JedisPoolConfig(), host, port, 20, password));
67+
pool = new JedisPool(new JedisPoolConfig(), host, port, 2000, ssl);
5268
}
69+
70+
api.setPool(pool);
5371
} 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 + "'");
5573
}
5674

5775
instance = api;
5876
return api;
5977
}
6078

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+
61132
/**
62133
* Starts listeners for the Redis Pub/Sub channels
63134
*/
@@ -78,16 +149,6 @@ public void onMessage(String channel, String message) {
78149
}).start();
79150
}
80151

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-
91152
/**
92153
* This method is used to set a filter ID onto your Redis Pool, you can then use this when publishing messages to
93154
* a channel to ensure that a specific RedisAPI instance receives your message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.swofty.redisapi.api;
2+
3+
import lombok.*;
4+
import lombok.experimental.FieldDefaults;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
9+
public class RedisCredentials {
10+
11+
@NonNull
12+
String host;
13+
14+
@NonNull
15+
int port;
16+
17+
String user;
18+
String password;
19+
20+
boolean ssl;
21+
22+
public RedisCredentials(@NonNull String host, @NonNull int port, @NonNull String password, boolean ssl) {
23+
this.host = host;
24+
this.port = port;
25+
this.password = password;
26+
this.user = null;
27+
this.ssl = ssl;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "RedisCredentials(" +
33+
"host=" + this.getHost() + ", " +
34+
"port=" + this.getPort() + ", " +
35+
"user=" + this.getUser() + ", " +
36+
"password=" + this.getPassword() +
37+
")";
38+
}
39+
40+
}

0 commit comments

Comments
 (0)