Skip to content

Commit 9abca48

Browse files
committed
Fix HAL browser entry point with contextPath
Previously, if the `contextPath` of the application wasn't the root, the HAL browser could not initialize since the `entryPoint` was referring to an invalid location. This commit makes sure to take the `contextPath` into account. Closes gh-5814
1 parent 72b8879 commit 9abca48

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpoint.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -38,6 +38,7 @@
3838
* @author Dave Syer
3939
* @author Phillip Webb
4040
* @author Andy Wilkinson
41+
* @author Stephane Nicoll
4142
* @since 1.3.0
4243
*/
4344
public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint
@@ -142,15 +143,15 @@ public Resource transform(HttpServletRequest request, Resource resource,
142143
resource = transformerChain.transform(request, resource);
143144
if (resource.getFilename().equalsIgnoreCase(
144145
HalBrowserMvcEndpoint.this.location.getHtmlFile())) {
145-
return replaceInitialLink(resource);
146+
return replaceInitialLink(request.getContextPath(), resource);
146147
}
147148
return resource;
148149
}
149150

150-
private Resource replaceInitialLink(Resource resource) throws IOException {
151+
private Resource replaceInitialLink(String contextPath, Resource resource) throws IOException {
151152
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
152153
String content = new String(bytes, DEFAULT_CHARSET);
153-
String initial = getManagementServletContext().getContextPath() + getPath();
154+
String initial = contextPath + getManagementServletContext().getContextPath() + getPath();
154155
content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'");
155156
return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET));
156157
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointManagementContextPathIntegrationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.springframework.web.bind.annotation.RestController;
3737
import org.springframework.web.context.WebApplicationContext;
3838

39+
import static org.hamcrest.CoreMatchers.containsString;
3940
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
4041
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
4143
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
4244
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
4345
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -88,6 +90,13 @@ public void actuatorHomeHtml() throws Exception {
8890
.andExpect(forwardedUrl("/admin/browser.html"));
8991
}
9092

93+
@Test
94+
public void actuatorBrowserHtml() throws Exception {
95+
this.mockMvc.perform(get("/admin/browser.html").accept(MediaType.APPLICATION_JSON))
96+
.andExpect(status().isOk())
97+
.andExpect(content().string(containsString("entryPoint: '/admin'")));
98+
}
99+
91100
@Test
92101
public void trace() throws Exception {
93102
this.mockMvc.perform(get("/admin/trace").accept(MediaType.APPLICATION_JSON))

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public void actuatorBrowser() throws Exception {
8585
entity.getBody().contains("<title"));
8686
}
8787

88+
@Test
89+
public void actuatorBrowserEntryPoint() throws Exception {
90+
HttpHeaders headers = new HttpHeaders();
91+
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
92+
ResponseEntity<String> entity = new TestRestTemplate().exchange(
93+
"http://localhost:" + this.port + "/spring/actuator/browser.html", HttpMethod.GET,
94+
new HttpEntity<Void>(null, headers), String.class);
95+
assertEquals(HttpStatus.OK, entity.getStatusCode());
96+
assertTrue("Wrong body: " + entity.getBody(),
97+
entity.getBody().contains("entryPoint: '/spring/actuator'"));
98+
}
99+
88100
@Test
89101
public void actuatorLinks() throws Exception {
90102
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)