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

File Context.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
76% of files have more coverage

Code metrics

14
51
40
1
633
206
47
0.92
1.27
40
1.17

Classes

Class Line # Actions
Context 41 51 0% 47 53
0.495238149.5%
 

Contributing tests

This file is covered by 24 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.api;
21   
22    import java.util.List;
23    import java.util.Locale;
24   
25    import org.xwiki.model.reference.DocumentReference;
26   
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.doc.XWikiDocument;
29    import com.xpn.xwiki.util.Programming;
30    import com.xpn.xwiki.validation.XWikiValidationStatus;
31    import com.xpn.xwiki.web.XWikiRequest;
32    import com.xpn.xwiki.web.XWikiResponse;
33    import com.xpn.xwiki.web.XWikiURLFactory;
34   
35    /**
36    * Provides a secure proxy for the internal {@code XWikiContext} class, that can be used safely in scripts. All
37    * dangerous methods are protected by requiring Programming Rights.
38    *
39    * @version $Id: 39efe94d382e64476438e080add261afe4307303 $
40    */
 
41    public class Context extends Api
42    {
43    /**
44    * The Constructor.
45    *
46    * @param context The {@link com.xpn.xwiki.XWikiContext} to wrap.
47    */
 
48  1997037 toggle public Context(XWikiContext context)
49    {
50  1996587 super(context);
51    }
52   
53    /**
54    * Returns the current request object. If the request has been made to a servlet container using the HTTP protocol
55    * then the returned object wraps a <code>HttpServletRequest</code> instance.
56    *
57    * @return an object wrapping the current request object
58    */
 
59  0 toggle public XWikiRequest getRequest()
60    {
61  0 return getXWikiContext().getRequest();
62    }
63   
64    /**
65    * Returns the current response object. If the request has been made to a servlet container using the HTTP protocol
66    * then the returned object wraps a <code>HttpServletResponse</code> instance.
67    *
68    * @return an object wrapping the current response object
69    */
 
70  0 toggle public XWikiResponse getResponse()
71    {
72  0 return getXWikiContext().getResponse();
73    }
74   
75    /**
76    * Specifies the container or environment in which XWiki is currently running. See the following table for possible
77    * values it can return:
78    * <table summary="Return values">
79    * <thead>
80    * <tr>
81    * <th>Return</th>
82    * <th>Meaning</th>
83    * </tr>
84    * </thead> <tbody>
85    * <tr>
86    * <td>0</td>
87    * <td>Servlet Container</td>
88    * </tr>
89    * <tr>
90    * <td>1</td>
91    * <td>Portlet Container</td>
92    * </tr>
93    * <tr>
94    * <td>2</td>
95    * <td>XML RPC</td>
96    * </tr>
97    * <tr>
98    * <td>3</td>
99    * <td>Atom</td>
100    * </tr>
101    * <tr>
102    * <td>4</td>
103    * <td>PDF</td>
104    * </tr>
105    * <tr>
106    * <td>5</td>
107    * <td>GWT</td>
108    * </tr>
109    * <tr>
110    * <td>6</td>
111    * <td>GWT Debugging</td>
112    * </tr>
113    * </tbody>
114    * </table>
115    *
116    * @return an integer constant identifying the container or environment in which XWiki is currently running
117    */
 
118  50232 toggle public int getMode()
119    {
120  50231 return getXWikiContext().getMode();
121    }
122   
123    /**
124    * Returns the current database name. If {@link #isMainWiki()} returns <code>true</code> then the current database
125    * name is the same as {@link #getMainWikiName()}. Otherwise, each virtual wiki has it's own database. In this case
126    * the returned string identifies the current virtual wiki we operate on and prefixes document names like in
127    * <i>databaseName:spaceName.pageName</i>.
128    *
129    * @return The current database name.
130    * @see #isMainWiki()
131    * @see #getMainWikiName()
132    */
 
133  90192 toggle public String getDatabase()
134    {
135  90193 return getXWikiContext().getWikiId();
136    }
137   
138    /**
139    * Returns the name of the main wiki. In non-virtual mode there is only one wiki called <i>xwiki</i>. In virtual
140    * mode, the main wiki stores information about all the other virtual wikis.
141    *
142    * @return The name of the main wiki.
143    */
 
144  25688 toggle public String getMainWikiName()
145    {
146  25688 return getXWikiContext().getMainXWiki();
147    }
148   
149    /**
150    * Returns the name of the original database. Here, original means the database corresponding to the requested URL,
151    * which can be changed when including a document from a different database, by using, for example,
152    * <code>#includeTopic("virtualwiki:Some.Document")</code>.
153    *
154    * @return The original database name
155    */
 
156  0 toggle public String getOriginalDatabase()
157    {
158  0 return getXWikiContext().getOriginalWikiId();
159    }
160   
161    /**
162    * Sets the current database. You need programming rights to be able to call this method.
163    *
164    * @param database a database name
165    * @see #getDatabase()
166    */
 
167  0 toggle @Programming
168    public void setDatabase(String database)
169    {
170  0 if (hasProgrammingRights()) {
171  0 getXWikiContext().setWikiId(database);
172    }
173    }
174   
175    /**
176    * Returns the URL factory matching both the protocol used to make the current request and the container or
177    * environment in which XWiki is running. The most used concrete implementation of the <code>XWikiURLFactory</code>
178    * is <code>XWikiServletURLFactory</code>. <code>XWikiURLFactory</code> offers a generic way of creating XWiki
179    * specific URLs that should be chosen instead of the basic string concatenation. Hard-coding the protocol used,
180    * like HTTP, inside wiki pages should be avoided.
181    *
182    * @return The URL factory.
183    */
 
184  0 toggle public XWikiURLFactory getURLFactory()
185    {
186  0 return getXWikiContext().getURLFactory();
187    }
188   
189    /**
190    * <p>
191    * Specifies if the current request was made to a virtual (non-main) wiki, or to the main wiki.
192    * </p>
193    * <p>
194    * In virtual mode the server can host more than one wiki, each having it's own database and its own URL. We refer
195    * to them as <i>virtual wikis</i>. One of them stores information about the others and it is called <i>the main
196    * wiki</i>. You can switch to virtual mode by changing the <code>xwiki.virtual</code> parameter from <code>0</code>
197    * to <code>1</code> in the configuration file.
198    * </p>
199    *
200    * @return <code>true</code> if XWiki is in the main wiki, or if virtual mode is disabled.
201    * @see #getDatabase()
202    * @see #getMainWikiName()
203    */
 
204  36553 toggle public boolean isMainWiki()
205    {
206  36553 return getXWikiContext().isMainWiki();
207    }
208   
209    /**
210    * Returns the current action. XWiki is built on top of the Struts framework, so here "action" means a StrutsAction.
211    * Some predefined actions are: <i>view</i>, <i>edit</i>, <i>delete</i>, <i>download</i> and <i>export</i>. Custom
212    * actions can be defined. The action and it's target are extracted from the request URL. For instance, in
213    * http://platform.xwiki.org/xwiki/bin/view/DevGuide/API the action is <i>view</i> and it's target is the
214    * <i>DevGuide.API</i> document.
215    *
216    * @return The action corresponding to the request URL.
217    */
 
218  310700 toggle public String getAction()
219    {
220  310702 return getXWikiContext().getAction();
221    }
222   
223    /**
224    * Same as {@link #getLocale()} but as String.
225    *
226    * @return The locale of the current request.
227    * @see #getInterfaceLanguage()
228    * @deprecated since 6.0M1, use {@link #getLocale()} instead
229    */
 
230  2 toggle @Deprecated
231    public String getLanguage()
232    {
233  2 return getXWikiContext().getLanguage();
234    }
235   
236    /**
237    * Returns the locale of the current request. If <code>multilingual</code> is turned off then the locale used is
238    * given by the <code>default_language</code> preference. Otherwise, the locale is taken from either the request
239    * object, the cookie, user preferences or from the navigator locale settings, the last having the lower priority.
240    *
241    * @return The locale of the current request.
242    * @see #getInterfaceLocale()
243    * @since 6.0M1
244    */
 
245  100332 toggle public Locale getLocale()
246    {
247  100332 return getXWikiContext().getLocale();
248    }
249   
250    /**
251    * Same as {@link #getInterfaceLocale()} but as String.
252    *
253    * @return The interface locale preference of the current user.
254    * @see #getLanguage()
255    * @deprecated since 6.0M1, use {@link #getInterfaceLocale()} instead
256    */
 
257  0 toggle @Deprecated
258    public String getInterfaceLanguage()
259    {
260  0 return getXWikiContext().getInterfaceLanguage();
261    }
262   
263    /**
264    * Returns the interface locale preference of the current user. If <code>multilingual</code> is turned off then the
265    * locale used is given by the <code>default_language</code> preference. Otherwise, the locale is taken from either
266    * the request object, the context, the cookie, user preferences or from the navigator locale settings, the last
267    * having the lower priority.
268    *
269    * @return The interface locale preference of the current user.
270    * @see #getLocale()
271    * @since 6.0M1
272    */
 
273  0 toggle public Locale getInterfaceLocale()
274    {
275  0 return getXWikiContext().getInterfaceLocale();
276    }
277   
278    /**
279    * Returns the XWiki object. Programming rights are needed in order to call this method. If programming rights are
280    * not available in the current document, the XWiki object can still be accessed through a secure API available as a
281    * predefined variable for scripting inside wiki pages; use <code>$xwiki</code> in Velocity.
282    *
283    * @return The internal XWiki object, if the document has programming rights, or <code>null</code> otherwise.
284    */
 
285  0 toggle @Programming
286    public com.xpn.xwiki.XWiki getXWiki()
287    {
288  0 if (hasProgrammingRights()) {
289  0 return getXWikiContext().getWiki();
290    } else {
291  0 return null;
292    }
293    }
294   
295    /**
296    * Returns the current requested document. Programming rights are needed in order to call this method. If
297    * programming rights are not available in the current document, the current can can still be accessed document
298    * through a secure API available as a predefined variable for scripting inside wiki pages; use <code>$doc</code> in
299    * Velocity.
300    *
301    * @return The current requested document, if the document has programming rights, or <code>null</code> otherwise.
302    */
 
303  311 toggle @Programming
304    public XWikiDocument getDoc()
305    {
306  311 if (hasProgrammingRights()) {
307  311 return getXWikiContext().getDoc();
308    } else {
309  0 return null;
310    }
311    }
312   
313    /**
314    * Returns the current user which made the request. If there's no currently logged in user in XWiki then the
315    * returned string is <i>XWiki.XWikiGuest</i> which represents any anonymous user. The name of the user is returned
316    * relative to the current wiki so if the user is in the current wiki or in non-virtual mode the name will be of the
317    * form <code>XWiki.UserLogin</code>. If the user comes from another wiki the full prefixed name will be returned as
318    * in <code>wikiid:XWiki.UserLogin</code>. At the same time this method returns the name of the document containing
319    * the current user's profile so in Velocity you can do, for instance,
320    * <code>$xwiki.getDocument($xcontext.user)</code> to find out more about the current user, like his/hers real name
321    * or e-mail address.
322    *
323    * @return The current user which made the request.
324    * @see #getLocalUser()
325    * @see #getDatabase()
326    * @see #getUserReference()
327    */
 
328  338265 toggle public String getUser()
329    {
330  338430 return getXWikiContext().getUser();
331    }
332   
333    /**
334    * Returns the document reference for the profile page of the current user which made the request. The returned
335    * reference can always be considered an absolute document reference, meaning that
336    * <code>getUserReference().getWikiReference().getName()</code> will always return the name of the user's wiki.
337    *
338    * @return The document reference for the current logged in user which made the request or <code>null</code> if
339    * there is no currently logged in user (anonymous/guest user).
340    * @since 3.2M3
341    */
 
342  85502 toggle public DocumentReference getUserReference()
343    {
344  85511 return getXWikiContext().getUserReference();
345    }
346   
347    /**
348    * Returns the current user which made the request. The difference from {@link #getUser()} is that the returned
349    * string is never prefixed with the database name, not even in virtual mode.
350    *
351    * @return The current user which made the request.
352    * @see #getUser()
353    * @see #getDatabase()
354    */
 
355  0 toggle public String getLocalUser()
356    {
357  0 return getXWikiContext().getLocalUser();
358    }
359   
360    /**
361    * Sets the current document. Programming rights are needed in order to call this method.
362    *
363    * @param doc XWiki document to set as the context document.
364    */
 
365  0 toggle @Programming
366    public void setDoc(XWikiDocument doc)
367    {
368  0 if (hasProgrammingRights()) {
369  0 getXWikiContext().setDoc(doc);
370    }
371    }
372   
373    /**
374    * Returns the XWiki context. Programming rights are needed in order to call this method. The XWiki context
375    * represents the execution environment for all the wiki pages. Accessing it directly in wiki pages may lead to
376    * security issues.
377    *
378    * @return The unwrapped version of the context if you have programming rights, or <code>null</code> otherwise.
379    */
 
380  465 toggle @Programming
381    public XWikiContext getContext()
382    {
383  465 if (hasProgrammingRights()) {
384  465 return super.getXWikiContext();
385    } else {
386  0 return null;
387    }
388    }
389   
390    /**
391    * Returns the value associated with the given key in the XWiki context. Programming rights are needed in order to
392    * call this method. The context can be seen as a map of (paramName, paramValue) pairs. This mechanism is useful for
393    * passing parameters between pages or from Java to Velocity. For instance an exception caught in Java code can be
394    * put on the context and handled in a user-friendly way in Velocity. This method is protected because sensitive
395    * information may be placed in the internal context, which shouldn't be publicly accessible.
396    *
397    * @param key The key to look for in the context.
398    * @return The value associated with the given key in the XWiki context, if you have programming rights, or
399    * <code>null</code> otherwise.
400    * @see #put(String, java.lang.Object)
401    */
 
402  68 toggle @Programming
403    public java.lang.Object get(String key)
404    {
405  68 if (hasProgrammingRights()) {
406  68 return getXWikiContext().get(key);
407    } else {
408  0 return null;
409    }
410    }
411   
412    /**
413    * Returns the list of TextArea fields that use the WYSIWYG editor. This list is automatically built when displaying
414    * TextArea properties.
415    *
416    * @deprecated since 8.2RC1 when we started using the Edit Module to load the configured WYSIWYG editor
417    * @return a string containing a comma-separated list of TextArea field names for which the WYSIWYG editor should be
418    * enabled
419    */
 
420  0 toggle @Deprecated
421    public String getEditorWysiwyg()
422    {
423  0 return getXWikiContext().getEditorWysiwyg();
424    }
425   
426    /**
427    * Puts an object on the context using the given key. The context can be seen as a map of (paramName, paramValue)
428    * pairs. Requires programming rights.
429    *
430    * @param key The parameter name.
431    * @param value The parameter value.
432    * @see #get(String)
433    */
 
434  130 toggle @Programming
435    public void put(String key, java.lang.Object value)
436    {
437  130 if (hasProgrammingRights()) {
438  130 getXWikiContext().put(key, value);
439    }
440    }
441   
442    /**
443    * Specifies if the current page should be sent to the client or not. When the context is finished, the client
444    * response contains only the (HTTP) headers and no body (as in the case of a response to a HTTP HEAD request). This
445    * is useful for instance when exporting the entire wiki as a <code>.xar</code> archive.
446    *
447    * @param finished <code>true</code> to avoid rendering of the current page
448    */
 
449  3427 toggle public void setFinished(boolean finished)
450    {
451  3427 getXWikiContext().setFinished(finished);
452    }
453   
454    /**
455    * Returns the amount of time this document should be cached.
456    *
457    * @return The cache duration, in seconds.
458    * @see #setCacheDuration(int)
459    */
 
460  0 toggle public int getCacheDuration()
461    {
462  0 return getXWikiContext().getCacheDuration();
463    }
464   
465    /**
466    * Sets the cache duration in seconds. Setting this to a non-zero, positive value will cause the rendered document
467    * to be stored in a cache, so next time a client requests this document, if it is still in the cache, and the
468    * document content did not change, then it will be taken from the cache and will not be parsed/rendered again.
469    * While it is a good idea to cache pages containing only static content (no scripting), it should be used with care
470    * for documents that gather information from the wiki using queries.
471    *
472    * @param duration The cache duration specified in seconds.
473    * @see #getCacheDuration()
474    */
 
475  0 toggle public void setCacheDuration(int duration)
476    {
477  0 getXWikiContext().setCacheDuration(duration);
478    }
479   
480    /**
481    * Sets the action to be used instead of the <i>view</i> action inside URLs. The XWiki URL factories will replace
482    * the <i>view</i> action with the given action when creating URLs.
483    *
484    * @param action <i>view</i> action replacement
485    * @see #unsetLinksAction()
486    * @see #getLinksAction()
487    * @see #getURLFactory()
488    */
 
489  0 toggle public void setLinksAction(String action)
490    {
491  0 getXWikiContext().setLinksAction(action);
492    }
493   
494    /**
495    * Stops the <i>view</i> action from being replaced with another action inside URLs.
496    *
497    * @see #setLinksAction(String)
498    * @see #getLinksAction()
499    */
 
500  0 toggle public void unsetLinksAction()
501    {
502  0 getXWikiContext().unsetLinksAction();
503    }
504   
505    /**
506    * Returns the action used by XWiki URL factories to replace the <i>view</i> action when creating URLs. If no action
507    * replacement has been specified, it returns <code>null</code>.
508    *
509    * @return The <i>view</i> action replacement, or <code>null</code>.
510    * @see #setLinksAction(String)
511    * @see #unsetLinksAction()
512    * @see #getURLFactory()
513    */
 
514  0 toggle public String getLinksAction()
515    {
516  0 return getXWikiContext().getLinksAction();
517    }
518   
519    /**
520    * Sets an extra query string to be added to all the URLs created by XWiki URL factories.
521    *
522    * @param value The additional query string to be added to all the URLs created by XWiki URL factories.
523    * @see #unsetLinksQueryString()
524    * @see #getLinksQueryString()
525    * @see #getURLFactory()
526    */
 
527  2 toggle public void setLinksQueryString(String value)
528    {
529  2 getXWikiContext().setLinksQueryString(value);
530    }
531   
532    /**
533    * Specifies that no additional query string should be added to XWiki URLs.
534    *
535    * @see #setLinksQueryString(String)
536    * @see #getLinksQueryString()
537    */
 
538  2 toggle public void unsetLinksQueryString()
539    {
540  2 getXWikiContext().unsetLinksQueryString();
541    }
542   
543    /**
544    * Returns the extra query string that is added to all the URLs created by XWiki URL factories. If no such string
545    * has been specified it returns <code>null</code>.
546    *
547    * @return The additional query string that is added to all XWiki URLs, or <code>null</code>.
548    * @see #setLinksQueryString(String)
549    * @see #unsetLinksQueryString()
550    * @see #getURLFactory()
551    */
 
552  0 toggle public String getLinksQueryString()
553    {
554  0 return getXWikiContext().getLinksQueryString();
555    }
556   
557    /**
558    * Returns the form field validation status, which contains the exceptions or errors that may have occured during
559    * the validation process performed during a <i>save</i>.
560    *
561    * @return The validation status.
562    */
 
563  6 toggle public XWikiValidationStatus getValidationStatus()
564    {
565  6 return getXWikiContext().getValidationStatus();
566    }
567   
568    /**
569    * Returns the list with the currently displayed fields. Each time we call <code>display</code> on a document for a
570    * specific field that field is added to the list returned by this method.
571    *
572    * @return The list with the currently displayed fields.
573    * @see Document#display(String)
574    */
 
575  0 toggle public List<String> getDisplayedFields()
576    {
577  0 return getXWikiContext().getDisplayedFields();
578    }
579   
580    /**
581    * Sets the default field display mode, when using {@link Document#display(String)} or
582    * {@link Document#display(String, Object)}. It is automatically set to "edit" when the action is "edit" or
583    * "inline", and to "view" in all other cases.
584    *
585    * @param mode the display mode, one of "view", "edit", "hidden", "search", "rendered".
586    */
 
587  1 toggle public void setDisplayMode(String mode)
588    {
589  1 getXWikiContext().put("display", mode);
590    }
591   
592    /**
593    * Retrieves the information about the currently executing macro. This method is only useful inside wiki macros.
594    *
595    * @return macro information, normally a {@link java.util.Map} containing the macro {@code content}, the
596    * {@code params}, and the macro execution {@code context}
597    */
 
598  150939 toggle public java.lang.Object getMacro()
599    {
600  150939 return getXWikiContext().get("macro");
601    }
602   
603    /**
604    * After this is called: 1. {@link com.xpn.xwiki.api.Api#hasProgrammingRights()} will always return false. 2.
605    * {@link com.xpn.xwiki.api.XWiki#getDocumentAsAuthor(org.xwiki.model.reference.DocumentReference)},
606    * {@link com.xpn.xwiki.api.XWiki#getDocumentAsAuthor(String)}, {@link com.xpn.xwiki.api.Document#saveAsAuthor()},
607    * {@link com.xpn.xwiki.api.Document#saveAsAuthor(String)},
608    * {@link com.xpn.xwiki.api.Document#saveAsAuthor(String, boolean)}, and
609    * {@link com.xpn.xwiki.api.Document#deleteAsAuthor()} will perform all of their actions as if the document's
610    * content author was the guest user (XWiki.XWikiGuest). In effect, no code requiring "programming right" will run,
611    * and if the document content author (see: {@link com.xpn.xwiki.api.Document#getContentAuthor()}) is a user who has
612    * "programming right", there will be no way for code following this call to save another document as this user,
613    * blessing it too with programming right. Once dropped, permissions cannot be regained for the duration of the
614    * request.
615    *
616    * @since 2.5M2
617    */
 
618  7613 toggle public void dropPermissions()
619    {
620  7613 getXWikiContext().dropPermissions();
621    }
622   
623    /**
624    * Get the registered (generally error) message for the previous action.
625    *
626    * @return the registered message
627    * @since 5.2RC1
628    */
 
629  85 toggle public String getMessage()
630    {
631  85 return (String) this.context.get("message");
632    }
633    }