Add EndpointHandlerMapping.getEndpoints(Class)

Add an additional method to EndpointHandlerMapping which allows
endpoints of a specific type to be returned.

See gh-7108
This commit is contained in:
Madhura Bhave 2016-10-11 14:40:18 -07:00 committed by Phillip Webb
parent 7f1ff968a1
commit 0be8a30276
2 changed files with 31 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.boot.actuate.endpoint.mvc;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -196,9 +197,29 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping {
/**
* Return the endpoints.
* @return the endpoints
* @see #getEndpoints(Class)
*/
public Set<? extends MvcEndpoint> getEndpoints() {
return new HashSet<MvcEndpoint>(this.endpoints);
return getEndpoints(MvcEndpoint.class);
}
/**
* Return the endpoints of the specified type.
* @param <E> the endpoint type
* @param type the endpoint type
* @return the endpoints
* @see #getEndpoints()
* @since 1.5.0
*/
@SuppressWarnings("unchecked")
public <E extends MvcEndpoint> Set<E> getEndpoints(Class<E> type) {
Set<E> result = new HashSet<E>(this.endpoints.size());
for (MvcEndpoint candidate : this.endpoints) {
if (type.isInstance(candidate)) {
result.add((E) candidate);
}
}
return Collections.unmodifiableSet(result);
}
@Override

View File

@ -136,6 +136,15 @@ public class EndpointHandlerMappingTests {
assertThat(mapping.getHandler(request("POST", "/a"))).isNull();
}
@Test
public void getEndpointsForSpecifiedType() throws Exception {
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
TestActionEndpoint other = new TestActionEndpoint(new TestEndpoint("b"));
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint, other));
assertThat(mapping.getEndpoints(TestMvcEndpoint.class)).containsExactly(endpoint);
}
private MockHttpServletRequest request(String method, String requestURI) {
return new MockHttpServletRequest(method, requestURI);
}