1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.render

File XWikiScriptContextInitializer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
32
2
1
137
78
11
0.34
16
2
5.5

Classes

Class Line # Actions
XWikiScriptContextInitializer 50 32 0% 11 4
0.916666791.7%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.render;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26    import javax.script.ScriptContext;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.rendering.syntax.SyntaxFactory;
31    import org.xwiki.script.ScriptContextInitializer;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.XWikiException;
35    import com.xpn.xwiki.api.Context;
36    import com.xpn.xwiki.api.Document;
37    import com.xpn.xwiki.api.XWiki;
38    import com.xpn.xwiki.doc.XWikiDocument;
39   
40    /**
41    * Inject in the {@link ScriptContext} the XWiki context and the {@link XWiki} instance for backward compatibility.
42    * <p>
43    * Bridge. To be removed later.
44    *
45    * @version $Id: 266062f625e30fe2446a1c646a9a4ff1f44c6b31 $
46    */
47    @Component
48    @Named("xwiki")
49    @Singleton
 
50    public class XWikiScriptContextInitializer implements ScriptContextInitializer
51    {
52    @Inject
53    private Logger logger;
54   
55    @Inject
56    private SyntaxFactory syntaxFactory;
57   
58    @Inject
59    private Provider<XWikiContext> xcontextProvider;
60   
 
61  109615 toggle @Override
62    public void initialize(ScriptContext scriptContext)
63    {
64  109600 XWikiContext xcontext = this.xcontextProvider.get();
65   
66  109597 if (scriptContext.getAttribute("util") == null) {
67    // Put the Util API in the Script context.
68  15674 scriptContext.setAttribute("util", new com.xpn.xwiki.api.Util(xcontext.getWiki(), xcontext),
69    ScriptContext.ENGINE_SCOPE);
70   
71    // We put the com.xpn.xwiki.api.XWiki object into the context and not the com.xpn.xwiki.XWiki one which is
72    // for internal use only. In this manner we control what the user can access.
73  15648 scriptContext.setAttribute("xwiki", new XWiki(xcontext.getWiki(), xcontext), ScriptContext.ENGINE_SCOPE);
74   
75  15660 scriptContext.setAttribute("request", xcontext.getRequest(), ScriptContext.ENGINE_SCOPE);
76  15663 scriptContext.setAttribute("response", xcontext.getResponse(), ScriptContext.ENGINE_SCOPE);
77   
78    // We put the com.xpn.xwiki.api.Context object into the context and not the com.xpn.xwiki.XWikiContext one
79    // which is for internal use only. In this manner we control what the user can access.
80    // We use "xcontext" because "context" is a reserved binding in JSR-223 specifications
81  15662 scriptContext.setAttribute("xcontext", new Context(xcontext), ScriptContext.ENGINE_SCOPE);
82   
83    // Make the Syntax Factory component available from Script.
84    // TODO: We need to decide how we want to expose components in general and how to protect users from
85    // "dangerous" apis.
86    // TODO: Actually this should probably be moved to legacy, we have a rendering ScriptService now
87  15658 scriptContext.setAttribute("syntaxFactory", this.syntaxFactory, ScriptContext.ENGINE_SCOPE);
88    }
89   
90    // Current document
91  109569 XWikiDocument doc = xcontext.getDoc();
92  109584 if (doc != null) {
93  100081 setDocument(scriptContext, "doc", doc, xcontext);
94   
95  100080 XWikiDocument tdoc = (XWikiDocument) xcontext.get("tdoc");
96  100088 if (tdoc == null) {
97  9436 try {
98  9437 tdoc = doc.getTranslatedDocument(xcontext);
99    } catch (XWikiException e) {
100  0 this.logger.warn("Failed to retrieve the translated document for [{}]. "
101    + "Continue using the default translation.", doc.getDocumentReference(), e);
102  0 tdoc = doc;
103    }
104    }
105  100078 setDocument(scriptContext, "tdoc", tdoc, xcontext);
106   
107  100104 XWikiDocument cdoc = (XWikiDocument) xcontext.get("cdoc");
108  100098 if (cdoc == null) {
109  9435 cdoc = tdoc;
110  9438 if (cdoc == null) {
111  0 cdoc = doc;
112    }
113    }
114  100088 setDocument(scriptContext, "cdoc", cdoc, xcontext);
115    }
116   
117    // Current secure document
118  109599 XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
119  109586 if (sdoc != null) {
120  94176 setDocument(scriptContext, "sdoc", sdoc, xcontext);
121    }
122   
123    // Miscellaneous
124  109574 scriptContext.setAttribute("locale", xcontext.getLocale(), ScriptContext.ENGINE_SCOPE);
125    }
126   
 
127  394422 toggle private void setDocument(ScriptContext scriptContext, String key, XWikiDocument document, XWikiContext xcontext)
128    {
129    // Change the Document instance only if it's not already wrapping the same XWikiDocument (otherwise we might
130    // loose modifications made in a previous script and not yet saved)
131  394421 Document previousDoc = (Document) scriptContext.getAttribute(key);
132  394464 if (previousDoc == null || !previousDoc.same(document)) {
133  110358 Document apiDocument = document.newDocument(xcontext);
134  110340 scriptContext.setAttribute(key, apiDocument, ScriptContext.ENGINE_SCOPE);
135    }
136    }
137    }