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

File XWikiContext.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

80
196
88
1
1,022
619
132
0.67
2.23
88
1.5

Classes

Class Line # Actions
XWikiContext 71 196 0% 132 78
0.7857142778.6%
 

Contributing tests

This file is covered by 471 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;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.net.URL;
24    import java.util.ArrayList;
25    import java.util.Collections;
26    import java.util.Hashtable;
27    import java.util.List;
28    import java.util.Locale;
29    import java.util.Map;
30   
31    import javax.inject.Provider;
32   
33    import org.apache.commons.collections4.map.LRUMap;
34    import org.apache.commons.lang3.StringUtils;
35    import org.slf4j.Logger;
36    import org.slf4j.LoggerFactory;
37    import org.xwiki.component.util.DefaultParameterizedType;
38    import org.xwiki.context.Execution;
39    import org.xwiki.context.ExecutionContext;
40    import org.xwiki.localization.LocaleUtils;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.DocumentReferenceResolver;
43    import org.xwiki.model.reference.EntityReferenceSerializer;
44    import org.xwiki.model.reference.SpaceReference;
45    import org.xwiki.model.reference.WikiReference;
46    import org.xwiki.velocity.VelocityManager;
47    import org.xwiki.velocity.internal.VelocityExecutionContextInitializer;
48   
49    import com.xpn.xwiki.doc.XWikiDocument;
50    import com.xpn.xwiki.objects.classes.BaseClass;
51    import com.xpn.xwiki.user.api.XWikiRightService;
52    import com.xpn.xwiki.user.api.XWikiUser;
53    import com.xpn.xwiki.util.Util;
54    import com.xpn.xwiki.validation.XWikiValidationStatus;
55    import com.xpn.xwiki.web.Utils;
56    import com.xpn.xwiki.web.XWikiEngineContext;
57    import com.xpn.xwiki.web.XWikiForm;
58    import com.xpn.xwiki.web.XWikiMessageTool;
59    import com.xpn.xwiki.web.XWikiRequest;
60    import com.xpn.xwiki.web.XWikiResponse;
61    import com.xpn.xwiki.web.XWikiURLFactory;
62   
63    /**
64    * Represents the execution environment for all the wiki pages. An instance of the <code>Context</code> class is
65    * available as a predefined variable for scripting inside any wiki page. You can access it using <code>$xcontext</code>
66    * in Velocity scripts or simply <code>xcontext</code> in Groovy ones. The <code>Context</code> class provides a means
67    * of getting contextual information about the current request or configuring XWiki on the fly.
68    *
69    * @version $Id: a8bb4f7318670118ece81e791f3321522e2fc3e9 $
70    */
 
