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