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.cache.CacheConstants; |
37 |
|
import info.magnolia.context.MgnlContext; |
38 |
|
import info.magnolia.jcr.util.NodeUtil; |
39 |
|
import info.magnolia.module.ModuleRegistry; |
40 |
|
import info.magnolia.module.cache.Cache; |
41 |
|
import info.magnolia.module.cache.CacheModule; |
42 |
|
import info.magnolia.module.cache.CachePolicy; |
43 |
|
import info.magnolia.module.cache.CachePolicyResult; |
44 |
|
import info.magnolia.module.cache.filter.CacheResponseWrapper; |
45 |
|
import info.magnolia.module.cache.filter.CachedEntry; |
46 |
|
import info.magnolia.module.cache.filter.CachedError; |
47 |
|
import info.magnolia.module.cache.filter.CachedRedirect; |
48 |
|
import info.magnolia.module.cache.filter.ContentCachedEntry; |
49 |
|
import info.magnolia.module.cache.filter.DelegatingBlobCachedEntry; |
50 |
|
import info.magnolia.module.cache.filter.InMemoryCachedEntry; |
51 |
|
import info.magnolia.module.cache.filter.UncacheableEntry; |
52 |
|
import info.magnolia.objectfactory.Components; |
53 |
|
|
54 |
|
import java.io.IOException; |
55 |
|
|
56 |
|
import javax.inject.Inject; |
57 |
|
import javax.jcr.Node; |
58 |
|
import javax.jcr.RepositoryException; |
59 |
|
import javax.servlet.FilterChain; |
60 |
|
import javax.servlet.ServletException; |
61 |
|
import javax.servlet.http.HttpServletRequest; |
62 |
|
import javax.servlet.http.HttpServletResponse; |
63 |
|
|
64 |
|
import org.apache.commons.lang3.StringUtils; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
|
|
| 51.1% |
Uncovered Elements: 46 (94) |
Complexity: 23 |
Complexity Density: 0.34 |
|
69 |
|
public class Store extends AbstractExecutor { |
70 |
|
|
71 |
|
private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Store.class); |
72 |
|
private final CacheModule cacheModule; |
73 |
|
|
74 |
|
|
75 |
|
@deprecated@link |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
0 |
public Store() {... |
78 |
0 |
this.cacheModule = Components.getComponent(ModuleRegistry.class).getModuleInstance(CacheModule.class); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
25 |
@Inject... |
82 |
|
public Store(CacheModule cacheModule) { |
83 |
25 |
this.cacheModule = cacheModule; |
84 |
|
} |
85 |
|
|
|
|
| 56.5% |
Uncovered Elements: 20 (46) |
Complexity: 11 |
Complexity Density: 0.31 |
|
86 |
6 |
@Override... |
87 |
|
public void processCacheRequest(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Cache cache, CachePolicyResult cachePolicyResult) throws IOException, ServletException { |
88 |
|
|
89 |
6 |
CachedEntry cachedEntry = null; |
90 |
6 |
final Object key = cachePolicyResult.getCacheKey(); |
91 |
|
|
92 |
6 |
final CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response, CacheResponseWrapper.DEFAULT_THRESHOLD, false); |
93 |
6 |
responseWrapper.setResponseExpirationDetectionEnabled(); |
94 |
|
|
95 |
|
|
96 |
6 |
final long cacheStorageDate = System.currentTimeMillis(); |
97 |
6 |
responseWrapper.setDateHeader("Last-Modified", cacheStorageDate); |
98 |
|
|
99 |
6 |
try { |
100 |
6 |
chain.doFilter(request, responseWrapper); |
101 |
6 |
if (responseWrapper.getStatus() == HttpServletResponse.SC_NOT_MODIFIED) { |
102 |
0 |
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
103 |
|
} else { |
104 |
6 |
responseWrapper.flushBuffer(); |
105 |
6 |
cachedEntry = makeCachedEntry(request, responseWrapper, cache, cachePolicyResult); |
106 |
6 |
if (cachedEntry.getTimeToLiveInSeconds() == 0) { |
107 |
6 |
cachedEntry = new UncacheableEntry(cachedEntry); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
} catch (IOException e) { |
112 |
0 |
responseWrapper.cleanUp(); |
113 |
0 |
throw e; |
114 |
|
} catch (ServletException e) { |
115 |
0 |
responseWrapper.cleanUp(); |
116 |
0 |
throw e; |
117 |
|
} catch (Throwable e) { |
118 |
0 |
responseWrapper.cleanUp(); |
119 |
0 |
throw new RuntimeException("Failed to process request with: " + e.getMessage(), e); |
120 |
|
} |
121 |
|
|
122 |
6 |
if (cachedEntry == null) { |
123 |
|
|
124 |
0 |
cache.put(key, null); |
125 |
0 |
return; |
126 |
|
} |
127 |
|
|
128 |
6 |
int timeToLiveInSeconds = cachedEntry.getTimeToLiveInSeconds(); |
129 |
6 |
cachePolicyResult.setCachedEntry(cachedEntry); |
130 |
|
|
131 |
6 |
if (timeToLiveInSeconds == -1) { |
132 |
6 |
cache.put(key, cachedEntry); |
133 |
|
} else { |
134 |
0 |
cache.put(key, cachedEntry, timeToLiveInSeconds); |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
6 |
final Node content = MgnlContext.getAggregationState().getMainContentNode(); |
139 |
6 |
try { |
140 |
6 |
if (content != null && NodeUtil.isNodeType(content, "mix:referenceable")) { |
141 |
0 |
final String uuid = content.getIdentifier(); |
142 |
0 |
String repo = content.getSession().getWorkspace().getName(); |
143 |
0 |
getCachePolicy(cache).persistCacheKey(repo, uuid, key); |
144 |
|
} |
145 |
|
} catch (RepositoryException e) { |
146 |
|
|
147 |
0 |
responseWrapper.cleanUp(); |
148 |
0 |
throw new RuntimeException(e); |
149 |
|
} |
150 |
|
} |
151 |
|
|
|
|
| 63.6% |
Uncovered Elements: 8 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
152 |
6 |
protected CachedEntry makeCachedEntry(HttpServletRequest request, CacheResponseWrapper cachedResponse, Cache cache, CachePolicyResult cachePolicyResult) throws IOException {... |
153 |
|
|
154 |
6 |
final String originalUrl = request.getRequestURL().toString(); |
155 |
6 |
int status = cachedResponse.getStatus(); |
156 |
6 |
int timeToLiveInSeconds = this.getTimeToLive(cachedResponse, cache); |
157 |
|
|
158 |
|
|
159 |
6 |
if (cachedResponse.getRedirectionLocation() != null) { |
160 |
0 |
return new CachedRedirect(cachedResponse.getStatus(), cachedResponse.getRedirectionLocation(), originalUrl, timeToLiveInSeconds); |
161 |
|
} |
162 |
|
|
163 |
6 |
if (cachedResponse.isError()) { |
164 |
0 |
return new CachedError(cachedResponse.getStatus(), originalUrl, timeToLiveInSeconds); |
165 |
|
} |
166 |
|
|
167 |
6 |
final long modificationDate = cachedResponse.getLastModified(); |
168 |
6 |
final String contentType = cachedResponse.getContentType(); |
169 |
|
|
170 |
6 |
ContentCachedEntry cacheEntry; |
171 |
6 |
if (!cachedResponse.isThresholdExceeded()) { |
172 |
6 |
cacheEntry = new InMemoryCachedEntry(cachedResponse.getBufferedContent(), |
173 |
|
contentType, |
174 |
|
cachedResponse.getCharacterEncoding(), |
175 |
|
status, |
176 |
|
cachedResponse.getHeaders(), |
177 |
|
modificationDate, |
178 |
|
originalUrl, |
179 |
|
timeToLiveInSeconds); |
180 |
|
} else { |
181 |
0 |
cacheEntry = new DelegatingBlobCachedEntry(cachedResponse.getContentLength(), |
182 |
|
contentType, |
183 |
|
cachedResponse.getCharacterEncoding(), |
184 |
|
status, |
185 |
|
cachedResponse.getHeaders(), |
186 |
|
modificationDate, |
187 |
|
originalUrl, |
188 |
|
timeToLiveInSeconds); |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
0 |
((DelegatingBlobCachedEntry) cacheEntry).bindContentFileToCurrentRequest(request, cachedResponse.getContentFile()); |
193 |
0 |
cachedResponse.getThresholdingOutputStream().close(); |
194 |
|
} |
195 |
6 |
return cacheEntry; |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
6 |
protected int getTimeToLive(CacheResponseWrapper cachedResponse, Cache cache) {... |
199 |
6 |
return this.getCachePolicy(cache).getTtlVoters().vote(cachedResponse); |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
6 |
protected CachePolicy getCachePolicy(Cache cache) {... |
203 |
6 |
return cacheModule.getContentCaching(cache.getName()).getCachePolicy(); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
@deprecated@link |
209 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
210 |
0 |
@Deprecated... |
211 |
|
protected int getTimeToLive(HttpServletRequest request, CacheResponseWrapper cachedResponse) { |
212 |
0 |
Object attribute = request.getAttribute(CacheConstants.HEADER_X_MAGNOLIA_CACHE); |
213 |
0 |
if (attribute instanceof String) { |
214 |
0 |
String attributeAsString = (String) attribute; |
215 |
0 |
String[] splitted = StringUtils.split(attributeAsString, ","); |
216 |
0 |
for (String headerValue : splitted) { |
217 |
0 |
if (headerValue.contains(CacheConstants.HEADER_VALUE_TTL + "=")) { |
218 |
0 |
String ttlString = StringUtils.substringAfter(headerValue, CacheConstants.HEADER_VALUE_TTL + "="); |
219 |
0 |
try { |
220 |
0 |
return Integer.parseInt(ttlString); |
221 |
|
} catch (NumberFormatException e) { |
222 |
0 |
log.error("Unparsable TTL in '{}' attribute: {}", CacheConstants.HEADER_X_MAGNOLIA_CACHE, headerValue); |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
0 |
return cachedResponse.getTimeToLiveInSeconds(); |
228 |
|
} |
229 |
|
|
230 |
|
} |