Skip to content

Commit 5b709a7

Browse files
author
Johannes Stelzer
committed
Better normalization of paths
1 parent c52a06e commit 5b709a7

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
2323
import org.springframework.context.ApplicationEvent;
2424
import org.springframework.context.ApplicationListener;
25+
import org.springframework.util.StringUtils;
2526

2627
import de.codecentric.boot.admin.model.Application;
2728
import de.codecentric.boot.admin.registry.ApplicationRegistry;
@@ -81,28 +82,32 @@ public void discover() {
8182
}
8283

8384
private Application convert(ServiceInstance instance) {
84-
String managementUrl = instance.getUri()
85-
.resolve(managementContextPath)
86-
.toString();
87-
String serviceUrl = instance.getUri()
88-
.resolve(serviceContextPath)
89-
.toString();
90-
String healthUrl = managementUrl + "/" + healthEndpoint;
85+
String serviceUrl = append(instance.getUri().toString(), serviceContextPath);
86+
String managementUrl = append(instance.getUri().toString(), managementContextPath);
87+
String healthUrl = append(managementUrl, healthEndpoint);
9188

9289
return new Application(healthUrl, managementUrl, serviceUrl, instance.getServiceId());
9390
}
9491

9592
public void setManagementContextPath(String managementContextPath) {
96-
this.managementContextPath = managementContextPath.startsWith("/") ? managementContextPath
97-
: "/" + managementContextPath;
93+
this.managementContextPath = managementContextPath;
9894
}
9995

10096
public void setServiceContextPath(String serviceContextPath) {
101-
this.serviceContextPath = serviceContextPath.startsWith("/") ? serviceContextPath
102-
: "/" + serviceContextPath;
97+
this.serviceContextPath = serviceContextPath;
10398
}
10499

105100
public void setHealthEndpoint(String healthEndpoint) {
106101
this.healthEndpoint = healthEndpoint;
107102
}
103+
104+
private String append(String uri, String path) {
105+
String baseUri = uri.replaceFirst("/+$", "");
106+
if (StringUtils.isEmpty(path)) {
107+
return baseUri;
108+
}
109+
110+
String normPath = path.replaceFirst("^/+", "").replaceFirst("/+$", "");
111+
return baseUri + "/" + normPath;
112+
}
108113
}

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/AdminClientProperties.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,16 @@ private String getHostname() {
176176

177177
private String createLocalUri(int port, String path) {
178178
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https" : "http";
179-
return append(scheme + "://" + getHostname() + ":" + port + "/", path);
179+
return append(scheme + "://" + getHostname() + ":" + port, path);
180180
}
181181

182182
private String append(String uri, String path) {
183+
String baseUri = uri.replaceFirst("/+$", "");
183184
if (StringUtils.isEmpty(path)) {
184-
return uri;
185+
return baseUri;
185186
}
186187

187-
String baseUri = uri.endsWith("/") ? uri.replaceFirst("/+$", "") : uri;
188-
return baseUri + (path.startsWith("/") ? "" : "/") + path
189-
+ (path.endsWith("/") ? "" : "/");
188+
String normPath = path.replaceFirst("^/+", "").replaceFirst("/+$", "");
189+
return baseUri + "/" + normPath;
190190
}
191191
}

spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/config/AdminClientPropertiesTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public void test_mgmtPortPath() {
7070
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
7171

7272
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname()
73-
+ ":8081/admin/"));
73+
+ ":8081/admin"));
7474
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname()
75-
+ ":8081/admin/alive/"));
75+
+ ":8081/admin/alive"));
7676
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
77-
+ ":8080/"));
77+
+ ":8080"));
7878
}
7979

8080
@Test
@@ -87,11 +87,11 @@ public void test_mgmtPort() {
8787
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
8888

8989
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname()
90-
+ ":8081/"));
90+
+ ":8081"));
9191
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname()
92-
+ ":8081/health/"));
92+
+ ":8081/health"));
9393
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
94-
+ ":8080/"));
94+
+ ":8080"));
9595
}
9696

9797
@Test
@@ -104,11 +104,11 @@ public void test_contextPath_mgmtPath() {
104104
publishServletContainerInitializedEvent(clientProperties, 8080, null);
105105

106106
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname()
107-
+ ":8080/app/admin/"));
107+
+ ":8080/app/admin"));
108108
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname()
109-
+ ":8080/app/admin/health/"));
109+
+ ":8080/app/admin/health"));
110110
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
111-
+ ":8080/app/"));
111+
+ ":8080/app"));
112112
}
113113

114114

@@ -121,11 +121,11 @@ public void test_contextPath() {
121121
publishServletContainerInitializedEvent(clientProperties, 80, null);
122122

123123
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname()
124-
+ ":80/app/"));
124+
+ ":80/app"));
125125
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname()
126-
+ ":80/app/health/"));
126+
+ ":80/app/health"));
127127
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
128-
+ ":80/app/"));
128+
+ ":80/app"));
129129
}
130130

131131

@@ -139,11 +139,11 @@ public void test_default() {
139139
publishServletContainerInitializedEvent(clientProperties, 8080, null);
140140

141141
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname()
142-
+ ":8080/"));
142+
+ ":8080"));
143143
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname()
144-
+ ":8080/health/"));
144+
+ ":8080/health"));
145145
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
146-
+ ":8080/"));
146+
+ ":8080"));
147147
}
148148

149149
@Test
@@ -155,11 +155,11 @@ public void testSsl() {
155155
publishServletContainerInitializedEvent(clientProperties, 8080, null);
156156

157157
assertThat(clientProperties.getManagementUrl(), is("https://" + getHostname()
158-
+ ":8080/"));
158+
+ ":8080"));
159159
assertThat(clientProperties.getHealthUrl(), is("https://" + getHostname()
160-
+ ":8080/health/"));
160+
+ ":8080/health"));
161161
assertThat(clientProperties.getServiceUrl(), is("https://" + getHostname()
162-
+ ":8080/"));
162+
+ ":8080"));
163163
}
164164

165165
private String getHostname() {

0 commit comments

Comments
 (0)