Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RESP3 protocol through URI/URL #3403

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public Jedis(URI uri) {
connection = new Connection(new HostAndPort(uri.getHost(), uri.getPort()),
DefaultJedisClientConfig.builder().user(JedisURIHelper.getUser(uri))
.password(JedisURIHelper.getPassword(uri)).database(JedisURIHelper.getDBIndex(uri))
.protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build());
}

Expand Down Expand Up @@ -203,6 +204,7 @@ public Jedis(final URI uri, JedisClientConfig config) {
.blockingSocketTimeoutMillis(config.getBlockingSocketTimeoutMillis())
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).clientName(config.getClientName())
.protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(config.getSslSocketFactory())
.sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier())
.build());
Expand Down
1 change: 1 addition & 0 deletions src/main/java/redis/clients/jedis/JedisFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected JedisFactory(final URI uri, final int connectionTimeout, final int soT
.socketTimeoutMillis(soTimeout).blockingSocketTimeoutMillis(infiniteSoTimeout)
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).clientName(clientName)
.protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(sslSocketFactory)
.sslParameters(sslParameters).hostnameVerifier(hostnameVerifier).build();
this.jedisSocketFactory = new DefaultJedisSocketFactory(new HostAndPort(uri.getHost(), uri.getPort()), this.clientConfig);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/redis/clients/jedis/JedisPooled.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,13 @@ public JedisPooled(final GenericObjectPoolConfig<Connection> poolConfig, final U
final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout,
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
this(new HostAndPort(uri.getHost(), uri.getPort()), DefaultJedisClientConfig.create(
connectionTimeout, soTimeout, infiniteSoTimeout, JedisURIHelper.getUser(uri),
JedisURIHelper.getPassword(uri), JedisURIHelper.getDBIndex(uri), null,
JedisURIHelper.isRedisSSLScheme(uri), sslSocketFactory, sslParameters, hostnameVerifier,
null), poolConfig);
this(new HostAndPort(uri.getHost(), uri.getPort()), DefaultJedisClientConfig.builder()
.connectionTimeoutMillis(connectionTimeout).socketTimeoutMillis(soTimeout)
.blockingSocketTimeoutMillis(infiniteSoTimeout).user(JedisURIHelper.getUser(uri))
.password(JedisURIHelper.getPassword(uri)).database(JedisURIHelper.getDBIndex(uri))
.protocol(JedisURIHelper.getRedisProtocol(uri)).ssl(JedisURIHelper.isRedisSSLScheme(uri))
.sslSocketFactory(sslSocketFactory).sslParameters(sslParameters)
.hostnameVerifier(hostnameVerifier).build(), poolConfig);
}

public JedisPooled(final HostAndPort hostAndPort, final GenericObjectPoolConfig<Connection> poolConfig) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public UnifiedJedis(final String url) {
public UnifiedJedis(final URI uri) {
this(JedisURIHelper.getHostAndPort(uri), DefaultJedisClientConfig.builder()
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).ssl(JedisURIHelper.isRedisSSLScheme(uri)).build());
.database(JedisURIHelper.getDBIndex(uri)).protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build());
}

public UnifiedJedis(final URI uri, JedisClientConfig config) {
Expand All @@ -75,6 +76,7 @@ public UnifiedJedis(final URI uri, JedisClientConfig config) {
.blockingSocketTimeoutMillis(config.getBlockingSocketTimeoutMillis())
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).clientName(config.getClientName())
.protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(config.getSslSocketFactory())
.sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier())
.build());
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/redis/clients/jedis/util/JedisURIHelper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package redis.clients.jedis.util;

import java.net.URI;
import java.util.Locale;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.RedisProtocol;

