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 |
|
package info.magnolia.module.cache.executor; |
35 |
|
|
36 |
|
import info.magnolia.cms.cache.CacheConstants; |
37 |
|
import info.magnolia.module.cache.BrowserCachePolicy; |
38 |
|
import info.magnolia.module.cache.BrowserCachePolicyResult; |
39 |
|
import info.magnolia.module.cache.Cache; |
40 |
|
import info.magnolia.module.cache.CachePolicyResult; |
41 |
|
import info.magnolia.module.cache.filter.CachedEntry; |
42 |
|
|
43 |
|
import java.io.IOException; |
44 |
|
|
45 |
|
import javax.servlet.FilterChain; |
46 |
|
import javax.servlet.ServletException; |
47 |
|
import javax.servlet.http.HttpServletRequest; |
48 |
|
import javax.servlet.http.HttpServletResponse; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
|
|
| 66.7% |
Uncovered Elements: 9 (27) |
Complexity: 8 |
Complexity Density: 0.5 |
|
53 |
|
public class SetExpirationHeaders extends AbstractExecutor { |
54 |
|
|
|
|
| 65.4% |
Uncovered Elements: 9 (26) |
Complexity: 8 |
Complexity Density: 0.5 |
|
55 |
2 |
@Override... |
56 |
|
public void processCacheRequest(HttpServletRequest request, |
57 |
|
HttpServletResponse response, FilterChain chain, Cache cache, |
58 |
|
CachePolicyResult cachePolicyResult) throws IOException, ServletException { |
59 |
|
|
60 |
2 |
BrowserCachePolicy browserCachePolicy = this.getContentCachingConfiguration().getBrowserCachePolicy(); |
61 |
2 |
BrowserCachePolicyResult clientCacheResult = browserCachePolicy.canCacheOnClient(cachePolicyResult); |
62 |
|
|
63 |
2 |
if (clientCacheResult != null) { |
64 |
2 |
if (clientCacheResult.getExpirationDate() == BrowserCachePolicyResult.NO_CACHE.getExpirationDate()) { |
65 |
1 |
response.setHeader(CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE); |
66 |
1 |
response.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_CACHE_CONTROL_VALUE_DISABLE_CACHE); |
67 |
1 |
response.setDateHeader(CacheConstants.HEADER_EXPIRES, 0L); |
68 |
|
} else { |
69 |
1 |
long maxAgeSeconds = (clientCacheResult.getExpirationDate() - System.currentTimeMillis()) / 1000L; |
70 |
|
|
71 |
|
|
72 |
1 |
Object cachedEntry = cachePolicyResult != null ? cachePolicyResult.getCachedEntry() : null; |
73 |
1 |
if (cachedEntry != null && (cachedEntry instanceof CachedEntry)) { |
74 |
0 |
CachedEntry e = (CachedEntry) cachedEntry; |
75 |
0 |
int maxAgeFromCachedContent = e.getTimeToLiveInSeconds(); |
76 |
0 |
if (maxAgeFromCachedContent != -1 && maxAgeFromCachedContent < maxAgeSeconds) |
77 |
0 |
maxAgeSeconds = maxAgeFromCachedContent; |
78 |
|
} |
79 |
|
|
80 |
1 |
response.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=" + maxAgeSeconds + ", " + CacheConstants.HEADER_VALUE_PUBLIC); |
81 |
1 |
response.setDateHeader(CacheConstants.HEADER_EXPIRES, System.currentTimeMillis() + maxAgeSeconds * 1000); |
82 |
|
} |
83 |
|
} |
84 |
|
} |
85 |
|
} |