Package com.xpn.xwiki.api
Class Context
- java.lang.Object
-
- com.xpn.xwiki.api.Api
-
- com.xpn.xwiki.api.Context
-
- Direct Known Subclasses:
DeprecatedContext
public class Context extends Api
Provides a secure proxy for the internalXWikiContext
class, that can be used safely in scripts. All dangerous methods are protected by requiring Programming Rights.- Version:
- $Id: 674723936f93789fe42c4282a63c71c68bf77309 $
-
-
Constructor Summary
Constructors Constructor Description Context(XWikiContext context)
The Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
dropPermissions()
After this is called: 1.Object
get(String key)
Returns the value associated with the given key in the XWiki context.String
getAction()
Returns the current action.int
getCacheDuration()
Returns the amount of time this document should be cached.XWikiContext
getContext()
Returns the XWiki context.String
getDatabase()
Returns the current database name.List<String>
getDisplayedFields()
Returns the list with the currently displayed fields.String
getDisplayMode()
Returns the default field display mode, when usingDocument.display(String)
orDocument.display(String, Object)
.XWikiDocument
getDoc()
Returns the current requested document.String
getEditorWysiwyg()
Deprecated.since 8.2RC1 when we started using the Edit Module to load the configured WYSIWYG editorString
getInterfaceLanguage()
Deprecated.since 6.0M1, usegetInterfaceLocale()
insteadLocale
getInterfaceLocale()
Returns the interface locale preference of the current user.String
getLanguage()
Deprecated.since 6.0M1, usegetLocale()
insteadString
getLinksAction()
Returns the action used by XWiki URL factories to replace the view action when creating URLs.String
getLinksQueryString()
Returns the extra query string that is added to all the URLs created by XWiki URL factories.Locale
getLocale()
Returns the locale of the current request.String
getLocalUser()
Returns the current user which made the request.Object
getMacro()
Retrieves the information about the currently executing macro.String
getMainWikiName()
Returns the name of the main wiki.String
getMessage()
Get the registered (generally error) message for the previous action.int
getMode()
Specifies the container or environment in which XWiki is currently running.String
getOriginalDatabase()
Returns the name of the original database.XWikiRequest
getRequest()
Returns the current request object.XWikiResponse
getResponse()
Returns the current response object.XWikiURLFactory
getURLFactory()
Returns the URL factory matching both the protocol used to make the current request and the container or environment in which XWiki is running.String
getUser()
Returns the current user which made the request.DocumentReference
getUserReference()
Returns the document reference for the profile page of the current user which made the request.XWikiValidationStatus
getValidationStatus()
Returns the form field validation status, which contains the exceptions or errors that may have occured during the validation process performed during a save.XWiki
getXWiki()
Returns the XWiki object.boolean
isMainWiki()
Specifies if the current request was made to a virtual (non-main) wiki, or to the main wiki.void
put(String key, Object value)
Puts an object on the context using the given key.void
setCacheDuration(int duration)
Sets the cache duration in seconds.void
setDatabase(String database)
Sets the current database.void
setDisplayMode(String mode)
Sets the default field display mode, when usingDocument.display(String)
orDocument.display(String, Object)
.void
setDoc(XWikiDocument doc)
Sets the current document.void
setFinished(boolean finished)
Specifies if the current page should be sent to the client or not.void
setLinksAction(String action)
Sets the action to be used instead of the view action inside URLs.void
setLinksQueryString(String value)
Sets an extra query string to be added to all the URLs created by XWiki URL factories.void
unsetLinksAction()
Stops the view action from being replaced with another action inside URLs.void
unsetLinksQueryString()
Specifies that no additional query string should be added to XWiki URLs.-
Methods inherited from class com.xpn.xwiki.api.Api
convert, convert, convert, convertAttachments, getAuthorizationManager, getContextualAuthorizationManager, getXWikiContext, hasAccess, hasAccessLevel, hasAdminRights, hasProgrammingRights, hasWikiAdminRights
-
-
-
-
Constructor Detail
-
Context
public Context(XWikiContext context)
The Constructor.- Parameters:
context
- TheXWikiContext
to wrap.
-
-
Method Detail
-
getRequest
public XWikiRequest getRequest()
Returns the current request object. If the request has been made to a servlet container using the HTTP protocol then the returned object wraps aHttpServletRequest
instance.- Returns:
- an object wrapping the current request object
-
getResponse
public XWikiResponse getResponse()
Returns the current response object. If the request has been made to a servlet container using the HTTP protocol then the returned object wraps aHttpServletResponse
instance.- Returns:
- an object wrapping the current response object
-
getMode
public int getMode()
Specifies the container or environment in which XWiki is currently running. See the following table for possible values it can return:Return values Return Meaning 0 Servlet Container 1 Portlet Container 2 XML RPC 3 Atom 4 PDF 5 GWT 6 GWT Debugging - Returns:
- an integer constant identifying the container or environment in which XWiki is currently running
-
getDatabase
public String getDatabase()
Returns the current database name. IfisMainWiki()
returnstrue
then the current database name is the same asgetMainWikiName()
. Otherwise, each virtual wiki has it's own database. In this case the returned string identifies the current virtual wiki we operate on and prefixes document names like in databaseName:spaceName.pageName.- Returns:
- The current database name.
- See Also:
isMainWiki()
,getMainWikiName()
-
getMainWikiName
public String getMainWikiName()
Returns the name of the main wiki. In non-virtual mode there is only one wiki called xwiki. In virtual mode, the main wiki stores information about all the other virtual wikis.- Returns:
- The name of the main wiki.
-
getOriginalDatabase
public String getOriginalDatabase()
Returns the name of the original database. Here, original means the database corresponding to the requested URL, which can be changed when including a document from a different database, by using, for example,#includeTopic("virtualwiki:Some.Document")
.- Returns:
- The original database name
-
setDatabase
@Programming public void setDatabase(String database)
Sets the current database. You need programming rights to be able to call this method.- Parameters:
database
- a database name- See Also:
getDatabase()
-
getURLFactory
public XWikiURLFactory getURLFactory()
Returns the URL factory matching both the protocol used to make the current request and the container or environment in which XWiki is running. The most used concrete implementation of theXWikiURLFactory
isXWikiServletURLFactory
.XWikiURLFactory
offers a generic way of creating XWiki specific URLs that should be chosen instead of the basic string concatenation. Hard-coding the protocol used, like HTTP, inside wiki pages should be avoided.- Returns:
- The URL factory.
-
isMainWiki
public boolean isMainWiki()
Specifies if the current request was made to a virtual (non-main) wiki, or to the main wiki.
In virtual mode the server can host more than one wiki, each having it's own database and its own URL. We refer to them as virtual wikis. One of them stores information about the others and it is called the main wiki. You can switch to virtual mode by changing the
xwiki.virtual
parameter from0
to1
in the configuration file.- Returns:
true
if XWiki is in the main wiki, or if virtual mode is disabled.- See Also:
getDatabase()
,getMainWikiName()
-
getAction
public String getAction()
Returns the current action. XWiki is built on top of the Struts framework, so here "action" means a StrutsAction. Some predefined actions are: view, edit, delete, download and export. Custom actions can be defined. The action and it's target are extracted from the request URL. For instance, in http://platform.xwiki.org/xwiki/bin/view/DevGuide/API the action is view and it's target is the DevGuide.API document.- Returns:
- The action corresponding to the request URL.
-
getLanguage
@Deprecated public String getLanguage()
Deprecated.since 6.0M1, usegetLocale()
insteadSame asgetLocale()
but as String.- Returns:
- The locale of the current request.
- See Also:
getInterfaceLanguage()
-
getLocale
public Locale getLocale()
Returns the locale of the current request. Ifmultilingual
is turned off then the locale used is given by thedefault_language
preference. Otherwise, the locale is taken from either the request object, the cookie, user preferences or from the navigator locale settings, the last having the lower priority.- Returns:
- The locale of the current request.
- Since:
- 6.0M1
- See Also:
getInterfaceLocale()
-
getInterfaceLanguage
@Deprecated public String getInterfaceLanguage()
Deprecated.since 6.0M1, usegetInterfaceLocale()
insteadSame asgetInterfaceLocale()
but as String.- Returns:
- The interface locale preference of the current user.
- See Also:
getLanguage()
-
getInterfaceLocale
public Locale getInterfaceLocale()
Returns the interface locale preference of the current user. Ifmultilingual
is turned off then the locale used is given by thedefault_language
preference. Otherwise, the locale is taken from either the request object, the context, the cookie, user preferences or from the navigator locale settings, the last having the lower priority.- Returns:
- The interface locale preference of the current user.
- Since:
- 6.0M1
- See Also:
getLocale()
-
getXWiki
@Programming public XWiki getXWiki()
Returns the XWiki object. Programming rights are needed in order to call this method. If programming rights are not available in the current document, the XWiki object can still be accessed through a secure API available as a predefined variable for scripting inside wiki pages; use$xwiki
in Velocity.- Returns:
- The internal XWiki object, if the document has programming rights, or
null
otherwise.
-
getDoc
@Programming public XWikiDocument getDoc()
Returns the current requested document. Programming rights are needed in order to call this method. If programming rights are not available in the current document, the current can can still be accessed document through a secure API available as a predefined variable for scripting inside wiki pages; use$doc
in Velocity.- Returns:
- The current requested document, if the document has programming rights, or
null
otherwise.
-
getUser
public String getUser()
Returns the current user which made the request. If there's no currently logged in user in XWiki then the returned string is XWiki.XWikiGuest which represents any anonymous user. The name of the user is returned 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 formXWiki.UserLogin
. If the user comes from another wiki the full prefixed name will be returned as inwikiid:XWiki.UserLogin
. At the same time this method returns the name of the document containing the current user's profile so in Velocity you can do, for instance,$xwiki.getDocument($xcontext.user)
to find out more about the current user, like his/hers real name or e-mail address.- Returns:
- The current user which made the request.
- See Also:
getLocalUser()
,getDatabase()
,getUserReference()
-
getUserReference
public DocumentReference getUserReference()
Returns the document reference for the profile page of the current user which made the request. The returned reference can always be considered an absolute document reference, meaning thatgetUserReference().getWikiReference().getName()
will always return the name of the user's wiki.- Returns:
- The document reference for the current logged in user which made the request or
null
if there is no currently logged in user (anonymous/guest user). - Since:
- 3.2M3
-
getLocalUser
public String getLocalUser()
Returns the current user which made the request. The difference fromgetUser()
is that the returned string is never prefixed with the database name, not even in virtual mode.- Returns:
- The current user which made the request.
- See Also:
getUser()
,getDatabase()
-
setDoc
@Programming public void setDoc(XWikiDocument doc)
Sets the current document. Programming rights are needed in order to call this method.- Parameters:
doc
- XWiki document to set as the context document.
-
getContext
@Programming public XWikiContext getContext()
Returns the XWiki context. Programming rights are needed in order to call this method. The XWiki context represents the execution environment for all the wiki pages. Accessing it directly in wiki pages may lead to security issues.- Returns:
- The unwrapped version of the context if you have programming rights, or
null
otherwise.
-
get
@Programming public Object get(String key)
Returns the value associated with the given key in the XWiki context. Programming rights are needed in order to call this method. The context can be seen as a map of (paramName, paramValue) pairs. This mechanism is useful for passing parameters between pages or from Java to Velocity. For instance an exception caught in Java code can be put on the context and handled in a user-friendly way in Velocity. This method is protected because sensitive information may be placed in the internal context, which shouldn't be publicly accessible.- Parameters:
key
- The key to look for in the context.- Returns:
- The value associated with the given key in the XWiki context, if you have programming rights, or
null
otherwise. - See Also:
put(String, java.lang.Object)
-
getEditorWysiwyg
@Deprecated public String getEditorWysiwyg()
Deprecated.since 8.2RC1 when we started using the Edit Module to load the configured WYSIWYG editorReturns the list of TextArea fields that use the WYSIWYG editor. This list is automatically built when displaying TextArea properties.- Returns:
- a string containing a comma-separated list of TextArea field names for which the WYSIWYG editor should be enabled
-
put
@Programming public void put(String key, Object value)
Puts an object on the context using the given key. The context can be seen as a map of (paramName, paramValue) pairs. Requires programming rights.- Parameters:
key
- The parameter name.value
- The parameter value.- See Also:
get(String)
-
setFinished
public void setFinished(boolean finished)
Specifies if the current page should be sent to the client or not. When the context is finished, the client response contains only the (HTTP) headers and no body (as in the case of a response to a HTTP HEAD request). This is useful for instance when exporting the entire wiki as a.xar
archive.- Parameters:
finished
-true
to avoid rendering of the current page
-
getCacheDuration
public int getCacheDuration()
Returns the amount of time this document should be cached.- Returns:
- The cache duration, in seconds.
- See Also:
setCacheDuration(int)
-
setCacheDuration
public void setCacheDuration(int duration)
Sets the cache duration in seconds. Setting this to a non-zero, positive value will cause the rendered document to be stored in a cache, so next time a client requests this document, if it is still in the cache, and the document content did not change, then it will be taken from the cache and will not be parsed/rendered again. While it is a good idea to cache pages containing only static content (no scripting), it should be used with care for documents that gather information from the wiki using queries.- Parameters:
duration
- The cache duration specified in seconds.- See Also:
getCacheDuration()
-
setLinksAction
public void setLinksAction(String action)
Sets the action to be used instead of the view action inside URLs. The XWiki URL factories will replace the view action with the given action when creating URLs.- Parameters:
action
- view action replacement- See Also:
unsetLinksAction()
,getLinksAction()
,getURLFactory()
-
unsetLinksAction
public void unsetLinksAction()
Stops the view action from being replaced with another action inside URLs.- See Also:
setLinksAction(String)
,getLinksAction()
-
getLinksAction
public String getLinksAction()
Returns the action used by XWiki URL factories to replace the view action when creating URLs. If no action replacement has been specified, it returnsnull
.- Returns:
- The view action replacement, or
null
. - See Also:
setLinksAction(String)
,unsetLinksAction()
,getURLFactory()
-
setLinksQueryString
public void setLinksQueryString(String value)
Sets an extra query string to be added to all the URLs created by XWiki URL factories.- Parameters:
value
- The additional query string to be added to all the URLs created by XWiki URL factories.- See Also:
unsetLinksQueryString()
,getLinksQueryString()
,getURLFactory()
-
unsetLinksQueryString
public void unsetLinksQueryString()
Specifies that no additional query string should be added to XWiki URLs.- See Also:
setLinksQueryString(String)
,getLinksQueryString()
-
getLinksQueryString
public String getLinksQueryString()
Returns the extra query string that is added to all the URLs created by XWiki URL factories. If no such string has been specified it returnsnull
.- Returns:
- The additional query string that is added to all XWiki URLs, or
null
. - See Also:
setLinksQueryString(String)
,unsetLinksQueryString()
,getURLFactory()
-
getValidationStatus
public XWikiValidationStatus getValidationStatus()
Returns the form field validation status, which contains the exceptions or errors that may have occured during the validation process performed during a save.- Returns:
- The validation status.
-
getDisplayedFields
public List<String> getDisplayedFields()
Returns the list with the currently displayed fields. Each time we calldisplay
on a document for a specific field that field is added to the list returned by this method.- Returns:
- The list with the currently displayed fields.
- See Also:
Document.display(String)
-
setDisplayMode
public void setDisplayMode(String mode)
Sets the default field display mode, when usingDocument.display(String)
orDocument.display(String, Object)
. It is automatically set to "edit" when the action is "edit" or "inline", and to "view" in all other cases.- Parameters:
mode
- the display mode, one of "view", "edit", "hidden", "search", "rendered".
-
getDisplayMode
public String getDisplayMode()
Returns the default field display mode, when usingDocument.display(String)
orDocument.display(String, Object)
.- Returns:
- the display mode, one of "view", "edit", "hidden", "search" or "rendered"
- Since:
- 13.4.7, 13.10.3, 14.0RC1
-
getMacro
public Object getMacro()
Retrieves the information about the currently executing macro. This method is only useful inside wiki macros.- Returns:
- macro information, normally a
Map
containing the macrocontent
, theparams
, and the macro executioncontext
-
dropPermissions
public void dropPermissions()
After this is called: 1.Api.hasProgrammingRights()
will always return false. 2.XWiki.getDocumentAsAuthor(org.xwiki.model.reference.DocumentReference)
,XWiki.getDocumentAsAuthor(String)
,Document.saveAsAuthor()
,Document.saveAsAuthor(String)
,Document.saveAsAuthor(String, boolean)
, andDocument.deleteAsAuthor()
will perform all of their actions as if the document's content author was the guest user (XWiki.XWikiGuest). In effect, no code requiring "programming right" will run, and if the document content author (see:Document.getContentAuthor()
) is a user who has "programming right", there will be no way for code following this call to save another document as this user, blessing it too with programming right. Once dropped, permissions cannot be regained for the duration of the request.- Since:
- 2.5M2
-
getMessage
public String getMessage()
Get the registered (generally error) message for the previous action.- Returns:
- the registered message
- Since:
- 5.2RC1
-
-