I’m working with Tomcat7 on JSF 2.17 Mojarra. I’d like to place a servlet filter in front of CSS and Javascript requests to /javax.faces.resource/* to rewrites certain text references in our development and rc environments. I don’t seem to be able to alter these CSS and Javascript files using a traditional servlet filter. Is there some other way to accomplish this?
For example, I’m looking to replace references to urls found inside the CSS files from:
prod.ourdomain.com
to
dev.ourdomain.com
something like that. Thanks!
You basically need to override
HttpServletResponse#getOutputStream()with a customServletOutputStreamwhich writes to a local buffer and then do a string replacement in there and finally write the modified string to the response. This is quite some code, so here are some helpful classes to assist you further:HttpServletResponseOutputWrapperBufferedHttpServletResponseThen you can basically implement the filter as follows:
This is however open for further optimization. Instead of buffering the entire response, you could also perform the job inside the custom output stream and buffer only the characters starting with
http://prod.ourdomain.com, and then discard it and write the new string instead and then continue.Update: an entirely different alternative, after all actually better, is to use EL straight in the CSS files. CSS resource requests as performed by JSF
<h:outputStylesheet>namely by default supports EL in CSS files. For example,