Interface Context

  • All Superinterfaces:
    java.lang.Cloneable, Command
    All Known Implementing Classes:
    WikiContext

    public interface Context
    extends java.lang.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()!

    See Also:
    Counter
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ATTR_CONTEXT  
      static java.lang.String VAR_EXECUTE_PLUGINS
      Variable name which tells whether plugins should be executed or not.
      static java.lang.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

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      Context clone()
      Context deepClone()
      Creates a deep clone of the Context.
      static Context findContext​(javax.servlet.jsp.PageContext pageContext)
      This method can be used to find the WikiContext programmatically from a JSP PageContext.
      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 Context.
      java.security.Principal getCurrentUser()
      Convenience method that gets the current user.
      Engine 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 a 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.
      java.lang.String getRedirectURL()
      Figure out to which WikiPage we are really going to.
      java.lang.String getTemplate()
      Gets the template that is to be used throughout this request.
      default java.lang.String getURL​(java.lang.String context, java.lang.String page)
      Creates an URL for the given request context.
      default java.lang.String getURL​(java.lang.String context, java.lang.String page, java.lang.String params)
      Returns an URL from a page.
      <T> T getVariable​(java.lang.String key)
      Gets a previously set variable.
      java.lang.String getViewURL​(java.lang.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​(java.lang.String context)
      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.
    • Method Detail

      • 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.
        See Also:
        InsertPageTag, JSPWikiMarkupParser
      • 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
        See Also:
        InsertPageTag
      • getEngine

        Engine getEngine()
        Returns the handling engine.
        Returns:
        The wikiengine owning this context.
      • setRequestContext

        void setRequestContext​(java.lang.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​(java.lang.String key)
        Gets a previously set variable.
        Parameters:
        key - The variable name.
        Returns:
        The variable contents.
      • setVariable

        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 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​(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.
        Parameters:
        key - What key are we searching for?
        defValue - Default value for the boolean
        Returns:
        true or false.
      • getHttpParameter

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

        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

        void setTemplate​(java.lang.String dir)
        Sets the template to be used for this request.
        Parameters:
        dir - The template name
        Since:
        2.1.15.
      • getTemplate

        java.lang.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

        java.security.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

        java.lang.String getViewURL​(java.lang.String WikiPage)
        A shortcut to generate a VIEW url.
        Parameters:
        WikiPage - The WikiPage to which to link.
        Returns:
        An URL to the page. This honours the current absolute/relative setting.
      • getRedirectURL

        java.lang.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 java.lang.String getURL​(java.lang.String context,
                                        java.lang.String page)
        Creates an URL for the given request context.
        Parameters:
        context - e.g. WikiContext.EDIT
        page - The WikiPage to which to link
        Returns:
        An URL to the page.
      • getURL

        default java.lang.String getURL​(java.lang.String context,
                                        java.lang.String page,
                                        java.lang.String params)
        Returns an 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:
        An URL to the given context and page.
      • 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​(javax.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