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
This commit is contained in:
parent
72b88790f0
commit
9abca48a7f
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -38,6 +38,7 @@ import org.springframework.web.servlet.resource.TransformedResource;
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint
|
public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint
|
||||||
|
@ -142,15 +143,15 @@ public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint
|
||||||
resource = transformerChain.transform(request, resource);
|
resource = transformerChain.transform(request, resource);
|
||||||
if (resource.getFilename().equalsIgnoreCase(
|
if (resource.getFilename().equalsIgnoreCase(
|
||||||
HalBrowserMvcEndpoint.this.location.getHtmlFile())) {
|
HalBrowserMvcEndpoint.this.location.getHtmlFile())) {
|
||||||
return replaceInitialLink(resource);
|
return replaceInitialLink(request.getContextPath(), resource);
|
||||||
}
|
}
|
||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource replaceInitialLink(Resource resource) throws IOException {
|
private Resource replaceInitialLink(String contextPath, Resource resource) throws IOException {
|
||||||
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
|
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
|
||||||
String content = new String(bytes, DEFAULT_CHARSET);
|
String content = new String(bytes, DEFAULT_CHARSET);
|
||||||
String initial = getManagementServletContext().getContextPath() + getPath();
|
String initial = contextPath + getManagementServletContext().getContextPath() + getPath();
|
||||||
content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'");
|
content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'");
|
||||||
return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET));
|
return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET));
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
@ -88,6 +90,13 @@ public class HalBrowserMvcEndpointManagementContextPathIntegrationTests {
|
||||||
.andExpect(forwardedUrl("/admin/browser.html"));
|
.andExpect(forwardedUrl("/admin/browser.html"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void actuatorBrowserHtml() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/admin/browser.html").accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(containsString("entryPoint: '/admin'")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void trace() throws Exception {
|
public void trace() throws Exception {
|
||||||
this.mockMvc.perform(get("/admin/trace").accept(MediaType.APPLICATION_JSON))
|
this.mockMvc.perform(get("/admin/trace").accept(MediaType.APPLICATION_JSON))
|
||||||
|
|
|
@ -85,6 +85,18 @@ public class HalBrowserMvcEndpointServerContextPathIntegrationTests {
|
||||||
entity.getBody().contains("<title"));
|
entity.getBody().contains("<title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void actuatorBrowserEntryPoint() throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||||
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
|
"http://localhost:" + this.port + "/spring/actuator/browser.html", HttpMethod.GET,
|
||||||
|
new HttpEntity<Void>(null, headers), String.class);
|
||||||
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
|
assertTrue("Wrong body: " + entity.getBody(),
|
||||||
|
entity.getBody().contains("entryPoint: '/spring/actuator'"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void actuatorLinks() throws Exception {
|
public void actuatorLinks() throws Exception {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
Loading…
Reference in New Issue