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.core.Content;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.module.cache.Cache;
39 import info.magnolia.module.cache.CacheModule;
40 import info.magnolia.module.cache.CachePolicy;
41 import info.magnolia.module.cache.CachePolicyResult;
42 import info.magnolia.module.cache.filter.CacheResponseWrapper;
43 import info.magnolia.module.cache.filter.CachedEntry;
44 import info.magnolia.module.cache.filter.CachedError;
45 import info.magnolia.module.cache.filter.ContentCachedEntry;
46 import info.magnolia.module.cache.filter.CachedRedirect;
47 import info.magnolia.module.cache.filter.InMemoryCachedEntry;
48 import info.magnolia.module.cache.filter.DelegatingBlobCachedEntry;
49
50 import java.io.IOException;
51
52 import javax.servlet.FilterChain;
53 import javax.servlet.ServletException;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56
57
58
59
60
61
62
63 public class Store extends AbstractExecutor {
64
65 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Store.class);
66
67 public void processCacheRequest(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Cache cache, CachePolicyResult cachePolicyResult) throws IOException, ServletException {
68 CachedEntry cachedEntry = null;
69 final Object key = cachePolicyResult.getCacheKey();
70
71 final CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response, CacheResponseWrapper.DEFAULT_THRESHOLD, false);
72
73
74 final long cacheStorageDate = System.currentTimeMillis();
75 responseWrapper.setDateHeader("Last-Modified", cacheStorageDate);
76 chain.doFilter(request, responseWrapper);
77
78 if (responseWrapper.getStatus() == HttpServletResponse.SC_NOT_MODIFIED) {
79 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
80 }
81 else{
82 responseWrapper.flushBuffer();
83 cachedEntry = makeCachedEntry(request, responseWrapper);
84 }
85
86
87 cache.put(key, cachedEntry);
88
89 if(cachedEntry != null){
90 cachePolicyResult.setCachedEntry(cachedEntry);
91
92 final Content content = MgnlContext.getAggregationState().getCurrentContent();
93 if (content != null && content.isNodeType("mix:referenceable")) {
94 final String uuid = content.getUUID();
95 String repo = content.getHierarchyManager().getName();
96 getCachePolicy(cache).persistCacheKey(repo, uuid, key);
97 }
98 }
99 }
100
101 protected CachedEntry makeCachedEntry(HttpServletRequest request, CacheResponseWrapper cachedResponse) throws IOException {
102 int status = cachedResponse.getStatus();
103
104 if (cachedResponse.getRedirectionLocation() != null) {
105 return new CachedRedirect(cachedResponse.getStatus(), cachedResponse.getRedirectionLocation());
106 }
107
108 if (cachedResponse.isError()) {
109 return new CachedError(cachedResponse.getStatus());
110 }
111
112 final long modificationDate = cachedResponse.getLastModified();
113 final String contentType = cachedResponse.getContentType();
114
115 ContentCachedEntry cacheEntry;
116 if(!cachedResponse.isThresholdExceeded()){
117 cacheEntry = new InMemoryCachedEntry(cachedResponse.getBufferedContent(),
118 contentType,
119 cachedResponse.getCharacterEncoding(),
120 status,
121 cachedResponse.getHeaders(),
122 modificationDate);
123 }
124 else{
125 cacheEntry = new DelegatingBlobCachedEntry(cachedResponse.getContentLength(),
126 contentType,
127 cachedResponse.getCharacterEncoding(),
128 status,
129 cachedResponse.getHeaders(),
130 modificationDate);
131
132
133
134 ((DelegatingBlobCachedEntry)cacheEntry).bindContentFileToCurrentRequest(request, cachedResponse.getContentFile());
135 }
136
137 return cacheEntry;
138 }
139
140 protected CachePolicy getCachePolicy(Cache cache) {
141 return getModule().getConfiguration(cache.getName()).getCachePolicy();
142 }
143
144 protected CacheModule getModule() {
145 return CacheModule.getInstance();
146 }
147 }