com.ecyrd.jspwiki
Class WikiContext

java.lang.Object
  extended by com.ecyrd.jspwiki.WikiContext
All Implemented Interfaces:
Cloneable

public class WikiContext
extends Object
implements Cloneable

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

A WikiContext 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 WikiContext variable is valid until the processing of the page has ended. For an example, please see the Counter plugin.

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

Author:
Janne Jalkanen, Andrew R. Jaquith
See Also:
Counter

Field Summary
static String ATTACH
           
static String COMMENT
           
static String CONFLICT
          User has an internal conflict, and does quite not know what to do.
static String CREATE_GROUP
           
static String DELETE
           
static String DIFF
          User is viewing a DIFF between the two versions of the page.
static String EDIT
          The EDIT context - the user is editing the page.
static String ERROR
          An error has been encountered and the user needs to be informed.
static String FIND
           
static String INFO
          User is viewing page history.
protected static org.apache.log4j.Logger log
           
static String LOGIN
          User is preparing for a login/authentication.
static String LOGOUT
          User is preparing to log out.
static String NONE
           
static String OTHER
           
static String PREFS
           
static String PREVIEW
          User is previewing the changes he just made.
static String RENAME
           
static String RSS
           
static String UPLOAD
           
static String VIEW
          The VIEW context - the user just wants to view the page contents.
 
Constructor Summary
WikiContext(WikiEngine engine, javax.servlet.http.HttpServletRequest request, WikiPage page)
           Creates a new WikiContext for the given WikiEngine, WikiPage and HttpServletRequest.
WikiContext(WikiEngine engine, WikiPage page)
          Create a new WikiContext for the given WikiPage.
 
Method Summary
 Object clone()
          Returns a shallow clone of the WikiContext.
static WikiContext findContext(javax.servlet.jsp.PageContext pageContext)
          This method can be used to find the WikiContext programmatically from a JSP PageContext.
 Principal getCurrentUser()
          Convenience method that gets the current user.
 WikiEngine getEngine()
          Returns the handling engine.
 String getHttpParameter(String paramName)
          This method will safely return any HTTP parameters that might have been defined.
 javax.servlet.http.HttpServletRequest getHttpRequest()
          If the request did originate from a HTTP request, then the HTTP request can be fetched here.
 WikiPage getPage()
          Returns the page that is being handled.
 WikiPage getRealPage()
           
 String getRequestContext()
          Returns the request context.
 String getTemplate()
          Gets the template that is to be used throughout this request.
 String getURL(String context, String page)
           
 String getURL(String context, String page, String params)
          Returns an URL from a page.
 Object getVariable(String key)
          Gets a previously set variable.
 String getViewURL(String page)
           
 WikiSession getWikiSession()
          Returns the WikiSession associated with the context.
 boolean hasAccess(javax.servlet.http.HttpServletResponse response)
          Checks whether the current user has access to this wiki context, by obtaining the required Permission (requiredPermission()) and delegating the access check to AuthorizationManager.checkPermission(WikiSession, Permission).
 boolean hasAccess(javax.servlet.http.HttpServletResponse response, boolean redirect)
          Checks whether the current user has access to this wiki context (and optionally redirects if not), by obtaining the required Permission (requiredPermission()) and delegating the access check to AuthorizationManager.checkPermission(WikiSession, Permission).
 Permission requiredPermission()
          Returns the permission required to successfully execute this context.
 void setPage(WikiPage page)
          Sets the page that is being handled.
 WikiPage setRealPage(WikiPage page)
          Sometimes you may want to render the page using some other page's context.
 void setRequestContext(String arg)
          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.
protected  void updatePermission()
          Protected method that updates the value returned by the requiredPermission() method.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VIEW

public static final String VIEW
The VIEW context - the user just wants to view the page contents.

See Also:
Constant Field Values

EDIT

public static final String EDIT
The EDIT context - the user is editing the page.

See Also:
Constant Field Values

LOGIN

public static final String LOGIN
User is preparing for a login/authentication.

See Also:
Constant Field Values

LOGOUT

public static final String LOGOUT
User is preparing to log out.

See Also:
Constant Field Values

DIFF

public static final String DIFF
User is viewing a DIFF between the two versions of the page.

See Also:
Constant Field Values

INFO

public static final String INFO
User is viewing page history.

See Also:
Constant Field Values

PREVIEW

public static final String PREVIEW
User is previewing the changes he just made.

See Also:
Constant Field Values

CONFLICT

public static final String CONFLICT
User has an internal conflict, and does quite not know what to do. Please provide some counseling.

See Also:
Constant Field Values

ERROR

public static final String ERROR
An error has been encountered and the user needs to be informed.

See Also:
Constant Field Values

UPLOAD

public static final String UPLOAD
See Also:
Constant Field Values

COMMENT

public static final String COMMENT
See Also:
Constant Field Values

FIND

public static final String FIND
See Also:
Constant Field Values

CREATE_GROUP

