Commit 0be8a302 authored by Madhura Bhave's avatar Madhura Bhave Committed by Phillip Webb

Add EndpointHandlerMapping.getEndpoints(Class)

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

See gh-7108
parent 7f1ff968
......@@ -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
......
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment