Commit d8eadfdd authored by Phillip Webb's avatar Phillip Webb

Limit Servlet Listener registration

Limit Servlet Listener registration to the specific supported types.

Issue: #54112999
parent f25b9e10
...@@ -258,7 +258,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext ...@@ -258,7 +258,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
for (Entry<String, EventListener> listenerBean : getOrderedBeansOfType(EventListener.class)) { for (Entry<String, EventListener> listenerBean : getOrderedBeansOfType(EventListener.class)) {
String name = listenerBean.getKey(); String name = listenerBean.getKey();
EventListener listener = listenerBean.getValue(); EventListener listener = listenerBean.getValue();
if (!filterRegistrations.contains(listener)) { if (ServletListenerRegistrationBean.isSupportedType(listener)
&& !filterRegistrations.contains(listener)) {
ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>( ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>(
listener); listener);
registration.setName(name); registration.setName(name);
......
...@@ -25,12 +25,12 @@ import org.springframework.core.Conventions; ...@@ -25,12 +25,12 @@ import org.springframework.core.Conventions;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Base class for {@link ServletRegistrationBean servlet} and * Base class for Servlet 3.0+ based registration beans.
* {@link FilterRegistrationBean filter} registration beans.
* *
* @author Phillip Webb * @author Phillip Webb
* @see ServletRegistrationBean * @see ServletRegistrationBean
* @see FilterRegistrationBean * @see FilterRegistrationBean
* @see ServletListenerRegistrationBean
*/ */
public abstract class RegistrationBean implements ServletContextInitializer { public abstract class RegistrationBean implements ServletContextInitializer {
......
...@@ -16,31 +16,82 @@ ...@@ -16,31 +16,82 @@
package org.springframework.boot.context.embedded; package org.springframework.boot.context.embedded;
import java.util.Collections;
import java.util.EventListener; import java.util.EventListener;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/** /**
* A {@link ServletContextInitializer} to register {@link EventListener}s in a Servlet
* 3.0+ container. Similar to the {@link ServletContext#addListener(EventListener)
* registration} features provided by {@link ServletContext} but with a Spring Bean
* friendly design.
*
* This bean can be used to register the following types of listener:
* <ul>
* <li>{@link ServletContextAttributeListener}</li>
* <li>{@link ServletRequestListener}</li>
* <li>{@link ServletRequestAttributeListener}</li>
* <li>{@link HttpSessionAttributeListener}</li>
* <li>{@link HttpSessionListener}</li>
* <li>{@link ServletContextListener}</li>
* </ul>
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb
* @param <T> the type of listener
*/ */
public class ServletListenerRegistrationBean<T extends EventListener> extends public class ServletListenerRegistrationBean<T extends EventListener> extends
RegistrationBean { RegistrationBean {
private static final Set<Class<?>> SUPPORTED_TYPES;
static {
Set<Class<?>> types = new HashSet<Class<?>>();
types.add(ServletContextAttributeListener.class);
types.add(ServletRequestListener.class);
types.add(ServletRequestAttributeListener.class);
types.add(HttpSessionAttributeListener.class);
types.add(HttpSessionListener.class);
types.add(ServletContextListener.class);
SUPPORTED_TYPES = Collections.unmodifiableSet(types);
}
private T listener; private T listener;
/** /**
* Create a new {@link ServletListenerRegistrationBean} instance.
*/
public ServletListenerRegistrationBean() {
}
/**
* Create a new {@link ServletListenerRegistrationBean} instance.
* @param listener the listener to register * @param listener the listener to register
*/ */
public ServletListenerRegistrationBean(T listener) { public ServletListenerRegistrationBean(T listener) {
super(); Assert.notNull(listener, "Listener must not be null");
Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type");
this.listener = listener; this.listener = listener;
} }
/** /**
* Set the listener that will be registered.
* @param listener the listener to register * @param listener the listener to register
*/ */
public void setListener(T listener) { public void setListener(T listener) {
Assert.notNull(listener, "Listener must not be null");
Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type");
this.listener = listener; this.listener = listener;
} }
...@@ -53,4 +104,18 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends ...@@ -53,4 +104,18 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends
return this.listener; return this.listener;
} }
/**
* Returns {@code true} if the specified listener is one of the supported types.
* @param listener the listener to test
* @return if the listener is of a supported type
*/
public static boolean isSupportedType(EventListener listener) {
for (Class<?> type : SUPPORTED_TYPES) {
if (ClassUtils.isAssignableValue(type, listener)) {
return true;
}
}
return false;
}
} }
...@@ -20,7 +20,6 @@ import java.util.HashMap; ...@@ -20,7 +20,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource;
...@@ -132,4 +131,9 @@ public class ConfigFileApplicationContextInitializerTests { ...@@ -132,4 +131,9 @@ public class ConfigFileApplicationContextInitializerTests {
assertThat(property, equalTo("fromspecificlocation")); assertThat(property, equalTo("fromspecificlocation"));
} }
@Test
public void defaultApplicationProperties() throws Exception {
}
} }
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