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