org.apache.wiki.ui
Class CommandResolver

java.lang.Object
  extended by org.apache.wiki.ui.CommandResolver

public final class CommandResolver
extends Object

Resolves special pages, JSPs and Commands on behalf of a WikiEngine. CommandResolver will automatically resolve page names with singular/plural variants. It can also detect the correct Command based on parameters supplied in an HTTP request, or due to the JSP being accessed.

CommandResolver's static findCommand(String) method is the simplest method; it looks up and returns the Command matching a supplied wiki context. For example, looking up the request context view returns PageCommand.VIEW. Use this method to obtain static Command instances that aren't targeted at a particular page or group.

For more complex lookups in which the caller supplies an HTTP request, findCommand(HttpServletRequest, String) will look up and return the correct Command. The String parameter defaultContext supplies the request context to use if it cannot be detected. However, note that the default wiki context may be over-ridden if the request was for a "special page."

For example, suppose the WikiEngine's properties specify a special page called UserPrefs that redirects to UserPreferences.jsp. The ordinary lookup method findCommand(String) using a supplied context view would return PageCommand.VIEW. But the findCommand(HttpServletRequest, String) method, when passed the same context (view) and an HTTP request containing the page parameter value UserPrefs, will instead return WikiCommand.PREFS.

Since:
2.4.22

Constructor Summary
CommandResolver(WikiEngine engine, Properties properties)
          Constructs a CommandResolver for a given WikiEngine.
 
Method Summary
protected  Command extractCommandFromPath(javax.servlet.http.HttpServletRequest request)
          Extracts a Command based on the JSP path of an HTTP request.
protected  String extractPageFromParameter(String requestContext, javax.servlet.http.HttpServletRequest request)
          Determines the correct wiki page based on a supplied request context and HTTP request.
 Command findCommand(javax.servlet.http.HttpServletRequest request, String defaultContext)
           Attempts to locate a Command for a supplied wiki context and HTTP request, incorporating the correct WikiPage into the command if reqiured.
static Command findCommand(String context)
          Attempts to locate a wiki command for a supplied request context.
 String getFinalPageName(String page)
           Returns the correct page name, or null, if no such page can be found.
 String getSpecialPageReference(String page)
           If the page is a special page, this method returns a direct URL to that page; otherwise, it returns null.
protected  WikiPage resolvePage(javax.servlet.http.HttpServletRequest request, String page)
          Looks up and returns the correct, versioned WikiPage based on a supplied page name and optional version parameter passed in an HTTP request.
protected  boolean simplePageExists(String page)
          Determines whether a "page" exists by examining the list of special pages and querying the page manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandResolver

public CommandResolver(WikiEngine engine,
                       Properties properties)
Constructs a CommandResolver for a given WikiEngine. This constructor will extract the special page references for this wiki and store them in a cache used for resolution.

Parameters:
engine - the wiki engine
properties - the properties used to initialize the wiki
Method Detail

findCommand

public static Command findCommand(String context)
Attempts to locate a wiki command for a supplied request context. The resolution technique is simple: we examine the list of Commands returned by AbstractCommand.allCommands() and return the one whose requestContext matches the supplied context. If the supplied context does not resolve to a known Command, this method throws an IllegalArgumentException.

Parameters:
context - the request context
Returns:
the resolved context

findCommand

public Command findCommand(javax.servlet.http.HttpServletRequest request,
                           String defaultContext)

Attempts to locate a Command for a supplied wiki context and HTTP request, incorporating the correct WikiPage into the command if reqiured. This method will first determine what page the user requested by delegating to extractPageFromParameter(String, HttpServletRequest). If this page equates to a special page, we return the Command corresponding to that page. Otherwise, this method simply returns the Command for the supplied request context.

The reason this method attempts to resolve against special pages is because some of them resolve to contexts that may be different from the one supplied. For example, a VIEW request context for the special page "UserPreferences" should return a PREFS context instead.

When the caller supplies a request context and HTTP request that specifies an actual wiki page (rather than a special page), this method will return a "targeted" Command that includes the resolved WikiPage as the target. (See resolvePage(HttpServletRequest, String) for the resolution algorithm). Specifically, the Command will return a non-null value for its AbstractCommand.getTarget() method.

Note: if this method determines that the Command is the VIEW PageCommand, then the Command returned will always be targeted to the front page.

Parameters:
request - the HTTP request; if null, delegates to findCommand(String)
defaultContext - the request context to use by default
Returns:
the resolved wiki command

getFinalPageName

public String getFinalPageName(String page)
                        throws ProviderException

Returns the correct page name, or null, if no such page can be found. Aliases are considered.

In some cases, page names can refer to other pages. For example, when you have matchEnglishPlurals set, then a page name "Foobars" will be transformed into "Foobar", should a page "Foobars" not exist, but the page "Foobar" would. This method gives you the correct page name to refer to.

This facility can also be used to rewrite any page name, for example, by using aliases. It can also be used to check the existence of any page.

Parameters:
page - the page name.
Returns:
The rewritten page name, or null, if the page does not exist.
Throws:
ProviderException - if the underlyng page provider that locates pages throws an exception
Since:
2.4.20

getSpecialPageReference

public String getSpecialPageReference(String page)

If the page is a special page, this method returns a direct URL to that page; otherwise, it returns null.

Special pages are non-existant references to other pages. For example, you could define a special page reference "RecentChanges" which would always be redirected to "RecentChanges.jsp" instead of trying to find a Wiki page called "RecentChanges".

Parameters:
page - the page name ro search for
Returns:
the URL of the special page, if the supplied page is one, or null

extractCommandFromPath

protected Command extractCommandFromPath(javax.servlet.http.HttpServletRequest request)
Extracts a Command based on the JSP path of an HTTP request. If the JSP requested matches a Command's getJSP() value, that Command is returned.

Parameters:
request - the HTTP request
Returns:
the resolved Command, or null if not found

extractPageFromParameter

protected String extractPageFromParameter(String requestContext,
                                          javax.servlet.http.HttpServletRequest request)
Determines the correct wiki page based on a supplied request context and HTTP request. This method attempts to determine the page requested by a user, taking into acccount special pages. The resolution algorithm will:

Parameters:
requestContext - the request context
request - the HTTP request
Returns:
the resolved page name

resolvePage

protected WikiPage resolvePage(javax.servlet.http.HttpServletRequest request,
                               String page)
Looks up and returns the correct, versioned WikiPage based on a supplied page name and optional version parameter passed in an HTTP request. If the version parameter does not exist in the request, the latest version is returned.

Parameters:
request - the HTTP request
page - the name of the page to look up; this page must exist
Returns:
the wiki page

simplePageExists

protected boolean simplePageExists(String page)
                            throws ProviderException
Determines whether a "page" exists by examining the list of special pages and querying the page manager.

Parameters:
page - the page to seek
Returns:
true if the page exists, false otherwise
Throws:
ProviderException - if the underlyng page provider that locates pages throws an exception


Copyright © {inceptionYear}-2014 The Apache Software Foundation. All rights reserved.