public final class JedisURIHelper {

private static final int DEFAULT_DB = 0;

private static final String REDIS = "redis";
private static final String REDISS = "rediss";

Expand Down Expand Up @@ -43,12 +44,40 @@ public static int getDBIndex(URI uri) {
if (pathSplit.length > 1) {
String dbIndexStr = pathSplit[1];
if (dbIndexStr.isEmpty()) {
return DEFAULT_DB;
return Protocol.DEFAULT_DATABASE;
}
return Integer.parseInt(dbIndexStr);
} else {
return DEFAULT_DB;
return Protocol.DEFAULT_DATABASE;
}
}

public static RedisProtocol getRedisProtocol(URI uri) {
if (uri.getQuery() == null) return null;

String[] pairs = uri.getQuery().split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
//if ("protocol".equals(URLDecoder.decode(pair.substring(0, idx), "UTF-8").toLowerCase(Locale.ENGLISH))) {
if ("protocol".equals(pair.substring(0, idx).toLowerCase(Locale.ENGLISH))) {
//String val = URLDecoder.decode(pair.substring(idx + 1), "UTF-8").toUpperCase(Locale.ENGLISH);
String val = pair.substring(idx + 1).toUpperCase(Locale.ENGLISH);
if (val.length() > 1) {
return RedisProtocol.valueOf(val);
} else if (val.length() == 1) {
int ver = Integer.parseInt(val);
for (RedisProtocol proto : RedisProtocol.values()) {
if (proto.version() == ver) {
return proto;
}
}
throw new IllegalArgumentException("Unknown protocol " + ver);
} else {
return null; // null (default) when not defined
}
}
}
return null; // null (default) when not defined
}

public static boolean isValid(URI uri) {
Expand Down
32 changes: 2 additions & 30 deletions src/test/java/redis/clients/jedis/ACLJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,14 @@ public void startWithUri() throws URISyntaxException {
assertEquals("OK", j.select(2));
j.set("foo", "bar");
}
try (Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))) {
assertEquals("PONG", j2.ping());
assertEquals("bar", j2.get("foo"));
}
}

@Test
public void connectWithURICredentials() throws URISyntaxException {
jedis.set("foo", "bar");

try (Jedis j1 = new Jedis(new URI("redis://default:foobared@localhost:6379"))) {
try (Jedis j1 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))) {
assertEquals("PONG", j1.ping());
assertEquals("bar", j1.get("foo"));
}

try (Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379"))) {
try (Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))) {
assertEquals("PONG", j2.ping());
assertEquals("bar", j2.get("foo"));
}
}

@Test
public void allowUrlWithNoDBAndNoPassword() {
try (Jedis j1 = new Jedis("redis://localhost:6379")) {
assertEquals("OK", j1.auth("acljedis", "fizzbuzz"));
// assertEquals("localhost", j1.getClient().getHost());
// assertEquals(6379, j1.getClient().getPort());
assertEquals(0, j1.getDB());
}

try (Jedis j2 = new Jedis("redis://localhost:6379/")) {
assertEquals("OK", j2.auth("acljedis", "fizzbuzz"));
// assertEquals("localhost", j2.getClient().getHost());
// assertEquals(6379, j2.getClient().getPort());
assertEquals(0, j2.getDB());
}
}

}
42 changes: 36 additions & 6 deletions src/test/java/redis/clients/jedis/JedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,20 @@ public String getPassword() {
}

@Test
public void resp3Protocol() {
public void connectOnResp3Protocol() {
try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder()
.protocol(RedisProtocol.RESP3).user("default").password("foobared").build())) {
.protocol(RedisProtocol.RESP3).password("foobared").build())) {
assertEquals("PONG", jedis.ping());
assertEquals(RedisProtocol.RESP3, jedis.getConnection().getRedisProtocol());
}
}

@Test
public void resp3Shortcut() {
public void connectOnResp3ProtocolShortcut() {
try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().resp3()
.user("default").password("foobared").build())) {
.password("foobared").build())) {
assertEquals("PONG", jedis.ping());
assertEquals(RedisProtocol.RESP3, jedis.getConnection().getRedisProtocol());
}
}

Expand Down Expand Up @@ -153,7 +155,7 @@ public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxExcept
// }

