Question

I have been porting a working Primefaces JSF 2 app from spring XML configuration to the newer Spring 3.2 Java Configuration model. So at the same time I decided to port the web.xml configuration as well. Things have gone pretty good but I seem to be stuck with one particular thing. The root question I have is how to set init parms for a filter in the class implementing WebApplicationInitializer.

So I have the following section of web.xml

 <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
        org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <!-- we set the threshold size to be exactly 1 megabyte -->
        <param-name>thresholdSize</param-name>
        <param-value>1048576</param-value>
    </init-param>
    <init-param>
        <!-- this is the location for the upload  -->
        <param-name>uploadDirectory</param-name>
        <!-- we select the system tmp directory for this -->
        <param-value>/tmp/myapp/uploads</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>facesServlet</servlet-name>
</filter-mapping>

In my ApplicationInitializer which implements WebApplicationInitializer, I defined a filter. But I cannot see how I would set the init parms for thresholdSize and uploadDirectory.
Is there a straightforward way to do this?

Thanks

Was it helpful?

Solution

Ok I figured it out. I created my own implementation of the FilterConfig interface and after I created the filter and passed it in through the init method on the filter.

For those interested here is the code

 private void setupFileUploadFilter(ServletContext container) {
    try {
        // create the filter config for the file upload filter
        FilterConfig filterConfig = setupFileUpLoadFilterConfig(container);

        // create the filter
        FileUploadFilter fileUploadFilter = new FileUploadFilter();

        // initialize the file upload filter with the specified filter config
        fileUploadFilter.init(filterConfig);

        // register the filter to the main container
        FilterRegistration.Dynamic fileUploadFilterReg = container.addFilter("PrimeFaces FileUpload Filter", fileUploadFilter);

        // map it for all patterns
        fileUploadFilterReg.addMappingForUrlPatterns(null, false, "/*");

        // add a mapping to the faces Servlet
        fileUploadFilterReg.addMappingForServletNames(null, false, "facesServlet");

    } catch (ServletException e) {
        e.printStackTrace();
    }
}

/**
 * create the initialization parameters for the file upload filter
 *
 * @param container the main container
 * @return the created filter config
 */
private FilterConfig setupFileUpLoadFilterConfig(ServletContext container) {
    CustomFilterConfig filterConfig = new CustomFilterConfig(container, "PrimeFaces FileUpload Filter");

    // add the size parameter which is 1 Megabyte
    filterConfig.addInitParameter("thresholdSize", "1048576");

    // add the size parameter
    filterConfig.addInitParameter("uploadDirectory", "/tmp/myapp/uploads");

    return filterConfig;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top