* the caching directives.
*
* @see #getCachingDirectives()
*/
private void addCachingHeaders() {
final ResponseCachingDirectives cachingDirectives =
getCachingDirectives();
final HttpServletResponse response =
(HttpServletResponse) requestContext.getResponse();
if (cachingDirectives != null && cachingDirectives.isEnabled() &&
cachingDirectives.getExpires() != null) {
// if enabled, set Expires, Cache-Control/max-age and Vary response
// headers
final Time expires = cachingDirectives.getExpires();
if (expires != Time.NEVER) {
response.addDateHeader("Expires", expires.inMillis());
// compute max-age value
final Period timeToLive = cachingDirectives.getTimeToLive();
long maxAgeInSeconds = 0;
if (timeToLive != null) {
maxAgeInSeconds =
LongHelper.asInt(timeToLive.inMillis() / 1000);
if (maxAgeInSeconds < 0) {
maxAgeInSeconds = 0;
}
}
response.addHeader("Cache-Control", "max-age=" + maxAgeInSeconds);
} else {
// from the spec:
// To mark a response as "never expires," an origin server sends
// an Expires date approximately one year from the time the
// response is sent. HTTP/1.1 servers SHOULD NOT send Expires
// dates more than one year in the future.
final SystemClock clock = cachingDirectives.getClock();
final long oneYearInSeconds = 365 * 24 * 60 * 60;
response.addDateHeader("Expires",
clock.getCurrentTime().inMillis() + oneYearInSeconds * 1000);
response.addHeader("Cache-Control", "max-age=" + oneYearInSeconds);
}