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.jcr.util.NodeUtil;
39 import info.magnolia.module.cache.Cache;
40 import info.magnolia.module.cache.CacheModule;
41 import info.magnolia.module.cache.CachePolicy;
42 import info.magnolia.module.cache.CachePolicyResult;
43 import info.magnolia.module.cache.filter.CacheResponseWrapper;
44 import info.magnolia.module.cache.filter.CachedEntry;
45 import info.magnolia.module.cache.filter.CachedError;
46 import info.magnolia.module.cache.filter.CachedRedirect;
47 import info.magnolia.module.cache.filter.ContentCachedEntry;
48 import info.magnolia.module.cache.filter.DelegatingBlobCachedEntry;
49 import info.magnolia.module.cache.filter.InMemoryCachedEntry;
50
51 import java.io.IOException;
52
53 import javax.jcr.Node;
54 import javax.jcr.RepositoryException;
55 import javax.servlet.FilterChain;
56 import javax.servlet.ServletException;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59
60
61
62
63
64
65
66 public class Store extends AbstractExecutor {
67
68 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Store.class);
69
70 @Override
71 public void processCacheRequest(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Cache cache, CachePolicyResult cachePolicyResult) throws IOException, ServletException {
72 CachedEntry cachedEntry = null;
73 final Object key = cachePolicyResult.getCacheKey();
74
75 final CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response, CacheResponseWrapper.DEFAULT_THRESHOLD, false);
76 responseWrapper.setResponseExpirationDetectionEnabled();
77
78
79 final long cacheStorageDate = System.currentTimeMillis();
80 responseWrapper.setDateHeader("Last-Modified", cacheStorageDate);
81 chain.doFilter(request, responseWrapper);
82
83 if (responseWrapper.getStatus() == HttpServletResponse.SC_NOT_MODIFIED) {
84 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
85 }
86 else {
87 responseWrapper.flushBuffer();
88 cachedEntry = makeCachedEntry(request, responseWrapper);
89 }
90
91 if (cachedEntry == null) {
92
93 cache.put(key, null);
94 return;
95 }
96
97 cachePolicyResult.setCachedEntry(cachedEntry);
98
99 int timeToLiveInSeconds = cachedEntry.getTimeToLiveInSeconds();
100 if (timeToLiveInSeconds == 0) {
101
102 cache.put(key, null);
103
104
105 cachedEntry.replay(request, response, chain);
106 response.flushBuffer();
107
108 return;
109 }
110
111 if (timeToLiveInSeconds == -1) {
112 cache.put(key, cachedEntry);
113 } else {
114 cache.put(key, cachedEntry, timeToLiveInSeconds);
115 }
116
117
118 final Content currentContent = MgnlContext.getAggregationState().getCurrentContent();
119 final Node content = currentContent != null ? currentContent.getJCRNode() : null;
120 try {
121 if (content != null && NodeUtil.isNodeType(content, "mix:referenceable")) {
122 final String uuid = content.getIdentifier();
123 String repo = content.getSession().getWorkspace().getName();
124 getCachePolicy(cache).persistCacheKey(repo, uuid, key);
125 }
126 } catch (RepositoryException e) {
127
128 throw new RuntimeException(e);
129 }
130 }
131
132 protected CachedEntry makeCachedEntry(HttpServletRequest request, CacheResponseWrapper cachedResponse) throws IOException {
133
134 final String originalUrl = request.getRequestURL().toString();
135 int status = cachedResponse.getStatus();
136 int timeToLiveInSeconds = cachedResponse.getTimeToLiveInSeconds();
137
138
139 if (cachedResponse.getRedirectionLocation() != null) {
140 return new CachedRedirect(cachedResponse.getStatus(), cachedResponse.getRedirectionLocation(), originalUrl, timeToLiveInSeconds);
141 }
142
143 if (cachedResponse.isError()) {
144 return new CachedError(cachedResponse.getStatus(), originalUrl, timeToLiveInSeconds);
145 }
146
147 final long modificationDate = cachedResponse.getLastModified();
148 final String contentType = cachedResponse.getContentType();
149
150 ContentCachedEntry cacheEntry;
151 if(!cachedResponse.isThresholdExceeded()){
152 cacheEntry = new InMemoryCachedEntry(cachedResponse.getBufferedContent(),
153 contentType,
154 cachedResponse.getCharacterEncoding(),
155 status,
156 cachedResponse.getHeaders(),
157 modificationDate,
158 originalUrl,
159 timeToLiveInSeconds);
160 }
161 else{
162 cacheEntry = new DelegatingBlobCachedEntry(cachedResponse.getContentLength(),
163 contentType,
164 cachedResponse.getCharacterEncoding(),
165 status,
166 cachedResponse.getHeaders(),
167 modificationDate,
168 originalUrl,
169 timeToLiveInSeconds);
170
171
172
173 ((DelegatingBlobCachedEntry)cacheEntry).bindContentFileToCurrentRequest(request, cachedResponse.getContentFile());
174 cachedResponse.getThresholdingOutputStream().close();
175 }
176
177 return cacheEntry;
178 }
179
180 protected CachePolicy getCachePolicy(Cache cache) {
181 return getModule().getConfiguration(cache.getName()).getCachePolicy();
182 }
183
184
185
186
187 @Deprecated
188 protected CacheModule getModule() {
189 return CacheModule.getInstance();
190 }
191 }