Apply server.jetty.* config to reactive servers

This commit applies `server.jetty.*` configuration properties
to Jetty when configured as a reactive web server.

It also removes some infrastructure support for Jetty 8, which
is not supported anymore in Spring Boot 2.0 (partial fix for
gh-11504).

See gh-11500
This commit is contained in:
Brian Clozel
2018-01-11 11:15:17 +01:00
parent d264af8142
commit 15bc718248
10 changed files with 470 additions and 251 deletions

View File

@@ -0,0 +1,56 @@
/*
* 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.jetty;
import org.eclipse.jetty.server.Server;
/**
* Web Server Factory configuration for Jetty-specific features.
*
* @author Brian Clozel
* @since 2.0.0
* @see JettyServletWebServerFactory
* @see JettyReactiveWebServerFactory
*/
public interface ConfigurableJettyWebServerFactory {
/**
* Set the number of acceptor threads to use.
* @param acceptors the number of acceptor threads to use
*/
void setAcceptors(int acceptors);
/**
* Set the number of selector threads to use.
* @param selectors the number of selector threads to use
*/
void setSelectors(int selectors);
/**
* Set if x-forward-* headers should be processed.
* @param useForwardHeaders if x-forward headers should be used
*/
void setUseForwardHeaders(boolean useForwardHeaders);
/**
* Add {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started.
* @param customizers the customizers to add
*/
void addServerCustomizers(JettyServerCustomizer... customizers);
}

View File

@@ -0,0 +1,45 @@
/*
* 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.jetty;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
/**
* {@link JettyServerCustomizer} to add {@link ForwardedRequestCustomizer}.
* @author Phillip Webb
*/
class ForwardHeadersCustomizer implements JettyServerCustomizer {
@Override
public void customize(Server server) {
ForwardedRequestCustomizer customizer = new ForwardedRequestCustomizer();
for (Connector connector : server.getConnectors()) {
for (ConnectionFactory connectionFactory : connector
.getConnectionFactories()) {
if (connectionFactory instanceof HttpConfiguration.ConnectionFactory) {
((HttpConfiguration.ConnectionFactory) connectionFactory)
.getHttpConfiguration().addCustomizer(customizer);
}
}
}
}
}

View File

@@ -46,7 +46,8 @@ import org.springframework.util.Assert;
* @author Brian Clozel
* @since 2.0.0
*/
public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory {
public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableJettyWebServerFactory {
private static final Log logger = LogFactory
.getLog(JettyReactiveWebServerFactory.class);
@@ -61,6 +62,8 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact
*/
private int selectors = -1;
private boolean useForwardHeaders;
private List<JettyServerCustomizer> jettyServerCustomizers = new ArrayList<>();
private ThreadPool threadPool;
@@ -80,6 +83,16 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact
super(port);
}
@Override
public void setUseForwardHeaders(boolean useForwardHeaders) {
this.useForwardHeaders = useForwardHeaders;
}
@Override
public void setAcceptors(int acceptors) {
this.acceptors = acceptors;
}
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
JettyHttpHandlerAdapter servlet = new JettyHttpHandlerAdapter(httpHandler);
@@ -87,6 +100,37 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact
return new JettyWebServer(server, getPort() >= 0);
}
@Override
public void addServerCustomizers(JettyServerCustomizer... customizers) {
Assert.notNull(customizers, "Customizers must not be null");
this.jettyServerCustomizers.addAll(Arrays.asList(customizers));
}
/**
* Sets {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started. Calling this method will replace any existing customizers.
* @param customizers the Jetty customizers to apply
*/
public void setServerCustomizers(
Collection<? extends JettyServerCustomizer> customizers) {
Assert.notNull(customizers, "Customizers must not be null");
this.jettyServerCustomizers = new ArrayList<>(customizers);
}
/**
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be
* applied to the {@link Server} before it is created.
* @return the Jetty customizers
*/
public Collection<JettyServerCustomizer> getServerCustomizers() {
return this.jettyServerCustomizers;
}
@Override
public void setSelectors(int selectors) {
this.selectors = selectors;
}
protected Server createJettyServer(JettyHttpHandlerAdapter servlet) {
int port = (getPort() >= 0 ? getPort() : 0);
InetSocketAddress address = new InetSocketAddress(getAddress(), port);
@@ -104,6 +148,9 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact
for (JettyServerCustomizer customizer : getServerCustomizers()) {
customizer.customize(server);
}
if (this.useForwardHeaders) {
new ForwardHeadersCustomizer().customize(server);
}
return server;
}
@@ -143,49 +190,4 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact
this.threadPool = threadPool;
}
/**
* Set the number of acceptor threads to use.
* @param acceptors the number of acceptor threads to use
*/
public void setAcceptors(int acceptors) {
this.acceptors = acceptors;
}
/**
* Sets {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started. Calling this method will replace any existing customizers.
* @param customizers the Jetty customizers to apply
*/
public void setServerCustomizers(
Collection<? extends JettyServerCustomizer> customizers) {
Assert.notNull(customizers, "Customizers must not be null");
this.jettyServerCustomizers = new ArrayList<>(customizers);
}
/**
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be
* applied to the {@link Server} before it is created.
* @return the Jetty customizers
*/
public Collection<JettyServerCustomizer> getServerCustomizers() {
return this.jettyServerCustomizers;
}
/**
* Add {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started.
* @param customizers the customizers to add
*/
public void addServerCustomizers(JettyServerCustomizer... customizers) {
Assert.notNull(customizers, "Customizers must not be null");
this.jettyServerCustomizers.addAll(Arrays.asList(customizers));
}
/**
* Set the number of selector threads to use.
* @param selectors the number of selector threads to use
*/
public void setSelectors(int selectors) {
this.selectors = selectors;
}
}