@Test
public void startWithUrl() {
public void connectWithUrl() {
try (Jedis j = new Jedis("localhost", 6380)) {
j.auth("foobared");
j.select(2);
Expand All @@ -167,7 +169,7 @@ public void startWithUrl() {
}

@Test
public void startWithUri() throws URISyntaxException {
public void connectWithUri() throws URISyntaxException {
try (Jedis j = new Jedis("localhost", 6380)) {
j.auth("foobared");
j.select(2);
Expand All @@ -180,6 +182,34 @@ public void startWithUri() throws URISyntaxException {
}
}

@Test
public void connectWithUrlOnResp3() {
try (Jedis j = new Jedis("localhost", 6380)) {
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
}

try (Jedis j2 = new Jedis("redis://:foobared@localhost:6380/2?protocol=3")) {
assertEquals("PONG", j2.ping());
assertEquals("bar", j2.get("foo"));
}
}

@Test
public void connectWithUriOnResp3() throws URISyntaxException {
try (Jedis j = new Jedis("localhost", 6380)) {
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
}

try (Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2?protocol=resp3"))) {
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
}
}

@Test
public void shouldNotUpdateDbIndexIfSelectFails() {
int currentDb = jedis.getDB();
Expand Down
26 changes: 2 additions & 24 deletions src/test/java/redis/clients/jedis/SSLACLJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,15 @@ public void connectWithSsl() {

@Test
public void connectWithConfig() {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisClientConfig
.builder().ssl(true).build())) {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390),
DefaultJedisClientConfig.builder().ssl(true).build())) {
jedis.auth("acljedis", "fizzbuzz");
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithUrl() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis("rediss://localhost:6390")) {
jedis.auth("default", "foobared");
assertEquals("PONG", jedis.ping());
}
try (Jedis jedis = new Jedis("rediss://localhost:6390")) {
jedis.auth("acljedis", "fizzbuzz");
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithCompleteCredentialsUrl() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) {
assertEquals("PONG", jedis.ping());
Expand All @@ -75,15 +62,6 @@ public void connectWithCompleteCredentialsUrl() {

@Test
public void connectWithUri() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) {
jedis.auth("acljedis", "fizzbuzz");
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithCompleteCredentialsUri() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) {
assertEquals("PONG", jedis.ping());
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/redis/clients/jedis/SSLJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void connectWithSsl() {

@Test
public void connectWithConfig() {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisClientConfig
.builder().ssl(true).build())) {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390),
DefaultJedisClientConfig.builder().ssl(true).build())) {
jedis.auth("foobared");
assertEquals("PONG", jedis.ping());
}
Expand All @@ -63,11 +63,11 @@ public void connectWithConfig() {
public void connectWithConfigInterface() {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390),
new JedisClientConfig() {
@Override
public boolean isSsl() {
return true;
}
})) {
@Override
public boolean isSsl() {
return true;
}
})) {
jedis.auth("foobared");
assertEquals("PONG", jedis.ping());
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/redis/clients/jedis/util/JedisURIHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package redis.clients.jedis.util;

import static redis.clients.jedis.util.JedisURIHelper.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
import redis.clients.jedis.RedisProtocol;

public class JedisURIHelperTest {

Expand Down Expand Up @@ -57,4 +60,21 @@ public void shouldValidateInvalidURIs() throws URISyntaxException {
assertFalse(JedisURIHelper.isValid(new URI("redis://host/0")));
}

@Test
public void shouldGetDefaultProtocolWhenNotDefined() {
assertNull(getRedisProtocol(URI.create("redis://host:1234")));
assertNull(getRedisProtocol(URI.create("redis://host:1234/1")));
}

@Test
public void shouldGetProtocolFromShortDefinition() {
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234?protocol=3")));
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234/1?protocol=3")));
}

@Test
public void shouldGetProtocolFromLongDefinition() {
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234?protocol=resp3")));
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(RedisProtocol.RESP3, getRedisProtocol(URI.create("redis://host:1234/1?protocol=resp3")));
}
}