Polish LinksEnhancer to use endpoint name

See gh-7164
See gh-7132
This commit is contained in:
Phillip Webb 2016-11-15 18:02:57 -08:00
parent ada02232b9
commit 449b42ffa0
2 changed files with 37 additions and 27 deletions

View File

@ -16,15 +16,14 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
import org.springframework.hateoas.ResourceSupport; import org.springframework.hateoas.ResourceSupport;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; 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}. * Adds endpoint links to {@link ResourceSupport}.
* *
* @author Dave Syer * @author Dave Syer
* @author Madhura Bhave
*/ */
class LinksEnhancer { class LinksEnhancer {
@ -50,26 +50,29 @@ class LinksEnhancer {
resource.add(linkTo(LinksEnhancer.class).slash(this.rootPath + self) resource.add(linkTo(LinksEnhancer.class).slash(this.rootPath + self)
.withSelfRel()); .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()) { for (MvcEndpoint endpoint : this.endpoints.getEndpoints()) {
if (!endpoint.getPath().equals(self)) {
String rel = getRel(endpoint); String rel = getRel(endpoint);
List<String> pathsForRel = added.get(rel) == null ? new ArrayList<String>() : added.get(rel); List<String> paths = added.get(rel);
if (paths == null || !paths.contains(endpoint.getPath())) {
if (!endpoint.getPath().equals(self) && !pathsForRel.contains(endpoint.getPath())) { addEndpointLink(resource, endpoint, rel);
addEndpointLink(resource, endpoint, rel); added.add(rel, endpoint.getPath());
pathsForRel.add(endpoint.getPath()); }
added.put(rel, pathsForRel);
} }
} }
} }
private String getRel(MvcEndpoint endpoint) { private String getRel(MvcEndpoint endpoint) {
String name = endpoint instanceof NamedMvcEndpoint ? ((NamedMvcEndpoint) endpoint).getName() : endpoint.getPath(); if (endpoint instanceof NamedMvcEndpoint) {
return (name.startsWith("/") ? name.substring(1) : name); 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(); Class<?> type = endpoint.getEndpointType();
type = (type == null ? Object.class : type); type = (type == null ? Object.class : type);
if (StringUtils.hasText(rel)) { if (StringUtils.hasText(rel)) {

View File

@ -55,7 +55,8 @@ public class LinksEnhancerTests {
public void useNameAsRelIfAvailable() throws Exception { public void useNameAsRelIfAvailable() throws Exception {
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a")); TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
endpoint.setPath("something-else"); endpoint.setPath("something-else");
LinksEnhancer enhancer = getLinksEnhancer(Collections.singletonList((MvcEndpoint) endpoint)); LinksEnhancer enhancer = getLinksEnhancer(
Collections.singletonList((MvcEndpoint) endpoint));
ResourceSupport support = new ResourceSupport(); ResourceSupport support = new ResourceSupport();
enhancer.addEndpointLinks(support, ""); enhancer.addEndpointLinks(support, "");
assertThat(support.getLink("a").getHref()).contains("/something-else"); assertThat(support.getLink("a").getHref()).contains("/something-else");
@ -86,20 +87,13 @@ public class LinksEnhancerTests {
endpoint.setPath("endpoint"); endpoint.setPath("endpoint");
TestMvcEndpoint otherEndpoint = new TestMvcEndpoint(new TestEndpoint("a")); TestMvcEndpoint otherEndpoint = new TestMvcEndpoint(new TestEndpoint("a"));
otherEndpoint.setPath("other-endpoint"); otherEndpoint.setPath("other-endpoint");
LinksEnhancer enhancer = getLinksEnhancer(Arrays.asList((MvcEndpoint) endpoint, otherEndpoint)); LinksEnhancer enhancer = getLinksEnhancer(
Arrays.asList((MvcEndpoint) endpoint, otherEndpoint));
ResourceSupport support = new ResourceSupport(); ResourceSupport support = new ResourceSupport();
enhancer.addEndpointLinks(support, ""); enhancer.addEndpointLinks(support, "");
assertThat(support.getLinks()).haveExactly(1, getCondition("a", "endpoint")); assertThat(support.getLinks()).haveExactly(1, getCondition("a", "endpoint"));
assertThat(support.getLinks()).haveExactly(1, getCondition("a", "other-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);
}
};
} }
private LinksEnhancer getLinksEnhancer(List<MvcEndpoint> endpoints) throws Exception { private LinksEnhancer getLinksEnhancer(List<MvcEndpoint> endpoints) throws Exception {
@ -114,6 +108,18 @@ public class LinksEnhancerTests {
return new LinksEnhancer("", mvcEndpoints); 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> { private static class TestEndpoint extends AbstractEndpoint<Object> {
TestEndpoint(String id) { TestEndpoint(String id) {
@ -140,6 +146,7 @@ public class LinksEnhancerTests {
NoNameTestMvcEndpoint(String path, boolean sensitive) { NoNameTestMvcEndpoint(String path, boolean sensitive) {
super(path, sensitive); super(path, sensitive);
} }
} }
} }