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.filter; |
35 |
|
|
36 |
|
import info.magnolia.cms.core.AggregationState; |
37 |
|
import info.magnolia.cms.filters.AbstractMgnlFilter; |
38 |
|
import info.magnolia.cms.util.ServletUtil; |
39 |
|
import info.magnolia.context.WebContext; |
40 |
|
import info.magnolia.module.cache.AbstractCacheModule; |
41 |
|
import info.magnolia.module.cache.BlockingCache; |
42 |
|
import info.magnolia.module.cache.Cache; |
43 |
|
import info.magnolia.module.cache.CacheModule; |
44 |
|
import info.magnolia.module.cache.CacheModuleLifecycleListener; |
45 |
|
import info.magnolia.module.cache.CachePolicyExecutor; |
46 |
|
import info.magnolia.module.cache.CachePolicyResult; |
47 |
|
import info.magnolia.module.cache.ContentCachingConfiguration; |
48 |
|
import info.magnolia.module.cache.cachepolicy.result.CachePolicyResultProvider; |
49 |
|
import info.magnolia.module.cache.exception.MgnlLockTimeoutException; |
50 |
|
import info.magnolia.module.cache.mbean.CacheMonitor; |
51 |
|
import info.magnolia.objectfactory.Components; |
52 |
|
|
53 |
|
import java.io.IOException; |
54 |
|
|
55 |
|
import javax.inject.Inject; |
56 |
|
import javax.inject.Provider; |
57 |
|
import javax.servlet.FilterChain; |
58 |
|
import javax.servlet.FilterConfig; |
59 |
|
import javax.servlet.ServletException; |
60 |
|
import javax.servlet.ServletResponse; |
61 |
|
import javax.servlet.http.HttpServletRequest; |
62 |
|
import javax.servlet.http.HttpServletResponse; |
63 |
|
import javax.servlet.http.HttpServletResponseWrapper; |
64 |
|
|
65 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
66 |
|
import org.slf4j.Logger; |
67 |
|
import org.slf4j.LoggerFactory; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
|
|
| 67.7% |
Uncovered Elements: 30 (93) |
Complexity: 28 |
Complexity Density: 0.46 |
|
73 |
|
public class CacheFilter extends AbstractMgnlFilter implements CacheModuleLifecycleListener { |
74 |
|
|
75 |
|
|
76 |
|
@deprecated@link |
77 |
|
|
78 |
|
@Deprecated |
79 |
|
public static final String DEFAULT_CONTENT_CACHING_CONFIGURATION_NAME = AbstractCacheModule.DEFAULT_CACHE_CONFIG; |
80 |
|
|
81 |
|
private static final Logger log = LoggerFactory.getLogger(CacheFilter.class); |
82 |
|
|
83 |
|
|
84 |
|
private String defaultContentCachingConfigurationName; |
85 |
|
private Cache cache; |
86 |
|
private ContentCachingConfiguration contentCaching; |
87 |
|
|
88 |
|
private final CacheModule cacheModule; |
89 |
|
private final CacheMonitor monitor; |
90 |
|
private final Provider<WebContext> webContextProvider; |
91 |
|
private final Provider<CachePolicyResultProvider> cachePolicyResultProviderProvider; |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
93 |
22 |
@Inject... |
94 |
|
public CacheFilter(Provider<WebContext> webContextProvider, CacheModule cacheModule, CacheMonitor cacheMonitor, Provider<CachePolicyResultProvider> cachePolicyResultProvider) { |
95 |
22 |
this.cacheModule = cacheModule; |
96 |
22 |
this.monitor = cacheMonitor; |
97 |
22 |
this.webContextProvider = webContextProvider; |
98 |
22 |
this.cachePolicyResultProviderProvider = cachePolicyResultProvider; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
@deprecated@link |
103 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
104 |
0 |
@Deprecated... |
105 |
|
public CacheFilter(CacheModule cacheModule, CacheMonitor cacheMonitor, Provider<AggregationState> aggregationStateProvider, Provider<CachePolicyResultProvider> cachePolicyResultProvider) { |
106 |
0 |
this(new Provider<WebContext>() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0 |
@Override... |
108 |
|
public WebContext get() { |
109 |
0 |
return Components.getComponent(WebContext.class); |
110 |
|
} |
111 |
|
}, cacheModule, cacheMonitor, cachePolicyResultProvider); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
@deprecated@link |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
0 |
@Deprecated... |
118 |
|
public CacheFilter(CacheModule cacheModule, CacheMonitor cacheMonitor) { |
119 |
0 |
this(new Provider<WebContext>() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
0 |
@Override... |
121 |
|
public WebContext get() { |
122 |
0 |
return Components.getComponent(WebContext.class); |
123 |
|
} |
124 |
|
}, cacheModule, cacheMonitor, new Provider<CachePolicyResultProvider>() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0 |
@Override... |
126 |
|
public CachePolicyResultProvider get() { |
127 |
0 |
return Components.getComponent(CachePolicyResultProvider.class); |
128 |
|
} |
129 |
|
} |
130 |
|
); |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
@deprecated@link |
135 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
136 |
|
@Deprecated... |
137 |
|
public String getCacheConfigurationName() { |
138 |
|
return getDefaultContentCachingConfigurationName(); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
|
@deprecated@link |
143 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
144 |
|
@Deprecated... |
145 |
|
public void setCacheConfigurationName(String cacheConfigurationName) { |
146 |
|
setDefaultContentCachingConfigurationName(cacheConfigurationName); |
147 |
|
} |
148 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
149 |
|
public String getDefaultContentCachingConfigurationName() {... |
150 |
|
return defaultContentCachingConfigurationName; |
151 |
|
} |
152 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
153 |
|
public void setDefaultContentCachingConfigurationName(String defaultContentCachingConfigurationName) {... |
154 |
|
this.defaultContentCachingConfigurationName = defaultContentCachingConfigurationName; |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
157 |
23 |
@Override... |
158 |
|
public void init(FilterConfig filterConfig) throws ServletException { |
159 |
23 |
super.init(filterConfig); |
160 |
23 |
cacheModule.register(this); |
161 |
|
|
162 |
23 |
onCacheModuleStart(); |
163 |
|
} |
164 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
165 |
24 |
@Override... |
166 |
|
public void onCacheModuleStart() { |
167 |
|
|
168 |
24 |
if (defaultContentCachingConfigurationName == null) { |
169 |
0 |
log.warn("The {} property is not set for the {} CacheFilter, falling back to {}.", "defaultContentCachingConfigurationName", getName(), AbstractCacheModule.DEFAULT_CACHE_CONFIG); |
170 |
0 |
this.defaultContentCachingConfigurationName = AbstractCacheModule.DEFAULT_CACHE_CONFIG; |
171 |
|
} |
172 |
24 |
this.contentCaching = cacheModule.getContentCaching(defaultContentCachingConfigurationName); |
173 |
24 |
cache = cacheModule.getCacheFactory().getCache(contentCaching.getCacheName()); |
174 |
|
|
175 |
24 |
if (contentCaching == null || cache == null) { |
176 |
1 |
log.error("The " + getName() + " CacheFilter is not properly configured, either default contentCaching(" + contentCaching + ") or cache(" + cache + ") is null. Check if " + defaultContentCachingConfigurationName + " is a valid content caching configuration name. Will disable temporarily."); |
177 |
1 |
setEnabled(false); |
178 |
|
} |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
17 |
protected Cache getCurrentPageCache() {... |
182 |
17 |
return cache; |
183 |
|
} |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
185 |
0 |
protected CacheModule getModule() {... |
186 |
0 |
return cacheModule; |
187 |
|
} |
188 |
|
|
|
|
| 68.3% |
Uncovered Elements: 13 (41) |
Complexity: 10 |
Complexity Density: 0.32 |
|
189 |
17 |
@Override... |
190 |
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
191 |
|
|
192 |
17 |
final CachePolicyResult cachePolicyResult; |
193 |
17 |
final Cache cache = this.getCurrentPageCache(); |
194 |
17 |
int blockingTimeout = -1; |
195 |
17 |
if (cache instanceof BlockingCache) { |
196 |
0 |
blockingTimeout = ((BlockingCache) cache).getBlockingTimeout(); |
197 |
|
} |
198 |
17 |
final ContentCachingConfiguration contentCachingConfiguration = cacheModule.getContentCaching(cache.getName()); |
199 |
17 |
try { |
200 |
17 |
cachePolicyResult = contentCachingConfiguration.getCachePolicy().shouldCache(cache, webContextProvider.get().getAggregationState(), contentCachingConfiguration.getFlushPolicy()); |
201 |
17 |
cachePolicyResult.setCacheName(cache.getName()); |
202 |
17 |
cachePolicyResultProviderProvider.get().setCachePolicyResult(cachePolicyResult); |
203 |
|
} catch (MgnlLockTimeoutException timeout) { |
204 |
0 |
log.warn("The following URL was blocked for longer than {} seconds and has timed-out. The request has been blocked as another request is already processing the same resource. [url={}]", blockingTimeout / 1000, request.getRequestURL()); |
205 |
0 |
throw timeout; |
206 |
|
} |
207 |
|
|
208 |
17 |
log.debug("Cache policy result: {}", cachePolicyResult); |
209 |
|
|
210 |
17 |
final CachePolicyResult.CachePolicyBehaviour behaviour = cachePolicyResult.getBehaviour(); |
211 |
17 |
monitor.logBehavior(behaviour.getName()); |
212 |
17 |
monitor.logAccess(cachePolicyResult.getCacheKey()); |
213 |
17 |
final CachePolicyExecutor executor = contentCachingConfiguration.getExecutor(behaviour); |
214 |
17 |
if (executor == null) { |
215 |
0 |
throw new IllegalStateException("Unexpected cache policy result: " + cachePolicyResult); |
216 |
|
} |
217 |
|
|
218 |
17 |
try { |
219 |
17 |
final long start = System.currentTimeMillis(); |
220 |
17 |
executor.processCacheRequest(request, response, chain, cache, cachePolicyResult); |
221 |
16 |
final long end = System.currentTimeMillis(); |
222 |
|
|
223 |
16 |
if (blockingTimeout != -1 && (end - start) >= blockingTimeout) { |
224 |
0 |
log.warn("The following URL took longer than {} seconds ({} ms) to render. This might cause timeout exceptions on other requests to the same URI. [url={}]", blockingTimeout / 1000, (end - start), request.getRequestURL()); |
225 |
|
} |
226 |
|
} catch (Throwable th) { |
227 |
1 |
if (cachePolicyResult.getBehaviour() == CachePolicyResult.store && cache instanceof BlockingCache) { |
228 |
0 |
log.error("A request started to cache but failed with an exception ({}). [url={}]", ExceptionUtils.getRootCauseMessage(th), request.getRequestURL()); |
229 |
0 |
((BlockingCache) cache).unlock(cachePolicyResult.getCacheKey()); |
230 |
|
} |
231 |
1 |
if (th instanceof RuntimeException) { |
232 |
1 |
throw (RuntimeException) th; |
233 |
|
} |
234 |
0 |
throw new RuntimeException(th); |
235 |
|
} |
236 |
|
} |
237 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
238 |
2 |
@Override... |
239 |
|
public boolean bypasses(HttpServletRequest request) { |
240 |
2 |
if (request.getAttribute(ServletUtil.ERROR_REQUEST_STATUS_CODE_ATTRIBUTE) != null) { |
241 |
1 |
return true; |
242 |
|
} |
243 |
1 |
ServletResponse responseWrapper = webContextProvider.get().getResponse(); |
244 |
2 |
while (responseWrapper instanceof HttpServletResponseWrapper) { |
245 |
2 |
if (responseWrapper instanceof CacheResponseWrapper && !(responseWrapper instanceof GZipFilter.GZipCacheResponseWrapper)) { |
246 |
1 |
return true; |
247 |
|
} else { |
248 |
1 |
responseWrapper = ((HttpServletResponseWrapper) responseWrapper).getResponse(); |
249 |
|
} |
250 |
|
} |
251 |
0 |
return super.bypasses(request); |
252 |
|
} |
253 |
|
} |