1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.render

File XWikiScriptContextInitializer.java

 

Coverage histogram

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

Code metrics

14
35
2
1
136
80
11
0.31
17.5
2
5.5

Classes

Class Line # Actions
XWikiScriptContextInitializer 50 35 0% 11 4
0.9215686392.2%
 

Contributing tests

This file is covered by 23 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   
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: 9cd278733acfd8ac2c60b826de0e2cb27bd1aef8 $
46    */
47    @Component
48    @Named("xwiki")
49    @Singleton
 
50    public class XWikiScriptContextInitializer implements ScriptContextInitializer
51    {
52    @Inject
53    private Logger logger;
54   
55   
56    @Inject
57    private Provider<XWikiContext> xcontextProvider;
58   
 
59  1827737 toggle @Override
60    public void initialize(ScriptContext scriptContext)
61    {
62  1828557 XWikiContext xcontext = this.xcontextProvider.get();
63   
64  1828984 if (scriptContext.getAttribute("util") == null) {
65    // Put the Util API in the Script context.
66  168764 scriptContext.setAttribute("util", new com.xpn.xwiki.api.Util(xcontext.getWiki(), xcontext),
67    ScriptContext.ENGINE_SCOPE);
68   
69    // We put the com.xpn.xwiki.api.XWiki object into the context and not the com.xpn.xwiki.XWiki one which is
70    // for internal use only. In this manner we control what the user can access.
71  168684 scriptContext.setAttribute("xwiki", new XWiki(xcontext.getWiki(), xcontext), ScriptContext.ENGINE_SCOPE);
72   
73  168951 scriptContext.setAttribute("request", xcontext.getRequest(), ScriptContext.ENGINE_SCOPE);
74  168706 scriptContext.setAttribute("response", xcontext.getResponse(), ScriptContext.ENGINE_SCOPE);
75   
76    // We put the com.xpn.xwiki.api.Context object into the context and not the com.xpn.xwiki.XWikiContext one
77    // which is for internal use only. In this manner we control what the user can access.
78    // We use "xcontext" because "context" is a reserved binding in JSR-223 specifications
79  168744 scriptContext.setAttribute("xcontext", new Context(xcontext), ScriptContext.ENGINE_SCOPE);
80    }
81   
82    // Current document
83  1828716 Document docAPI = null;
84  1829241 XWikiDocument doc = xcontext.getDoc();
85  1829394 if (doc != null) {
86  1784023 docAPI = setDocument(scriptContext, "doc", doc, xcontext);
87   
88  1783718 XWikiDocument tdoc = (XWikiDocument) xcontext.get("tdoc");
89  1783615 if (tdoc == null) {
90  224253 try {
91  224628 tdoc = doc.getTranslatedDocument(xcontext);
92    } catch (XWikiException e) {
93  0 this.logger.warn("Failed to retrieve the translated document for [{}]. "
94    + "Continue using the default translation.", doc.getDocumentReference(), e);
95  0 tdoc = doc;
96    }
97    }
98  1784021 Document tdocAPI = setDocument(scriptContext, "tdoc", tdoc, xcontext);
99   
100  1784293 XWikiDocument cdoc = (XWikiDocument) xcontext.get("cdoc");
101  1784269 if (cdoc == null) {
102  224883 Document cdocAPI = tdocAPI;
103  224876 if (cdocAPI == null) {
104  0 cdocAPI = docAPI;
105    }
106  224885 scriptContext.setAttribute("cdoc", cdocAPI, ScriptContext.ENGINE_SCOPE);
107    } else {
108  1559376 setDocument(scriptContext, "cdoc", cdoc, xcontext);
109    }
110    }
111   
112    // Current secure document
113  1829555 XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
114  1829565 if (sdoc == null) {
115  140331 scriptContext.setAttribute("sdoc", docAPI, ScriptContext.ENGINE_SCOPE);
116    } else {
117  1689226 setDocument(scriptContext, "sdoc", sdoc, xcontext);
118    }
119   
120    // Miscellaneous
121  1829567 scriptContext.setAttribute("locale", xcontext.getLocale(), ScriptContext.ENGINE_SCOPE);
122    }
123   
 
124  6814990 toggle private Document setDocument(ScriptContext scriptContext, String key, XWikiDocument document, XWikiContext xcontext)
125    {
126    // Change the Document instance only if it's not already wrapping the same XWikiDocument (otherwise we might
127    // loose modifications made in a previous script and not yet saved)
128  6815658 Document apiDocument = (Document) scriptContext.getAttribute(key);
129  6816296 if (apiDocument == null || !apiDocument.same(document)) {
130  1538822 apiDocument = document.newDocument(xcontext);
131  1539162 scriptContext.setAttribute(key, apiDocument, ScriptContext.ENGINE_SCOPE);
132    }
133   
134  6816896 return apiDocument;
135    }
136    }