Class WikiContext

  • All Implemented Interfaces:
    java.lang.Cloneable, Command, Context

    public class WikiContext
    extends java.lang.Object
    implements Context, Command

    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.

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

    See Also:
    Counter
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ADMIN
      User is doing administrative things.
      static java.lang.String ATTACH
      User is downloading an attachment.
      static java.lang.String COMMENT
      User is commenting something.
      static java.lang.String CONFLICT
      User has an internal conflict, and does quite not know what to do.
      static java.lang.String CREATE_GROUP
      User wishes to create a new group
      static java.lang.String DELETE
      User is deleting a page or an attachment.
      static java.lang.String DELETE_GROUP
      User is deleting an existing group.
      static java.lang.String DIFF
      User is viewing a DIFF between the two versions of the page.
      static java.lang.String EDIT
      The EDIT context - the user is editing the page.
      static java.lang.String EDIT_GROUP
      User is editing an existing group.
      static java.lang.String ERROR
      An error has been encountered and the user needs to be informed.
      static java.lang.String FIND
      User is searching for content.
      static java.lang.String INFO
      User is viewing page history.
      static java.lang.String INSTALL
      User is administering JSPWiki (Install, SecurityConfig).
      static java.lang.String LOGIN
      User is preparing for a login/authentication.
      static java.lang.String LOGOUT
      User is preparing to log out.
      protected javax.servlet.http.HttpServletRequest m_request
      Stores the HttpServletRequest.
      static java.lang.String MESSAGE
      JSPWiki wants to display a message.
      static java.lang.String NONE
      This is not a JSPWiki context, use it to access static files.
      static java.lang.String OTHER
      Same as NONE; this is just a clarification.
      static java.lang.String PREFS
      User is editing preferences
      static java.lang.String PREVIEW
      User is previewing the changes he just made.
      static java.lang.String RENAME
      User is renaming a page.
      static java.lang.String RSS
      RSS feed is being generated.
      static java.lang.String UPLOAD
      User is uploading something.
      static java.lang.String VIEW
      The VIEW context - the user just wants to view the page contents.
      static java.lang.String VIEW_GROUP
      User is viewing an existing group
      static java.lang.String WORKFLOW
      User wants to view or administer workflows.
    • Constructor Summary

      Constructors 
      Constructor Description
      WikiContext​(Engine engine, javax.servlet.http.HttpServletRequest request, java.lang.String requestContext)
      Creates a new WikiContext from a supplied HTTP request, using a default wiki context.
      WikiContext​(Engine engine, javax.servlet.http.HttpServletRequest request, Command command)
      Creates a new WikiContext for the given Engine, Command and HttpServletRequest.
      WikiContext​(Engine engine, javax.servlet.http.HttpServletRequest request, Page page)
      Creates a new WikiContext for the given Engine, WikiPage and HttpServletRequest.
      WikiContext​(Engine engine, Page page)
      Create a new WikiContext for the given WikiPage.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      WikiContext clone()
      Returns a shallow clone of the WikiContext.
      WikiContext deepClone()
      Creates a deep clone of the WikiContext.
      protected static Command findCommand​(Engine engine, javax.servlet.http.HttpServletRequest request, Page page)
      Looks up and returns a PageCommand based on a supplied WikiPage and HTTP request.
      static WikiContext findContext​(javax.servlet.jsp.PageContext pageContext)
      Deprecated.
      boolean getBooleanWikiProperty​(java.lang.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 WikiContext.
      java.lang.String getContentTemplate()
      Returns the content template associated with a Command, such as PreferencesContent.jsp.
      java.security.Principal getCurrentUser()
      Convenience method that gets the current user.
      WikiEngine getEngine()
      Returns the handling engine.
      java.lang.String getHttpParameter​(java.lang.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 an HTTP request, then the HTTP request can be fetched here.
      java.lang.String getJSP()
      Returns the JSP associated with the Command.
      java.lang.String getName()
      Returns the target of this wiki context: a page, group name or JSP.
      WikiPage getPage()
      Returns the page that is being handled.
      WikiPage getRealPage()
      Gets a reference to the real page whose content is currently being rendered.
      java.lang.String getRedirectURL()
      Figure out to which page we are really going to.
      java.lang.String getRequestContext()
      Returns the request context.
      java.lang.Object getTarget()
      Returns the target associated with a Command, if it was created with one.
      java.lang.String getTemplate()
      Gets the template that is to be used throughout this request.
      java.lang.String getURL​(java.lang.String context, java.lang.String page)
      Creates a URL for the given request context.
      java.lang.String getURL​(java.lang.String context, java.lang.String page, java.lang.String params)
      Returns a URL from a page.
      java.lang.String getURLPattern()
      Returns the URL pattern associated with this Command.
      <T> T getVariable​(java.lang.String key)
      Gets a previously set variable.
      java.lang.String getViewURL​(java.lang.String page)
      A shortcut to generate a VIEW url.
      WikiSession getWikiSession()
      Returns the Session associated with the context.
      boolean hasAdminPermissions()
      Returns true, if the current user has administrative permissions (i.e. the omnipotent AllPermission).
      java.security.Permission requiredPermission()
      Returns the permission required to successfully execute this context.
      protected void setDefaultTemplate​(javax.servlet.http.HttpServletRequest request)
      Figures out which template a new WikiContext should be using.
      void setPage​(Page page)
      Sets the page that is being handled.
      WikiPage setRealPage​(Page page)
      Sets a reference to the real page whose content is currently being rendered.
      void setRequestContext​(java.lang.String arg)
      Sets the request context.
      void setTemplate​(java.lang.String dir)
      Sets the template to be used for this request.
      void setVariable​(java.lang.String key, java.lang.Object data)
      Sets a variable.
      Command targetedCommand​(java.lang.Object target)
      Associates a target with the current Command and returns the new targeted Command.
      protected void updateCommand​(java.lang.String requestContext)
      Protected method that updates the internally cached Command.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_request

        protected javax.servlet.http.HttpServletRequest m_request
        Stores the HttpServletRequest. May be null, if the request did not come from a servlet.
      • ADMIN

        public static final java.lang.String ADMIN
        User is doing administrative things.
      • ATTACH

        public static final java.lang.String ATTACH
        User is downloading an attachment.
      • COMMENT

        public static final java.lang.String COMMENT
        User is commenting something.
      • CONFLICT

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

        public static final java.lang.String CREATE_GROUP
        User wishes to create a new group
      • DELETE

        public static final java.lang.String DELETE
        User is deleting a page or an attachment.
      • DELETE_GROUP

        public static final java.lang.String DELETE_GROUP
        User is deleting an existing group.
      • DIFF

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

        public static final java.lang.String EDIT
        The EDIT context - the user is editing the page.
      • EDIT_GROUP

        public static final java.lang.String EDIT_GROUP
        User is editing an existing group.
      • ERROR

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

        public static final java.lang.String FIND
        User is searching for content.
      • INFO

        public static final java.lang.String INFO
        User is viewing page history.
      • INSTALL

        public static final java.lang.String INSTALL
        User is administering JSPWiki (Install, SecurityConfig).
      • LOGIN

        public static final java.lang.String LOGIN
        User is preparing for a login/authentication.
      • LOGOUT

        public static final java.lang.String LOGOUT
        User is preparing to log out.
      • MESSAGE

        public static final java.lang.String MESSAGE
        JSPWiki wants to display a message.
      • NONE

        public static final java.lang.String NONE
        This is not a JSPWiki context, use it to access static files.
      • OTHER

        public static final java.lang.String OTHER
        Same as NONE; this is just a clarification.
      • PREFS

        public static final java.lang.String PREFS
        User is editing preferences
      • PREVIEW

        public static final java.lang.String PREVIEW
        User is previewing the changes he just made.
      • RENAME

        public static final java.lang.String RENAME
        User is renaming a page.
      • RSS

        public static final java.lang.String RSS
        RSS feed is being generated.
      • UPLOAD

        public static final java.lang.String UPLOAD
        User is uploading something.
      • VIEW

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

        public static final java.lang.String VIEW_GROUP
        User is viewing an existing group
      • WORKFLOW

        public static final java.lang.String WORKFLOW
        User wants to view or administer workflows.
    • Constructor Detail

      • WikiContext

        public WikiContext​(Engine engine,
                           Page page)
        Create a new WikiContext for the given WikiPage. Delegates to WikiContext(Engine, HttpServletRequest, Page).
        Parameters:
        engine - The Engine 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​(Engine engine,
                           javax.servlet.http.HttpServletRequest request,
                           Command command)
                    throws java.lang.IllegalArgumentException

        Creates a new WikiContext for the given Engine, Command and HttpServletRequest.

        This constructor will also look up the HttpSession associated with the request, and determine if a Session object is present. If not, a new one is created.

        Parameters:
        engine - The Engine that is handling the request
        request - The HttpServletRequest that should be associated with this context. This parameter may be null.
        command - the command
        Throws:
        java.lang.IllegalArgumentException - if engine or command are null
      • WikiContext

        public WikiContext​(Engine engine,
                           javax.servlet.http.HttpServletRequest request,
                           Page page)
        Creates a new WikiContext for the given Engine, WikiPage and HttpServletRequest. This method simply looks up the appropriate Command using findCommand(Engine, HttpServletRequest, Page) and delegates to WikiContext(Engine, HttpServletRequest, Command).
        Parameters:
        engine - The Engine 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
      • WikiContext

        public WikiContext​(Engine engine,
                           javax.servlet.http.HttpServletRequest request,
                           java.lang.String requestContext)
        Creates a new WikiContext from a supplied HTTP request, using a default wiki context.
        Parameters:
        engine - The Engine that is handling the request
        request - the HTTP request
        requestContext - the default context to use
        Since:
        2.1.15.
        See Also:
        CommandResolver, Command
    • Method Detail

      • getContentTemplate

        public java.lang.String getContentTemplate()
        Returns the content template associated with a Command, such as PreferencesContent.jsp. For Commands that are not page-related, this method will always return null. Calling methods should always check to see if the result of this method is null.
        Specified by:
        getContentTemplate in interface Command
        Returns:
        the content template
        See Also:
        Command.getContentTemplate()
      • getJSP

        public java.lang.String getJSP()
        Returns the JSP associated with the Command. The JSP is a "local" JSP within the JSPWiki webapp; it is not a general HTTP URL. If it exists, the JSP will be expressed relative to the webapp root, without a leading slash. This method is guaranteed to return a non-null result, although in some cases the result may be an empty string.
        Specified by:
        getJSP in interface Command
        Returns:
        the JSP or url associated with the wiki command
        See Also:
        Command.getJSP()
      • setRealPage

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

        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. 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 page added with InsertPageTag, you should consider what you want to do - do you wish to really reference the "master" page or the included page.

        Specified by:
        setRealPage in interface Context
        Parameters:
        page - The real page which is being rendered.
        Returns:
        The previous real page
        Since:
        2.3.14
        See Also:
        InsertPageTag
      • getRealPage

        public WikiPage getRealPage()
        Gets a reference to the real page 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 page added with InsertPageTag, you should consider what you want to do - do you wish to really reference the "master" page or the included page.

        For example, in the default template, there is a page called "LeftMenu". Whenever you access a page, e.g. "Main", the master page 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".

        Specified by:
        getRealPage in interface Context
        Returns:
        A reference to the real page.
        See Also:
        InsertPageTag, JSPWikiMarkupParser
      • getPage

        public WikiPage getPage()
        Returns the page that is being handled.
        Specified by:
        getPage in interface Context
        Returns:
        the page which was fetched.
      • setPage

        public void setPage​(Page page)
        Sets the page that is being handled.
        Specified by:
        setPage in interface Context
        Parameters:
        page - The wikipage
        Since:
        2.1.37.
      • setRequestContext

        public void setRequestContext​(java.lang.String arg)
        Sets the request context. See above for the different request contexts (VIEW, EDIT, etc.)
        Specified by:
        setRequestContext in interface Context
        Parameters:
        arg - The request context (one of the predefined contexts.)
      • getTarget

        public java.lang.Object getTarget()
        Returns the target associated with a Command, if it was created with one. Commands created with Command.targetedCommand(Object) will always return a non-null object. Calling methods should always check to see if the result of this method is null.
        Specified by:
        getTarget in interface Command
        Returns:
        the wiki page
        See Also:
        Command.getTarget()
      • getVariable

        public <T> T getVariable​(java.lang.String key)
        Gets a previously set variable.
        Specified by:
        getVariable in interface Context
        Parameters:
        key - The variable name.
        Returns:
        The variable contents.
      • setVariable

        public void setVariable​(java.lang.String key,
                                java.lang.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.
        Specified by:
        setVariable in interface Context
        Parameters:
        key - The variable name.
        data - The variable value.
      • getBooleanWikiProperty

        public boolean getBooleanWikiProperty​(java.lang.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.
        Specified by:
        getBooleanWikiProperty in interface Context
        Parameters:
        key - What key are we searching for?
        defValue - Default value for the boolean
        Returns:
        true or false.
      • getHttpParameter

        public java.lang.String getHttpParameter​(java.lang.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.
        Specified by:
        getHttpParameter in interface Context
        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 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!
        Specified by:
        getHttpRequest in interface Context
        Returns:
        Null, if no HTTP request was done.
        Since:
        2.0.13.
      • setTemplate

        public void setTemplate​(java.lang.String dir)
        Sets the template to be used for this request.
        Specified by:
        setTemplate in interface Context
        Parameters:
        dir - The template name
        Since:
        2.1.15.
      • getName

        public java.lang.String getName()
        Returns the target of this wiki context: a page, group name or JSP. If the associated Command is a PageCommand, this method returns the page's name. Otherwise, this method delegates to the associated Command's Command.getName() method. Calling classes can rely on the results of this method for looking up canonically-correct page or group names. Because it does not automatically assume that the wiki context is a PageCommand, calling this method is inherently safer than calling getPage().getName().
        Specified by:
        getName in interface Command
        Returns:
        the name of the target of this wiki context
        See Also:
        PageCommand.getName(), GroupCommand.getName()
      • getTemplate

        public java.lang.String getTemplate()
        Gets the template that is to be used throughout this request.
        Specified by:
        getTemplate in interface Context
        Returns:
        template name
        Since:
        2.1.15.
      • getCurrentUser

        public java.security.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.
        Specified by:
        getCurrentUser in interface Context
        Returns:
        The current user; or maybe null in case of internal calls.
      • getViewURL

        public java.lang.String getViewURL​(java.lang.String page)
        A shortcut to generate a VIEW url.
        Specified by:
        getViewURL in interface Context
        Parameters:
        page - The page to which to link.
        Returns:
        A URL to the page. This honours the current absolute/relative setting.
      • getURL

        public java.lang.String getURL​(java.lang.String context,
                                       java.lang.String page)
        Creates a URL for the given request context.
        Specified by:
        getURL in interface Context
        Parameters:
        context - e.g. WikiContext.EDIT
        page - The page to which to link
        Returns:
        A URL to the page, honours the absolute/relative setting in jspwiki.properties
      • getURL

        public java.lang.String getURL​(java.lang.String context,
                                       java.lang.String page,
                                       java.lang.String params)
        Returns a 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.
        Specified by:
        getURL in interface Context
        Parameters:
        context - The request context (e.g. WikiContext.UPLOAD)
        page - The page to which to link
        params - A list of parameters, separated with "&"
        Returns:
        A URL to the given context and page.
      • clone

        public WikiContext clone()
        Returns a shallow clone of the WikiContext.
        Specified by:
        clone in interface Context
        Overrides:
        clone in class java.lang.Object
        Returns:
        A shallow clone of the WikiContext
        Since:
        2.1.37.
      • deepClone

        public WikiContext deepClone()
        Creates a deep clone of the WikiContext. This is useful when you want to be sure that you don't accidentally mess with page attributes, etc.
        Specified by:
        deepClone in interface Context
        Returns:
        A deep clone of the WikiContext.
        Since:
        2.8.0
      • requiredPermission

        public java.security.Permission requiredPermission()
        Returns the permission required to successfully execute this context. For example, 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.
        Specified by:
        requiredPermission in interface Command
        Returns:
        the permission
        Since:
        2.4
      • targetedCommand

        public Command targetedCommand​(java.lang.Object target)
        Associates a target with the current Command and returns the new targeted Command. If the Command associated with this WikiContext is already "targeted", it is returned instead.
        Specified by:
        targetedCommand in interface Command
        Parameters:
        target - the object to combine, such as a GroupPrincipal or WikiPage
        Returns:
        the new, targeted Command
        See Also:
        Command.targetedCommand(java.lang.Object)
      • hasAdminPermissions

        public boolean hasAdminPermissions()
        Returns true, if the current user has administrative permissions (i.e. the omnipotent AllPermission).
        Specified by:
        hasAdminPermissions in interface Context
        Returns:
        true, if the user has all permissions.
        Since:
        2.4.46
      • setDefaultTemplate

        protected void setDefaultTemplate​(javax.servlet.http.HttpServletRequest request)
        Figures out which template a new WikiContext should be using.
        Parameters:
        request - the HTTP request
      • findCommand

        protected static Command findCommand​(Engine engine,
                                             javax.servlet.http.HttpServletRequest request,
                                             Page page)
        Looks up and returns a PageCommand based on a supplied WikiPage and HTTP request. First, the appropriate Command is obtained by examining the HTTP request; the default is ContextEnum.PAGE_VIEW. If the Command is a PageCommand (and it should be, in most cases), a targeted Command is created using the (non-null) WikiPage as target.
        Parameters:
        engine - the wiki engine
        request - the HTTP request
        page - the wiki page
        Returns:
        the correct command
      • updateCommand

        protected void updateCommand​(java.lang.String requestContext)
        Protected method that updates the internally cached Command. Will always be called when the page name, request context, or variable changes.
        Parameters:
        requestContext - the desired request context
        Since:
        2.4