1 <?xml version="1.0" encoding="iso-8859-1"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"><head> 4 <title>Treating the Path Like a Filesystem</title> 5 <link href="styles.css" rel="stylesheet" type="text/css" /></head> 6 <body> 7 <h1>Treating the Path Like a Filesystem</h1> 8 <p>...or as a reference into deeply categorized resources. In this approach, 9 we take a path like this...</p> 10 <pre>/documents/news/2005/article.html</pre> 11 <p>...and we consider <code>documents</code>, <code>news</code>, and 12 <code>2005</code> as directories, and <code>article.html</code> as a 13 file-like resource. If we ask for the following path...</p> 14 <pre>/documents/news/2005</pre> 15 <p>...we may decide to provide a listing of files within that directory, or 16 we may decide to refuse such a request. Indeed some kinds of applications insist 17 that such a listing may only be produced with the following path instead:</p> 18 <pre>/documents/news/2005/</pre> 19 <p>Applications of this kind are quite common since the publishing of files 20 on a Web server often just involves exposing parts of a real filesystem to 21 requests through the server.</p> 22 <h2>Resource Hierarchies in WebStack</h2> 23 <p>There are a number of different ways that paths can be interpreted and handled in WebStack 24 applications, including...</p> 25 <ul> 26 <li>Using predefined hierarchies of resources.</li> 27 <li>By inspecting the path in a top-level resource and then creating resources to deal with different 28 cases.</li> 29 <li>By handling all kinds of paths in the same resource.</li> 30 </ul> 31 <h3>Predefining Resource Hierarchies</h3> 32 <p>We might decide to represent components in these kinds of paths using 33 different resource classes; for example:</p> 34 <ul> 35 <li>Folders or directories are represented by a special resource class which contains other 36 folders and possibly some files.</li> 37 <li>Files or documents are represented by special resource classes which provide access 38 to the content of such files.</li> 39 </ul> 40 <p>We might then predefine a hierarchy of resources 41 so that when a request arrives for a resource, we can check it against the 42 hierarchy and process the request according to whichever type of resource is 43 being accessed. For example:</p> 44 <ul> 45 <li><code>documents</code> 46 <ul> 47 <li><code>news</code> 48 <ul> 49 <li><code>2005</code> 50 <ul> 51 <li><code>article.html</code></li> 52 <li><code>another.html</code></li> 53 </ul> 54 </li> 55 <li><code>2004</code> 56 <ul> 57 <li><code>document.html</code></li> 58 </ul> 59 </li> 60 </ul> 61 </li> 62 </ul> 63 </li> 64 </ul> 65 <p>Consider the above hierarchy; we would implement such a hierarchy with a 66 resource object mapped to <code>documents</code>, and that resource object 67 would contain a mapping of years to other resources. Eventually, at the 68 bottom of the hierarchy, individual resources would represent articles 69 and be mapped to names such as <code>article.html</code>.</p> 70 <div class="WebStack"> 71 <h3>WebStack API - Predefining Resource Hierarchies in Adapter Code</h3> 72 73 <p>WebStack provides the <code>MapResource</code> 74 class (in the <code>WebStack.Resources.ResourceMap</code> module) for convenient mapping of path 75 components to resource objects<code></code>. 76 See the <a href="resource-map.html">"ResourceMap - Simple Mappings from Names to Resources"</a> 77 document for a more detailed description of the <code>MapResource</code> class.</p> 78 79 <p>This class can be used in <a href="deploying.html">adapter code</a> 80 to initialise an application as follows:</p> 81 <pre>from WebStack.Resources.ResourceMap import MapResource<br />from MyApplication import FileResource # import some resource class<br /><br />article_resource = FileResource(...) # make a resource representing the article<br />document_resource = FileResource(...) # make a resource representing the document<br />year_2004_resource = MapResource({"document.html" : document_resource})<br />year_2005_resource = MapResource({"article.html" : article_resource})<br />news_resource = MapResource({"2005" : year_2005_resource, "2004" : year_2004_resource})<br />documents_resource = MapResource({"news" : news_resource})<br />top_resource = MapResource({"documents" : documents_resource})</pre> 82 </div> 83 <p>Of course, predefining resource 84 objects is not the only way to support such 85 hierarchies. We could inspect paths and act dynamically on the supplied 86 information, either choosing to create resources or choosing to handle 87 such paths in the same resource.</p> 88 </body></html>