OpenLiberty / open-liberty

Open Liberty is a highly composable, fast to start, dynamic application server runtime environment
https://openliberty.io
Eclipse Public License 2.0
1.14k stars 587 forks source link

NullPointerException when using dynamic filter to add mapping for servlet name #9386

Closed pmd1nh closed 4 years ago

pmd1nh commented 4 years ago

Application first adds a filter and maps it to a named servlet, for example:

FilterRegistration.Dynamic filter = servletContext.addFilter( "myFilter" , myFilter.class); filter.addMappingForServletNames(dispatcherTypes, true,"myServlet");

Note that the target servlet "myServlet" has not defined yet at this point. It is going to be defined later with ServletRegistration.Dynamic servlet = servletContext.addServlet("myServlet" , myServlet.class);

When the web request is processed later, a NullPointerException may occur with similar stack:

java.lang.NullPointerException at com.ibm.ws.webcontainer.filter.WebAppFilterManager.getFilterChainContents(WebAppFilterManager.java:801) at com.ibm.ws.webcontainer.filter.WebAppFilterManager.getFilterChain(WebAppFilterManager.java:357) at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:957) at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1131) at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:5017) ...................

A workaround is to define the servlets prior to use it in the dynamic filter mappings. However, this rearrangement does not work when using with an embedded Spring Boot container which always invoke the filter operations before the servlet regardless the defined order in the application.

pmd1nh commented 4 years ago

https://github.com/OpenLiberty/open-liberty/pull/9390