1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.render; |
21 |
|
|
22 |
|
import java.io.Reader; |
23 |
|
import java.io.Writer; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.HashSet; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
import java.util.Properties; |
30 |
|
import java.util.Set; |
31 |
|
|
32 |
|
import javax.inject.Inject; |
33 |
|
import javax.inject.Provider; |
34 |
|
import javax.inject.Singleton; |
35 |
|
import javax.script.ScriptContext; |
36 |
|
|
37 |
|
import org.apache.commons.io.output.NullWriter; |
38 |
|
import org.apache.velocity.VelocityContext; |
39 |
|
import org.apache.velocity.runtime.RuntimeConstants; |
40 |
|
import org.apache.velocity.runtime.RuntimeSingleton; |
41 |
|
import org.slf4j.Logger; |
42 |
|
import org.xwiki.component.annotation.Component; |
43 |
|
import org.xwiki.component.phase.Initializable; |
44 |
|
import org.xwiki.component.phase.InitializationException; |
45 |
|
import org.xwiki.context.Execution; |
46 |
|
import org.xwiki.context.ExecutionContext; |
47 |
|
import org.xwiki.observation.EventListener; |
48 |
|
import org.xwiki.observation.ObservationManager; |
49 |
|
import org.xwiki.observation.event.Event; |
50 |
|
import org.xwiki.script.ScriptContextManager; |
51 |
|
import org.xwiki.security.authorization.AuthorExecutor; |
52 |
|
import org.xwiki.skin.Skin; |
53 |
|
import org.xwiki.skin.SkinManager; |
54 |
|
import org.xwiki.template.Template; |
55 |
|
import org.xwiki.template.TemplateManager; |
56 |
|
import org.xwiki.template.event.TemplateDeletedEvent; |
57 |
|
import org.xwiki.template.event.TemplateEvent; |
58 |
|
import org.xwiki.template.event.TemplateUpdatedEvent; |
59 |
|
import org.xwiki.velocity.VelocityConfiguration; |
60 |
|
import org.xwiki.velocity.VelocityEngine; |
61 |
|
import org.xwiki.velocity.VelocityFactory; |
62 |
|
import org.xwiki.velocity.VelocityManager; |
63 |
|
import org.xwiki.velocity.XWikiVelocityException; |
64 |
|
import org.xwiki.velocity.XWikiWebappResourceLoader; |
65 |
|
import org.xwiki.velocity.internal.VelocityExecutionContextInitializer; |
66 |
|
|
67 |
|
import com.xpn.xwiki.XWikiContext; |
68 |
|
import com.xpn.xwiki.api.DeprecatedContext; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@version |
76 |
|
@since |
77 |
|
|
78 |
|
@Component |
79 |
|
@Singleton |
80 |
|
|
|
|
| 88.3% |
Uncovered Elements: 13 (111) |
Complexity: 26 |
Complexity Density: 0.35 |
|
81 |
|
public class DefaultVelocityManager implements VelocityManager, Initializable |
82 |
|
{ |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
private static final String RESOURCE_LOADER = "resource.loader"; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
private static final String RESOURCE_LOADER_CLASS = "xwiki.resource.loader.class"; |
94 |
|
|
95 |
|
private static final String VELOCITYENGINE_CACHEKEY_NAME = "velocity.engine.key"; |
96 |
|
|
97 |
|
private static final List<Event> EVENTS = |
98 |
|
Arrays.<Event>asList(new TemplateUpdatedEvent(), new TemplateDeletedEvent()); |
99 |
|
|
100 |
|
|
101 |
|
@link |
102 |
|
|
103 |
|
@Inject |
104 |
|
private Execution execution; |
105 |
|
|
106 |
|
|
107 |
|
@link |
108 |
|
|
109 |
|
@Inject |
110 |
|
private Provider<XWikiContext> xcontextProvider; |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
@Inject |
116 |
|
private ScriptContextManager scriptContextManager; |
117 |
|
|
118 |
|
@Inject |
119 |
|
private VelocityFactory velocityFactory; |
120 |
|
|
121 |
|
@Inject |
122 |
|
private VelocityConfiguration velocityConfiguration; |
123 |
|
|
124 |
|
|
125 |
|
@link@link@link |
126 |
|
|
127 |
|
@Inject |
128 |
|
private Provider<TemplateManager> templates; |
129 |
|
|
130 |
|
@Inject |
131 |
|
private SkinManager skinManager; |
132 |
|
|
133 |
|
@Inject |
134 |
|
private ObservationManager observation; |
135 |
|
|
136 |
|
@Inject |
137 |
|
private AuthorExecutor authorExecutor; |
138 |
|
|
139 |
|
@Inject |
140 |
|
private Logger logger; |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
private final Set<String> reservedBindings = new HashSet<>(); |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
147 |
84 |
@Override... |
148 |
|
public void initialize() throws InitializationException |
149 |
|
{ |
150 |
84 |
this.observation.addListener(new EventListener() |
151 |
|
{ |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
152 |
0 |
@Override... |
153 |
|
public void onEvent(Event event, Object source, Object data) |
154 |
|
{ |
155 |
0 |
if (event instanceof TemplateEvent) { |
156 |
0 |
TemplateEvent templateEvent = (TemplateEvent) event; |
157 |
|
|
158 |
0 |
DefaultVelocityManager.this.velocityFactory.removeVelocityEngine(templateEvent.getId()); |
159 |
|
} |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
162 |
332 |
@Override... |
163 |
|
public String getName() |
164 |
|
{ |
165 |
332 |
return DefaultVelocityManager.class.getName(); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
83 |
@Override... |
169 |
|
public List<Event> getEvents() |
170 |
|
{ |
171 |
83 |
return EVENTS; |
172 |
|
} |
173 |
|
}); |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
84 |
this.reservedBindings.add("context"); |
179 |
|
|
180 |
|
|
181 |
84 |
this.reservedBindings.add("macro"); |
182 |
|
|
183 |
84 |
this.reservedBindings.add("foreach"); |
184 |
84 |
this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.COUNTER_NAME, |
185 |
|
RuntimeSingleton.getString(RuntimeConstants.COUNTER_NAME))); |
186 |
84 |
this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.HAS_NEXT_NAME, |
187 |
|
RuntimeSingleton.getString(RuntimeConstants.HAS_NEXT_NAME))); |
188 |
|
|
189 |
84 |
this.reservedBindings.add("evaluate"); |
190 |
|
|
191 |
84 |
this.reservedBindings.add("exception"); |
192 |
84 |
this.reservedBindings.add("try"); |
193 |
|
|
194 |
84 |
this.reservedBindings.add("define"); |
195 |
|
|
196 |
84 |
this.reservedBindings.add("template"); |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
199 |
109396 |
@Override... |
200 |
|
public VelocityContext getVelocityContext() |
201 |
|
{ |
202 |
109383 |
ScriptVelocityContext velocityContext; |
203 |
|
|
204 |
|
|
205 |
109380 |
VelocityContext currentVelocityContext = getCurrentVelocityContext(); |
206 |
109377 |
if (currentVelocityContext instanceof ScriptVelocityContext) { |
207 |
97964 |
velocityContext = (ScriptVelocityContext) currentVelocityContext; |
208 |
|
} else { |
209 |
11413 |
velocityContext = new ScriptVelocityContext(currentVelocityContext, this.reservedBindings); |
210 |
11404 |
this.execution.getContext().setProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID, |
211 |
|
velocityContext); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
109376 |
ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); |
216 |
109365 |
velocityContext.setScriptContext(scriptContext); |
217 |
|
|
218 |
|
|
219 |
109371 |
XWikiContext xcontext = this.xcontextProvider.get(); |
220 |
|
|
221 |
109383 |
velocityContext.put("context", new DeprecatedContext(xcontext)); |
222 |
|
|
223 |
109362 |
return velocityContext; |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
117964 |
@Override... |
227 |
|
public VelocityContext getCurrentVelocityContext() |
228 |
|
{ |
229 |
|
|
230 |
|
|
231 |
117971 |
return (VelocityContext) this.execution.getContext() |
232 |
|
.getProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID); |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
|
@return |
237 |
|
|
238 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
239 |
161249 |
private Template getVelocityEngineMacrosTemplate()... |
240 |
|
{ |
241 |
161253 |
Template template = null; |
242 |
161254 |
Map<String, Template> templateCache = null; |
243 |
|
|
244 |
161250 |
Skin currentSkin = this.skinManager.getCurrentSkin(true); |
245 |
|
|
246 |
|
|
247 |
161277 |
ExecutionContext econtext = this.execution.getContext(); |
248 |
161276 |
if (econtext != null) { |
249 |
161278 |
templateCache = (Map<String, Template>) econtext.getProperty(VELOCITYENGINE_CACHEKEY_NAME); |
250 |
161278 |
if (templateCache == null) { |
251 |
11017 |
templateCache = new HashMap<>(); |
252 |
11018 |
econtext.setProperty(VELOCITYENGINE_CACHEKEY_NAME, templateCache); |
253 |
|
} else { |
254 |
150255 |
template = templateCache.get(currentSkin.getId()); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
161278 |
if (template == null) { |
259 |
11089 |
template = this.templates.get().getTemplate("macros.vm"); |
260 |
|
|
261 |
11085 |
if (templateCache != null) { |
262 |
11075 |
templateCache.put(currentSkin.getId(), template); |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
161267 |
return template; |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
|
@return |
271 |
|
|
272 |
|
|
273 |
|
@throws |
274 |
|
|
|
|
| 88.9% |
Uncovered Elements: 5 (45) |
Complexity: 12 |
Complexity Density: 0.41 |
|
275 |
161262 |
@Override... |
276 |
|
public VelocityEngine getVelocityEngine() throws XWikiVelocityException |
277 |
|
{ |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
161259 |
XWikiContext xcontext = this.xcontextProvider.get(); |
288 |
|
|
289 |
161273 |
final Template template; |
290 |
161271 |
if (xcontext != null && xcontext.getWiki() != null) { |
291 |
161262 |
template = getVelocityEngineMacrosTemplate(); |
292 |
|
} else { |
293 |
0 |
template = null; |
294 |
|
} |
295 |
|
|
296 |
161259 |
String cacheKey = template != null ? template.getId() : "default"; |
297 |
|
|
298 |
|
|
299 |
161257 |
VelocityEngine velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey); |
300 |
161260 |
if (velocityEngine == null) { |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
51 |
synchronized (this) { |
307 |
51 |
velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey); |
308 |
51 |
if (velocityEngine == null) { |
309 |
|
|
310 |
48 |
Properties properties = new Properties(); |
311 |
|
|
312 |
|
|
313 |
|
|
314 |
48 |
if (!this.velocityConfiguration.getProperties().containsKey(RESOURCE_LOADER)) { |
315 |
48 |
properties.setProperty(RESOURCE_LOADER, "xwiki"); |
316 |
48 |
properties.setProperty(RESOURCE_LOADER_CLASS, XWikiWebappResourceLoader.class.getName()); |
317 |
|
} |
318 |
|
|
319 |
48 |
if (xcontext != null && xcontext.getWiki() != null) { |
320 |
|
|
321 |
|
|
322 |
48 |
String macroList = xcontext.getWiki().Param("xwiki.render.velocity.macrolist"); |
323 |
48 |
if (macroList == null) { |
324 |
32 |
macroList = "/templates/macros.vm"; |
325 |
|
} |
326 |
48 |
properties.put(RuntimeConstants.VM_LIBRARY, macroList); |
327 |
|
} |
328 |
48 |
velocityEngine = this.velocityFactory.createVelocityEngine(cacheKey, properties); |
329 |
|
|
330 |
48 |
if (template != null) { |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
32 |
try { |
335 |
32 |
final VelocityEngine finalVelocityEngine = velocityEngine; |
336 |
|
|
337 |
32 |
this.authorExecutor.call(() -> { |
338 |
32 |
finalVelocityEngine.evaluate(new VelocityContext(), NullWriter.NULL_WRITER, "", |
339 |
|
template.getContent().getContent()); |
340 |
|
|
341 |
32 |
return null; |
342 |
|
}, template.getContent().getAuthorReference()); |
343 |
|
} catch (Exception e) { |
344 |
0 |
this.logger.error("Failed to evaluate macros templates [{}]", template.getPath(), e); |
345 |
|
} |
346 |
|
} |
347 |
|
} |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
161266 |
return velocityEngine; |
352 |
|
} |
353 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
354 |
88417 |
@Override... |
355 |
|
public boolean evaluate(Writer out, String templateName, Reader source) throws XWikiVelocityException |
356 |
|
{ |
357 |
|
|
358 |
88414 |
VelocityContext velocityContext = getVelocityContext(); |
359 |
|
|
360 |
|
|
361 |
88412 |
return getVelocityEngine().evaluate(velocityContext, out, templateName, source); |
362 |
|
} |
363 |
|
} |