1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| private CacheControl cacheControl; protected final void prepareResponse(HttpServletResponse response) { if (this.cacheControl != null) { applyCacheControl(response, this.cacheControl); } else { applyCacheSeconds(response, this.cacheSeconds); } if (this.varyByRequestHeaders != null) { for (String value : getVaryRequestHeadersToAdd(response, this.varyByRequestHeaders)) { response.addHeader("Vary", value); } } } protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl) { String ccValue = cacheControl.getHeaderValue(); if (ccValue != null) { response.setHeader(HEADER_CACHE_CONTROL, ccValue);
if (response.containsHeader(HEADER_PRAGMA)) { response.setHeader(HEADER_PRAGMA, ""); } if (response.containsHeader(HEADER_EXPIRES)) { response.setHeader(HEADER_EXPIRES, ""); } } } protected final void cacheForSeconds(HttpServletResponse response, int seconds) { cacheForSeconds(response, seconds, false); } protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds) { if (this.useExpiresHeader || !this.useCacheControlHeader) { if (cacheSeconds > 0) { cacheForSeconds(response, cacheSeconds); } else if (cacheSeconds == 0) { preventCaching(response); } } else { CacheControl cControl; if (cacheSeconds > 0) { cControl = CacheControl.maxAge(cacheSeconds, TimeUnit.SECONDS); if (this.alwaysMustRevalidate) { cControl = cControl.mustRevalidate(); } } else if (cacheSeconds == 0) { cControl = (this.useCacheControlNoStore ? CacheControl.noStore() : CacheControl.noCache()); } else { cControl = CacheControl.empty(); } applyCacheControl(response, cControl); } } protected final void cacheForSeconds(HttpServletResponse response, int seconds) { cacheForSeconds(response, seconds, false); }
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) { if (this.useExpiresHeader) { response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L); } else if (response.containsHeader(HEADER_EXPIRES)) { response.setHeader(HEADER_EXPIRES, ""); }
if (this.useCacheControlHeader) { String headerValue = "max-age=" + seconds; if (mustRevalidate || this.alwaysMustRevalidate) { headerValue += ", must-revalidate"; } response.setHeader(HEADER_CACHE_CONTROL, headerValue); }
if (response.containsHeader(HEADER_PRAGMA)) { response.setHeader(HEADER_PRAGMA, ""); } } protected final void preventCaching(HttpServletResponse response) { response.setHeader(HEADER_PRAGMA, "no-cache");
if (this.useExpiresHeader) { response.setDateHeader(HEADER_EXPIRES, 1L); }
if (this.useCacheControlHeader) { response.setHeader(HEADER_CACHE_CONTROL, "no-cache"); if (this.useCacheControlNoStore) { response.addHeader(HEADER_CACHE_CONTROL, "no-store"); } } }
public class CacheControl { private String toHeaderValue() { StringBuilder headerValue = new StringBuilder(); if (this.maxAge != null) { appendDirective(headerValue, "max-age=" + this.maxAge.getSeconds()); } if (this.noCache) { appendDirective(headerValue, "no-cache"); } if (this.noStore) { appendDirective(headerValue, "no-store"); } if (this.mustRevalidate) { appendDirective(headerValue, "must-revalidate"); } if (this.noTransform) { appendDirective(headerValue, "no-transform"); } if (this.cachePublic) { appendDirective(headerValue, "public"); } if (this.cachePrivate) { appendDirective(headerValue, "private"); } if (this.proxyRevalidate) { appendDirective(headerValue, "proxy-revalidate"); } if (this.sMaxAge != null) { appendDirective(headerValue, "s-maxage=" + this.sMaxAge.getSeconds()); } if (this.staleIfError != null) { appendDirective(headerValue, "stale-if-error=" + this.staleIfError.getSeconds()); } if (this.staleWhileRevalidate != null) { appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate.getSeconds()); } return headerValue.toString(); } }
|