71    public class XWikiContext extends Hashtable<Object, Object>
72    {
73    /**
74    * Type instance for {@code Provider<XWikiContext>}.
75    *
76    * @since 5.0M1
77    */
78    public static final ParameterizedType TYPE_PROVIDER =
79    new DefaultParameterizedType(null, Provider.class, XWikiContext.class);
80   
81    public static final int MODE_SERVLET = 0;
82   
83    public static final int MODE_PORTLET = 1;
84   
85    public static final int MODE_XMLRPC = 2;
86   
87    public static final int MODE_ATOM = 3;
88   
89    public static final int MODE_PDF = 4;
90   
91    public static final int MODE_GWT = 5;
92   
93    public static final int MODE_GWT_DEBUG = 6;
94   
95    public static final String EXECUTIONCONTEXT_KEY = "xwikicontext";
96   
97    /**
98    * @deprecated use {@link VelocityManager#getVelocityContext()} instead
99    */
100    public static final String KEY_LEGACY_VELOCITYCONTEXT = "vcontext";
101   
102    /** Logging helper object. */
103    protected static final Logger LOGGER = LoggerFactory.getLogger(XWikiContext.class);
104   
105    private static final String WIKI_KEY = "wiki";
106   
107    private static final String ORIGINAL_WIKI_KEY = "originalWiki";
108   
109    private static final String USER_KEY = "user";
110   
111    private static final String USERREFERENCE_KEY = "userreference";
112   
113    /**
114    * Used to resolve a string into a proper Document Reference using the current document's reference to fill the
115    * blanks, except for the page name for which the default page name is used instead and for the wiki name for which
116    * the current wiki is used instead of the current document reference's wiki.
117    */
118    private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver;
119   
120    /**
121    * Used to convert a proper Document Reference to a string but without the wiki name.
122    */
123    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
124   
125    /**
126    * Used to convert a Document Reference to string (compact form without the wiki part if it matches the current
127    * wiki).
128    */
129    private EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer;
130   
131    /** The Execution so that we can check if permissions were dropped there. */
132    private Execution execution;
133   
134    private boolean finished = false;
135   
136    private XWiki wiki;
137   
138    private XWikiEngineContext engine_context;
139   
140    private XWikiRequest request;
141   
142    private XWikiResponse response;
143   
144    private XWikiForm form;
145   
146    private String action;
147   
148    private String orig_wikiId;
149   
150    private WikiReference wikiReference;
151   
152    private DocumentReference userReference;
153   
154    private Locale locale;
155   
156    private static final String LANGUAGE_KEY = "language";
157   
158    private Locale interfaceLocale;
159   
160    private int mode;
161   
162    private URL url;
163   
164    private XWikiURLFactory URLFactory;
165   
166    private int cacheDuration = 0;
167   
168    private int classCacheSize = 20;
169   
170    // Used to avoid recursive loading of documents if there are recursives usage of classes
171    // FIXME: why synchronized since a context is supposed to be tied to a thread ?
172    @SuppressWarnings("unchecked")
173    private Map<DocumentReference, BaseClass> classCache = Collections.synchronizedMap(new LRUMap(this.classCacheSize));
174   
175    // FIXME: why synchronized since a context is supposed to be tied to a thread ?
176    private List<String> displayedFields = Collections.synchronizedList(new ArrayList<String>());
177   
 
178  12078 toggle public XWikiContext()
179    {
180    }
181   
 
182  1655 toggle private DocumentReferenceResolver<String> getCurrentMixedDocumentReferenceResolver()
183    {
184  1657 if (this.currentMixedDocumentReferenceResolver == null) {
185  1037 this.currentMixedDocumentReferenceResolver =
186    Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
187    }
188   
189  1676 return this.currentMixedDocumentReferenceResolver;
190    }
191   
 
192  2 toggle private EntityReferenceSerializer<String> getLocalEntityReferenceSerializer()
193    {
194  2 if (this.localEntityReferenceSerializer == null) {
195  1 this.localEntityReferenceSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
196    }
197   
198  2 return this.localEntityReferenceSerializer;
199    }
200   
 
201  111078 toggle private EntityReferenceSerializer<String> getCompactWikiEntityReferenceSerializer()
202    {
203  111087 if (this.compactWikiEntityReferenceSerializer == null) {
204  10004 this.compactWikiEntityReferenceSerializer =
205    Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
206    }
207   
208  111109 return this.compactWikiEntityReferenceSerializer;
209    }
210   
 
211  9619 toggle private Execution getExecution()
212    {
213  9617 if (this.execution == null) {
214  6940 this.execution = Utils.getComponent(Execution.class);
215    }
216   
217  9619 return this.execution;
218    }
219   
 
220  2 toggle private ExecutionContext getExecutionContext()
221    {
222  2 if (getExecution() != null) {
223  2 return getExecution().getContext();
224    }
225   
226  0 return null;
227    }
228   
 
229  2317007 toggle public XWiki getWiki()
230    {
231  2317006 return this.wiki;
232    }
233   
 
234  1083 toggle public Util getUtil()
235    {
236  1083 Util util = (Util) this.get("util");
237  1083 if (util == null) {
238  144 util = new Util();
239  144 this.put("util", util);
240    }
241   
242  1083 return util;
243    }
244   
 
245  12107 toggle public void setWiki(XWiki wiki)
246    {
247  12107 this.wiki = wiki;
248    }
249   
 
250  12588 toggle public XWikiEngineContext getEngineContext()
251    {
252  12607 return this.engine_context;
253    }
254   
 
255  11470 toggle public void setEngineContext(XWikiEngineContext engine_context)
256    {
257  11472 this.engine_context = engine_context;
258    }
259   
 
260  480239 toggle public XWikiRequest getRequest()
261    {
262  480256 return this.request;
263    }
264   
 
265  18619 toggle public void setRequest(XWikiRequest request)
266    {
267  18645 this.request = request;
268    }
269   
 
270  106733 toggle public String getAction()
271    {
272  106722 return this.action;
273    }
274   
 
275  11412 toggle public void setAction(String action)
276    {
277  11441 this.action = action;
278    }
279   
 
280  267003 toggle public XWikiResponse getResponse()
281    {
282  267004 return this.response;
283    }
284   
 
285  18869 toggle public void setResponse(XWikiResponse response)
286    {
287  18887 this.response = response;
288    }
289   
290    /**
291    * @deprecated since 6.1M1, use {@link #getWikiId()} instead
292    */
 
293  0 toggle @Deprecated
294    public String getDatabase()
295    {
296  0 return getWikiId();
297    }
298   
299    /**
300    * @return the id of the current wiki, or null if none is set
301    * @since 6.1M1
302    */
 
303  5432033 toggle public String getWikiId()
304    {
305  5432023 return this.wikiReference != null ? this.wikiReference.getName() : null;
306    }
307   
308    /**
309    * @return the reference of the current wiki or null if none is set
310    * @since 7.2M1
311    */
 
312  6928062 toggle public WikiReference getWikiReference()
313    {
314  6928039 return this.wikiReference;
315    }
316   
317    /**
318    * @param wikiId the current wiki id
319    * @deprecated since 6.1M1, use {@link #setWikiId(String)} instead
320    */
 
321  0 toggle @Deprecated
322    public void setDatabase(String wikiId)
323    {
324