views:

58

answers:

1

I need to perform pre- and post-processing of all incomming requests to a web server. The functionality is both url-level access restriction and language translation but also other special cases that need to be handled globaly.

Typically this can be achieved with servlet filters but when the number of web applications grow it becomes desirable not to bundle the filters with every application since all applications need to be rebuilt and re-deployed when making a change to a filter.

Instead I would like to install the filters globally on the server and I have found two possible solutions of which I'm not satisfied with any of them.

  1. On Tomcat it is possible to deploy server-wide filters in the "lib" directory and configure the server web.xml to map them to incoming requests. The problem I see is that any filter dependencies also need to be deployed globally in the lib directory. From what I understand this can cause hard to solve dependency conflicts with installed applications. (http://stackoverflow.com/questions/267953/managing-libraries-in-tomcat)

  2. Deploying the filters in a simple web application that mainly acts as a proxy would at least bundle the filters with their corresponding dependencies. This application can then be deployed on the server and take all incoming requests before forwarding them to the target application using the crossContext config parameter. (http://stackoverflow.com/questions/2815370/requestdispatcher-forward-between-tomcat-instances) However, this requires fiddling with the urls such that all links point to the "proxy".

Neither of these solutions seem to be satisfactory. They are both platform dependent since they rely on Tomcat. They also both seem to have possible problems and require special handling of dependencies.

What is the best practise when using server wide functionality?

+1  A: 

This is my untested thought (so not a best practice) - which is a variation of option 2 in your list.

You can use Sitemesh (which is actually meant for decorating your multiple web apps with a common header/footer - but in this case dont use the header/footer).

Host the Sitemesh as a separate web app with crossContext = true.

Sitemesh will be invoked as a Filter for each web app, so the URLs that the end user sees will not change at all. But you will have to define the decortaor.xml for each web app.

You can write your actual Filter processor and chain it after the Sitemesh Filter. All requests will go to the Sitemesh app first - then to your Filter - then to the individual servlet within the web app.

JoseK