Skip to content

Commit 7f83167

Browse files
author
Dave Syer
committed
Additionally check for null on registring Servlets and Filters
See spring-projectsgh-482
1 parent 3d43771 commit 7f83167

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ public boolean isMatchAfter() {
230230
@Override
231231
public void onStartup(ServletContext servletContext) throws ServletException {
232232
Assert.notNull(this.filter, "Filter must not be null");
233-
configure(servletContext.addFilter(getOrDeduceName(this.filter), this.filter));
233+
String name = getOrDeduceName(this.filter);
234+
FilterRegistration.Dynamic added = servletContext.addFilter(name, this.filter);
235+
if (added == null) {
236+
logger.info("Filter " + name
237+
+ " was not registered (possibly already registered?)");
238+
return;
239+
}
240+
configure(added);
234241
}
235242

236243
/**

spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.servlet.ServletContext;
2727
import javax.servlet.ServletException;
2828
import javax.servlet.ServletRegistration;
29+
import javax.servlet.ServletRegistration.Dynamic;
2930

3031
import org.apache.commons.logging.Log;
3132
import org.apache.commons.logging.LogFactory;
@@ -156,8 +157,15 @@ public String getServletName() {
156157
@Override
157158
public void onStartup(ServletContext servletContext) throws ServletException {
158159
Assert.notNull(this.servlet, "Servlet must not be null");
159-
logger.info("Mapping servlet: '" + getServletName() + "' to " + this.urlMappings);
160-
configure(servletContext.addServlet(getServletName(), this.servlet));
160+
String name = getServletName();
161+
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
162+
Dynamic added = servletContext.addServlet(name, this.servlet);
163+
if (added == null) {
164+
logger.info("Servlet " + name
165+
+ " was not registered (possibly already registered?)");
166+
return;
167+
}
168+
configure(added);
161169
}
162170

163171
/**

spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.mockito.BDDMockito.given;
3939
import static org.mockito.Matchers.anyObject;
4040
import static org.mockito.Matchers.anyString;
41+
import static org.mockito.Mockito.times;
4142
import static org.mockito.Mockito.verify;
4243

4344
/**
@@ -79,6 +80,16 @@ public void startupWithDefaults() throws Exception {
7980
verify(this.registration).addMapping("/*");
8081
}
8182

83+
@Test
84+
public void startupWithDoubleRegistration() throws Exception {
85+
ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet);
86+
given(this.servletContext.addServlet(anyString(), (Servlet) anyObject()))
87+
.willReturn(null);
88+
bean.onStartup(this.servletContext);
89+
verify(this.servletContext).addServlet("mockServlet", this.servlet);
90+
verify(this.registration, times(0)).setAsyncSupported(true);
91+
}
92+
8293
@Test
8394
public void startupWithSpecifiedValues() throws Exception {
8495
ServletRegistrationBean bean = new ServletRegistrationBean();

0 commit comments

Comments
 (0)