Skip to content

Commit 31c18d8

Browse files
fix: ignore servlet registrations without server-class definition (#17749) (#17757)
ServletDeployer performs several checks on servlet registrations to determine if the Vaadin Servlet must be registered automatically. However, as of servlet 3.0, <servlet-class> is optional in <servlet> declaration in web.xml, and in this case ServletDeployer fails with a NPE. With this change ServletDeployer ignores servlet registration without a servlet-class definition. Fixes #17748 Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent 247938c commit 31c18d8

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/startup/ServletDeployer.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ private Collection<DeploymentConfiguration> getServletConfigurations(
192192
Collection<DeploymentConfiguration> result = new ArrayList<>(
193193
registrations.size());
194194
for (ServletRegistration registration : registrations) {
195-
loadClass(context.getClassLoader(), registration.getClassName())
196-
.ifPresent(servletClass -> result.add(
197-
StubServletConfig.createDeploymentConfiguration(
198-
context, registration, servletClass)));
195+
if (registration.getClassName() != null) {
196+
loadClass(context.getClassLoader(), registration.getClassName())
197+
.ifPresent(servletClass -> result.add(
198+
StubServletConfig.createDeploymentConfiguration(
199+
context, registration, servletClass)));
200+
}
199201
}
200202
return result;
201203
}
@@ -268,6 +270,7 @@ private ServletRegistration findServletByPathPart(ServletContext context,
268270

269271
private ServletRegistration findVaadinServlet(ServletContext context) {
270272
return context.getServletRegistrations().values().stream()
273+
.filter(registration -> registration.getClassName() != null)
271274
.filter(registration -> isVaadinServlet(
272275
context.getClassLoader(), registration.getClassName()))
273276
.findAny().orElse(null);

‎flow-server/src/test/java/com/vaadin/flow/server/startup/ServletDeployerTest.java‎

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.vaadin.flow.server.startup;
22

3-
import static java.util.Collections.singletonList;
4-
import static java.util.Collections.singletonMap;
5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertTrue;
3+
import jakarta.servlet.Registration;
4+
import jakarta.servlet.Servlet;
5+
import jakarta.servlet.ServletConfig;
6+
import jakarta.servlet.ServletContext;
7+
import jakarta.servlet.ServletContextEvent;
8+
import jakarta.servlet.ServletRegistration;
9+
import jakarta.servlet.ServletRequest;
10+
import jakarta.servlet.ServletResponse;
711

812
import java.io.File;
913
import java.nio.charset.StandardCharsets;
@@ -17,14 +21,13 @@
1721
import java.util.stream.Collectors;
1822
import java.util.stream.Stream;
1923

20-
import jakarta.servlet.Registration;
21-
import jakarta.servlet.Servlet;
22-
import jakarta.servlet.ServletConfig;
23-
import jakarta.servlet.ServletContext;
24-
import jakarta.servlet.ServletContextEvent;
25-
import jakarta.servlet.ServletRegistration;
26-
import jakarta.servlet.ServletRequest;
27-
import jakarta.servlet.ServletResponse;
24+
import org.apache.commons.io.FileUtils;
25+
import org.junit.Before;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.rules.TemporaryFolder;
29+
import org.mockito.ArgumentMatchers;
30+
import org.mockito.Mockito;
2831

2932
import com.vaadin.flow.component.Component;
3033
import com.vaadin.flow.di.Lookup;
@@ -33,13 +36,10 @@
3336
import com.vaadin.flow.server.InitParameters;
3437
import com.vaadin.flow.server.frontend.FrontendUtils;
3538

36-
import org.apache.commons.io.FileUtils;
37-
import org.junit.Before;
38-
import org.junit.Rule;
39-
import org.junit.Test;
40-
import org.junit.rules.TemporaryFolder;
41-
import org.mockito.ArgumentMatchers;
42-
import org.mockito.Mockito;
39+
import static java.util.Collections.singletonList;
40+
import static java.util.Collections.singletonMap;
41+
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertTrue;
4343

4444
public class ServletDeployerTest {
4545
private final ServletDeployer deployer = new ServletDeployer();
@@ -143,6 +143,18 @@ public void frontendServletIsNotRegistered_whenMainServletIsRegistered()
143143
assertLoadOnStartupSet();
144144
}
145145

146+
@Test
147+
public void servletsWithoutClassName_registrationDoesNotFail()
148+
throws Exception {
149+
deployer.contextInitialized(getContextEvent(getServletRegistration(
150+
"test", null, singletonList("/WEB-INF/test.jsp"),
151+
Collections.emptyMap())));
152+
153+
assertMappingsCount(1, 1);
154+
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
155+
assertLoadOnStartupSet();
156+
}
157+
146158
@Test
147159
public void servletIsNotRegisteredWhenAnotherHasTheSamePathMapping_mainServlet()
148160
throws Exception {
@@ -281,8 +293,8 @@ private ServletRegistration getServletRegistration(String servletName,
281293
Map<String, String> initParameters) {
282294
ServletRegistration registrationMock = Mockito
283295
.mock(ServletRegistration.class);
284-
Mockito.when(registrationMock.getClassName())
285-
.thenReturn(servletClass.getName());
296+
Mockito.when(registrationMock.getClassName()).thenReturn(
297+
servletClass != null ? servletClass.getName() : null);
286298
Mockito.when(registrationMock.getMappings()).thenReturn(pathMappings);
287299
Mockito.when(registrationMock.getName()).thenReturn(servletName);
288300
Mockito.when(registrationMock.getInitParameters())

0 commit comments

Comments
 (0)