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