Introduce "server.servlet" configuration prefix

This commit refactors the `ServerProperties` property keys and
introduces a separate "server.servlet" namespace to isolate
servlet-specific properties from the rest.

Closes gh-8066
This commit is contained in:
Brian Clozel
2017-01-26 21:43:04 +01:00
parent a31a792192
commit 12d883f6b9
37 changed files with 239 additions and 195 deletions

View File

@@ -200,7 +200,7 @@ public class SpringBootWebSecurityConfiguration {
if (this.errorController != null) {
ignored.add(normalizePath(this.errorController.getErrorPath()));
}
String[] paths = this.server.getPathsArray(ignored);
String[] paths = this.server.getServlet().getPathsArray(ignored);
List<RequestMatcher> matchers = new ArrayList<RequestMatcher>();
if (!ObjectUtils.isEmpty(paths)) {
for (String pattern : paths) {

View File

@@ -139,7 +139,7 @@ public class DispatcherServletAutoConfiguration {
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
dispatcherServlet, this.serverProperties.getServlet().getServletMapping());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());

View File

@@ -309,7 +309,7 @@ public class ErrorMvcAutoConfiguration {
@Override
public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
ErrorPage errorPage = new ErrorPage(this.properties.getServletPrefix()
ErrorPage errorPage = new ErrorPage(this.properties.getServlet().getServletPrefix()
+ this.properties.getError().getPath());
errorPageRegistry.addErrorPages(errorPage);
}

View File

@@ -20,10 +20,7 @@ import java.io.File;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
@@ -57,7 +54,7 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomi
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.InitParameterConfiguringServletContextInitializer;
import org.springframework.boot.context.embedded.JspServlet;
import org.springframework.boot.context.embedded.Servlet;
import org.springframework.boot.context.embedded.Ssl;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
@@ -73,7 +70,6 @@ import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -106,11 +102,6 @@ public class ServerProperties
*/
private InetAddress address;
/**
* Context path of the application.
*/
private String contextPath;
/**
* Display name of the application.
*/
@@ -119,16 +110,6 @@ public class ServerProperties
@NestedConfigurationProperty
private ErrorProperties error = new ErrorProperties();
/**
* Path of the main dispatcher servlet.
*/
private String servletPath = "/";
/**
* ServletContext parameters.
*/
private final Map<String, String> contextParameters = new HashMap<String, String>();
/**
* If X-Forwarded-* headers should be applied to the HttpRequest.
*/
@@ -160,7 +141,7 @@ public class ServerProperties
private Compression compression = new Compression();
@NestedConfigurationProperty
private JspServlet jspServlet;
private Servlet servlet = new Servlet();
private final Tomcat tomcat = new Tomcat();
@@ -188,8 +169,8 @@ public class ServerProperties
if (getAddress() != null) {
container.setAddress(getAddress());
}
if (getContextPath() != null) {
container.setContextPath(getContextPath());
if (getServlet().getContextPath() != null) {
container.setContextPath(getServlet().getContextPath());
}
if (getDisplayName() != null) {
container.setDisplayName(getDisplayName());
@@ -202,8 +183,8 @@ public class ServerProperties
if (getSsl() != null) {
container.setSsl(getSsl());
}
if (getJspServlet() != null) {
container.setJspServlet(getJspServlet());
if (getServlet().getJsp() != null) {
container.setJsp(getServlet().getJsp());
}
if (getCompression() != null) {
container.setCompression(getCompression());
@@ -224,57 +205,7 @@ public class ServerProperties
}
container.addInitializers(new SessionConfiguringInitializer(this.session));
container.addInitializers(new InitParameterConfiguringServletContextInitializer(
getContextParameters()));
}
public String getServletMapping() {
if (this.servletPath.equals("") || this.servletPath.equals("/")) {
return "/";
}
if (this.servletPath.contains("*")) {
return this.servletPath;
}
if (this.servletPath.endsWith("/")) {
return this.servletPath + "*";
}
return this.servletPath + "/*";
}
public String getPath(String path) {
String prefix = getServletPrefix();
if (!path.startsWith("/")) {
path = "/" + path;
}
return prefix + path;
}
public String getServletPrefix() {
String result = this.servletPath;
if (result.contains("*")) {
result = result.substring(0, result.indexOf("*"));
}
if (result.endsWith("/")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
public String[] getPathsArray(Collection<String> paths) {
String[] result = new String[paths.size()];
int i = 0;
for (String path : paths) {
result[i++] = getPath(path);
}
return result;
}
public String[] getPathsArray(String[] paths) {
String[] result = new String[paths.length];
int i = 0;
for (String path : paths) {
result[i++] = getPath(path);
}
return result;
getServlet().getContextParameters()));
}
public void setLoader(String value) {
@@ -297,21 +228,6 @@ public class ServerProperties
this.address = address;
}
public String getContextPath() {
return this.contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = cleanContextPath(contextPath);
}
private String cleanContextPath(String contextPath) {
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
return contextPath.substring(0, contextPath.length() - 1);
}
return contextPath;
}
public String getDisplayName() {
return this.displayName;
}
@@ -320,19 +236,6 @@ public class ServerProperties
this.displayName = displayName;
}
public String getServletPath() {
return this.servletPath;
}
public void setServletPath(String servletPath) {
Assert.notNull(servletPath, "ServletPath must not be null");
this.servletPath = servletPath;
}
public Map<String, String> getContextParameters() {
return this.contextParameters;
}
public Boolean isUseForwardHeaders() {
return this.useForwardHeaders;
}
@@ -397,12 +300,12 @@ public class ServerProperties
return this.compression;
}
public JspServlet getJspServlet() {
return this.jspServlet;
public Servlet getServlet() {
return this.servlet;
}
public void setJspServlet(JspServlet jspServlet) {
this.jspServlet = jspServlet;
public void setServlet(Servlet servlet) {
this.servlet = servlet;
}
public Tomcat getTomcat() {

View File

@@ -372,7 +372,7 @@
]
},
{
"name": "server.jsp-servlet.class-name",
"name": "server.servlet.jsp.class-name",
"providers": [
{
"name": "class-reference",

View File

@@ -54,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.jersey.type=filter", "server.contextPath=/app" })
"spring.jersey.type=filter", "server.servlet.contextPath=/app" })
@DirtiesContext
public class JerseyAutoConfigurationCustomFilterContextPathTests {

View File

@@ -54,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "server.contextPath=/app")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "server.servlet.contextPath=/app")
@DirtiesContext
public class JerseyAutoConfigurationCustomServletContextPathTests {

View File

@@ -122,7 +122,8 @@ public class EmbeddedServletContainerAutoConfigurationTests {
public void initParametersAreConfiguredOnTheServletContext() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"server.context_parameters.a:alpha", "server.context_parameters.b:bravo");
"server.servlet.context_parameters.a:alpha",
"server.servlet.context_parameters.b:bravo");
this.context.register(BaseConfiguration.class);
this.context.refresh();

View File

@@ -43,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "server.servletPath:/spring/*")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "server.servlet.path:/spring/*")
@DirtiesContext
public class RemappedErrorViewIntegrationTests {
@@ -89,7 +89,7 @@ public class RemappedErrorViewIntegrationTests {
// For manual testing
public static void main(String[] args) {
new SpringApplicationBuilder(TestConfiguration.class)
.properties("server.servletPath:spring/*").run(args);
.properties("server.servlet.path:spring/*").run(args);
}
}

View File

@@ -125,20 +125,20 @@ public class ServerPropertiesTests {
public void testServletPathAsMapping() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.servletPath", "/foo/*")));
Collections.singletonMap("server.servlet.path", "/foo/*")));
assertThat(binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.properties.getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServletPrefix()).isEqualTo("/foo");
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
}
@Test
public void testServletPathAsPrefix() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.servletPath", "/foo")));
Collections.singletonMap("server.servlet.path", "/foo")));
assertThat(binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.properties.getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServletPrefix()).isEqualTo("/foo");
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
}
@Test
@@ -238,15 +238,15 @@ public class ServerPropertiesTests {
@Test
public void testTrailingSlashOfContextPathIsRemoved() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
Collections.singletonMap("server.contextPath", "/foo/")));
assertThat(this.properties.getContextPath()).isEqualTo("/foo");
Collections.singletonMap("server.servlet.contextPath", "/foo/")));
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/foo");
}
@Test
public void testSlashOfContextPathIsDefaultValue() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
Collections.singletonMap("server.contextPath", "/")));
assertThat(this.properties.getContextPath()).isEqualTo("");
Collections.singletonMap("server.servlet.contextPath", "/")));
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("");
}
@Test