1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html"> 5 <title>Treating the Path Like a Filesystem</title> 6 <meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/"> 7 <link href="styles.css" rel="stylesheet" type="text/css"> 8 </head> 9 10 <body> 11 <h1>Treating the Path Like a Filesystem</h1> 12 13 <p>...or as a reference into deeply categorized resources. In this approach, 14 we take a path like this...</p> 15 <pre>/documents/news/2005/article.html</pre> 16 17 <p>...and we consider <code>documents</code>, <code>news</code>, and 18 <code>2005</code> as directories, and <code>article.html</code> as a 19 file-like resource. If we ask for the following path...</p> 20 <pre>/documents/news/2005</pre> 21 22 <p>...we may decide to provide a listing of files within that directory, or 23 we may decide to refuse such a request. Indeed some approaches will insist 24 that such a listing may only be produced with the following path instead:</p> 25 <pre>/documents/news/2005/</pre> 26 27 <p>Applications of this kind are quite common since the publishing of files 28 on a Web server often just involves exposing parts of a real filesystem to 29 requests through the server.</p> 30 31 <h2>Resource Hierarchies in WebStack</h2> 32 33 <p>We might decide to represent components in these kinds of paths using 34 different resource classes, so that folders or directories are represented by 35 one kind of resource class and files or documents are represented by other 36 kinds of resource classes. We might then predefine a hierarchy of resources 37 so that when a request arrives for a resource, we can check it against the 38 hierarchy and process the request according to whichever type of resource is 39 being accessed.</p> 40 41 <p>Consider the above hierarchy; we would implement such a hierarchy with a 42 resource object mapped to <code>documents</code>, and that resource object 43 would contain a mapping of years to other resources. Eventually, at the 44 bottom of the hierarchy, individual resources would represent articles and be 45 mapped to names such as <code>article.html</code>.</p> 46 47 <div class="WebStack"> 48 <h3>WebStack API - Predefining Resource Hierarchies in Adapter Code</h3> 49 50 <p>WebStack provides a resource class for convenient mapping of path 51 components (ie. names) to resource objects: 52 <code>WebStack.Resources.ResourceMap.MapResource</code></p> 53 54 <p>This class can be used in adapter or "glue" code to initialise an 55 application as follows:</p> 56 <pre>from WebStack.Resources.ResourceMap import MapResource 57 article_resource = [some resource representing the article] 58 year_2004_resource = [a MapResource with definitions] 59 year_2005_resource = MapResource({"article.html" : article_resource}) 60 news_resource = MapResource({"2005" : year_2005_resource, "2004" : year_2004_resource}) 61 documents_resource = MapResource({"news" : news_resource}) 62 top_resource = MapResource({"documents" : documents_resource})</pre> 63 </div> 64 65 <p>Of course, predefining hierarchies is not the only way to support such 66 hierarchies. We could inspect paths and act dynamically on the supplied 67 information.</p> 68 </body> 69 </html>