views:

326

answers:

2

I have the following returned from a Jersey @GET method. It works, fine but always includes the No-cache header. I'd like to allow the client to cache this data since it rarely changes.

ResponseBuilder rb = Response.ok(c);
    CacheControl cc = new CacheControl();
    cc.setMaxAge(60);
    cc.setNoCache(false);
    return rb.cacheControl(cc).build();

The response is always:

 Server Apache-Coyote/1.1
 Pragma No-cache
 Cache-Control  no-cache, no-transform, max-age=60
 Expires    Wed, 31 Dec 1969 19:00:00 EST
 Content-Type   application/xml
 Content-Length 291
 Date   Tue, 16 Feb 2010 01:54:02 GMT

That am I doing wrong here?

A: 

Your code looks okay.

Which container are you using? Make sure cache is not disabled on it. Also verify downstream response handlers or filters aren't setting the no-cache directive.

marklai
Using Tomcat6. I'll take a look at that, thanks! I'm going crazy over here!
jr
Also using Spring, not sure if that matters. I don't see anything in container which would set this. Still looking though.
jr
Cache headers can be set in Spring as well. I think the "no-cache" usually doesn't get set by default. Try hitting a static resource to rule out Tomcat first. Then depending on how complex your app is, peel it one layer at a time, e.g. 1) wire up a plain servlet or jsp in Spring and check the cache-control header; 2) temporarily disable all filters; etc.
marklai
Turns out that I think it is having something to do with BasicAuth.. I need dig in more.
jr
A: 

This was caused by having BASIC auth turned on.

Specifying this in the context will correct the issue:

<Valve className="org.apache.catalina.authenticator.BasicAuthenticator"  disable ProxyCaching="false" />

Hope this helps someone else out.

jr