Reputation:
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
Upvotes: 0
Views: 1479
Reputation: 6484
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.
Upvotes: 1