View File

@@ -40,7 +40,6 @@ import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Request;
@@ -96,7 +95,7 @@ import org.springframework.util.StringUtils;
* @see JettyWebServer
*/
public class JettyServletWebServerFactory extends AbstractServletWebServerFactory
implements ResourceLoaderAware {
implements ConfigurableJettyWebServerFactory, ResourceLoaderAware {
private List<Configuration> configurations = new ArrayList<>();
@@ -438,29 +437,17 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
this.resourceLoader = resourceLoader;
}
/**
* Set if x-forward-* headers should be processed.
* @param useForwardHeaders if x-forward headers should be used
* @since 1.3.0
*/
@Override
public void setUseForwardHeaders(boolean useForwardHeaders) {
this.useForwardHeaders = useForwardHeaders;
}
/**
* Set the number of acceptor threads to use.
* @param acceptors the number of acceptor threads to use
* @since 1.4.0
*/
@Override
public void setAcceptors(int acceptors) {
this.acceptors = acceptors;
}
/**
* Set the number of selector threads to use.
* @param selectors the number of selector threads to use
* @since 1.4.0
*/
@Override
public void setSelectors(int selectors) {
this.selectors = selectors;
}
@@ -485,11 +472,7 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
return this.jettyServerCustomizers;
}
/**
* Add {@link JettyServerCustomizer}s that will be applied to the {@link Server}
* before it is started.
* @param customizers the customizers to add
*/
@Override
public void addServerCustomizers(JettyServerCustomizer... customizers) {
Assert.notNull(customizers, "Customizers must not be null");
this.jettyServerCustomizers.addAll(Arrays.asList(customizers));
@@ -565,28 +548,6 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
}
}
/**
* {@link JettyServerCustomizer} to add {@link ForwardedRequestCustomizer}. Only
* supported with Jetty 9 (hence the inner class)
*/
private static class ForwardHeadersCustomizer implements JettyServerCustomizer {
@Override
public void customize(Server server) {
ForwardedRequestCustomizer customizer = new ForwardedRequestCustomizer();
for (Connector connector : server.getConnectors()) {
for (ConnectionFactory connectionFactory : connector
.getConnectionFactories()) {
if (connectionFactory instanceof HttpConfiguration.ConnectionFactory) {
((HttpConfiguration.ConnectionFactory) connectionFactory)
.getHttpConfiguration().addCustomizer(customizer);
}
}
}
}
}
/**
* {@link HandlerWrapper} to add a custom {@code server} header.
*/