Package org.apache.wiki.api.core

# Interface Context

All Superinterfaces:
  `Cloneable`, `Command`

public interface Context extends Cloneable, Command

Provides state information throughout the processing of a page. A Context is born when the JSP pages that are the main entry points, are invoked. The JSPWiki engine creates the new Context, which basically holds information about the page, the handling engine, and in which context (view, edit, etc) the call was done.

A Context also provides request-specific variables, which can be used to communicate between plugins on the same page, or between different instances of the same plugin. A Context variable is valid until the processing of the WikiPage has ended. For an example, please see the Counter plugin.

When a Context is created, it automatically associates a `Session` object with the user's HttpSession. The Session contains information about the user's authentication status, and is consulted by `getCurrentUser()` object.

Do not cache the WikiPage object that you get from the WikiContext; always use getPage()!

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`ATTR_CONTEXT`
`static final String`
`VAR_EXECUTE_PLUGINS`
Variable name which tells whether plugins should be executed or not.
`static final String`
`VAR_WYSIWYG_EDITOR_MODE`
Name of the variable which is set to Boolean.TRUE or Boolean.FALSE depending on whether WYSIWYG is currently in effect.

## Method Summary

Modifier and Type
Method
Description
`Context`
`clone()`
`Context`
`deepClone()`
Creates a deep clone of the Context.
`static Context`
`findContext(jakarta.servlet.jsp.PageContext pageContext)`
This method can be used to find the WikiContext programmatically from a JSP PageContext.
`boolean`
`getBooleanWikiProperty(String key, boolean defValue)`
This is just a simple helper method which will first check the context if there is already an override in place, and if there is not, it will then check the given properties.
`Command`
`getCommand()`
Returns the Command associated with this Context.
`Principal`
`getCurrentUser()`
Convenience method that gets the current user.
`Engine`
`getEngine()`
Returns the handling engine.
`String`
`getHttpParameter(String paramName)`
This method will safely return any HTTP parameters that might have been defined.
`jakarta.servlet.http.HttpServletRequest`
`getHttpRequest()`
If the request did originate from an HTTP request, then the HTTP request can be fetched here.
`Page`
`getPage()`
Returns the WikiPage that is being handled.
`Page`
`getRealPage()`
Gets a reference to the real WikiPage whose content is currently being rendered.
`String`
`getRedirectURL()`
Figure out to which WikiPage we are really going to.
`String`
`getTemplate()`
Gets the template that is to be used throughout this request.
`default String`
`getURL(String context, String page)`
Creates a URL for the given request context.
`default String`
`getURL(String context, String page, String params)`
Returns a URL from a page.
`<T> T`
`getVariable(String key)`
Gets a previously set variable.
`String`
`getViewURL(String WikiPage)`
A shortcut to generate a VIEW url.
`Session`
`getWikiSession()`
Returns the Session associated with the context.
`boolean`
`hasAdminPermissions()`
Returns true, if the current user has administrative permissions (i.e. the omnipotent AllPermission).
`void`
`setPage(Page wikiPage)`
Sets the WikiPage that is being handled.
`Page`
`setRealPage(Page wikiPage)`
Sets a reference to the real WikiPage whose content is currently being rendered.
`void`
`setRequestContext(String context)`
Sets the request context.
`void`
`setTemplate(String dir)`
Sets the template to be used for this request.
`void`
`setVariable(String key, Object data)`
Sets a variable.

### Methods inherited from interface org.apache.wiki.api.core.Command

`getContentTemplate, getJSP, getName, getRequestContext, getTarget, getURLPattern, requiredPermission, targetedCommand`

## Field Details

### ATTR_CONTEXT

static final String ATTR_CONTEXT

See Also:
  - Constant Field Values

### VAR_EXECUTE_PLUGINS

static final String VAR_EXECUTE_PLUGINS
Variable name which tells whether plugins should be executed or not. Value can be either `Boolean.TRUE` or `Boolean.FALSE`. While not set its value is `null`.

See Also:
  - Constant Field Values

### VAR_WYSIWYG_EDITOR_MODE

static final String VAR_WYSIWYG_EDITOR_MODE
Name of the variable which is set to Boolean.TRUE or Boolean.FALSE depending on whether WYSIWYG is currently in effect.

See Also:
  - Constant Field Values

## Method Details

### getPage

Page getPage()
Returns the WikiPage that is being handled.

Returns:
  the WikiPage which was fetched.

### setPage

void setPage(Page wikiPage)
Sets the WikiPage that is being handled.

Parameters:
  `wikiPage` - The wikipage
Since:
  2.1.37.

### getRealPage

Page getRealPage()
Gets a reference to the real WikiPage whose content is currently being rendered. If your plugin e.g. does some variable setting, be aware that if it is embedded in the LeftMenu or some other WikiPage added with InsertPageTag, you should consider what you want to do - do you wish to really reference the "master" WikiPage or the included page.

For example, in the default template, there is a WikiPage called "LeftMenu". Whenever you access a page, e.g. "Main", the master WikiPage will be Main, and that's what the getPage() will return - regardless of whether your plugin resides on the LeftMenu or on the Main page. However, getRealPage() will return "LeftMenu".

Returns:
  A reference to the real page.

### setRealPage

