1

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.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
rawdog
  • 712
  • 8
  • 24
  • _" Do I have to reconfigure the upload filter?"_ Did you try? Seems a simple thing to do... But yes, I would think so. – Kukeltje May 26 '20 at 16:03
  • I just added an other order to the filter. But this changed nothing. – rawdog May 26 '20 at 16:50
  • uhhh... there is one filter and one servlet. Filters **always** run before servlets, regardless of order. Changing the filter path so it does not match the url pattern of the servlet is more logical then. But also check https://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked-or-uploaded for hints – Kukeltje May 26 '20 at 18:56

1 Answers1

4

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>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Thanks for leading me to the right direction. I had to register a second filter (org.springframework.web.multipart.support.MultipartFilter) for the servlet. I used the urlpattern of my servlet for this filter and changed the order to a lower one than the Primefaces Upload filter. Now it works. https://github.com/rawdog71/Clownfish/blob/master/clownfish/src/main/java/io/clownfish/clownfish/Initializer.java – rawdog May 27 '20 at 07:28