Commit d264af81 authored by Brian Clozel's avatar Brian Clozel

Apply server.tomcat.* config to reactive servers

This commit applies most `server.tomcat.*` configuration
properties to Tomcat when set up as a reactive web server.

Some Servlet-specific properties are not applied:

* server.tomcat.additional-tld-skip-patterns
* server.tomcat.redirect-context-root
* server.tomcat.use-relative-redirects

Fixes gh-11334
parent 4b59d5f5
...@@ -22,7 +22,7 @@ import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType; ...@@ -22,7 +22,7 @@ import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerFactoryCustomizer; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.reactive.DefaultReactiveWebServerCustomizer; import org.springframework.boot.autoconfigure.web.reactive.DefaultReactiveWebServerFactoryCustomizer;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -58,7 +58,7 @@ public class ReactiveManagementChildContextConfiguration { ...@@ -58,7 +58,7 @@ public class ReactiveManagementChildContextConfiguration {
ManagementServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> { ManagementServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> {
ReactiveManagementServerFactoryCustomizer(ListableBeanFactory beanFactory) { ReactiveManagementServerFactoryCustomizer(ListableBeanFactory beanFactory) {
super(beanFactory, DefaultReactiveWebServerCustomizer.class); super(beanFactory, DefaultReactiveWebServerFactoryCustomizer.class);
} }
} }
......
/*
* 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.tomcat;
import java.time.Duration;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/**
* Customization for Tomcat-specific features common
* for both Servlet and Reactive servers.
*
* @author Brian Clozel
* @since 2.0.0
*/
public final class TomcatCustomizer {
private TomcatCustomizer() {
}
public static void customizeTomcat(ServerProperties serverProperties,
Environment environment, ConfigurableTomcatWebServerFactory factory) {
ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat();
if (tomcatProperties.getBasedir() != null) {
factory.setBaseDirectory(tomcatProperties.getBasedir());
}
if (tomcatProperties.getBackgroundProcessorDelay() != null) {
factory.setBackgroundProcessorDelay((int) tomcatProperties
.getBackgroundProcessorDelay().getSeconds());
}
customizeRemoteIpValve(serverProperties, environment, factory);
if (tomcatProperties.getMaxThreads() > 0) {
customizeMaxThreads(factory, tomcatProperties.getMaxThreads());
}
if (tomcatProperties.getMinSpareThreads() > 0) {
customizeMinThreads(factory, tomcatProperties.getMinSpareThreads());
}
int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0
? serverProperties.getMaxHttpHeaderSize()
: tomcatProperties.getMaxHttpHeaderSize());
if (maxHttpHeaderSize > 0) {
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize);
}
if (tomcatProperties.getMaxHttpPostSize() != 0) {
customizeMaxHttpPostSize(factory, tomcatProperties.getMaxHttpPostSize());
}
if (tomcatProperties.getAccesslog().isEnabled()) {
customizeAccessLog(tomcatProperties, factory);
}
if (tomcatProperties.getUriEncoding() != null) {
factory.setUriEncoding(tomcatProperties.getUriEncoding());
}
if (serverProperties.getConnectionTimeout() != null) {
customizeConnectionTimeout(factory,
serverProperties.getConnectionTimeout());
}
if (tomcatProperties.getMaxConnections() > 0) {
customizeMaxConnections(factory, tomcatProperties.getMaxConnections());
}
if (tomcatProperties.getAcceptCount() > 0) {
customizeAcceptCount(factory, tomcatProperties.getAcceptCount());
}
customizeStaticResources(serverProperties.getTomcat().getResource(), factory);
}
private static void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory,
int acceptCount) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setAcceptCount(acceptCount);
}
});
}
private static void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory,
int maxConnections) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setMaxConnections(maxConnections);
}
});
}
private static void customizeConnectionTimeout(
ConfigurableTomcatWebServerFactory factory, Duration connectionTimeout) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setConnectionTimeout((int) connectionTimeout.toMillis());
}
});
}
private static void customizeRemoteIpValve(ServerProperties properties,
Environment environment, ConfigurableTomcatWebServerFactory factory) {
String protocolHeader = properties.getTomcat().getProtocolHeader();
String remoteIpHeader = properties.getTomcat().getRemoteIpHeader();
// For back compatibility the valve is also enabled if protocol-header is set
if (StringUtils.hasText(protocolHeader) || StringUtils.hasText(remoteIpHeader)
|| getOrDeduceUseForwardHeaders(properties, environment)) {
RemoteIpValve valve = new RemoteIpValve();
valve.setProtocolHeader(StringUtils.hasLength(protocolHeader)
? protocolHeader : "X-Forwarded-Proto");
if (StringUtils.hasLength(remoteIpHeader)) {
valve.setRemoteIpHeader(remoteIpHeader);
}
// The internal proxies default to a white list of "safe" internal IP
// addresses
valve.setInternalProxies(properties.getTomcat().getInternalProxies());
valve.setPortHeader(properties.getTomcat().getPortHeader());
valve.setProtocolHeaderHttpsValue(
properties.getTomcat().getProtocolHeaderHttpsValue());
// ... so it's safe to add this valve by default.
factory.addEngineValves(valve);
}
}
private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
Environment environment) {
if (serverProperties.isUseForwardHeaders() != null) {
return serverProperties.isUseForwardHeaders();
}
CloudPlatform platform = CloudPlatform.getActive(environment);
return platform != null && platform.isUsingForwardHeaders();
}
@SuppressWarnings("rawtypes")
private static void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory,
int maxThreads) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMaxThreads(maxThreads);
}
});
}
@SuppressWarnings("rawtypes")
private static void customizeMinThreads(ConfigurableTomcatWebServerFactory factory,
int minSpareThreads) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMinSpareThreads(minSpareThreads);
}
});
}
@SuppressWarnings("rawtypes")
private static void customizeMaxHttpHeaderSize(
ConfigurableTomcatWebServerFactory factory, int maxHttpHeaderSize) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
protocol.setMaxHttpHeaderSize(maxHttpHeaderSize);
}
});
}
private static void customizeMaxHttpPostSize(
ConfigurableTomcatWebServerFactory factory, int maxHttpPostSize) {
factory.addConnectorCustomizers(
(connector) -> connector.setMaxPostSize(maxHttpPostSize));
}
private static void customizeAccessLog(ServerProperties.Tomcat tomcatProperties,
ConfigurableTomcatWebServerFactory factory) {
AccessLogValve valve = new AccessLogValve();
valve.setPattern(tomcatProperties.getAccesslog().getPattern());
valve.setDirectory(tomcatProperties.getAccesslog().getDirectory());
valve.setPrefix(tomcatProperties.getAccesslog().getPrefix());
valve.setSuffix(tomcatProperties.getAccesslog().getSuffix());
valve.setRenameOnRotate(tomcatProperties.getAccesslog().isRenameOnRotate());
valve.setFileDateFormat(tomcatProperties.getAccesslog().getFileDateFormat());
valve.setRequestAttributesEnabled(
tomcatProperties.getAccesslog().isRequestAttributesEnabled());
valve.setRotatable(tomcatProperties.getAccesslog().isRotate());
valve.setBuffered(tomcatProperties.getAccesslog().isBuffered());
factory.addEngineValves(valve);
}
private static void customizeStaticResources(ServerProperties.Tomcat.Resource resource,
ConfigurableTomcatWebServerFactory factory) {
if (resource.getCacheTtl() == null) {
return;
}
factory.addContextCustomizers((context) -> {
context.addLifecycleListener((event) -> {
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
});
});
}
}
/*
* 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;
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,9 +17,13 @@ ...@@ -17,9 +17,13 @@
package org.springframework.boot.autoconfigure.web.reactive; package org.springframework.boot.autoconfigure.web.reactive;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.embedded.tomcat.TomcatCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
/** /**
* Default {@link WebServerFactoryCustomizer} for reactive servers. * Default {@link WebServerFactoryCustomizer} for reactive servers.
...@@ -27,12 +31,14 @@ import org.springframework.core.Ordered; ...@@ -27,12 +31,14 @@ import org.springframework.core.Ordered;
* @author Brian Clozel * @author Brian Clozel
* @since 2.0.0 * @since 2.0.0
*/ */
public class DefaultReactiveWebServerCustomizer implements public class DefaultReactiveWebServerFactoryCustomizer implements
WebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory>, Ordered { WebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory>, EnvironmentAware, Ordered {
private final ServerProperties serverProperties; private final ServerProperties serverProperties;
public DefaultReactiveWebServerCustomizer(ServerProperties serverProperties) { private Environment environment;
public DefaultReactiveWebServerFactoryCustomizer(ServerProperties serverProperties) {
this.serverProperties = serverProperties; this.serverProperties = serverProperties;
} }
...@@ -42,21 +48,30 @@ public class DefaultReactiveWebServerCustomizer implements ...@@ -42,21 +48,30 @@ public class DefaultReactiveWebServerCustomizer implements
} }
@Override @Override
public void customize(ConfigurableReactiveWebServerFactory server) { public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void customize(ConfigurableReactiveWebServerFactory factory) {
if (this.serverProperties.getPort() != null) { if (this.serverProperties.getPort() != null) {
server.setPort(this.serverProperties.getPort()); factory.setPort(this.serverProperties.getPort());
} }
if (this.serverProperties.getAddress() != null) { if (this.serverProperties.getAddress() != null) {
server.setAddress(this.serverProperties.getAddress()); factory.setAddress(this.serverProperties.getAddress());
} }
if (this.serverProperties.getSsl() != null) { if (this.serverProperties.getSsl() != null) {
server.setSsl(this.serverProperties.getSsl()); factory.setSsl(this.serverProperties.getSsl());
} }
if (this.serverProperties.getCompression() != null) { if (this.serverProperties.getCompression() != null) {
server.setCompression(this.serverProperties.getCompression()); factory.setCompression(this.serverProperties.getCompression());
} }
if (this.serverProperties.getHttp2() != null) { if (this.serverProperties.getHttp2() != null) {
server.setHttp2(this.serverProperties.getHttp2()); factory.setHttp2(this.serverProperties.getHttp2());
}
if (factory instanceof TomcatReactiveWebServerFactory) {
TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment,
(TomcatReactiveWebServerFactory) factory);
} }
} }
......
...@@ -59,9 +59,9 @@ public class ReactiveWebServerAutoConfiguration { ...@@ -59,9 +59,9 @@ public class ReactiveWebServerAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Bean @Bean
public DefaultReactiveWebServerCustomizer defaultReactiveWebServerCustomizer( public DefaultReactiveWebServerFactoryCustomizer defaultReactiveWebServerCustomizer(
ServerProperties serverProperties) { ServerProperties serverProperties) {
return new DefaultReactiveWebServerCustomizer(serverProperties); return new DefaultReactiveWebServerFactoryCustomizer(serverProperties);
} }
/** /**
......
/*
* 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.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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link DefaultReactiveWebServerCustomizer}.
*
* @author Brian Clozel
*/
public class DefaultReactiveWebServerCustomizerTests {
private final ServerProperties properties = new ServerProperties();
private DefaultReactiveWebServerCustomizer customizer;
@Before
public void setup() {
this.customizer = new DefaultReactiveWebServerCustomizer(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);
}
}
...@@ -54,7 +54,7 @@ public class ReactiveWebServerAutoConfigurationTests { ...@@ -54,7 +54,7 @@ public class ReactiveWebServerAutoConfigurationTests {
.hasSize(1); .hasSize(1);
assertThat(this.context.getBeansOfType(WebServerFactoryCustomizer.class)) assertThat(this.context.getBeansOfType(WebServerFactoryCustomizer.class))
.hasSize(1); .hasSize(1);
assertThat(this.context.getBeansOfType(DefaultReactiveWebServerCustomizer.class)) assertThat(this.context.getBeansOfType(DefaultReactiveWebServerFactoryCustomizer.class))
.hasSize(1); .hasSize(1);
} }
......
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
<subpackage name="client"> <subpackage name="client">
<allow pkg="org.springframework.boot.web.client" /> <allow pkg="org.springframework.boot.web.client" />
</subpackage> </subpackage>
<subpackage name="embedded.tomcat">
<allow pkg="org.springframework.boot.web.embedded.tomcat" />
</subpackage>
<subpackage name="servlet"> <subpackage name="servlet">
<allow pkg="javax.servlet" /> <allow pkg="javax.servlet" />
<allow pkg="org.springframework.boot.web.embedded" /> <allow pkg="org.springframework.boot.web.embedded" />
......
/*
* 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.web.embedded.tomcat;
import java.io.File;
import java.nio.charset.Charset;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Connector;
/**
* Web Server Factory configuration for Tomcat-specific features.
*
* @author Brian Clozel
* @since 2.0.0
* @see TomcatServletWebServerFactory
* @see TomcatReactiveWebServerFactory
*/
public interface ConfigurableTomcatWebServerFactory {
/**
* Set the Tomcat base directory. If not specified a temporary directory will be used.
* @param baseDirectory the tomcat base directory
*/
void setBaseDirectory(File baseDirectory);
/**
* Sets the background processor delay in seconds.
* @param delay the delay in seconds
*/
void setBackgroundProcessorDelay(int delay);
/**
* Add {@link Valve}s that should be applied to the Tomcat {@link Engine}.
* @param engineValves the valves to add
*/
void addEngineValves(Valve... engineValves);
/**
* Add {@link TomcatConnectorCustomizer}s that should be added to the Tomcat
* {@link Connector}.
* @param tomcatConnectorCustomizers the customizers to add
*/
void addConnectorCustomizers(TomcatConnectorCustomizer... tomcatConnectorCustomizers);
/**
* Add {@link TomcatContextCustomizer}s that should be added to the Tomcat
* {@link Context}.
* @param tomcatContextCustomizers the customizers to add
*/
void addContextCustomizers(TomcatContextCustomizer... tomcatContextCustomizers);
/**
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will
* be used.
* @param uriEncoding the uri encoding to set
*/
void setUriEncoding(Charset uriEncoding);
}
...@@ -22,7 +22,7 @@ import org.apache.catalina.connector.Connector; ...@@ -22,7 +22,7 @@ import org.apache.catalina.connector.Connector;
* Callback interface that can be used to customize a Tomcat {@link Connector}. * Callback interface that can be used to customize a Tomcat {@link Connector}.
* *
* @author Dave Syer * @author Dave Syer
* @see TomcatServletWebServerFactory * @see ConfigurableTomcatWebServerFactory
* @since 2.0.0 * @since 2.0.0
*/ */
@FunctionalInterface @FunctionalInterface
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package org.springframework.boot.web.embedded.tomcat; package org.springframework.boot.web.embedded.tomcat;
import java.io.File; import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
...@@ -24,14 +26,17 @@ import java.util.Collections; ...@@ -24,14 +26,17 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host; import org.apache.catalina.Host;
import org.apache.catalina.LifecycleListener; import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.loader.WebappLoader; import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.http2.Http2Protocol; import org.apache.coyote.http2.Http2Protocol;
import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
...@@ -48,14 +53,19 @@ import org.springframework.util.StringUtils; ...@@ -48,14 +53,19 @@ import org.springframework.util.StringUtils;
* @author Brian Clozel * @author Brian Clozel
* @since 2.0.0 * @since 2.0.0
*/ */
public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFactory { public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableTomcatWebServerFactory {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
/** /**
* The class name of default protocol used. * The class name of default protocol used.
*/ */
public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol"; public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
private String protocol = DEFAULT_PROTOCOL; private File baseDirectory;
private List<Valve> engineValves = new ArrayList<>();
private List<LifecycleListener> contextLifecycleListeners = new ArrayList<>( private List<LifecycleListener> contextLifecycleListeners = new ArrayList<>(
Collections.singleton(new AprLifecycleListener())); Collections.singleton(new AprLifecycleListener()));
...@@ -64,6 +74,13 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -64,6 +74,13 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>(); private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>();
private String protocol = DEFAULT_PROTOCOL;
private Charset uriEncoding = DEFAULT_CHARSET;
private int backgroundProcessorDelay;
/** /**
* Create a new {@link TomcatServletWebServerFactory} instance. * Create a new {@link TomcatServletWebServerFactory} instance.
*/ */
...@@ -81,22 +98,26 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -81,22 +98,26 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
@Override @Override
public WebServer getWebServer(HttpHandler httpHandler) { public WebServer getWebServer(HttpHandler httpHandler) {
Tomcat tomcatServer = createTomcatServer();
TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler);
prepareContext(tomcatServer.getHost(), servlet);
return new TomcatWebServer(tomcatServer, getPort() >= 0);
}
private Tomcat createTomcatServer() {
Tomcat tomcat = new Tomcat(); Tomcat tomcat = new Tomcat();
File baseDir = createTempDir("tomcat"); File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath()); tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol); Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector); tomcat.getService().addConnector(connector);
customizeConnector(connector); customizeConnector(connector);
tomcat.setConnector(connector); tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false); tomcat.getHost().setAutoDeploy(false);
return tomcat; configureEngine(tomcat.getEngine());
TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler);
prepareContext(tomcat.getHost(), servlet);
return new TomcatWebServer(tomcat, getPort() >= 0);
}
private void configureEngine(Engine engine) {
engine.setBackgroundProcessorDelay(this.backgroundProcessorDelay);
for (Valve valve : this.engineValves) {
engine.getPipeline().addValve(valve);
}
} }
protected void prepareContext(Host host, TomcatHttpHandlerAdapter servlet) { protected void prepareContext(Host host, TomcatHttpHandlerAdapter servlet) {
...@@ -106,6 +127,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -106,6 +127,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
context.setDocBase(docBase.getAbsolutePath()); context.setDocBase(docBase.getAbsolutePath());
context.addLifecycleListener(new Tomcat.FixContextListener()); context.addLifecycleListener(new Tomcat.FixContextListener());
context.setParentClassLoader(ClassUtils.getDefaultClassLoader()); context.setParentClassLoader(ClassUtils.getDefaultClassLoader());
skipAllTldScanning(context);
WebappLoader loader = new WebappLoader(context.getParentClassLoader()); WebappLoader loader = new WebappLoader(context.getParentClassLoader());
loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName()); loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
loader.setDelegate(true); loader.setDelegate(true);
...@@ -116,6 +138,12 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -116,6 +138,12 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
host.addChild(context); host.addChild(context);
} }
private void skipAllTldScanning(TomcatEmbeddedContext context) {
StandardJarScanFilter filter = new StandardJarScanFilter();
filter.setTldSkip("*.jar");
context.getJarScanner().setJarScanFilter(filter);
}
/** /**
* Configure the Tomcat {@link Context}. * Configure the Tomcat {@link Context}.
* @param context the Tomcat context * @param context the Tomcat context
...@@ -135,6 +163,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -135,6 +163,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
if (connector.getProtocolHandler() instanceof AbstractProtocol) { if (connector.getProtocolHandler() instanceof AbstractProtocol) {
customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler()); customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler());
} }
if (getUriEncoding() != null) {
connector.setURIEncoding(getUriEncoding().name());
}
// Don't bind to the socket prematurely if ApplicationContext is slow to start // Don't bind to the socket prematurely if ApplicationContext is slow to start
connector.setProperty("bindOnInit", "false"); connector.setProperty("bindOnInit", "false");
if (getSsl() != null && getSsl().isEnabled()) { if (getSsl() != null && getSsl().isEnabled()) {
...@@ -161,6 +192,16 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -161,6 +192,16 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
} }
} }
@Override
public void setBaseDirectory(File baseDirectory) {
this.baseDirectory = baseDirectory;
}
@Override
public void setBackgroundProcessorDelay(int delay) {
this.backgroundProcessorDelay = delay;
}
/** /**
* Set {@link TomcatContextCustomizer}s that should be applied to the Tomcat * Set {@link TomcatContextCustomizer}s that should be applied to the Tomcat
* {@link Context}. Calling this method will replace any existing customizers. * {@link Context}. Calling this method will replace any existing customizers.
...@@ -187,6 +228,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -187,6 +228,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
* {@link Context}. * {@link Context}.
* @param tomcatContextCustomizers the customizers to add * @param tomcatContextCustomizers the customizers to add
*/ */
@Override
public void addContextCustomizers( public void addContextCustomizers(
TomcatContextCustomizer... tomcatContextCustomizers) { TomcatContextCustomizer... tomcatContextCustomizers) {
Assert.notNull(tomcatContextCustomizers, Assert.notNull(tomcatContextCustomizers,
...@@ -211,6 +253,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -211,6 +253,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
* {@link Connector}. * {@link Connector}.
* @param tomcatConnectorCustomizers the customizers to add * @param tomcatConnectorCustomizers the customizers to add
*/ */
@Override
public void addConnectorCustomizers( public void addConnectorCustomizers(
TomcatConnectorCustomizer... tomcatConnectorCustomizers) { TomcatConnectorCustomizer... tomcatConnectorCustomizers) {
Assert.notNull(tomcatConnectorCustomizers, Assert.notNull(tomcatConnectorCustomizers,
...@@ -227,6 +270,39 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac ...@@ -227,6 +270,39 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
return this.tomcatConnectorCustomizers; return this.tomcatConnectorCustomizers;
} }
@Override
public void addEngineValves(Valve... engineValves) {
Assert.notNull(engineValves, "Valves must not be null");
this.engineValves.addAll(Arrays.asList(engineValves));
}
/**
* Returns a mutable collection of the {@link Valve}s that will be applied to the
* Tomcat {@link Engine}.
* @return the engine valves that will be applied
*/
public List<Valve> getEngineValves() {
return this.engineValves;
}
/**
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will
* be used.
* @param uriEncoding the uri encoding to set
*/
@Override
public void setUriEncoding(Charset uriEncoding) {
this.uriEncoding = uriEncoding;
}
/**
* Returns the character encoding to use for URL decoding.
* @return the URI encoding
*/
public Charset getUriEncoding() {
return this.uriEncoding;
}
/** /**
* Set {@link LifecycleListener}s that should be applied to the Tomcat {@link Context}. * Set {@link LifecycleListener}s that should be applied to the Tomcat {@link Context}.
* Calling this method will replace any existing listeners. * Calling this method will replace any existing listeners.
......
...@@ -96,7 +96,7 @@ import org.springframework.util.StringUtils; ...@@ -96,7 +96,7 @@ import org.springframework.util.StringUtils;
* @see TomcatWebServer * @see TomcatWebServer
*/ */
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory
implements ResourceLoaderAware { implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
...@@ -418,10 +418,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -418,10 +418,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
} }
/** @Override
* Set the Tomcat base directory. If not specified a temporary directory will be used.
* @param baseDirectory the tomcat base directory
*/
public void setBaseDirectory(File baseDirectory) { public void setBaseDirectory(File baseDirectory) {
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
} }
...@@ -483,10 +480,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -483,10 +480,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
return this.engineValves; return this.engineValves;
} }
/** @Override
* Add {@link Valve}s that should be applied to the Tomcat {@link Engine}.
* @param engineValves the valves to add
*/
public void addEngineValves(Valve... engineValves) { public void addEngineValves(Valve... engineValves) {
Assert.notNull(engineValves, "Valves must not be null"); Assert.notNull(engineValves, "Valves must not be null");
this.engineValves.addAll(Arrays.asList(engineValves)); this.engineValves.addAll(Arrays.asList(engineValves));
...@@ -574,11 +568,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -574,11 +568,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
return this.tomcatContextCustomizers; return this.tomcatContextCustomizers;
} }
/** @Override
* Add {@link TomcatContextCustomizer}s that should be added to the Tomcat
* {@link Context}.
* @param tomcatContextCustomizers the customizers to add
*/
public void addContextCustomizers( public void addContextCustomizers(
TomcatContextCustomizer... tomcatContextCustomizers) { TomcatContextCustomizer... tomcatContextCustomizers) {
Assert.notNull(tomcatContextCustomizers, Assert.notNull(tomcatContextCustomizers,
...@@ -598,11 +588,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -598,11 +588,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
this.tomcatConnectorCustomizers = new ArrayList<>(tomcatConnectorCustomizers); this.tomcatConnectorCustomizers = new ArrayList<>(tomcatConnectorCustomizers);
} }
/** @Override
* Add {@link TomcatConnectorCustomizer}s that should be added to the Tomcat
* {@link Connector}.
* @param tomcatConnectorCustomizers the customizers to add
*/
public void addConnectorCustomizers( public void addConnectorCustomizers(
TomcatConnectorCustomizer... tomcatConnectorCustomizers) { TomcatConnectorCustomizer... tomcatConnectorCustomizers) {
Assert.notNull(tomcatConnectorCustomizers, Assert.notNull(tomcatConnectorCustomizers,
...@@ -637,11 +623,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -637,11 +623,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
return this.additionalTomcatConnectors; return this.additionalTomcatConnectors;
} }
/** @Override
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will
* be used.
* @param uriEncoding the uri encoding to set
*/
public void setUriEncoding(Charset uriEncoding) { public void setUriEncoding(Charset uriEncoding) {
this.uriEncoding = uriEncoding; this.uriEncoding = uriEncoding;
} }
...@@ -654,11 +636,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto ...@@ -654,11 +636,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
return this.uriEncoding; return this.uriEncoding;
} }
/** @Override
* Sets the background processor delay in seconds.
* @param delay the delay in seconds
* @since 1.4.1
*/
public void setBackgroundProcessorDelay(int delay) { public void setBackgroundProcessorDelay(int delay) {
this.backgroundProcessorDelay = delay; this.backgroundProcessorDelay = delay;
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -42,7 +42,8 @@ import org.springframework.util.Assert; ...@@ -42,7 +42,8 @@ import org.springframework.util.Assert;
/** /**
* {@link WebServer} that can be used to control a Tomcat web server. Usually this class * {@link WebServer} that can be used to control a Tomcat web server. Usually this class
* should be created using the {@link TomcatReactiveWebServerFactory} and not directly. * should be created using the {@link TomcatReactiveWebServerFactory}
* of {@link TomcatServletWebServerFactory}, but not directly.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Kristine Jetzke * @author Kristine Jetzke
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment