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.OncePerRequestAbstractMgnlFilter;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.module.ModuleRegistry;
40 import info.magnolia.module.cache.Cache;
41 import info.magnolia.module.cache.CacheConfiguration;
42 import info.magnolia.module.cache.CacheModuleLifecycleListener;
43 import info.magnolia.module.cache.CacheModule;
44 import info.magnolia.module.cache.CachePolicyExecutor;
45 import info.magnolia.module.cache.CachePolicyResult;
46 import info.magnolia.module.cache.mbean.CacheMonitor;
47
48 import javax.servlet.FilterChain;
49 import javax.servlet.FilterConfig;
50 import javax.servlet.ServletException;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import java.io.IOException;
55
56
57
58
59
60
61
62
63 public class CacheFilter extends OncePerRequestAbstractMgnlFilter implements CacheModuleLifecycleListener {
64 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CacheFilter.class);
65
66 private static final String DEFAULT_CACHE_CONFIG = "default";
67
68 private CacheMonitor monitor;
69 private String cacheConfigurationName;
70 private CacheConfiguration cacheConfig;
71 private Cache cache;
72
73 public String getCacheConfigurationName() {
74 return cacheConfigurationName;
75 }
76
77 public void setCacheConfigurationName(String cacheConfigurationName) {
78 this.cacheConfigurationName = cacheConfigurationName;
79 }
80
81 public void init(FilterConfig filterConfig) throws ServletException {
82 super.init(filterConfig);
83 CacheModule.getInstance().register(this);
84
85 onCacheModuleStart();
86 }
87
88 public void onCacheModuleStart() {
89 if (cacheConfigurationName == null) {
90 log.warn("The cacheConfigurationName property is not set for the {} CacheFilter, falling back to {}.", getName(), DEFAULT_CACHE_CONFIG);
91 this.cacheConfigurationName = DEFAULT_CACHE_CONFIG;
92 }
93
94 final CacheModule cacheModule = getModule();
95 this.cacheConfig = cacheModule.getConfiguration(cacheConfigurationName);
96 this.cache = cacheModule.getCacheFactory().getCache(cacheConfigurationName);
97
98 if (cacheConfig == null || cache == null) {
99 log.error("The " + getName() + " CacheFilter is not properly configured, either cacheConfig(" + cacheConfig + ") or cache(" + cache + ") is null. Check if " + cacheConfigurationName + " is a valid cache configuration name. Will disable temporarily.");
100 setEnabled(false);
101 }
102
103 monitor = CacheMonitor.getInstance();
104 }
105
106 protected CacheModule getModule() {
107 return CacheModule.getInstance();
108 }
109
110 public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
111 final AggregationState aggregationState = MgnlContext.getAggregationState();
112 final CachePolicyResult cachePolicyResult = cacheConfig.getCachePolicy().shouldCache(cache, aggregationState, cacheConfig.getFlushPolicy());
113
114 log.debug("Cache policy result: {}", cachePolicyResult);
115
116 final CachePolicyResult.CachePolicyBehaviour behaviour = cachePolicyResult.getBehaviour();
117 monitor.logBehavior(behaviour.getName());
118 monitor.logAccess(cachePolicyResult.getCacheKey());
119 final CachePolicyExecutor executor = cacheConfig.getExecutor(behaviour);
120 if (executor == null) {
121 throw new IllegalStateException("Unexpected cache policy result: " + cachePolicyResult);
122 }
123 executor.processCacheRequest(request, response, chain, cache, cachePolicyResult);
124
125
126
127
128
129
130
131
132
133
134
135
136
137 }
138
139 }