Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable browser caching in Vaadin

My question is short (and hopefully simple to solve!): How can I completely disable browser-caching in my webservice realized with vaadin?

I want to completely disable caching since I'm getting problems when I try to do some PDF streaming and displaying them in my browers.

I have read about a solution for my problem for example here:

Using <meta> tags to turn off caching in all browsers?

They talk about adding some headers to the web application that disable browser caching. But how do I add them to my Vaadin application?

A short code snippet would be very welcome (and helpful!)

Thanks once again for every answer and thought you're sharing with me.

like image 649
Waylander Avatar asked Sep 08 '12 15:09

Waylander


1 Answers

It seems to me that you want to disable caching when downloading a PDF file. Assuming you are using a DownloadStream to stream the content, then setting the Content-Disposition and Cache-Control headers as follows should work.

 DownloadStream stream = new DownloadStream(getStreamSource().getStream(), contentType, filename);
stream.setParameter("Content-Disposition", "attachment;filename=" + filename);
// This magic incantation should prevent anyone from caching the data
stream.setParameter("Cache-Control", "private,no-cache,no-store");    
// In theory <=0 disables caching. In practice Chrome, Safari (and, apparently, IE) all ignore <=0. Set to 1s 
stream.setCacheTime(1000);

If you want to disable caching for all Vaadin requests, you'll have to look at the source of AbstractApplicationServlet, and extend methods such as #serveStaticResourcesInVAADIN and others - this is quick tricky, as alot of them are private methods.

A simpler method may be to use an Http Servlet Filter to add the appropriate parameters to the response, without having to modify your app at all. You can write this yourself - should be quick easy - although a quick search finds the Apache2 licensed Cache-Filter : http://code.google.com/p/cache-filter/wiki/NoCacheFilter

I have not used Cache-Filter, but a quick skim suggests it'll work just fine for you.

like image 83
Charles Anthony Avatar answered Nov 15 '22 10:11

Charles Anthony