I want to wrap and modify the response to an HTTP request to my Java EE/tomcat application. That is, I have a number of Struts 2 actions that generate results, and I’d like to put a front end on them that modifies the response to do things like hide links, etc. Modifying each of the sources is out of the question because of the large number and variety of them.
I know I can write a Servlet Filter that does what I want, but I’d like to build on a standard, well-tested solution rather than re-inventing the wheel. I’ve searched for one, and there are places in the Java EE docs that describe such a beast, but have not been able to find one that I can download.
Edit: There are comments that this is a terrible idea. I’m open to suggestions as to a better one. The situation is that I have a large number of actions out there, written over a long period of time, and there are various systemic modifications that I’d like to do to their output. I don’t want to modify all the actions because that violates DRY (and would be a ton of work). It seems to me that this is exactly what filters are made for – essentially AOP for HTTP responses. Am I thinking wrong?
Edit #2: As to the significance of the change, it’s hard to say. It’s generally not significant in the sense of adding a lot of new content to the page. It’s more scanning the response and making relatively small changes: for example, adding classes to links under certain conditions, injecting small bits of code in various places on the page, or (in the extreme case) clipping out a div that should not appear to the user. I would say the biggest issue is that the system consists of approximately 4000 unique pages, each generated by a separate action and/or view using several different technologies. We can say this system structure stinks, but that’s the reality of life working on a very large, 15 year old piece of software. My other idea is to only implement a filter to do the scanning, and inject modifications in javascript. This does not help in a situation that is truly security-related (i.e. don’t want to let the user even see the content), but it might help in a number of other cases that are done primarily for user convenience. Thanks everybody for the discussion, please keep it coming.
If your changes can be classified, I recommend using jQuery.
All you have to do is to add jQuery to your page, select your tags according to your conditions, and then apply what you need to them.
If you have a layout manager (I believe you must have SiteMesh or Tiles if it is a Struts 2 application), you can just add what you need in your layout templates, and everything will be ok.
Another solution which comes to my mind is applyable if you have used consistent tag libraries inside your project. For example if you have used
<xyz:myLink/>tag all your project, (even if they are Strtus2 standard tags, or ….), you can just re-implement your tags, and then assign them to old names in TLD files.I believe a combination of my previous solution (jQuery) and this solution (reimplementing tags) could help you much, and it would be much easier and more perform than parsing the result DOM and recreating it again.
And one another solution, if your result pages are XHTML (well formed XML), you can use an XSLT transformer to transform your XHTML to another XHTML.