public static final String CREATE_GROUP
See Also:
Constant Field Values

PREFS

public static final String PREFS
See Also:
Constant Field Values

RENAME

public static final String RENAME
See Also:
Constant Field Values

DELETE

public static final String DELETE
See Also:
Constant Field Values

ATTACH

public static final String ATTACH
See Also:
Constant Field Values

RSS

public static final String RSS
See Also:
Constant Field Values

NONE

public static final String NONE
See Also:
Constant Field Values

OTHER

public static final String OTHER
See Also:
Constant Field Values

log

protected static org.apache.log4j.Logger log
Constructor Detail

WikiContext

public WikiContext(WikiEngine engine,
                   WikiPage page)
Create a new WikiContext for the given WikiPage. Delegates to WikiContext(WikiEngine, HttpServletRequest, WikiPage).

Parameters:
engine - The WikiEngine that is handling the request.
page - The WikiPage. If you want to create a WikiContext for an older version of a page, you must use this constructor.

WikiContext

public WikiContext(WikiEngine engine,
                   javax.servlet.http.HttpServletRequest request,
                   WikiPage page)

Creates a new WikiContext for the given WikiEngine, WikiPage and HttpServletRequest. This constructor will also look up the HttpSession associated with the request, and determine if a WikiSession object is present. If not, a new one is created.

After the WikiSession object is obtained, the current authentication status is checked. If not authenticated, or if the login status reported by the container has changed, the constructor attempts to log in the user with AuthenticationManager.login(HttpServletRequest).

Parameters:
engine - The WikiEngine that is handling the request
request - The HttpServletRequest that should be associated with this context. This parameter may be null.
page - The WikiPage. If you want to create a WikiContext for an older version of a page, you must supply this parameter
Method Detail

setRealPage

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

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

getRealPage

public WikiPage getRealPage()

getEngine

public WikiEngine getEngine()
Returns the handling engine.


getPage

public WikiPage getPage()
Returns the page that is being handled.


setPage

public void setPage(WikiPage page)
Sets the page that is being handled.

Since:
2.1.37.

getRequestContext

public String getRequestContext()
Returns the request context.


setRequestContext

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

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

getVariable

public Object getVariable(String key)
Gets a previously set variable.

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

setVariable

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

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

getHttpParameter

public 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 of 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

public javax.servlet.http.HttpServletRequest getHttpRequest()
If the request did originate from a HTTP request, then the HTTP request can be fetched here. However, it the request did NOT originate from a 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

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

Since:
2.1.15.

getTemplate

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

Since:
2.1.15.

getCurrentUser

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


getViewURL

public String getViewURL(String page)

getURL

public String getURL(String context,
                     String page)

getURL

public String getURL(String context,
                     String page,
                     String params)
Returns an URL from a page. It this WikiContext 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.


clone

public Object clone()
Returns a shallow clone of the WikiContext.

Overrides:
clone in class Object
Since:
2.1.37.

getWikiSession

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


findContext

public static WikiContext findContext(javax.servlet.jsp.PageContext pageContext)
This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the page context scope first, then the request context. The wiki context, if it exists, is looked up using the key WikiTagBase.ATTR_CONTEXT.

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

requiredPermission

public Permission requiredPermission()
Returns the permission required to successfully execute this context. For example, the a wiki context of VIEW for a certain page means that the PagePermission "view" is required for the page. In some cases, no particular permission is required, in which case a dummy permission will be returned (PropertyPermission "os.name", "read"). This method is guaranteed to always return a valid, non-null permission.

Returns:
the permission
Since:
2.4

hasAccess

public boolean hasAccess(javax.servlet.http.HttpServletResponse response)
                  throws IOException
Checks whether the current user has access to this wiki context, by obtaining the required Permission (requiredPermission()) and delegating the access check to AuthorizationManager.checkPermission(WikiSession, Permission). If the user is allowed, this method returns true; false otherwise. If access is allowed, the wiki context will be added to the request as an attribute with the key name WikiTagBase.ATTR_CONTEXT. Note that this method will automatically redirect the user to a login or error page, as appropriate, if access fails. This is NOT guaranteed to be default behavior in the future.

Parameters:
response - the http response
Returns:
the result of the access check
Throws:
IOException

hasAccess

public boolean hasAccess(javax.servlet.http.HttpServletResponse response,
                         boolean redirect)
                  throws IOException
Checks whether the current user has access to this wiki context (and optionally redirects if not), by obtaining the required Permission (requiredPermission()) and delegating the access check to AuthorizationManager.checkPermission(WikiSession, Permission). If the user is allowed, this method returns true; false otherwise. If access is allowed, the wiki context will be added to the request as attribute with the key name WikiTagBase.ATTR_CONTEXT.

Returns:
the result of the access check
Throws:
IOException

updatePermission

protected void updatePermission()
Protected method that updates the value returned by the requiredPermission() method. Will always be called when the page name, request context, or variable changes.

Since:
2.4