Search code examples
jsfrichfacesjboss-eap-7

How to fix RichFaces' resources being included multiple times with JBoss EAP 7.3.0?


I'm migrating from JBoss EAP 7.1 to 7.3 at the moment and one of my problems is related to RichFaces – which is EOL with v4.5.17 for a very long time – I know. But nevertheless I need to cope with this at the moment.

Somehow the resources for RichFaces are now included multiple times within the generated HTML markup which delays the loading of the UI significantly. The browser now has to handle multiple unnecessary requests which takes several seconds while loading the page.

Is there any known solution to this problem other than ditching RichFaces completely?

Example of generated HTML

I could not find anything so far...


Solution

  • I've implemented a Servlet Filter to fix the resulting HTML output before it is handed over to the client. It removes all duplicates of <link> und <script> elements at all – not only limited to these particular RichFaces embeds.

    public class RichFacesDuplicatesFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
            // Ignore resources...
            final String path = ((HttpServletRequest) request).getServletPath();
            if (path.startsWith("/javax.faces.resource")) {
                chain.doFilter(request, response);
                return;
            }
    
            // Wrapper via https://stackoverflow.com/a/23381235
            CapturingResponseWrapper wrapper = new CapturingResponseWrapper((HttpServletResponse) response);
            chain.doFilter(request, wrapper);
    
            String content = wrapper.getCaptureAsString();
            content = this.removeDuplicates(content, "<link [^>]+\\/>");
            content = this.removeDuplicates(content, "<script [^>]+><\\/script>");
    
            response.getWriter().write(content);
        }
    
        private String removeDuplicates(String content, String regex) {
            int     index   = 0;
            Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
            Matcher matcher = pattern.matcher(content);
            while (matcher.find(index)) {
                index = matcher.end();
                content = content.substring(0, index) 
                        + content.substring(index).replace(matcher.group(), "");
                matcher = pattern.matcher(content);
            }
            return content;
        }
    }
    

    This adds a (very) little overhead to each request and solves that problem for now. We hope to get rid of RichFaces at all in the future.

    PS: Wrapper via https://stackoverflow.com/a/23381235