Skip to content

Commit 0f94530

Browse files
committed
Polish "Polish PropertiesRedisConnectionDetails"
See gh-43825
1 parent a18c2f8 commit 0f94530

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3939
@Override
4040
public String getUsername() {
4141
if (this.properties.getUrl() != null) {
42-
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
42+
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
4343
return connectionInfo.getUsername();
4444
}
4545
return this.properties.getUsername();
@@ -48,7 +48,7 @@ public String getUsername() {
4848
@Override
4949
public String getPassword() {
5050
if (this.properties.getUrl() != null) {
51-
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
51+
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
5252
return connectionInfo.getPassword();
5353
}
5454
return this.properties.getPassword();
@@ -57,7 +57,7 @@ public String getPassword() {
5757
@Override
5858
public Standalone getStandalone() {
5959
if (this.properties.getUrl() != null) {
60-
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
60+
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
6161
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
6262
this.properties.getDatabase());
6363
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,42 +172,14 @@ private List<RedisNode> createSentinels(Sentinel sentinel) {
172172
}
173173

174174
protected final boolean urlUsesSsl() {
175-
return parseUrl(this.properties.getUrl()).isUseSsl();
175+
return ConnectionInfo.of(this.properties.getUrl()).isUseSsl();
176176
}
177177

178178
protected final RedisConnectionDetails getConnectionDetails() {
179179
return this.connectionDetails;
180180
}
181181

182-
static ConnectionInfo parseUrl(String url) {
183-
try {
184-
URI uri = new URI(url);
185-
String scheme = uri.getScheme();
186-
if (!"redis".equals(scheme) && !"rediss".equals(scheme)) {
187-
throw new RedisUrlSyntaxException(url);
188-
}
189-
boolean useSsl = ("rediss".equals(scheme));
190-
String username = null;
191-
String password = null;
192-
if (uri.getUserInfo() != null) {
193-
String candidate = uri.getUserInfo();
194-
int index = candidate.indexOf(':');
195-
if (index >= 0) {
196-
username = candidate.substring(0, index);
197-
password = candidate.substring(index + 1);
198-
}
199-
else {
200-
password = candidate;
201-
}
202-
}
203-
return new ConnectionInfo(uri, useSsl, username, password);
204-
}
205-
catch (URISyntaxException ex) {
206-
throw new RedisUrlSyntaxException(url, ex);
207-
}
208-
}
209-
210-
static class ConnectionInfo {
182+
static final class ConnectionInfo {
211183

212184
private final URI uri;
213185

@@ -217,7 +189,7 @@ static class ConnectionInfo {
217189

218190
private final String password;
219191

220-
ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
192+
private ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
221193
this.uri = uri;
222194
this.useSsl = useSsl;
223195
this.username = username;
@@ -240,6 +212,34 @@ String getPassword() {
240212
return this.password;
241213
}
242214

215+
static ConnectionInfo of(String url) {
216+
try {
217+
URI uri = new URI(url);
218+
String scheme = uri.getScheme();
219+
if (!"redis".equals(scheme) && !"rediss".equals(scheme)) {
220+
throw new RedisUrlSyntaxException(url);
221+
}
222+
boolean useSsl = ("rediss".equals(scheme));
223+
String username = null;
224+
String password = null;
225+
if (uri.getUserInfo() != null) {
226+
String candidate = uri.getUserInfo();
227+
int index = candidate.indexOf(':');
228+
if (index >= 0) {
229+
username = candidate.substring(0, index);
230+
password = candidate.substring(index + 1);
231+
}
232+
else {
233+
password = candidate;
234+
}
235+
}
236+
return new ConnectionInfo(uri, useSsl, username, password);
237+
}
238+
catch (URISyntaxException ex) {
239+
throw new RedisUrlSyntaxException(url, ex);
240+
}
241+
}
242+
243243
}
244244

245245
}

0 commit comments

Comments
 (0)