Rework common server customization
Update the configurable embedded web server factory interfaces to extend `ConfigurableWebServerFactory` so that the can be used in a `WebServerFactoryCustomizer`. Extract server specific customization to their own auto-configuration and align reactive/servlet server auto-configuration. Closes gh-8573pull/12149/head
parent
aafa1e9615
commit
46021928ba
52
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerFactoryCustomizer.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementWebServerFactoryCustomizer.java
52
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerFactoryCustomizer.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementWebServerFactoryCustomizer.java
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.embedded;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.coyote.UpgradeProtocol;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.util.Loader;
|
||||
import org.eclipse.jetty.webapp.WebAppContext;
|
||||
import org.xnio.SslClientAuthMode;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for embedded servlet and reactive
|
||||
* web servers customizations.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ServerProperties.class)
|
||||
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
|
||||
|
||||
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
|
||||
public static class TomcatWebServerFactoryCustomizerConfiguration {
|
||||
|
||||
@Bean
|
||||
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
|
||||
Environment environment, ServerProperties serverProperties) {
|
||||
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested configuration if Jetty is being used.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
|
||||
public static class JettyWebServerFactoryCustomizerConfiguration {
|
||||
|
||||
@Bean
|
||||
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
|
||||
Environment environment, ServerProperties serverProperties) {
|
||||
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested configuration if Undertow is being used.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
|
||||
public static class UndertowWebServerFactoryCustomizerConfiguration {
|
||||
|
||||
@Bean
|
||||
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
|
||||
Environment environment, ServerProperties serverProperties) {
|
||||
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
64
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java → spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java
64
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java → spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java
@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration for embedded reactive and servlet Jetty web servers.
|
||||
*
|
||||
* @see org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.web.embedded.jetty;
|
@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Configuration for embedded reactive and servlet Tomcat web servers.
|
||||
*
|
||||
* @see org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.web.embedded.tomcat;
|
34
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java → spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryCustomizer.java
34
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java → spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryCustomizer.java
@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.autoconfigure.web.embedded.jetty.JettyCustomizer;
|
||||
import org.springframework.boot.autoconfigure.web.embedded.tomcat.TomcatCustomizer;
|
||||
import org.springframework.boot.autoconfigure.web.embedded.undertow.UndertowCustomizer;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Default {@link WebServerFactoryCustomizer} for {@link ServerProperties}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Stephane Nicoll
|
||||
* @author Olivier Lamy
|
||||
* @author Yunkun Huang
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class DefaultServletWebServerFactoryCustomizer
|
||||
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>,
|
||||
EnvironmentAware, Ordered {
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
private Environment environment;
|
||||
|
||||
public DefaultServletWebServerFactoryCustomizer(ServerProperties serverProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
}
|
||||
|
||||
public void setLoader(String value) {
|
||||
// no op to support Tomcat running as a traditional server (not embedded)
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableServletWebServerFactory factory) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(this.serverProperties::getPort).to(factory::setPort);
|
||||
map.from(this.serverProperties::getAddress).to(factory::setAddress);
|
||||
map.from(this.serverProperties.getServlet()::getContextPath)
|
||||
.to(factory::setContextPath);
|
||||
map.from(this.serverProperties.getServlet()::getApplicationDisplayName)
|
||||
.to(factory::setDisplayName);
|
||||
map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);
|
||||
map.from(this.serverProperties::getSsl).to(factory::setSsl);
|
||||
map.from(this.serverProperties::getServlet).as(ServerProperties.Servlet::getJsp)
|
||||
.to(factory::setJsp);
|
||||
map.from(this.serverProperties::getCompression).to(factory::setCompression);
|
||||
map.from(this.serverProperties::getHttp2).to(factory::setHttp2);
|
||||
map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader);
|
||||
map.from(() -> factory).whenInstanceOf(TomcatServletWebServerFactory.class)
|
||||
.to((tomcatFactory) -> {
|
||||
TomcatCustomizer.customizeTomcat(this.serverProperties,
|
||||
this.environment, tomcatFactory);
|
||||
TomcatServletCustomizer.customizeTomcat(this.serverProperties,
|
||||
this.environment, tomcatFactory);
|
||||
});
|
||||
map.from(() -> factory).whenInstanceOf(JettyServletWebServerFactory.class).to(
|
||||
(jettyFactory) -> JettyCustomizer.customizeJetty(this.serverProperties,
|
||||
this.environment, jettyFactory));
|
||||
map.from(() -> factory).whenInstanceOf(UndertowServletWebServerFactory.class)
|
||||
.to((undertowFactory) -> UndertowCustomizer.customizeUndertow(
|
||||
this.serverProperties, this.environment, undertowFactory));
|
||||
map.from(this.serverProperties.getServlet()::getContextParameters)
|
||||
.to(factory::setInitParameters);
|
||||
}
|
||||
|
||||
private static class TomcatServletCustomizer {
|
||||
|
||||
public static void customizeTomcat(ServerProperties serverProperties,
|
||||
Environment environment, TomcatServletWebServerFactory factory) {
|
||||
ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat();
|
||||
if (!ObjectUtils.isEmpty(tomcatProperties.getAdditionalTldSkipPatterns())) {
|
||||
factory.getTldSkipPatterns()
|
||||
.addAll(tomcatProperties.getAdditionalTldSkipPatterns());
|
||||
}
|
||||
if (tomcatProperties.getRedirectContextRoot() != null) {
|
||||
customizeRedirectContextRoot(factory,
|
||||
tomcatProperties.getRedirectContextRoot());
|
||||
}
|
||||
if (tomcatProperties.getUseRelativeRedirects() != null) {
|
||||
customizeUseRelativeRedirects(factory,
|
||||
tomcatProperties.getUseRelativeRedirects());
|
||||
}
|
||||
}
|
||||
|
||||
private static void customizeRedirectContextRoot(
|
||||
ConfigurableTomcatWebServerFactory factory, boolean redirectContextRoot) {
|
||||
factory.addContextCustomizers((context) -> context
|
||||
.setMapperContextRootRedirectEnabled(redirectContextRoot));
|
||||
}
|
||||
|
||||
private static void customizeUseRelativeRedirects(
|
||||
ConfigurableTomcatWebServerFactory factory,
|
||||
boolean useRelativeRedirects) {
|
||||
factory.addContextCustomizers(
|
||||
(context) -> context.setUseRelativeRedirects(useRelativeRedirects));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.coyote.UpgradeProtocol;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.util.Loader;
|
||||
import org.eclipse.jetty.webapp.WebAppContext;
|
||||
import org.xnio.SslClientAuthMode;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
|
||||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Configuration classes for servlet web servers
|
||||
* <p>
|
||||
* Those should be {@code @Import} in a regular auto-configuration class to guarantee
|
||||
* their order of execution.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
* @author Ivan Sopov
|
||||
* @author Brian Clozel
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class ServletWebServerFactoryConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
|
||||
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
|
||||
public static class EmbeddedTomcat {
|
||||
|
||||
@Bean
|
||||
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
|
||||
return new TomcatServletWebServerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested configuration if Jetty is being used.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
|
||||
WebAppContext.class })
|
||||
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
|
||||
public static class EmbeddedJetty {
|
||||
|
||||
@Bean
|
||||
public JettyServletWebServerFactory JettyServletWebServerFactory() {
|
||||
return new JettyServletWebServerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested configuration if Undertow is being used.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
|
||||
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
|
||||
public static class EmbeddedUndertow {
|
||||
|
||||
@Bean
|
||||
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
|
||||
return new UndertowServletWebServerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* {@link WebServerFactoryCustomizer} to apply {@link ServerProperties} to servlet web
|
||||
* servers.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Stephane Nicoll
|
||||
* @author Olivier Lamy
|
||||
* @author Yunkun Huang
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class ServletWebServerFactoryCustomizer implements
|
||||
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
public ServletWebServerFactoryCustomizer(ServerProperties serverProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableServletWebServerFactory factory) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(this.serverProperties::getPort).to(factory::setPort);
|
||||
map.from(this.serverProperties::getAddress).to(factory::setAddress);
|
||||
map.from(this.serverProperties.getServlet()::getContextPath)
|
||||
.to(factory::setContextPath);
|
||||
map.from(this.serverProperties.getServlet()::getApplicationDisplayName)
|
||||
.to(factory::setDisplayName);
|
||||
map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);
|
||||
map.from(this.serverProperties::getSsl).to(factory::setSsl);
|
||||
map.from(this.serverProperties::getServlet).as(ServerProperties.Servlet::getJsp)
|
||||
.to(factory::setJsp);
|
||||
map.from(this.serverProperties::getCompression).to(factory::setCompression);
|
||||
map.from(this.serverProperties::getHttp2).to(factory::setHttp2);
|
||||
map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader);
|
||||
map.from(this.serverProperties.getServlet()::getContextParameters)
|
||||
.to(factory::setInitParameters);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* {@link WebServerFactoryCustomizer} to apply {@link ServerProperties} to Tomcat web
|
||||
* servers.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class TomcatServletWebServerFactoryCustomizer
|
||||
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>, Ordered {
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
public TomcatServletWebServerFactoryCustomizer(ServerProperties serverProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(TomcatServletWebServerFactory factory) {
|
||||
ServerProperties.Tomcat tomcatProperties = this.serverProperties.getTomcat();
|
||||
if (!ObjectUtils.isEmpty(tomcatProperties.getAdditionalTldSkipPatterns())) {
|
||||
factory.getTldSkipPatterns()
|
||||
.addAll(tomcatProperties.getAdditionalTldSkipPatterns());
|
||||
}
|
||||
if (tomcatProperties.getRedirectContextRoot() != null) {
|
||||
customizeRedirectContextRoot(factory,
|
||||
tomcatProperties.getRedirectContextRoot());
|
||||
}
|
||||
if (tomcatProperties.getUseRelativeRedirects() != null) {
|
||||
customizeUseRelativeRedirects(factory,
|
||||
tomcatProperties.getUseRelativeRedirects());
|
||||
}
|
||||
}
|
||||
|
||||
private void customizeRedirectContextRoot(ConfigurableTomcatWebServerFactory factory,
|
||||
boolean redirectContextRoot) {
|
||||
factory.addContextCustomizers((context) -> context
|
||||
.setMapperContextRootRedirectEnabled(redirectContextRoot));
|
||||
}
|
||||
|
||||
private void customizeUseRelativeRedirects(ConfigurableTomcatWebServerFactory factory,
|
||||
boolean useRelativeRedirects) {
|
||||
factory.addContextCustomizers(
|
||||
(context) -> context.setUseRelativeRedirects(useRelativeRedirects));
|
||||
}
|
||||
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
/**
|
||||
* An implementation of {@link WebMvcRegistrations} with empty methods allowing
|
||||
* sub-classes to override only the methods they're interested in.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 1.4.0
|
||||
* @deprecated as of 2.0.0 {@link WebMvcRegistrations} has default methods (made possible
|
||||
* by a Java 8 baseline) and can be implemented directly without the need for this adapter
|
||||
*/
|
||||
@Deprecated
|
||||
public class WebMvcRegistrationsAdapter implements WebMvcRegistrations {
|
||||
|
||||
}
|
@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.embedded;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.eclipse.jetty.server.NCSARequestLog;
|
||||
import org.eclipse.jetty.server.RequestLog;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link JettyWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JettyWebServerFactoryCustomizerTests {
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
private JettyWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.serverProperties = new ServerProperties();
|
||||
ConfigurationPropertySources.attach(this.environment);
|
||||
this.customizer = new JettyWebServerFactoryCustomizer(this.environment,
|
||||
this.serverProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeaders() {
|
||||
this.environment.setProperty("DYNO", "-");
|
||||
ConfigurableJettyWebServerFactory factory = mock(
|
||||
ConfigurableJettyWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultUseForwardHeaders() {
|
||||
ConfigurableJettyWebServerFactory factory = mock(
|
||||
ConfigurableJettyWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogCanBeCustomized() throws IOException {
|
||||
File logFile = File.createTempFile("jetty_log", ".log");
|
||||
String timezone = TimeZone.getDefault().getID();
|
||||
bind("server.jetty.accesslog.enabled=true",
|
||||
"server.jetty.accesslog.filename=" + logFile.getAbsolutePath(),
|
||||
"server.jetty.accesslog.file-date-format=yyyy-MM-dd",
|
||||
"server.jetty.accesslog.retention-period=42",
|
||||
"server.jetty.accesslog.append=true",
|
||||
"server.jetty.accesslog.extended-format=true",
|
||||
"server.jetty.accesslog.date-format=HH:mm:ss",
|
||||
"server.jetty.accesslog.locale=en_BE",
|
||||
"server.jetty.accesslog.time-zone=" + timezone,
|
||||
"server.jetty.accesslog.log-cookies=true",
|
||||
"server.jetty.accesslog.log-server=true",
|
||||
"server.jetty.accesslog.log-latency=true");
|
||||
JettyWebServer server = customizeAndGetServer();
|
||||
NCSARequestLog requestLog = getNCSARequestLog(server);
|
||||
assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
|
||||
assertThat(requestLog.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
|
||||
assertThat(requestLog.getRetainDays()).isEqualTo(42);
|
||||
assertThat(requestLog.isAppend()).isTrue();
|
||||
assertThat(requestLog.isExtended()).isTrue();
|
||||
assertThat(requestLog.getLogDateFormat()).isEqualTo("HH:mm:ss");
|
||||
assertThat(requestLog.getLogLocale()).isEqualTo(new Locale("en", "BE"));
|
||||
assertThat(requestLog.getLogTimeZone()).isEqualTo(timezone);
|
||||
assertThat(requestLog.getLogCookies()).isTrue();
|
||||
assertThat(requestLog.getLogServer()).isTrue();
|
||||
assertThat(requestLog.getLogLatency()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogCanBeEnabled() {
|
||||
bind("server.jetty.accesslog.enabled=true");
|
||||
JettyWebServer server = customizeAndGetServer();
|
||||
NCSARequestLog requestLog = getNCSARequestLog(server);
|
||||
assertThat(requestLog.getFilename()).isNull();
|
||||
assertThat(requestLog.isAppend()).isFalse();
|
||||
assertThat(requestLog.isExtended()).isFalse();
|
||||
assertThat(requestLog.getLogCookies()).isFalse();
|
||||
assertThat(requestLog.getLogServer()).isFalse();
|
||||
assertThat(requestLog.getLogLatency()).isFalse();
|
||||
}
|
||||
|
||||
private NCSARequestLog getNCSARequestLog(JettyWebServer server) {
|
||||
RequestLog requestLog = server.getServer().getRequestLog();
|
||||
assertThat(requestLog).isInstanceOf(NCSARequestLog.class);
|
||||
return (NCSARequestLog) requestLog;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeaders() {
|
||||
this.serverProperties.setUseForwardHeaders(true);
|
||||
ConfigurableJettyWebServerFactory factory = mock(
|
||||
ConfigurableJettyWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
private void bind(String... inlinedProperties) {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
||||
inlinedProperties);
|
||||
new Binder(ConfigurationPropertySources.get(this.environment)).bind("server",
|
||||
Bindable.ofInstance(this.serverProperties));
|
||||
}
|
||||
|
||||
private JettyWebServer customizeAndGetServer() {
|
||||
JettyServletWebServerFactory factory = customizeAndGetFactory();
|
||||
return (JettyWebServer) factory.getWebServer();
|
||||
}
|
||||
|
||||
private JettyServletWebServerFactory customizeAndGetFactory() {
|
||||
JettyServletWebServerFactory factory = new JettyServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,285 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.embedded;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Valve;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.valves.AccessLogValve;
|
||||
import org.apache.catalina.valves.ErrorReportValve;
|
||||
import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.apache.coyote.AbstractProtocol;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TomcatWebServerFactoryCustomizer}
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class TomcatWebServerFactoryCustomizerTests {
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
private TomcatWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.serverProperties = new ServerProperties();
|
||||
ConfigurationPropertySources.attach(this.environment);
|
||||
this.customizer = new TomcatWebServerFactoryCustomizer(this.environment,
|
||||
this.serverProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customAcceptCount() {
|
||||
bind("server.tomcat.accept-count=10");
|
||||
customizeAndRunServer((server) -> assertThat(((AbstractProtocol<?>) server
|
||||
.getTomcat().getConnector().getProtocolHandler()).getAcceptCount())
|
||||
.isEqualTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customBackgroundProcessorDelay() {
|
||||
bind("server.tomcat.background-processor-delay=5");
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customDisableMaxHttpPostSize() {
|
||||
bind("server.tomcat.max-http-post-size=-1");
|
||||
customizeAndRunServer(
|
||||
(server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize())
|
||||
.isEqualTo(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customMaxConnections() {
|
||||
bind("server.tomcat.max-connections=5");
|
||||
customizeAndRunServer((server) -> assertThat(((AbstractProtocol<?>) server
|
||||
.getTomcat().getConnector().getProtocolHandler()).getMaxConnections())
|
||||
.isEqualTo(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customMaxHttpPostSize() {
|
||||
bind("server.tomcat.max-http-post-size=10000");
|
||||
customizeAndRunServer(
|
||||
(server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize())
|
||||
.isEqualTo(10000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customRemoteIpValve() {
|
||||
bind("server.tomcat.remote-ip-header=x-my-remote-ip-header",
|
||||
"server.tomcat.protocol-header=x-my-protocol-header",
|
||||
"server.tomcat.internal-proxies=192.168.0.1",
|
||||
"server.tomcat.port-header=x-my-forward-port",
|
||||
"server.tomcat.protocol-header-https-value=On");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("On");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("x-my-remote-ip-header");
|
||||
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customStaticResourceCacheTtl() {
|
||||
bind("server.tomcat.resource.cache-ttl=10000");
|
||||
customizeAndRunServer((server) -> {
|
||||
Tomcat tomcat = server.getTomcat();
|
||||
Context context = (Context) tomcat.getHost().findChildren()[0];
|
||||
assertThat(context.getResources().getCacheTtl()).isEqualTo(10000L);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeaders() {
|
||||
this.environment.setProperty("DYNO", "-");
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultRemoteIpValve() {
|
||||
// Since 1.1.7 you need to specify at least the protocol
|
||||
bind("server.tomcat.protocol-header=X-Forwarded-Proto",
|
||||
"server.tomcat.remote-ip-header=X-Forwarded-For");
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeaders() {
|
||||
// Since 1.3.0 no need to explicitly set header names if use-forward-header=true
|
||||
this.serverProperties.setUseForwardHeaders(true);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
private void testRemoteIpValveConfigured() {
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("https");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("X-Forwarded-For");
|
||||
String expectedInternalProxies = "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 10/8
|
||||
+ "192\\.168\\.\\d{1,3}\\.\\d{1,3}|" // 192.168/16
|
||||
+ "169\\.254\\.\\d{1,3}\\.\\d{1,3}|" // 169.254/16
|
||||
+ "127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 127/8
|
||||
+ "172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|" // 172.16/12
|
||||
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|"
|
||||
+ "172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}";
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo(expectedInternalProxies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultBackgroundProcessorDelay() {
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableRemoteIpValve() {
|
||||
bind("server.tomcat.remote-ip-header=", "server.tomcat.protocol-header=");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorReportValveIsConfiguredToNotReportStackTraces() {
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
Valve[] valves = server.getTomcat().getHost().getPipeline().getValves();
|
||||
assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
|
||||
for (Valve valve : valves) {
|
||||
if (valve instanceof ErrorReportValve) {
|
||||
ErrorReportValve errorReportValve = (ErrorReportValve) valve;
|
||||
assertThat(errorReportValve.isShowReport()).isFalse();
|
||||
assertThat(errorReportValve.isShowServerInfo()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeMinSpareThreads() {
|
||||
bind("server.tomcat.min-spare-threads=10");
|
||||
assertThat(this.serverProperties.getTomcat().getMinSpareThreads()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogBufferingCanBeDisabled() {
|
||||
bind("server.tomcat.accesslog.enabled=true",
|
||||
"server.tomcat.accesslog.buffered=false");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogCanBeEnabled() {
|
||||
bind("server.tomcat.accesslog.enabled=true");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
assertThat(factory.getEngineValves()).first().isInstanceOf(AccessLogValve.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogFileDateFormatByDefault() {
|
||||
bind("server.tomcat.accesslog.enabled=true");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo(".yyyy-MM-dd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogFileDateFormatCanBeRedefined() {
|
||||
bind("server.tomcat.accesslog.enabled=true",
|
||||
"server.tomcat.accesslog.file-date-format=yyyy-MM-dd.HH");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogIsBufferedByDefault() {
|
||||
bind("server.tomcat.accesslog.enabled=true");
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessLogIsDisabledByDefault() {
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
private void bind(String... inlinedProperties) {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
||||
inlinedProperties);
|
||||
new Binder(ConfigurationPropertySources.get(this.environment)).bind("server",
|
||||
Bindable.ofInstance(this.serverProperties));
|
||||
}
|
||||
|
||||
private void customizeAndRunServer(Consumer<TomcatWebServer> consumer) {
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
server.start();
|
||||
try {
|
||||
consumer.accept(server);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private TomcatWebServer customizeAndGetServer() {
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
return (TomcatWebServer) factory.getWebServer();
|
||||
}
|
||||
|
||||
private TomcatServletWebServerFactory customizeAndGetFactory() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.embedded;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.web.embedded.undertow.ConfigurableUndertowWebServerFactory;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link UndertowWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class UndertowWebServerFactoryCustomizerTests {
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
private UndertowWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.serverProperties = new ServerProperties();
|
||||
ConfigurationPropertySources.attach(this.environment);
|
||||
this.customizer = new UndertowWebServerFactoryCustomizer(this.environment,
|
||||
this.serverProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeUndertowAccessLog() {
|
||||
bind("server.undertow.accesslog.enabled=true",
|
||||
"server.undertow.accesslog.pattern=foo",
|
||||
"server.undertow.accesslog.prefix=test_log",
|
||||
"server.undertow.accesslog.suffix=txt",
|
||||
"server.undertow.accesslog.dir=test-logs",
|
||||
"server.undertow.accesslog.rotate=false");
|
||||
ConfigurableUndertowWebServerFactory factory = mock(
|
||||
ConfigurableUndertowWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setAccessLogEnabled(true);
|
||||
verify(factory).setAccessLogPattern("foo");
|
||||
verify(factory).setAccessLogPrefix("test_log");
|
||||
verify(factory).setAccessLogSuffix("txt");
|
||||
verify(factory).setAccessLogDirectory(new File("test-logs"));
|
||||
verify(factory).setAccessLogRotate(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersUndertow() {
|
||||
this.environment.setProperty("DYNO", "-");
|
||||
ConfigurableUndertowWebServerFactory factory = mock(
|
||||
ConfigurableUndertowWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultUseForwardHeadersUndertow() {
|
||||
ConfigurableUndertowWebServerFactory factory = mock(
|
||||
ConfigurableUndertowWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersUndertow() {
|
||||
this.serverProperties.setUseForwardHeaders(true);
|
||||
ConfigurableUndertowWebServerFactory factory = mock(
|
||||
ConfigurableUndertowWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipNullElementsForUndertow() {
|
||||
ConfigurableUndertowWebServerFactory factory = mock(
|
||||
ConfigurableUndertowWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory, never()).setAccessLogEnabled(anyBoolean());
|
||||
}
|
||||
|
||||
private void bind(String... inlinedProperties) {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
||||
inlinedProperties);
|
||||
new Binder(ConfigurationPropertySources.get(this.environment)).bind("server",
|
||||
Bindable.ofInstance(this.serverProperties));
|
||||
}
|
||||
|
||||
}
|
@ -1,538 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.reactive;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Valve;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.valves.AccessLogValve;
|
||||
import org.apache.catalina.valves.ErrorReportValve;
|
||||
import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.apache.coyote.AbstractProtocol;
|
||||
import org.eclipse.jetty.server.NCSARequestLog;
|
||||
import org.eclipse.jetty.server.RequestLog;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultReactiveWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Yunkun Huang
|
||||
*/
|
||||
public class DefaultReactiveWebServerFactoryCustomizerTests {
|
||||
|
||||
private final ServerProperties properties = new ServerProperties();
|
||||
|
||||
private DefaultReactiveWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.customizer = new DefaultReactiveWebServerFactoryCustomizer(this.properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerPort() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
this.properties.setPort(9000);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setPort(9000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerAddress() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
InetAddress address = mock(InetAddress.class);
|
||||
this.properties.setAddress(address);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setAddress(address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerSsl() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
Ssl ssl = mock(Ssl.class);
|
||||
this.properties.setSsl(ssl);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setSsl(ssl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogIsDisabledByDefault() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogCanBeEnabled() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
assertThat(factory.getEngineValves()).first().isInstanceOf(AccessLogValve.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogFileDateFormatByDefault() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo(".yyyy-MM-dd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogFileDateFormatCanBeRedefined() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
map.put("server.tomcat.accesslog.file-date-format", "yyyy-MM-dd.HH");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogIsBufferedByDefault() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogBufferingCanBeDisabled() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
map.put("server.tomcat.accesslog.buffered", "false");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote-ip-header", "");
|
||||
map.put("server.tomcat.protocol-header", "");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTomcatBackgroundProcessorDelay() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer webServer = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
assertThat(webServer.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(30);
|
||||
webServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatBackgroundProcessorDelay() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.background-processor-delay", "5");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer webServer = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
assertThat(webServer.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(5);
|
||||
webServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// Since 1.1.7 you need to specify at least the protocol
|
||||
map.put("server.tomcat.protocol-header", "X-Forwarded-Proto");
|
||||
map.put("server.tomcat.remote-ip-header", "X-Forwarded-For");
|
||||
bindProperties(map);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersTomcat() {
|
||||
// Since 1.3.0 no need to explicitly set header names if use-forward-header=true
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersTomcat() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
private void testRemoteIpValveConfigured() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("https");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("X-Forwarded-For");
|
||||
String expectedInternalProxies = "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 10/8
|
||||
+ "192\\.168\\.\\d{1,3}\\.\\d{1,3}|" // 192.168/16
|
||||
+ "169\\.254\\.\\d{1,3}\\.\\d{1,3}|" // 169.254/16
|
||||
+ "127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 127/8
|
||||
+ "172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|" // 172.16/12
|
||||
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|"
|
||||
+ "172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}";
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo(expectedInternalProxies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote-ip-header", "x-my-remote-ip-header");
|
||||
map.put("server.tomcat.protocol-header", "x-my-protocol-header");
|
||||
map.put("server.tomcat.internal-proxies", "192.168.0.1");
|
||||
map.put("server.tomcat.port-header", "x-my-forward-port");
|
||||
map.put("server.tomcat.protocol-header-https-value", "On");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("On");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("x-my-remote-ip-header");
|
||||
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatAcceptCount() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accept-count", "10");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
server.start();
|
||||
try {
|
||||
assertThat(((AbstractProtocol<?>) server.getTomcat().getConnector()
|
||||
.getProtocolHandler()).getAcceptCount()).isEqualTo(10);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatMaxConnections() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-connections", "5");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
server.start();
|
||||
try {
|
||||
assertThat(((AbstractProtocol<?>) server.getTomcat().getConnector()
|
||||
.getProtocolHandler()).getMaxConnections()).isEqualTo(5);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatMaxHttpPostSize() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-http-post-size", "10000");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
server.start();
|
||||
try {
|
||||
assertThat(server.getTomcat().getConnector().getMaxPostSize())
|
||||
.isEqualTo(10000);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatDisableMaxHttpPostSize() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-http-post-size", "-1");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
server.start();
|
||||
try {
|
||||
assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(-1);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatMinSpareThreads() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.min-spare-threads", "10");
|
||||
bindProperties(map);
|
||||
assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatStaticResourceCacheTtl() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.resource.cache-ttl", "10000");
|
||||
bindProperties(map);
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
server.start();
|
||||
try {
|
||||
Tomcat tomcat = server.getTomcat();
|
||||
Context context = (Context) tomcat.getHost().findChildren()[0];
|
||||
assertThat(context.getResources().getCacheTtl()).isEqualTo(10000L);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorReportValveIsConfiguredToNotReportStackTraces() {
|
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
Valve[] valves = ((TomcatWebServer) factory.getWebServer(mock(HttpHandler.class)))
|
||||
.getTomcat().getHost().getPipeline().getValves();
|
||||
assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
|
||||
for (Valve valve : valves) {
|
||||
if (valve instanceof ErrorReportValve) {
|
||||
ErrorReportValve errorReportValve = (ErrorReportValve) valve;
|
||||
assertThat(errorReportValve.isShowReport()).isFalse();
|
||||
assertThat(errorReportValve.isShowServerInfo()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultUseForwardHeadersJetty() {
|
||||
JettyReactiveWebServerFactory factory = spy(new JettyReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersJetty() {
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
JettyReactiveWebServerFactory factory = spy(new JettyReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersJetty() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
JettyReactiveWebServerFactory factory = spy(new JettyReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jettyAccessLogCanBeEnabled() {
|
||||
JettyReactiveWebServerFactory factory = new JettyReactiveWebServerFactory(0);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.jetty.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
JettyWebServer webServer = (JettyWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
try {
|
||||
NCSARequestLog requestLog = getNCSARequestLog(webServer);
|
||||
assertThat(requestLog.getFilename()).isNull();
|
||||
assertThat(requestLog.isAppend()).isFalse();
|
||||
assertThat(requestLog.isExtended()).isFalse();
|
||||
assertThat(requestLog.getLogCookies()).isFalse();
|
||||
assertThat(requestLog.getLogServer()).isFalse();
|
||||
assertThat(requestLog.getLogLatency()).isFalse();
|
||||
}
|
||||
finally {
|
||||
webServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jettyAccessLogCanBeCustomized() throws IOException {
|
||||
File logFile = File.createTempFile("jetty_log", ".log");
|
||||
JettyReactiveWebServerFactory factory = new JettyReactiveWebServerFactory(0);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
String timezone = TimeZone.getDefault().getID();
|
||||
map.put("server.jetty.accesslog.enabled", "true");
|
||||
map.put("server.jetty.accesslog.filename", logFile.getAbsolutePath());
|
||||
map.put("server.jetty.accesslog.file-date-format", "yyyy-MM-dd");
|
||||
map.put("server.jetty.accesslog.retention-period", "42");
|
||||
map.put("server.jetty.accesslog.append", "true");
|
||||
map.put("server.jetty.accesslog.extended-format", "true");
|
||||
map.put("server.jetty.accesslog.date-format", "HH:mm:ss");
|
||||
map.put("server.jetty.accesslog.locale", "en_BE");
|
||||
map.put("server.jetty.accesslog.time-zone", timezone);
|
||||
map.put("server.jetty.accesslog.log-cookies", "true");
|
||||
map.put("server.jetty.accesslog.log-server", "true");
|
||||
map.put("server.jetty.accesslog.log-latency", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
JettyWebServer webServer = (JettyWebServer) factory
|
||||
.getWebServer(mock(HttpHandler.class));
|
||||
NCSARequestLog requestLog = getNCSARequestLog(webServer);
|
||||
try {
|
||||
assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
|
||||
assertThat(requestLog.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
|
||||
assertThat(requestLog.getRetainDays()).isEqualTo(42);
|
||||
assertThat(requestLog.isAppend()).isTrue();
|
||||
assertThat(requestLog.isExtended()).isTrue();
|
||||
assertThat(requestLog.getLogDateFormat()).isEqualTo("HH:mm:ss");
|
||||
assertThat(requestLog.getLogLocale()).isEqualTo(new Locale("en", "BE"));
|
||||
assertThat(requestLog.getLogTimeZone()).isEqualTo(timezone);
|
||||
assertThat(requestLog.getLogCookies()).isTrue();
|
||||
assertThat(requestLog.getLogServer()).isTrue();
|
||||
assertThat(requestLog.getLogLatency()).isTrue();
|
||||
}
|
||||
finally {
|
||||
webServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeUndertowAccessLog() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.undertow.accesslog.enabled", "true");
|
||||
map.put("server.undertow.accesslog.pattern", "foo");
|
||||
map.put("server.undertow.accesslog.prefix", "test_log");
|
||||
map.put("server.undertow.accesslog.suffix", "txt");
|
||||
map.put("server.undertow.accesslog.dir", "test-logs");
|
||||
map.put("server.undertow.accesslog.rotate", "false");
|
||||
bindProperties(map);
|
||||
UndertowReactiveWebServerFactory factory = spy(
|
||||
new UndertowReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setAccessLogEnabled(true);
|
||||
verify(factory).setAccessLogPattern("foo");
|
||||
verify(factory).setAccessLogPrefix("test_log");
|
||||
verify(factory).setAccessLogSuffix("txt");
|
||||
verify(factory).setAccessLogDirectory(new File("test-logs"));
|
||||
verify(factory).setAccessLogRotate(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersUndertow() {
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
UndertowReactiveWebServerFactory factory = spy(
|
||||
new UndertowReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersUndertow() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
UndertowReactiveWebServerFactory factory = spy(
|
||||
new UndertowReactiveWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipNullElementsForUndertow() {
|
||||
UndertowReactiveWebServerFactory factory = mock(
|
||||
UndertowReactiveWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory, never()).setAccessLogEnabled(anyBoolean());
|
||||
}
|
||||
|
||||
private NCSARequestLog getNCSARequestLog(JettyWebServer webServer) {
|
||||
RequestLog requestLog = webServer.getServer().getRequestLog();
|
||||
assertThat(requestLog).isInstanceOf(NCSARequestLog.class);
|
||||
return (NCSARequestLog) requestLog;
|
||||
}
|
||||
|
||||
private void bindProperties(Map<String, String> map) {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(map);
|
||||
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.reactive;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Yunkun Huang
|
||||
*/
|
||||
public class ReactiveWebServerFactoryCustomizerTests {
|
||||
|
||||
private ServerProperties properties = new ServerProperties();
|
||||
|
||||
private ReactiveWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.customizer = new ReactiveWebServerFactoryCustomizer(
|
||||
this.properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerPort() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
this.properties.setPort(9000);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setPort(9000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerAddress() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
InetAddress address = mock(InetAddress.class);
|
||||
this.properties.setAddress(address);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setAddress(address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeServerSsl() {
|
||||
ConfigurableReactiveWebServerFactory factory = mock(
|
||||
ConfigurableReactiveWebServerFactory.class);
|
||||
Ssl ssl = mock(Ssl.class);
|
||||
this.properties.setSsl(ssl);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setSsl(ssl);
|
||||
}
|
||||
|
||||
}
|
@ -1,677 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Valve;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.catalina.valves.AccessLogValve;
|
||||
import org.apache.catalina.valves.ErrorReportValve;
|
||||
import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.apache.coyote.AbstractProtocol;
|
||||
import org.eclipse.jetty.server.NCSARequestLog;
|
||||
import org.eclipse.jetty.server.RequestLog;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.boot.web.servlet.server.Jsp;
|
||||
import org.springframework.boot.web.servlet.server.Session;
|
||||
import org.springframework.boot.web.servlet.server.Session.Cookie;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultServletWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Yunkun Huang
|
||||
*/
|
||||
public class DefaultServletWebServerFactoryCustomizerTests {
|
||||
|
||||
private final ServerProperties properties = new ServerProperties();
|
||||
|
||||
private DefaultServletWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ServletContextInitializer[]> initializersCaptor;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.customizer = new DefaultServletWebServerFactoryCustomizer(this.properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogIsDisabledByDefault() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogCanBeEnabled() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
assertThat(factory.getEngineValves()).first().isInstanceOf(AccessLogValve.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogFileDateFormatByDefault() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo(".yyyy-MM-dd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogFileDateFormatCanBeRedefined() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
map.put("server.tomcat.accesslog.file-date-format", "yyyy-MM-dd.HH");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogIsBufferedByDefault() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatAccessLogBufferingCanBeDisabled() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accesslog.enabled", "true");
|
||||
map.put("server.tomcat.accesslog.buffered", "false");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
|
||||
.isBuffered()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void redirectContextRootCanBeConfigured() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.redirect-context-root", "false");
|
||||
bindProperties(map);
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertThat(tomcat.getRedirectContextRoot()).isEqualTo(false);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
Context context = (Context) ((TomcatWebServer) factory.getWebServer()).getTomcat()
|
||||
.getHost().findChildren()[0];
|
||||
assertThat(context.getMapperContextRootRedirectEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useRelativeRedirectsCanBeConfigured() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.use-relative-redirects", "true");
|
||||
bindProperties(map);
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertThat(tomcat.getUseRelativeRedirects()).isTrue();
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
Context context = (Context) ((TomcatWebServer) factory.getWebServer()).getTomcat()
|
||||
.getHost().findChildren()[0];
|
||||
assertThat(context.getUseRelativeRedirects()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorReportValveIsConfiguredToNotReportStackTraces() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
Valve[] valves = ((TomcatWebServer) factory.getWebServer()).getTomcat().getHost()
|
||||
.getPipeline().getValves();
|
||||
assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
|
||||
for (Valve valve : valves) {
|
||||
if (valve instanceof ErrorReportValve) {
|
||||
ErrorReportValve errorReportValve = (ErrorReportValve) valve;
|
||||
assertThat(errorReportValve.isShowReport()).isFalse();
|
||||
assertThat(errorReportValve.isShowServerInfo()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcat() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory, never()).setContextPath("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDisplayName() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setDisplayName("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeDisplayName() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.properties.getServlet().setApplicationDisplayName("TestName");
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setDisplayName("TestName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeSsl() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
Ssl ssl = mock(Ssl.class);
|
||||
this.properties.setSsl(ssl);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setSsl(ssl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeJsp() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setJsp(any(Jsp.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeSessionProperties() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.session.timeout", "123");
|
||||
map.put("server.servlet.session.tracking-modes", "cookie,url");
|
||||
map.put("server.servlet.session.cookie.name", "testname");
|
||||
map.put("server.servlet.session.cookie.domain", "testdomain");
|
||||
map.put("server.servlet.session.cookie.path", "/testpath");
|
||||
map.put("server.servlet.session.cookie.comment", "testcomment");
|
||||
map.put("server.servlet.session.cookie.http-only", "true");
|
||||
map.put("server.servlet.session.cookie.secure", "true");
|
||||
map.put("server.servlet.session.cookie.max-age", "60");
|
||||
bindProperties(map);
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class);
|
||||
verify(factory).setSession(sessionCaptor.capture());
|
||||
assertThat(sessionCaptor.getValue().getTimeout())
|
||||
.isEqualTo(Duration.ofSeconds(123));
|
||||
Cookie cookie = sessionCaptor.getValue().getCookie();
|
||||
assertThat(cookie.getName()).isEqualTo("testname");
|
||||
assertThat(cookie.getDomain()).isEqualTo("testdomain");
|
||||
assertThat(cookie.getPath()).isEqualTo("/testpath");
|
||||
assertThat(cookie.getComment()).isEqualTo("testcomment");
|
||||
assertThat(cookie.getHttpOnly()).isTrue();
|
||||
assertThat(cookie.getMaxAge()).isEqualTo(Duration.ofSeconds(60));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatPort() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.properties.setPort(8080);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setPort(8080);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeTomcatDisplayName() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.application-display-name", "MyBootApp");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getDisplayName()).isEqualTo("MyBootApp");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote-ip-header", "");
|
||||
map.put("server.tomcat.protocol-header", "");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTomcatBackgroundProcessorDelay() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer webServer = (TomcatWebServer) factory.getWebServer();
|
||||
assertThat(webServer.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(30);
|
||||
webServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatBackgroundProcessorDelay() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.background-processor-delay", "5");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer webServer = (TomcatWebServer) factory.getWebServer();
|
||||
assertThat(webServer.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(5);
|
||||
webServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// Since 1.1.7 you need to specify at least the protocol
|
||||
map.put("server.tomcat.protocol-header", "X-Forwarded-Proto");
|
||||
map.put("server.tomcat.remote-ip-header", "X-Forwarded-For");
|
||||
bindProperties(map);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersTomcat() {
|
||||
// Since 1.3.0 no need to explicitly set header names if use-forward-header=true
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersTomcat() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
||||
private void testRemoteIpValveConfigured() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("https");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("X-Forwarded-For");
|
||||
String expectedInternalProxies = "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 10/8
|
||||
+ "192\\.168\\.\\d{1,3}\\.\\d{1,3}|" // 192.168/16
|
||||
+ "169\\.254\\.\\d{1,3}\\.\\d{1,3}|" // 169.254/16
|
||||
+ "127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 127/8
|
||||
+ "172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|" // 172.16/12
|
||||
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|"
|
||||
+ "172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}";
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo(expectedInternalProxies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatRemoteIpValve() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote-ip-header", "x-my-remote-ip-header");
|
||||
map.put("server.tomcat.protocol-header", "x-my-protocol-header");
|
||||
map.put("server.tomcat.internal-proxies", "192.168.0.1");
|
||||
map.put("server.tomcat.port-header", "x-my-forward-port");
|
||||
map.put("server.tomcat.protocol-header-https-value", "On");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getEngineValves()).hasSize(1);
|
||||
Valve valve = factory.getEngineValves().iterator().next();
|
||||
assertThat(valve).isInstanceOf(RemoteIpValve.class);
|
||||
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
||||
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header");
|
||||
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("On");
|
||||
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("x-my-remote-ip-header");
|
||||
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
|
||||
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatAcceptCount() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.accept-count", "10");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory.getWebServer();
|
||||
server.start();
|
||||
try {
|
||||
assertThat(((AbstractProtocol<?>) server.getTomcat().getConnector()
|
||||
.getProtocolHandler()).getAcceptCount()).isEqualTo(10);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatMaxConnections() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-connections", "5");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory.getWebServer();
|
||||
server.start();
|
||||
try {
|
||||
assertThat(((AbstractProtocol<?>) server.getTomcat().getConnector()
|
||||
.getProtocolHandler()).getMaxConnections()).isEqualTo(5);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatMaxHttpPostSize() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-http-post-size", "10000");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory.getWebServer();
|
||||
server.start();
|
||||
try {
|
||||
assertThat(server.getTomcat().getConnector().getMaxPostSize())
|
||||
.isEqualTo(10000);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatDisableMaxHttpPostSize() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.max-http-post-size", "-1");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory.getWebServer();
|
||||
server.start();
|
||||
try {
|
||||
assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(-1);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeUndertowAccessLog() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.undertow.accesslog.enabled", "true");
|
||||
map.put("server.undertow.accesslog.pattern", "foo");
|
||||
map.put("server.undertow.accesslog.prefix", "test_log");
|
||||
map.put("server.undertow.accesslog.suffix", "txt");
|
||||
map.put("server.undertow.accesslog.dir", "test-logs");
|
||||
map.put("server.undertow.accesslog.rotate", "false");
|
||||
bindProperties(map);
|
||||
UndertowServletWebServerFactory factory = spy(
|
||||
new UndertowServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setAccessLogEnabled(true);
|
||||
verify(factory).setAccessLogPattern("foo");
|
||||
verify(factory).setAccessLogPrefix("test_log");
|
||||
verify(factory).setAccessLogSuffix("txt");
|
||||
verify(factory).setAccessLogDirectory(new File("test-logs"));
|
||||
verify(factory).setAccessLogRotate(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatMinSpareThreads() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.min-spare-threads", "10");
|
||||
bindProperties(map);
|
||||
assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatTldSkip() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.additional-tld-skip-patterns", "foo.jar,bar.jar");
|
||||
bindProperties(map);
|
||||
testCustomTomcatTldSkip("foo.jar", "bar.jar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatTldSkipAsList() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.additional-tld-skip-patterns[0]", "biz.jar");
|
||||
map.put("server.tomcat.additional-tld-skip-patterns[1]", "bah.jar");
|
||||
bindProperties(map);
|
||||
testCustomTomcatTldSkip("biz.jar", "bah.jar");
|
||||
}
|
||||
|
||||
private void testCustomTomcatTldSkip(String... expectedJars) {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
assertThat(factory.getTldSkipPatterns()).contains(expectedJars);
|
||||
assertThat(factory.getTldSkipPatterns()).contains("junit-*.jar",
|
||||
"spring-boot-*.jar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultUseForwardHeadersUndertow() {
|
||||
UndertowServletWebServerFactory factory = spy(
|
||||
new UndertowServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersUndertow() {
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
UndertowServletWebServerFactory factory = spy(
|
||||
new UndertowServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersUndertow() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
UndertowServletWebServerFactory factory = spy(
|
||||
new UndertowServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultUseForwardHeadersJetty() {
|
||||
JettyServletWebServerFactory factory = spy(new JettyServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUseForwardHeadersJetty() {
|
||||
this.properties.setUseForwardHeaders(true);
|
||||
JettyServletWebServerFactory factory = spy(new JettyServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeadersJetty() {
|
||||
this.customizer.setEnvironment(new MockEnvironment().withProperty("DYNO", "-"));
|
||||
JettyServletWebServerFactory factory = spy(new JettyServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setUseForwardHeaders(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sessionStoreDir() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.session.store-dir", "myfolder");
|
||||
bindProperties(map);
|
||||
JettyServletWebServerFactory factory = spy(new JettyServletWebServerFactory());
|
||||
this.customizer.customize(factory);
|
||||
ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class);
|
||||
verify(factory).setSession(sessionCaptor.capture());
|
||||
assertThat(sessionCaptor.getValue().getStoreDir())
|
||||
.isEqualTo(new File("myfolder"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jettyAccessLogCanBeEnabled() {
|
||||
JettyServletWebServerFactory factory = new JettyServletWebServerFactory(0);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.jetty.accesslog.enabled", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
JettyWebServer webServer = (JettyWebServer) factory.getWebServer();
|
||||
try {
|
||||
NCSARequestLog requestLog = getNCSARequestLog(webServer);
|
||||
assertThat(requestLog.getFilename()).isNull();
|
||||
assertThat(requestLog.isAppend()).isFalse();
|
||||
assertThat(requestLog.isExtended()).isFalse();
|
||||
assertThat(requestLog.getLogCookies()).isFalse();
|
||||
assertThat(requestLog.getLogServer()).isFalse();
|
||||
assertThat(requestLog.getLogLatency()).isFalse();
|
||||
}
|
||||
finally {
|
||||
webServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jettyAccessLogCanBeCustomized() throws IOException {
|
||||
File logFile = File.createTempFile("jetty_log", ".log");
|
||||
JettyServletWebServerFactory factory = new JettyServletWebServerFactory(0);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
String timezone = TimeZone.getDefault().getID();
|
||||
map.put("server.jetty.accesslog.enabled", "true");
|
||||
map.put("server.jetty.accesslog.filename", logFile.getAbsolutePath());
|
||||
map.put("server.jetty.accesslog.file-date-format", "yyyy-MM-dd");
|
||||
map.put("server.jetty.accesslog.retention-period", "42");
|
||||
map.put("server.jetty.accesslog.append", "true");
|
||||
map.put("server.jetty.accesslog.extended-format", "true");
|
||||
map.put("server.jetty.accesslog.date-format", "HH:mm:ss");
|
||||
map.put("server.jetty.accesslog.locale", "en_BE");
|
||||
map.put("server.jetty.accesslog.time-zone", timezone);
|
||||
map.put("server.jetty.accesslog.log-cookies", "true");
|
||||
map.put("server.jetty.accesslog.log-server", "true");
|
||||
map.put("server.jetty.accesslog.log-latency", "true");
|
||||
bindProperties(map);
|
||||
this.customizer.customize(factory);
|
||||
JettyWebServer webServer = (JettyWebServer) factory.getWebServer();
|
||||
NCSARequestLog requestLog = getNCSARequestLog(webServer);
|
||||
try {
|
||||
assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
|
||||
assertThat(requestLog.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
|
||||
assertThat(requestLog.getRetainDays()).isEqualTo(42);
|
||||
assertThat(requestLog.isAppend()).isTrue();
|
||||
assertThat(requestLog.isExtended()).isTrue();
|
||||
assertThat(requestLog.getLogDateFormat()).isEqualTo("HH:mm:ss");
|
||||
assertThat(requestLog.getLogLocale()).isEqualTo(new Locale("en", "BE"));
|
||||
assertThat(requestLog.getLogTimeZone()).isEqualTo(timezone);
|
||||
assertThat(requestLog.getLogCookies()).isTrue();
|
||||
assertThat(requestLog.getLogServer()).isTrue();
|
||||
assertThat(requestLog.getLogLatency()).isTrue();
|
||||
}
|
||||
finally {
|
||||
webServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private NCSARequestLog getNCSARequestLog(JettyWebServer webServer) {
|
||||
RequestLog requestLog = webServer.getServer().getRequestLog();
|
||||
assertThat(requestLog).isInstanceOf(NCSARequestLog.class);
|
||||
return (NCSARequestLog) requestLog;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipNullElementsForUndertow() {
|
||||
UndertowServletWebServerFactory factory = mock(
|
||||
UndertowServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory, never()).setAccessLogEnabled(anyBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatStaticResourceCacheTtl() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.resource.cache-ttl", "10000");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
TomcatWebServer server = (TomcatWebServer) factory.getWebServer();
|
||||
server.start();
|
||||
try {
|
||||
Tomcat tomcat = server.getTomcat();
|
||||
Context context = (Context) tomcat.getHost().findChildren()[0];
|
||||
assertThat(context.getResources().getCacheTtl()).isEqualTo(10000L);
|
||||
}
|
||||
finally {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private void bindProperties(Map<String, String> map) {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(map);
|
||||
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.boot.web.servlet.server.Jsp;
|
||||
import org.springframework.boot.web.servlet.server.Session;
|
||||
import org.springframework.boot.web.servlet.server.Session.Cookie;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link ServletWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Yunkun Huang
|
||||
*/
|
||||
public class ServletWebServerFactoryCustomizerTests {
|
||||
|
||||
private final ServerProperties properties = new ServerProperties();
|
||||
|
||||
private ServletWebServerFactoryCustomizer customizer;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ServletContextInitializer[]> initializersCaptor;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.customizer = new ServletWebServerFactoryCustomizer(
|
||||
this.properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDisplayName() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setDisplayName("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeDisplayName() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.properties.getServlet().setApplicationDisplayName("TestName");
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setDisplayName("TestName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeSsl() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
Ssl ssl = mock(Ssl.class);
|
||||
this.properties.setSsl(ssl);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setSsl(ssl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeJsp() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setJsp(any(Jsp.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeSessionProperties() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.session.timeout", "123");
|
||||
map.put("server.servlet.session.tracking-modes", "cookie,url");
|
||||
map.put("server.servlet.session.cookie.name", "testname");
|
||||
map.put("server.servlet.session.cookie.domain", "testdomain");
|
||||
map.put("server.servlet.session.cookie.path", "/testpath");
|
||||
map.put("server.servlet.session.cookie.comment", "testcomment");
|
||||
map.put("server.servlet.session.cookie.http-only", "true");
|
||||
map.put("server.servlet.session.cookie.secure", "true");
|
||||
map.put("server.servlet.session.cookie.max-age", "60");
|
||||
bindProperties(map);
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class);
|
||||
verify(factory).setSession(sessionCaptor.capture());
|
||||
assertThat(sessionCaptor.getValue().getTimeout())
|
||||
.isEqualTo(Duration.ofSeconds(123));
|
||||
Cookie cookie = sessionCaptor.getValue().getCookie();
|
||||
assertThat(cookie.getName()).isEqualTo("testname");
|
||||
assertThat(cookie.getDomain()).isEqualTo("testdomain");
|
||||
assertThat(cookie.getPath()).isEqualTo("/testpath");
|
||||
assertThat(cookie.getComment()).isEqualTo("testcomment");
|
||||
assertThat(cookie.getHttpOnly()).isTrue();
|
||||
assertThat(cookie.getMaxAge()).isEqualTo(Duration.ofSeconds(60));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatPort() {
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.properties.setPort(8080);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setPort(8080);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeServletDisplayName() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.application-display-name", "MyBootApp");
|
||||
bindProperties(map);
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
verify(factory).setDisplayName("MyBootApp");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatMinSpareThreads() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.min-spare-threads", "10");
|
||||
bindProperties(map);
|
||||
assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sessionStoreDir() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.servlet.session.store-dir", "myfolder");
|
||||
bindProperties(map);
|
||||
ConfigurableServletWebServerFactory factory = mock(
|
||||
ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class);
|
||||
verify(factory).setSession(sessionCaptor.capture());
|
||||
assertThat(sessionCaptor.getValue().getStoreDir())
|
||||
.isEqualTo(new File("myfolder"));
|
||||
}
|
||||
|
||||
private void bindProperties(Map<String, String> map) {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(map);
|
||||
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TomcatServletWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class TomcatServletWebServerFactoryCustomizerTests {
|
||||
|
||||
private TomcatServletWebServerFactoryCustomizer customizer;
|
||||
|
||||
private MockEnvironment environment;
|
||||
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.environment = new MockEnvironment();
|
||||
this.serverProperties = new ServerProperties();
|
||||
ConfigurationPropertySources.attach(this.environment);
|
||||
this.customizer = new TomcatServletWebServerFactoryCustomizer(
|
||||
this.serverProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTldSkip() {
|
||||
bind("server.tomcat.additional-tld-skip-patterns=foo.jar,bar.jar");
|
||||
testCustomTldSkip("foo.jar", "bar.jar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTldSkipAsList() {
|
||||
bind("server.tomcat.additional-tld-skip-patterns[0]=biz.jar",
|
||||
"server.tomcat.additional-tld-skip-patterns[1]=bah.jar");
|
||||
testCustomTldSkip("biz.jar", "bah.jar");
|
||||
}
|
||||
|
||||
private void testCustomTldSkip(String... expectedJars) {
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
assertThat(factory.getTldSkipPatterns()).contains(expectedJars);
|
||||
assertThat(factory.getTldSkipPatterns()).contains("junit-*.jar",
|
||||
"spring-boot-*.jar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void redirectContextRootCanBeConfigured() {
|
||||
bind("server.tomcat.redirect-context-root=false");
|
||||
ServerProperties.Tomcat tomcat = this.serverProperties.getTomcat();
|
||||
assertThat(tomcat.getRedirectContextRoot()).isEqualTo(false);
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
Context context = (Context) server.getTomcat().getHost().findChildren()[0];
|
||||
assertThat(context.getMapperContextRootRedirectEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useRelativeRedirectsCanBeConfigured() {
|
||||
bind("server.tomcat.use-relative-redirects=true");
|
||||
assertThat(this.serverProperties.getTomcat().getUseRelativeRedirects()).isTrue();
|
||||
TomcatWebServer server = customizeAndGetServer();
|
||||
Context context = (Context) server.getTomcat().getHost().findChildren()[0];
|
||||
assertThat(context.getUseRelativeRedirects()).isTrue();
|
||||
}
|
||||
|
||||
private void bind(String... inlinedProperties) {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
||||
inlinedProperties);
|
||||
new Binder(ConfigurationPropertySources.get(this.environment)).bind("server",
|
||||
Bindable.ofInstance(this.serverProperties));
|
||||
}
|
||||
|
||||
private TomcatWebServer customizeAndGetServer() {
|
||||
TomcatServletWebServerFactory factory = customizeAndGetFactory();
|
||||
return (TomcatWebServer) factory.getWebServer();
|
||||
}
|
||||
|
||||
private TomcatServletWebServerFactory customizeAndGetFactory() {
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
|
||||
this.customizer.customize(factory);
|
||||
return factory;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue