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
91 cache.put(key, cachedEntry);
92
93 if(cachedEntry != null){
94 cachePolicyResult.setCachedEntry(cachedEntry);
95
96 final Content currentContent = MgnlContext.getAggregationState().getCurrentContent();
97 final Node content = currentContent != null ? currentContent.getJCRNode() : null;
98 try {
99 if (content != null && NodeUtil.isNodeType(content, "mix:referenceable")) {
100 final String uuid = content.getIdentifier();
101 String repo = content.getSession().getWorkspace().getName();
102 getCachePolicy(cache).persistCacheKey(repo, uuid, key);
103 }
104 } catch (RepositoryException e) {
105
106 throw new RuntimeException(e);
107 }
108
109 }
110 }
111
112 protected CachedEntry makeCachedEntry(HttpServletRequest request, CacheResponseWrapper cachedResponse) throws IOException {
113
114 final String originalUrl = request.getRequestURL().toString();
115 int status = cachedResponse.getStatus();
116
117 if (cachedResponse.getRedirectionLocation() != null) {
118 return new CachedRedirect(cachedResponse.getStatus(), cachedResponse.getRedirectionLocation(), originalUrl);
119 }
120
121 if (cachedResponse.isError()) {
122 return new CachedError(cachedResponse.getStatus(), originalUrl);
123 }
124
125 final long modificationDate = cachedResponse.getLastModified();
126 final String contentType = cachedResponse.getContentType();
127
128 ContentCachedEntry cacheEntry;
129 if(!cachedResponse.isThresholdExceeded()){
130 cacheEntry = new InMemoryCachedEntry(cachedResponse.getBufferedContent(),
131 contentType,
132 cachedResponse.getCharacterEncoding(),
133 status,
134 cachedResponse.getHeaders(),
135 modificationDate,
136 originalUrl);
137 }
138 else{
139 cacheEntry = new DelegatingBlobCachedEntry(cachedResponse.getContentLength(),
140 contentType,
141 cachedResponse.getCharacterEncoding(),
142 status,
143 cachedResponse.getHeaders(),
144 modificationDate,
145 originalUrl);
146
147
148
149 ((DelegatingBlobCachedEntry)cacheEntry).bindContentFileToCurrentRequest(request, cachedResponse.getContentFile());
150 }
151
152 return cacheEntry;
153 }
154
155 protected CachePolicy getCachePolicy(Cache cache) {
156 return getModule().getConfiguration(cache.getName()).getCachePolicy();
157 }
158
159
160
161
162 @Deprecated
163 protected CacheModule getModule() {
164 return CacheModule.getInstance();
165 }
166 }