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