Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The "Tapestry5HowToCreateADispatcher" page has been changed by LeonardLu. http://wiki.apache.org/tapestry/Tapestry5HowToCreateADispatcher?action=diff&rev1=4&rev2=5 -------------------------------------------------- http://tapestry.apache.org/tapestry5/guide/request.html - For dispatching and handling requests, Tapestry employs a chain of command implemented as the [http://tapestry.apache.org/tapestry5/guide/request.html |MasterDispatcher ] service. The framework uses Tapestry IoC to configure itself, and therefore has a [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html|TapestryModule]] (just like your apps have an "!AppModule"). Buried in this module is a method for configuring the !MasterDispatcher service: [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeMasterDispatcher%28org.apache.tapestry5.ioc.OrderedConfiguration%29|TapestryModule#contributeMasterDispatcher]]. I strongly recommend looking at the source of this file and especially at the contributions to the !MasterDispatcher, as that is how I learned what I am about to share. + For dispatching and handling requests, Tapestry employs a chain of command implemented as the [[http://tapestry.apache.org/tapestry5/guide/request.html|MasterDispatcher]] service. The framework uses Tapestry IoC to configure itself, and therefore has a [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html|TapestryModule]] (just like your apps have an "!AppModule"). Buried in this module is a method for configuring the !MasterDispatcher service: [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeMasterDispatcher%28org.apache.tapestry5.ioc.OrderedConfiguration%29|TapestryModule#contributeMasterDispatcher]]. I strongly recommend looking at the source of this file and especially at the contributions to the !MasterDispatcher, as that is how I learned what I am about to share. There are many reasons to intercept requests. Tapestry installs several dispatchers to intercept certain kinds of requests, in a certain order. The kind of configuration used by the !MasterDispatcher ([[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/ioc/OrderedConfiguration.html|OrderedConfiguration]]) allows us to insert a dispatcher anywhere in the chain, using the ''before:Id'' or ''after:Id'' syntax (again, read the source and api for more information on these). I had a personal interest in this because I work with applications that, like many, require a login and an access control mechanism. Logging in is easy, its implementing the access controls that embodies the real work. In the past I've worked with frameworks that have a roughly similar paradigm of pages, and the only way to implement access controls without coding logic into each controller (or page) class was to implement it in a common base class and subclass that. Well, times have changed. I don't like the fact that my pages know about access controls, it's none of their business. Tapestry 5 provides the infrastructure for a completely transparent access controller, and now we'll create a simple one to demonstrate the usefulness of both the configuration mechanism and the request processing pipeline in Tapestry 5. - When you look at the source of the [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeMasterDispatcher%28org.apache.tapestry5.ioc.OrderedConfiguration%29]] method, you'll notice several dispatchers get installed. The ids of them are: + When you look at the source of the [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeMasterDispatcher%28org.apache.tapestry5.ioc.OrderedConfiguration%29|contributeMasterDispatcher]] method, you'll notice several dispatchers get installed. The ids of them are: * !RootPath ([[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/internal/services/RootPathDispatcher.html|RootPathDispatcher]]) - Responsible for rendering the Start page when the app root is requested. * Asset ([[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/internal/services/AssetDispatcher.html|AssetDispatcher]]) - Handles serving assets from the classpath, context path, etc. @@ -18, +18 @@ These dispatchers are added and executed in this order. While it's certainly possible you might want to restrict other types of requests, I'd wager that most of the time developers need to protect pages, or entire sections of pages, from being accessed by unauthorized users. - Now that you have a decent understanding of what dispatchers are and how they work, you should realize that implementing a completely unobtrusive access control system is as easy as implementing a [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry/services/Dispatcher.html|Dispatcher]]. That part I assure you, is easy. The internal architecture of your system may very will be quite complex, but hooking it into Tapestry 5 is cake, and this is how you do it in a nutshell: + Now that you have a decent understanding of what dispatchers are and how they work, you should realize that implementing a completely unobtrusive access control system is as easy as implementing a [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/Dispatcher.html|Dispatcher]]. That part I assure you, is easy. The internal architecture of your system may very will be quite complex, but hooking it into Tapestry 5 is cake, and this is how you do it in a nutshell: == 1) Implement a Dispatcher == - This is a wonderfully brief interface with one method to implement: [http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/Dispatcher.html#dispatch%28org.apache.tapestry5.services.Request,%20org.apache.tapestry5.services.Response%29|dispatch]]. As expected, looking at the api/source for this interface, as well as some of the implementations will be helpful. For implementing an access controller, its essentially as easy as returning false or throwing an exception. Here's summary of the possible outcomes of this method: + This is a wonderfully brief interface with one method to implement: [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/services/Dispatcher.html#dispatch%28org.apache.tapestry5.services.Request,%20org.apache.tapestry5.services.Response%29|dispatch]]. As expected, looking at the api/source for this interface, as well as some of the implementations will be helpful. For implementing an access controller, its essentially as easy as returning false or throwing an exception. Here's summary of the possible outcomes of this method: * Return true. This will tell Tapestry that the request has been handled and no other dispatcher in the chain need be consulted. If you return true from your access controller dispatcher, you're likely to get a blank response (and a blank page). This is because "handling" the request also means returning a response, like a page. So for an access controller, you won't do this. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
