1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.cache.rendering; |
21 |
|
|
22 |
|
import java.io.UnsupportedEncodingException; |
23 |
|
import java.net.URLEncoder; |
24 |
|
import java.util.LinkedList; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.SortedMap; |
28 |
|
import java.util.TreeMap; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Provider; |
32 |
|
import javax.inject.Singleton; |
33 |
|
|
34 |
|
import org.xwiki.cache.CacheException; |
35 |
|
import org.xwiki.cache.config.CacheConfiguration; |
36 |
|
import org.xwiki.cache.eviction.LRUEvictionConfiguration; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.component.phase.Initializable; |
39 |
|
import org.xwiki.component.phase.InitializationException; |
40 |
|
import org.xwiki.model.reference.DocumentReference; |
41 |
|
|
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.internal.cache.DocumentCache; |
44 |
|
import com.xpn.xwiki.internal.cache.rendering.CachedItem.UsedExtension; |
45 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
46 |
|
import com.xpn.xwiki.plugin.XWikiPluginManager; |
47 |
|
|
48 |
|
|
49 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
|
|
| 76.1% |
Uncovered Elements: 22 (92) |
Complexity: 25 |
Complexity Density: 0.45 |
|
56 |
|
public class DefaultRenderingCache implements RenderingCache, Initializable |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private static final String UTF8 = "UTF-8"; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String NAME = "core.renderingcache"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private static final String PARAMETER_REFRESH = "refresh"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
private RenderingCacheConfiguration configuration; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@Inject |
83 |
|
private Provider<List<RenderingCacheAware>> renderingCacheAwareProvider; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private List<RenderingCacheAware> legacyRenderingCacheAware; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
private DocumentCache<CachedItem> cache; |
95 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
96 |
47 |
@Override... |
97 |
|
public void initialize() throws InitializationException |
98 |
|
{ |
99 |
47 |
if (this.configuration.isEnabled()) { |
100 |
1 |
CacheConfiguration cacheConfiguration = new CacheConfiguration(); |
101 |
1 |
cacheConfiguration.setConfigurationId(NAME); |
102 |
1 |
LRUEvictionConfiguration lru = new LRUEvictionConfiguration(); |
103 |
1 |
lru.setMaxEntries(this.configuration.getSize()); |
104 |
1 |
lru.setLifespan(this.configuration.getDuration()); |
105 |
1 |
cacheConfiguration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru); |
106 |
|
|
107 |
1 |
try { |
108 |
1 |
this.cache.create(cacheConfiguration); |
109 |
|
} catch (CacheException e) { |
110 |
0 |
throw new InitializationException("Failed to initialize core rendering cache", e); |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
117 |
6352 |
@Override... |
118 |
|
public String getRenderedContent(DocumentReference documentReference, String source, XWikiContext context) |
119 |
|
{ |
120 |
6353 |
String renderedContent = null; |
121 |
|
|
122 |
6352 |
if (this.configuration.isCached(documentReference)) { |
123 |
4 |
String refresh = context.getRequest() != null ? context.getRequest().getParameter(PARAMETER_REFRESH) : null; |
124 |
|
|
125 |
4 |
if (!"1".equals(refresh)) { |
126 |
4 |
CachedItem cachedItem = |
127 |
|
this.cache.get(documentReference, source, getAction(context), context.getLanguage(), |
128 |
|
getRequestParameters(context)); |
129 |
4 |
if (cachedItem != null) { |
130 |
2 |
renderedContent = restoreCachedItem(context, cachedItem); |
131 |
|
} |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
6352 |
return renderedContent; |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
138 |
6350 |
@Override... |
139 |
|
public void setRenderedContent(DocumentReference documentReference, String source, String renderedContent, |
140 |
|
XWikiContext context) |
141 |
|
{ |
142 |
6350 |
if (this.configuration.isCached(documentReference)) { |
143 |
1 |
this.cache.set(buildCachedItem(context, renderedContent), documentReference, source, getAction(context), |
144 |
|
context.getLanguage(), getRequestParameters(context)); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
@param |
153 |
|
@return |
154 |
|
|
|
|
| 55.6% |
Uncovered Elements: 8 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
|
155 |
1 |
private CachedItem buildCachedItem(XWikiContext context, String renderedContent)... |
156 |
|
{ |
157 |
1 |
CachedItem cachedItem = new CachedItem(); |
158 |
|
|
159 |
1 |
for (RenderingCacheAware component : this.renderingCacheAwareProvider.get()) { |
160 |
0 |
cachedItem.extensions.put(component, component.getCacheResources(context)); |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
1 |
if (this.legacyRenderingCacheAware == null) { |
165 |
1 |
this.legacyRenderingCacheAware = new LinkedList<RenderingCacheAware>(); |
166 |
1 |
XWikiPluginManager pluginManager = context.getWiki().getPluginManager(); |
167 |
1 |
for (String pluginName : pluginManager.getPlugins()) { |
168 |
0 |
XWikiPluginInterface plugin = pluginManager.getPlugin(pluginName); |
169 |
|
|
170 |
0 |
if (plugin instanceof RenderingCacheAware) { |
171 |
0 |
this.legacyRenderingCacheAware.add((RenderingCacheAware) plugin); |
172 |
|
} |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
1 |
for (RenderingCacheAware component : this.legacyRenderingCacheAware) { |
177 |
0 |
cachedItem.extensions.put(component, component.getCacheResources(context)); |
178 |
|
} |
179 |
|
|
180 |
1 |
cachedItem.rendered = renderedContent; |
181 |
1 |
return cachedItem; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
@param |
188 |
|
@param |
189 |
|
@return |
190 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
191 |
2 |
private String restoreCachedItem(XWikiContext context, CachedItem cachedItem)... |
192 |
|
{ |
193 |
2 |
for (Map.Entry<RenderingCacheAware, UsedExtension> item : cachedItem.extensions.entrySet()) { |
194 |
0 |
item.getKey().restoreCacheResources(context, item.getValue()); |
195 |
|
} |
196 |
|
|
197 |
2 |
return cachedItem.rendered; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@return |
205 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
206 |
5 |
private String getAction(XWikiContext context)... |
207 |
|
{ |
208 |
5 |
return context.getAction() != null ? context.getAction() : "view"; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
217 |
5 |
private String getRequestParameters(XWikiContext context)... |
218 |
|
{ |
219 |
5 |
if (context.getRequest() != null) { |
220 |
5 |
Map<String, String[]> parameters = context.getRequest().getParameterMap(); |
221 |
|
|
222 |
5 |
if (parameters != null) { |
223 |
|
|
224 |
|
|
225 |
5 |
SortedMap<String, String[]> sortedMap = new TreeMap<String, String[]>(parameters); |
226 |
5 |
return constructRequestString(sortedMap); |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
0 |
return ""; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
@param |
237 |
|
@return |
238 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
239 |
5 |
private String constructRequestString(SortedMap<String, String[]> sortedMap)... |
240 |
|
{ |
241 |
5 |
StringBuilder sb = new StringBuilder(); |
242 |
|
|
243 |
|
|
244 |
5 |
for (Map.Entry<String, String[]> entry : sortedMap.entrySet()) { |
245 |
|
|
246 |
1 |
if (!entry.getKey().equals(PARAMETER_REFRESH)) { |
247 |
1 |
for (String value : entry.getValue()) { |
248 |
2 |
if (sb.length() > 0) { |
249 |
1 |
sb.append('&'); |
250 |
|
} |
251 |
2 |
try { |
252 |
2 |
sb.append(URLEncoder.encode(entry.getKey(), UTF8)).append('=') |
253 |
|
.append(URLEncoder.encode(value, UTF8)); |
254 |
|
} catch (UnsupportedEncodingException e) { |
255 |
|
|
256 |
0 |
throw new RuntimeException( |
257 |
|
String.format("Failed to URL encode [[%s]:[%s]] parameter using UTF-8.", |
258 |
|
entry.getKey(), entry.getValue()), e); |
259 |
|
} |
260 |
|
} |
261 |
|
} |
262 |
|
} |
263 |
5 |
return sb.toString(); |
264 |
|
} |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
@Override... |
267 |
|
public void flushCache(DocumentReference documentReference) |
268 |
|
{ |
269 |
0 |
this.cache.removeAll(documentReference); |
270 |
|
} |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
272 |
0 |
@Override... |
273 |
|
public void flushWholeCache() |
274 |
|
{ |
275 |
0 |
this.cache.removeAll(); |
276 |
|
} |
277 |
|
} |