From 449b42ffa06b5ebfb953ff63a241683560ba6a1f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 15 Nov 2016 18:02:57 -0800 Subject: [PATCH] Polish LinksEnhancer to use endpoint name See gh-7164 See gh-7132 --- .../actuate/autoconfigure/LinksEnhancer.java | 33 ++++++++++--------- .../autoconfigure/LinksEnhancerTests.java | 31 ++++++++++------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancer.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancer.java index 71d5a5b28e8..b9a97d47808 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancer.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancer.java @@ -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> added = new HashMap>(); + MultiValueMap added = new LinkedMultiValueMap(); for (MvcEndpoint endpoint : this.endpoints.getEndpoints()) { - - String rel = getRel(endpoint); - List pathsForRel = added.get(rel) == null ? new ArrayList() : 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 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)) { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancerTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancerTests.java index 19cebdecf76..a443143b2bf 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancerTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancerTests.java @@ -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 getCondition(final String rel, final String href) { - return new Condition() { - @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 endpoints) throws Exception { @@ -114,6 +108,18 @@ public class LinksEnhancerTests { return new LinksEnhancer("", mvcEndpoints); } + private Condition getCondition(final String rel, final String href) { + return new Condition() { + + @Override + public boolean matches(Link link) { + return link.getRel().equals(rel) + && link.getHref().equals("http://localhost/" + href); + } + + }; + } + private static class TestEndpoint extends AbstractEndpoint { TestEndpoint(String id) { @@ -140,6 +146,7 @@ public class LinksEnhancerTests { NoNameTestMvcEndpoint(String path, boolean sensitive) { super(path, sensitive); } + } }