mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Polish LinksEnhancer to use endpoint name
See gh-7164 See gh-7132
This commit is contained in:
parent
ada02232b9
commit
449b42ffa0
@ -16,15 +16,14 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
|
||||
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||
@ -33,6 +32,7 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||
* Adds endpoint links to {@link ResourceSupport}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class LinksEnhancer {
|
||||
|
||||
@ -50,26 +50,29 @@ class LinksEnhancer {
|
||||
resource.add(linkTo(LinksEnhancer.class).slash(this.rootPath + self)
|
||||
.withSelfRel());
|
||||
}
|
||||
Map<String, List<String>> added = new HashMap<String, List<String>>();
|
||||
MultiValueMap<String, String> added = new LinkedMultiValueMap<String, String>();
|
||||
for (MvcEndpoint endpoint : this.endpoints.getEndpoints()) {
|
||||
|
||||
String rel = getRel(endpoint);
|
||||
List<String> pathsForRel = added.get(rel) == null ? new ArrayList<String>() : added.get(rel);
|
||||
|
||||
if (!endpoint.getPath().equals(self) && !pathsForRel.contains(endpoint.getPath())) {
|
||||
addEndpointLink(resource, endpoint, rel);
|
||||
pathsForRel.add(endpoint.getPath());
|
||||
added.put(rel, pathsForRel);
|
||||
if (!endpoint.getPath().equals(self)) {
|
||||
String rel = getRel(endpoint);
|
||||
List<String> paths = added.get(rel);
|
||||
if (paths == null || !paths.contains(endpoint.getPath())) {
|
||||
addEndpointLink(resource, endpoint, rel);
|
||||
added.add(rel, endpoint.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getRel(MvcEndpoint endpoint) {
|
||||
String name = endpoint instanceof NamedMvcEndpoint ? ((NamedMvcEndpoint) endpoint).getName() : endpoint.getPath();
|
||||
return (name.startsWith("/") ? name.substring(1) : name);
|
||||
if (endpoint instanceof NamedMvcEndpoint) {
|
||||
return ((NamedMvcEndpoint) endpoint).getName();
|
||||
}
|
||||
String path = endpoint.getPath();
|
||||
return (path.startsWith("/") ? path.substring(1) : path);
|
||||
}
|
||||
|
||||
private void addEndpointLink(ResourceSupport resource, MvcEndpoint endpoint, String rel) {
|
||||
private void addEndpointLink(ResourceSupport resource, MvcEndpoint endpoint,
|
||||
String rel) {
|
||||
Class<?> type = endpoint.getEndpointType();
|
||||
type = (type == null ? Object.class : type);
|
||||
if (StringUtils.hasText(rel)) {
|
||||
|
@ -55,7 +55,8 @@ public class LinksEnhancerTests {
|
||||
public void useNameAsRelIfAvailable() throws Exception {
|
||||
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
|
||||
endpoint.setPath("something-else");
|
||||
LinksEnhancer enhancer = getLinksEnhancer(Collections.singletonList((MvcEndpoint) endpoint));
|
||||
LinksEnhancer enhancer = getLinksEnhancer(
|
||||
Collections.singletonList((MvcEndpoint) endpoint));
|
||||
ResourceSupport support = new ResourceSupport();
|
||||
enhancer.addEndpointLinks(support, "");
|
||||
assertThat(support.getLink("a").getHref()).contains("/something-else");
|
||||
@ -86,20 +87,13 @@ public class LinksEnhancerTests {
|
||||
endpoint.setPath("endpoint");
|
||||
TestMvcEndpoint otherEndpoint = new TestMvcEndpoint(new TestEndpoint("a"));
|
||||
otherEndpoint.setPath("other-endpoint");
|
||||
LinksEnhancer enhancer = getLinksEnhancer(Arrays.asList((MvcEndpoint) endpoint, otherEndpoint));
|
||||
LinksEnhancer enhancer = getLinksEnhancer(
|
||||
Arrays.asList((MvcEndpoint) endpoint, otherEndpoint));
|
||||
ResourceSupport support = new ResourceSupport();
|
||||
enhancer.addEndpointLinks(support, "");
|
||||
assertThat(support.getLinks()).haveExactly(1, getCondition("a", "endpoint"));
|
||||
assertThat(support.getLinks()).haveExactly(1, getCondition("a", "other-endpoint"));
|
||||
}
|
||||
|
||||
private Condition<Link> getCondition(final String rel, final String href) {
|
||||
return new Condition<Link>() {
|
||||
@Override
|
||||
public boolean matches(Link link) {
|
||||
return link.getRel().equals(rel) && link.getHref().equals("http://localhost/" + href);
|
||||
}
|
||||
};
|
||||
assertThat(support.getLinks()).haveExactly(1,
|
||||
getCondition("a", "other-endpoint"));
|
||||
}
|
||||
|
||||
private LinksEnhancer getLinksEnhancer(List<MvcEndpoint> endpoints) throws Exception {
|
||||
@ -114,6 +108,18 @@ public class LinksEnhancerTests {
|
||||
return new LinksEnhancer("", mvcEndpoints);
|
||||
}
|
||||
|
||||
private Condition<Link> getCondition(final String rel, final String href) {
|
||||
return new Condition<Link>() {
|
||||
|
||||
@Override
|
||||
public boolean matches(Link link) {
|
||||
return link.getRel().equals(rel)
|
||||
&& link.getHref().equals("http://localhost/" + href);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private static class TestEndpoint extends AbstractEndpoint<Object> {
|
||||
|
||||
TestEndpoint(String id) {
|
||||
@ -140,6 +146,7 @@ public class LinksEnhancerTests {
|
||||
NoNameTestMvcEndpoint(String path, boolean sensitive) {
|
||||
super(path, sensitive);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user