Skip to content

Commit

Permalink
Remove throws exception when is reserved
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Sep 28, 2024
1 parent d6141da commit 5619a91
Showing 1 changed file with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
public class VertxRedisClientFactory {

public static final String DEFAULT_CLIENT = "<default>";
public static String NON_RESERVED_URI_PATTERN = "[^a-zA-Z0-9\\-_.~]";

private static final Logger LOGGER = Logger.getLogger(VertxRedisClientFactory.class);

Expand All @@ -56,8 +57,6 @@ public static Redis create(String name, Vertx vertx, RedisClientConfig config, T
RedisOptions options = new RedisOptions();

Consumer<Set<URI>> configureOptions = new Consumer<Set<URI>>() {
static String NON_RESERVED_PATTERN = "[^a-zA-Z0-9\\-_.~]";

@Override
public void accept(Set<URI> uris) {
for (URI uri : uris) {
Expand All @@ -70,32 +69,6 @@ public void accept(Set<URI> uris) {
}
}
}

public String validate(String input) {
if (input.matches(".*" + NON_RESERVED_PATTERN + ".*")) {
throw new IllegalArgumentException(
"Was not possible to configure the Redis connection string with the client " +
"query parameter: Only " + NON_RESERVED_PATTERN
+ " characters are supported, please provide a client " +
"name with a right pattern.");
}
return input;
}

private String applyClientQueryParam(String client, URI uri) {
validate(client);
String encodedClient = URLEncoder.encode(client, StandardCharsets.UTF_8);
String query = uri.getQuery() == null ? "client=" + encodedClient
: uri.getQuery() + "&client=" + encodedClient;
try {
return new URI(
uri.getScheme(), uri.getAuthority(), uri.getPath(), query, uri.getFragment()).toString().trim();
} catch (URISyntaxException e) {
LOGGER.warnf("Was not possible to generate a new Redis URL with client query parameter," +
"the value is: %s", client);
return uri.toString().trim();
}
}
};

List<URI> hosts = new ArrayList<>();
Expand Down Expand Up @@ -150,6 +123,31 @@ private String applyClientQueryParam(String client, URI uri) {
return Redis.createClient(vertx, options);
}

private static String applyClientQueryParam(String client, URI uri) {

if (client.matches(".*" + NON_RESERVED_URI_PATTERN + ".*")) {
LOGGER.warnf("The client query parameter contains reserved URI characters. " +
"This may result in an incorrect client name after URI encoding.");
}

String encodedClient = URLEncoder.encode(client, StandardCharsets.UTF_8);
String query = uri.getQuery() == null ? "client=" + encodedClient
: uri.getQuery() + "&client=" + encodedClient;
try {
return new URI(
uri.getScheme(), uri.getAuthority(), uri.getPath(), query, uri.getFragment()).toString().trim();
} catch (URISyntaxException e) {
LOGGER.warnf("Was not possible to generate a new Redis URL with client query parameter," +
"the value is: %s", client);
return uri.toString().trim();
}
}

public static String checkClient(String input) {

return input;
}

private static void customize(String name, RedisOptions options) {
if (Arc.container() != null) {
List<InstanceHandle<RedisOptionsCustomizer>> customizers = Arc.container().listAll(RedisOptionsCustomizer.class);
Expand Down

0 comments on commit 5619a91

Please sign in to comment.