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