Merge pull request #17323 from htztomic

* pr/17323:
  Provide ability to add additional connectors on reactive Tomcat

Closes gh-17323
pull/17330/head
Madhura Bhave 5 years ago
commit 8730f32a78

@ -78,6 +78,8 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
private Collection<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = new LinkedHashSet<>(); private Collection<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = new LinkedHashSet<>();
private final List<Connector> additionalTomcatConnectors = new ArrayList<>();
private String protocol = DEFAULT_PROTOCOL; private String protocol = DEFAULT_PROTOCOL;
private Charset uriEncoding = DEFAULT_CHARSET; private Charset uriEncoding = DEFAULT_CHARSET;
@ -122,6 +124,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
tomcat.setConnector(connector); tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false); tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine()); configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler); TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler);
prepareContext(tomcat.getHost(), servlet); prepareContext(tomcat.getHost(), servlet);
return new TomcatWebServer(tomcat, getPort() >= 0); return new TomcatWebServer(tomcat, getPort() >= 0);
@ -317,6 +322,24 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
return this.tomcatProtocolHandlerCustomizers; return this.tomcatProtocolHandlerCustomizers;
} }
/**
* Add {@link Connector}s in addition to the default connector, e.g. for SSL or AJP
* @param connectors the connectors to add
*/
public void addAdditionalTomcatConnectors(Connector... connectors) {
Assert.notNull(connectors, "Connectors must not be null");
this.additionalTomcatConnectors.addAll(Arrays.asList(connectors));
}
/**
* Returns a mutable collection of the {@link Connector}s that will be added to the
* Tomcat.
* @return the additionalTomcatConnectors
*/
public List<Connector> getAdditionalTomcatConnectors() {
return this.additionalTomcatConnectors;
}
@Override @Override
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");

@ -20,10 +20,12 @@ import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener; import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Service;
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.core.StandardContext; import org.apache.catalina.core.StandardContext;
@ -175,6 +177,24 @@ class TomcatReactiveWebServerFactoryTests extends AbstractReactiveWebServerFacto
} }
} }
@Test
void tomcatAdditionalConnectors() {
TomcatReactiveWebServerFactory factory = getFactory();
Connector[] connectors = new Connector[4];
Arrays.setAll(connectors, (i) -> new Connector());
factory.addAdditionalTomcatConnectors(connectors);
this.webServer = factory.getWebServer(mock(HttpHandler.class));
Map<Service, Connector[]> connectorsByService = ((TomcatWebServer) this.webServer).getServiceConnectors();
assertThat(connectorsByService.values().iterator().next().length).isEqualTo(connectors.length + 1);
}
@Test
void addNullAdditionalConnectorsThrows() {
TomcatReactiveWebServerFactory factory = getFactory();
assertThatIllegalArgumentException().isThrownBy(() -> factory.addAdditionalTomcatConnectors((Connector[]) null))
.withMessageContaining("Connectors must not be null");
}
@Test @Test
void useForwardedHeaders() { void useForwardedHeaders() {
TomcatReactiveWebServerFactory factory = getFactory(); TomcatReactiveWebServerFactory factory = getFactory();

Loading…
Cancel
Save