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 responseWrapper.setResponseExpirationDetectionEnabled();
73
74
75 final long cacheStorageDate = System.currentTimeMillis();
76 responseWrapper.setDateHeader("Last-Modified", cacheStorageDate);
77 chain.doFilter(request, responseWrapper);
78
79 if (responseWrapper.getStatus() == HttpServletResponse.SC_NOT_MODIFIED) {
80 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
81 }
82 else {
83 responseWrapper.flushBuffer();
84 cachedEntry = makeCachedEntry(request, responseWrapper);
85 }
86
87 if (cachedEntry == null) {
88
89 cache.put(key, null);
90 return;
91 }
92
93 cachePolicyResult.setCachedEntry(cachedEntry);
94
95 int timeToLiveInSeconds = cachedEntry.getTimeToLiveInSeconds();
96 if (timeToLiveInSeconds == 0) {
97
98 cache.put(key, null);
99
100
101 cachedEntry.replay(request, response, chain);
102 response.flushBuffer();
103
104 return;
105 }
106
107 if (timeToLiveInSeconds == -1) {
108 cache.put(key, cachedEntry);
109 } else {
110 cache.put(key, cachedEntry, timeToLiveInSeconds);
111 }
112
113
114 final Content content = MgnlContext.getAggregationState().getCurrentContent();
115 if (content != null && content.isNodeType("mix:referenceable")) {
116 final String uuid = content.getUUID();
117 String repo = content.getHierarchyManager().getName();
118 getCachePolicy(cache).persistCacheKey(repo, uuid, key);
119 }
120 }
121
122 protected CachedEntry makeCachedEntry(HttpServletRequest request, CacheResponseWrapper cachedResponse) throws IOException {
123
124 final String originalUrl = request.getRequestURL().toString();
125 int status = cachedResponse.getStatus();
126 int timeToLiveInSeconds = cachedResponse.getTimeToLiveInSeconds();
127
128
129 if (cachedResponse.getRedirectionLocation() != null) {
130 return new CachedRedirect(cachedResponse.getStatus(), cachedResponse.getRedirectionLocation(), originalUrl, timeToLiveInSeconds);
131 }
132
133 if (cachedResponse.isError()) {
134 return new CachedError(cachedResponse.getStatus(), originalUrl, timeToLiveInSeconds);
135 }
136
137 final long modificationDate = cachedResponse.getLastModified();
138 final String contentType = cachedResponse.getContentType();
139
140 ContentCachedEntry cacheEntry;
141 if(!cachedResponse.isThresholdExceeded()){
142 cacheEntry = new InMemoryCachedEntry(cachedResponse.getBufferedContent(),
143 contentType,
144 cachedResponse.getCharacterEncoding(),
145 status,
146 cachedResponse.getHeaders(),
147 modificationDate,
148 originalUrl,
149 timeToLiveInSeconds);
150 }
151 else{
152 cacheEntry = new DelegatingBlobCachedEntry(cachedResponse.getContentLength(),
153 contentType,
154 cachedResponse.getCharacterEncoding(),
155 status,
156 cachedResponse.getHeaders(),
157 modificationDate,
158 originalUrl,
159 timeToLiveInSeconds);
160
161
162
163 ((DelegatingBlobCachedEntry)cacheEntry).bindContentFileToCurrentRequest(request, cachedResponse.getContentFile());
164 cachedResponse.getThresholdingOutputStream().close();
165 }
166
167 return cacheEntry;
168 }
169
170 protected CachePolicy getCachePolicy(Cache cache) {
171 return getModule().getConfiguration(cache.getName()).getCachePolicy();
172 }
173
174 protected CacheModule getModule() {
175 return CacheModule.getInstance();
176 }
177 }