Page setRealPage(Page wikiPage)
Sets a reference to the real WikiPage whose content is currently being rendered.

Sometimes you may want to render the WikiPage using some other page's context. In those cases, it is highly recommended that you set the setRealPage() to point at the real WikiPage you are rendering. Please see InsertPageTag for an example.

Also, if your plugin e.g. does some variable setting, be aware that if it is embedded in the LeftMenu or some other WikiPage added with InsertPageTag, you should consider what you want to do - do you wish to really reference the "master" WikiPage or the included page.

Parameters:
  `wikiPage` - The real WikiPage which is being rendered.
Returns:
  The previous real page
Since:
  2.3.14

### getEngine

Engine getEngine()
Returns the handling engine.

Returns:
  The wikiengine owning this context.

### setRequestContext

void setRequestContext(String context)
Sets the request context. See above for the different request contexts (VIEW, EDIT, etc.)

Parameters:
  `context` - The request context (one of the predefined contexts.)

### getVariable

<T> T getVariable(String key)
Gets a previously set variable.

Parameters:
  `key` - The variable name.
Returns:
  The variable contents.

### setVariable

void setVariable(String key, Object data)
Sets a variable. The variable is valid while the WikiContext is valid, i.e. while WikiPage processing continues. The variable data is discarded once the WikiPage processing is finished.

Parameters:
  `key` - The variable name.
  `data` - The variable value.

### getBooleanWikiProperty

boolean getBooleanWikiProperty(String key, boolean defValue)
This is just a simple helper method which will first check the context if there is already an override in place, and if there is not, it will then check the given properties.

Parameters:
  `key` - What key are we searching for?
  `defValue` - Default value for the boolean
Returns:
  `true` or `false`.

### getHttpParameter

String getHttpParameter(String paramName)
This method will safely return any HTTP parameters that might have been defined. You should use this method instead of peeking directly into the result of getHttpRequest(), since this method is smart enough to do all the right things, figure out UTF-8 encoded parameters, etc.

Parameters:
  `paramName` - Parameter name to look for.
Returns:
  HTTP parameter, or null, if no such parameter existed.
Since:
  2.0.13.

### getHttpRequest

jakarta.servlet.http.HttpServletRequest getHttpRequest()
If the request did originate from an HTTP request, then the HTTP request can be fetched here. However, if the request did NOT originate from an HTTP request, then this method will return null, and YOU SHOULD CHECK FOR IT!

Returns:
  Null, if no HTTP request was done.
Since:
  2.0.13.

### setTemplate

void setTemplate(String dir)
Sets the template to be used for this request.

Parameters:
  `dir` - The template name
Since:
  2.1.15.

### getTemplate

String getTemplate()
Gets the template that is to be used throughout this request.

Returns:
  template name
Since:
  2.1.15.

### getWikiSession

Session getWikiSession()
Returns the Session associated with the context. This method is guaranteed to always return a valid Session. If this context was constructed without an associated HttpServletRequest, it will return a guest session.

Returns:
  The Session associate with this context.

### getCurrentUser

Principal getCurrentUser()
Convenience method that gets the current user. Delegates the lookup to the Session associated with this Context. May return null, in case the current user has not yet been determined; or this is an internal system. If the Session has not been set, always returns null.

Returns:
  The current user; or maybe null in case of internal calls.

### hasAdminPermissions

boolean hasAdminPermissions()
Returns true, if the current user has administrative permissions (i.e. the omnipotent AllPermission).

Returns:
  true, if the user has all permissions.
Since:
  2.4.46

### getViewURL

String getViewURL(String WikiPage)
A shortcut to generate a VIEW url.

Parameters:
  `WikiPage` - The WikiPage to which to link.
Returns:
  A URL to the page. This honours the current absolute/relative setting.

### getRedirectURL

String getRedirectURL()
Figure out to which WikiPage we are really going to. Considers special WikiPage names from the jspwiki.properties, and possible aliases.

Returns:
  A complete URL to the new WikiPage to redirect to
Since:
  2.2

### getCommand

Command getCommand()
Returns the Command associated with this Context.

Returns:
  the command

### getURL

default String getURL(String context, String page)
Creates a URL for the given request context.

Parameters:
  `context` - e.g. WikiContext.EDIT
  `page` - The WikiPage to which to link
Returns:
  A URL to the page.

### getURL

default String getURL(String context, String page, String params)
Returns a URL from a page. It this Context instance was constructed with an actual HttpServletRequest, we will attempt to construct the URL using HttpUtil, which preserves the HTTPS portion if it was used.

Parameters:
  `context` - The request context (e.g. WikiContext.UPLOAD)
  `page` - The WikiPage to which to link
  `params` - A list of parameters, separated with "&"
Returns:
  A URL to the given context and page.

### clone

Context clone()

### deepClone

Context deepClone()
Creates a deep clone of the Context. This is useful when you want to be sure that you don't accidentally mess with page attributes, etc.

Returns:
  A deep clone of the Context.
Since:
  2.8.0

### findContext

static Context findContext(jakarta.servlet.jsp.PageContext pageContext)
This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the request context. The wiki context, if it exists, is looked up using the key `ATTR_CONTEXT`.

Parameters:
  `pageContext` - the JSP page context
Returns:
  Current WikiContext, or null, of no context exists.
Since:
  2.4