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