Merge branch '2.0.x'
This commit is contained in:
@@ -16,10 +16,7 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint.web;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -30,7 +27,6 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* {@link ServletContextInitializer} to register {@link ExposableServletEndpoint servlet
|
||||
@@ -44,24 +40,14 @@ public class ServletEndpointRegistrar implements ServletContextInitializer {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ServletEndpointRegistrar.class);
|
||||
|
||||
private final Set<String> basePaths = new LinkedHashSet<>();
|
||||
private final String basePath;
|
||||
|
||||
private final Collection<ExposableServletEndpoint> servletEndpoints;
|
||||
|
||||
public ServletEndpointRegistrar(String basePath,
|
||||
Collection<ExposableServletEndpoint> servletEndpoints) {
|
||||
Assert.notNull(servletEndpoints, "ServletEndpoints must not be null");
|
||||
this.basePaths.add((basePath != null ? basePath : ""));
|
||||
this.servletEndpoints = servletEndpoints;
|
||||
}
|
||||
|
||||
public ServletEndpointRegistrar(Set<String> basePaths,
|
||||
Collection<ExposableServletEndpoint> servletEndpoints) {
|
||||
Assert.notNull(servletEndpoints, "ServletEndpoints must not be null");
|
||||
this.basePaths.addAll(basePaths);
|
||||
if (CollectionUtils.isEmpty(this.basePaths)) {
|
||||
this.basePaths.add("");
|
||||
}
|
||||
this.basePath = (basePath != null ? basePath : "");
|
||||
this.servletEndpoints = servletEndpoints;
|
||||
}
|
||||
|
||||
@@ -74,24 +60,14 @@ public class ServletEndpointRegistrar implements ServletContextInitializer {
|
||||
private void register(ServletContext servletContext,
|
||||
ExposableServletEndpoint endpoint) {
|
||||
String name = endpoint.getId() + "-actuator-endpoint";
|
||||
String path = this.basePath + "/" + endpoint.getRootPath();
|
||||
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
|
||||
EndpointServlet endpointServlet = endpoint.getEndpointServlet();
|
||||
Dynamic registration = servletContext.addServlet(name,
|
||||
endpointServlet.getServlet());
|
||||
String[] urlMappings = getUrlMappings(endpoint.getRootPath());
|
||||
registration.addMapping(urlMappings);
|
||||
if (logger.isInfoEnabled()) {
|
||||
Arrays.stream(urlMappings).forEach(
|
||||
(mapping) -> logger.info("Registered '" + mapping + "' to " + name));
|
||||
}
|
||||
registration.addMapping(urlMapping);
|
||||
registration.setInitParameters(endpointServlet.getInitParameters());
|
||||
}
|
||||
|
||||
private String[] getUrlMappings(String endpointPath) {
|
||||
return this.basePaths.stream()
|
||||
.map((basePath) -> (basePath != null ? basePath + "/" + endpointPath
|
||||
: "/" + endpointPath))
|
||||
.distinct().map((path) -> (path.endsWith("/") ? path + "*" : path + "/*"))
|
||||
.toArray(String[]::new);
|
||||
logger.info("Registered '" + path + "' to " + name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.boot.actuate.endpoint.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.GenericServlet;
|
||||
import javax.servlet.Servlet;
|
||||
@@ -49,7 +47,6 @@ import static org.mockito.Mockito.verify;
|
||||
* Tests for {@link ServletEndpointRegistrar}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class ServletEndpointRegistrarTests {
|
||||
|
||||
@@ -76,14 +73,14 @@ public class ServletEndpointRegistrarTests {
|
||||
public void createWhenServletEndpointsIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ServletEndpoints must not be null");
|
||||
new ServletEndpointRegistrar((String) null, null);
|
||||
new ServletEndpointRegistrar(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupShouldRegisterServlets() throws Exception {
|
||||
ExposableServletEndpoint endpoint = mockEndpoint(
|
||||
new EndpointServlet(TestServlet.class));
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar((String) null,
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(null,
|
||||
Collections.singleton(endpoint));
|
||||
registrar.onStartup(this.servletContext);
|
||||
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"),
|
||||
@@ -105,64 +102,6 @@ public class ServletEndpointRegistrarTests {
|
||||
verify(this.dynamic).addMapping("/actuator/test/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupWhenHasMultipleBasePathsShouldIncludeAllBasePaths()
|
||||
throws Exception {
|
||||
ExposableServletEndpoint endpoint = mockEndpoint(
|
||||
new EndpointServlet(TestServlet.class));
|
||||
Set<String> basePaths = new LinkedHashSet<>();
|
||||
basePaths.add("/actuator");
|
||||
basePaths.add("/admin");
|
||||
basePaths.add("/application");
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePaths,
|
||||
Collections.singleton(endpoint));
|
||||
registrar.onStartup(this.servletContext);
|
||||
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"),
|
||||
this.servlet.capture());
|
||||
assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class);
|
||||
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
|
||||
verify(this.dynamic).addMapping(captor.capture());
|
||||
assertThat(captor.getAllValues()).containsExactlyInAnyOrder("/application/test/*",
|
||||
"/admin/test/*", "/actuator/test/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupWhenHasEmptyBasePathsShouldIncludeRoot() throws Exception {
|
||||
ExposableServletEndpoint endpoint = mockEndpoint(
|
||||
new EndpointServlet(TestServlet.class));
|
||||
Set<String> basePaths = Collections.emptySet();
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePaths,
|
||||
Collections.singleton(endpoint));
|
||||
registrar.onStartup(this.servletContext);
|
||||
verify(this.dynamic).addMapping("/test/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupWhenHasBasePathsHasNullValueShouldIncludeRoot()
|
||||
throws Exception {
|
||||
ExposableServletEndpoint endpoint = mockEndpoint(
|
||||
new EndpointServlet(TestServlet.class));
|
||||
Set<String> basePaths = new LinkedHashSet<>();
|
||||
basePaths.add(null);
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePaths,
|
||||
Collections.singleton(endpoint));
|
||||
registrar.onStartup(this.servletContext);
|
||||
verify(this.dynamic).addMapping("/test/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupWhenDuplicateValuesShouldIncludeDistinct() throws Exception {
|
||||
ExposableServletEndpoint endpoint = mockEndpoint(
|
||||
new EndpointServlet(TestServlet.class));
|
||||
Set<String> basePaths = new LinkedHashSet<>();
|
||||
basePaths.add("");
|
||||
basePaths.add(null);
|
||||
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePaths,
|
||||
Collections.singleton(endpoint));
|
||||
registrar.onStartup(this.servletContext);
|
||||
verify(this.dynamic).addMapping("/test/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartupWhenHasInitParametersShouldRegisterInitParameters()
|
||||
throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user