Issue
I have the following problem: In my Spring Boot webapplication with a Primefaces UI I want to upload assets in the backend. I also want to make a webservice available for uploadig assets via a webservlet.
It seems that the Primefaces upload filter interferes the webservice.
My ServletContextInitializer class:
@Configuration
public class Initializer implements ServletContextInitializer {
@Bean
public FilterRegistrationBean FileUploadFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter());
registration.setName("PrimeFaces FileUpload Filter");
registration.addUrlPatterns("/*");
registration.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST);
registration.setAsyncSupported(true);
return registration;
}
}
My Servlet class looks like this:
@WebServlet(name = "InsertAsset", urlPatterns = {"/InsertAsset"})
@MultipartConfig
public class InsertAsset extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
System.out.println(servletContext.getMajorVersion() + "." + servletContext.getMinorVersion());
List<Part> fileParts = request.getParts().stream().filter(part -> "file".equals(part.getName()) && part.getSize() > 0).collect(Collectors.toList());
for (Part filePart : fileParts) {
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
InputStream fileContent = filePart.getInputStream();
}
}
}
When the Primefaces upload filter is active, the webservlet doesn't work (fileparts is empty). The upload handler for the Primefaces UI works properly. When I uncomment the Primefaces upload filter, then the webservlet works fine and the fileparts get filled. But the Primefaces UI upload handler doesn't get called at all.
What is the problem here? Do I have to reconfigure the upload filter?
Thanks for any help.
Solution
Effectively your filter is 'listening' too broad. The FilterRegistrationBean has the option to set a servlet name.
Registrations can be associated with URL patterns and/or servlets (either by name or via a ServletRegistrationBeans. When no URL pattern or servlets are specified the filter will be associated to '/*'. The filter name will be deduced if not specified.
Try that instead of a url pattern, like done in this answer
<servlet-name>facesServlet</servlet>
Answered By - Kukeltje
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)