From a18c2f8f7ecc3256722697810ac07effac70b6c0 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 15 Jan 2025 09:25:10 +0800 Subject: [PATCH 1/2] Polish PropertiesRedisConnectionDetails See gh-43825 Signed-off-by: Yanming Zhou --- .../data/redis/PropertiesRedisConnectionDetails.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java index 458baa938191..afef1f0d2dab 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails { @Override public String getUsername() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); + ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); return connectionInfo.getUsername(); } return this.properties.getUsername(); @@ -48,7 +48,7 @@ public String getUsername() { @Override public String getPassword() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); + ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); return connectionInfo.getPassword(); } return this.properties.getPassword(); @@ -57,17 +57,13 @@ public String getPassword() { @Override public Standalone getStandalone() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); + ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(), this.properties.getDatabase()); } return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase()); } - private ConnectionInfo connectionInfo(String url) { - return (url != null) ? RedisConnectionConfiguration.parseUrl(url) : null; - } - @Override public Sentinel getSentinel() { org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel sentinel = this.properties From 0f94530e15d1350be9557a27ac34404bf12fa618 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 15 Jan 2025 11:56:02 +0000 Subject: [PATCH 2/2] Polish "Polish PropertiesRedisConnectionDetails" See gh-43825 --- .../PropertiesRedisConnectionDetails.java | 6 +- .../redis/RedisConnectionConfiguration.java | 64 +++++++++---------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java index afef1f0d2dab..18371b3200fd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java @@ -39,7 +39,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails { @Override public String getUsername() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); + ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl()); return connectionInfo.getUsername(); } return this.properties.getUsername(); @@ -48,7 +48,7 @@ public String getUsername() { @Override public String getPassword() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); + ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl()); return connectionInfo.getPassword(); } return this.properties.getPassword(); @@ -57,7 +57,7 @@ public String getPassword() { @Override public Standalone getStandalone() { if (this.properties.getUrl() != null) { - ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl()); + ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl()); return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(), this.properties.getDatabase()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java index 8d25cc49d130..4b19d54550b4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -172,42 +172,14 @@ private List createSentinels(Sentinel sentinel) { } protected final boolean urlUsesSsl() { - return parseUrl(this.properties.getUrl()).isUseSsl(); + return ConnectionInfo.of(this.properties.getUrl()).isUseSsl(); } protected final RedisConnectionDetails getConnectionDetails() { return this.connectionDetails; } - static ConnectionInfo parseUrl(String url) { - try { - URI uri = new URI(url); - String scheme = uri.getScheme(); - if (!"redis".equals(scheme) && !"rediss".equals(scheme)) { - throw new RedisUrlSyntaxException(url); - } - boolean useSsl = ("rediss".equals(scheme)); - String username = null; - String password = null; - if (uri.getUserInfo() != null) { - String candidate = uri.getUserInfo(); - int index = candidate.indexOf(':'); - if (index >= 0) { - username = candidate.substring(0, index); - password = candidate.substring(index + 1); - } - else { - password = candidate; - } - } - return new ConnectionInfo(uri, useSsl, username, password); - } - catch (URISyntaxException ex) { - throw new RedisUrlSyntaxException(url, ex); - } - } - - static class ConnectionInfo { + static final class ConnectionInfo { private final URI uri; @@ -217,7 +189,7 @@ static class ConnectionInfo { private final String password; - ConnectionInfo(URI uri, boolean useSsl, String username, String password) { + private ConnectionInfo(URI uri, boolean useSsl, String username, String password) { this.uri = uri; this.useSsl = useSsl; this.username = username; @@ -240,6 +212,34 @@ String getPassword() { return this.password; } + static ConnectionInfo of(String url) { + try { + URI uri = new URI(url); + String scheme = uri.getScheme(); + if (!"redis".equals(scheme) && !"rediss".equals(scheme)) { + throw new RedisUrlSyntaxException(url); + } + boolean useSsl = ("rediss".equals(scheme)); + String username = null; + String password = null; + if (uri.getUserInfo() != null) { + String candidate = uri.getUserInfo(); + int index = candidate.indexOf(':'); + if (index >= 0) { + username = candidate.substring(0, index); + password = candidate.substring(index + 1); + } + else { + password = candidate; + } + } + return new ConnectionInfo(uri, useSsl, username, password); + } + catch (URISyntaxException ex) { + throw new RedisUrlSyntaxException(url, ex); + } + } + } }