Commit 8ecf45e0 authored by Phillip Webb's avatar Phillip Webb

Merge pull request #6311 from qerub/tomcat-engine-valves

* tomcat-engine-valves:
  Register valves at the engine level
  Add support for engine valves
parents be80a587 3a8cf7ac
...@@ -851,7 +851,7 @@ public class ServerProperties ...@@ -851,7 +851,7 @@ public class ServerProperties
valve.setPortHeader(getPortHeader()); valve.setPortHeader(getPortHeader());
valve.setProtocolHeaderHttpsValue(getProtocolHeaderHttpsValue()); valve.setProtocolHeaderHttpsValue(getProtocolHeaderHttpsValue());
// ... so it's safe to add this valve by default. // ... so it's safe to add this valve by default.
factory.addContextValves(valve); factory.addEngineValves(valve);
} }
} }
...@@ -925,7 +925,7 @@ public class ServerProperties ...@@ -925,7 +925,7 @@ public class ServerProperties
valve.setPrefix(this.accesslog.getPrefix()); valve.setPrefix(this.accesslog.getPrefix());
valve.setSuffix(this.accesslog.getSuffix()); valve.setSuffix(this.accesslog.getSuffix());
valve.setRenameOnRotate(this.accesslog.isRenameOnRotate()); valve.setRenameOnRotate(this.accesslog.isRenameOnRotate());
factory.addContextValves(valve); factory.addEngineValves(valve);
} }
private void customizeRedirectContextRoot( private void customizeRedirectContextRoot(
......
...@@ -339,7 +339,7 @@ public class ServerPropertiesTests { ...@@ -339,7 +339,7 @@ public class ServerPropertiesTests {
bindProperties(map); bindProperties(map);
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
this.properties.customize(container); this.properties.customize(container);
assertThat(container.getValves()).isEmpty(); assertThat(container.getEngineValves()).isEmpty();
} }
@Test @Test
...@@ -368,8 +368,8 @@ public class ServerPropertiesTests { ...@@ -368,8 +368,8 @@ public class ServerPropertiesTests {
private void testRemoteIpValveConfigured() { private void testRemoteIpValveConfigured() {
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
this.properties.customize(container); this.properties.customize(container);
assertThat(container.getValves()).hasSize(1); assertThat(container.getEngineValves()).hasSize(1);
Valve valve = container.getValves().iterator().next(); Valve valve = container.getEngineValves().iterator().next();
assertThat(valve).isInstanceOf(RemoteIpValve.class); assertThat(valve).isInstanceOf(RemoteIpValve.class);
RemoteIpValve remoteIpValve = (RemoteIpValve) valve; RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto"); assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto");
...@@ -398,8 +398,8 @@ public class ServerPropertiesTests { ...@@ -398,8 +398,8 @@ public class ServerPropertiesTests {
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
this.properties.customize(container); this.properties.customize(container);
assertThat(container.getValves()).hasSize(1); assertThat(container.getEngineValves()).hasSize(1);
Valve valve = container.getValves().iterator().next(); Valve valve = container.getEngineValves().iterator().next();
assertThat(valve).isInstanceOf(RemoteIpValve.class); assertThat(valve).isInstanceOf(RemoteIpValve.class);
RemoteIpValve remoteIpValve = (RemoteIpValve) valve; RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header"); assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header");
......
...@@ -34,6 +34,7 @@ import javax.servlet.ServletContainerInitializer; ...@@ -34,6 +34,7 @@ import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
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.Lifecycle; import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleEvent;
...@@ -88,6 +89,7 @@ import org.springframework.util.StringUtils; ...@@ -88,6 +89,7 @@ import org.springframework.util.StringUtils;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Christoffer Sawicki
* @see #setPort(int) * @see #setPort(int)
* @see #setContextLifecycleListeners(Collection) * @see #setContextLifecycleListeners(Collection)
* @see TomcatEmbeddedServletContainer * @see TomcatEmbeddedServletContainer
...@@ -106,6 +108,8 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -106,6 +108,8 @@ public class TomcatEmbeddedServletContainerFactory
private File baseDirectory; private File baseDirectory;
private List<Valve> engineValves = new ArrayList<Valve>();
private List<Valve> contextValves = new ArrayList<Valve>(); private List<Valve> contextValves = new ArrayList<Valve>();
private List<LifecycleListener> contextLifecycleListeners = new ArrayList<LifecycleListener>(); private List<LifecycleListener> contextLifecycleListeners = new ArrayList<LifecycleListener>();
...@@ -162,7 +166,7 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -162,7 +166,7 @@ public class TomcatEmbeddedServletContainerFactory
customizeConnector(connector); customizeConnector(connector);
tomcat.setConnector(connector); tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false); tomcat.getHost().setAutoDeploy(false);
tomcat.getEngine().setBackgroundProcessorDelay(-1); configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) { for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector); tomcat.getService().addConnector(additionalConnector);
} }
...@@ -170,6 +174,13 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -170,6 +174,13 @@ public class TomcatEmbeddedServletContainerFactory
return getTomcatEmbeddedServletContainer(tomcat); return getTomcatEmbeddedServletContainer(tomcat);
} }
private void configureEngine(Engine engine) {
engine.setBackgroundProcessorDelay(-1);
for (Valve valve : this.engineValves) {
engine.getPipeline().addValve(valve);
}
}
protected void prepareContext(Host host, ServletContextInitializer[] initializers) { protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
File docBase = getValidDocumentRoot(); File docBase = getValidDocumentRoot();
docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase")); docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
...@@ -527,9 +538,37 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -527,9 +538,37 @@ public class TomcatEmbeddedServletContainerFactory
this.protocol = protocol; this.protocol = protocol;
} }
/**
* Set {@link Valve}s that should be applied to the Tomcat {@link Engine}. Calling
* this method will replace any existing valves.
* @param engineValves the valves to set
*/
public void setEngineValves(Collection<? extends Valve> engineValves) {
Assert.notNull(engineValves, "Valves must not be null");
this.engineValves = new ArrayList<Valve>(engineValves);
}
/**
* Returns a mutable collection of the {@link Valve}s that will be applied to the
* Tomcat {@link Engine}.
* @return the engineValves the valves that will be applied
*/
public Collection<Valve> getEngineValves() {
return this.engineValves;
}
/**
* Add {@link Valve}s that should be applied to the Tomcat {@link Engine}.
* @param engineValves the valves to add
*/
public void addEngineValves(Valve... engineValves) {
Assert.notNull(engineValves, "Valves must not be null");
this.engineValves.addAll(Arrays.asList(engineValves));
}
/** /**
* Set {@link Valve}s that should be applied to the Tomcat {@link Context}. Calling * Set {@link Valve}s that should be applied to the Tomcat {@link Context}. Calling
* this method will replace any existing listeners. * this method will replace any existing valves.
* @param contextValves the valves to set * @param contextValves the valves to set
*/ */
public void setContextValves(Collection<? extends Valve> contextValves) { public void setContextValves(Collection<? extends Valve> contextValves) {
...@@ -541,8 +580,20 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -541,8 +580,20 @@ public class TomcatEmbeddedServletContainerFactory
* Returns a mutable collection of the {@link Valve}s that will be applied to the * Returns a mutable collection of the {@link Valve}s that will be applied to the
* Tomcat {@link Context}. * Tomcat {@link Context}.
* @return the contextValves the valves that will be applied * @return the contextValves the valves that will be applied
* @deprecated as of 1.4 in favor of {@link #getContextValves()}
*/ */
@Deprecated
public Collection<Valve> getValves() { public Collection<Valve> getValves() {
return getContextValves();
}
/**
* Returns a mutable collection of the {@link Valve}s that will be applied to the
* Tomcat {@link Context}.
* @return the contextValves the valves that will be applied
* @see #getEngineValves()
*/
public Collection<Valve> getContextValves() {
return this.contextValves; return this.contextValves;
} }
......
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