views:

214

answers:

1

Hi,

I have a need to write a Servlet filter to inspect the HTML being sent out and modify all the links that point to /images in it to a different domain altogether so that they are served from a CDN(content delivery network) rather than my site.

Is this recommended and how can I achieve this?

-thanks

+1  A: 

The most efficient way would be to keep the image path in app configuration and use different configurations for development and production. The dev version will do localhost (or whatever) and the prod version will point to your CDN.

If configuration is not an option, Jason Hunter's Java Servlet Programming has an example of search/replacing outgoing html with a regex. You can use and adaptation of this to replace your image URLs.

If your app server is fronted by a load balancer or Apache you could also do the replacement there. It has the benefit of not polluting your app with CDN logic. But the drawback is that it is harder to version control, etc.

leonm
Thank you. However, the most efficient way you suggested will result in changing a hell lot of files in my app since currently the images are referred as /images/... everywhere. And it is not just a question of images ! javascript, css, help files etc, everything will need to be externalized.Hence I was looking for a less intrusive way of doing this. I kindof tried your last sugg. URL rewriting in Apache, by doing a 301 redirect for the images. However, the requests come to my server first and then get redirected to the CDN which is a huge overhead.I dont want to rely on browser-caching.
Mak
Some load balancers can actually modify the outbound html so no need for 301 magic. It actually does exactly the same as the search/replace servlet but in hardware that is not your app server.
leonm
great .. thanks!
Mak