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