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 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 4 5 <title>Paths To and Within Applications</title><meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 6 <link href="styles.css" rel="stylesheet" type="text/css" /></head> 7 <body> 8 <h1>Paths To and 9 Within Applications</h1> 10 <p>One thing to be aware of in the 11 code of an application is which part 12 of 13 a 14 path refers to the location of the application in a server environment 15 and 16 which refers to some resource within the application itself. Consider 17 this 18 path:</p> 19 <pre>/folder/application/resource</pre> 20 <p>Let us say that the application 21 was deployed in a Zope server 22 instance 23 inside 24 <code>folder</code> 25 and with the name <code>application</code>. 26 We may 27 then 28 say that the path to the application is this: 29 </p> 30 <pre>/folder/application</pre> 31 <p>Meanwhile, the path within the 32 application is just this: 33 </p> 34 <pre>/resource</pre> 35 <p>In WebStack, we refer to this latter case - the path within the 36 application - as the "path info".</p> 37 <div class="WebStack"> 38 <h3>WebStack API - Paths To 39 Resources Within Applications</h3> 40 <p>On transaction objects, the 41 following methods exist to inspect paths 42 to 43 resources within applications.</p> 44 <dl> 45 <dt><code>get_path_info</code></dt> 46 <dd>This gets the path of a 47 resource within an application. The path should always contain a 48 leading <code>/</code> character at the very least.<br /> 49 An optional <code>encoding</code> parameter may be used to assist the process of converting the path to a Unicode object - see <a href="encodings.html">"Character Encodings"</a> for more information.</dd> 50 <dt><code>get_virtual_path_info</code></dt> 51 <dd>This gets the path of a 52 resource within a part of an application 53 - the application itself decides the scope of the path and can set the 54 "virtual path info" using the <code>set_virtual_path_info</code> 55 method. The path should either contain a leading <code>/</code> 56 character optionally followed by other characters, or an empty string.<br /> 57 58 An optional <code>encoding</code> parameter may be used to assist the process of converting the path to a Unicode object - see <a href="encodings.html">"Character Encodings"</a> for more information.</dd> 59 </dl> 60 </div> 61 <h2>Choosing the Right Path Value</h2> 62 <p>Given that the path may change depending on where an 63 application is deployed in a server environment, it may not be very 64 easy to use when determining which resources are being requested or 65 accessed within your application. Conversely, given that the "path 66 info" does not mention the full path to where the resources are, 67 it may be difficult to use that to provide references or links to those 68 resources. Here is a summary of how you might use the different path 69 values:</p> 70 <table style="text-align: left; width: 80%;" align="center" border="1" cellpadding="5" cellspacing="0" width="80%"> 71 <tbody> 72 <tr> 73 <th style="text-align: center;">Type of information</th> 74 <th style="text-align: center;">Possible uses</th> 75 </tr> 76 <tr> 77 <td align="undefined" valign="undefined">Path</td> 78 <td align="undefined" valign="undefined">Building links to 79 resources within an application - subtract the "path info" from 80 the end and you should get the location of the application.</td> 81 </tr> 82 <tr> 83 <td align="undefined" valign="undefined">Path info</td> 84 <td align="undefined" valign="undefined">Determining which 85 resources are being accessed within an application.</td> 86 </tr> 87 <tr> 88 <td align="undefined" valign="undefined">Virtual path info</td> 89 <td align="undefined" valign="undefined">This is an 90 application-defined version of "path info" and is discussed below.</td> 91 </tr> 92 </tbody> 93 </table> 94 <h2>Using the Virtual Path</h2> 95 <p>Although WebStack sets the "path info" so that applications 96 know which part of themselves are being accessed, you may decide 97 that upon 98 processing the request, these different parts of your application 99 should be 100 presented with different path information. For example, in a 101 hierarchical 102 structure of resources, each resource might use the first part of the 103 "path info" as an input to some kind of processing, but then have the 104 need to remove the 105 part they used, passing on a modified path to the other resources. For 106 such approaches, the "virtual path info" may be used instead, since it 107 permits modification within an application.</p> 108 <p>So starting with a virtual path like this (which would be the same 109 as the "path info")...</p> 110 <pre>/company/department/employee</pre> 111 <p>...a resource might extract <code>company</code> from the start 112 of the path as follows:</p> 113 <pre> # Inside a respond method...<br /> path = trans.get_virtual_path_info() # get the virtual path<br /> parts = path.split("/") # split the path into components - the first will be empty</pre> 114 <p>Then, having processed the first non-empty part (remembering that 115 the first part will be an empty string)...</p> 116 <pre> if len(parts) > 1: # check to see how deep we are in the path<br /> process_something(parts[1]) # process the first non-empty part</pre> 117 <p>...it will reconstruct the path, removing the processed part (but 118 remembering to preserve a leading <code>/</code> character)...</p> 119 <pre> trans.set_virtual_path_info("/" + "/".join(parts[2:]))</pre> 120 <p>...and hand over control to another resource which would do the same 121 thing with the first of the other path components (<code>department</code> 122 and <code>employee</code>), and so on.</p> 123 <p>The compelling thing about this strategy is the way that each 124 resource would only need to take the "virtual path info" into 125 consideration, and that each resource would believe that it is running 126 independently from any "parent" resource. Moreover, such resources 127 could be deployed independently and still operate in the same way 128 without being "hardcoded" into assuming that they always reside at a 129 particular level in a resource hierarchy.</p> 130 <div class="WebStack"> 131 <h3>WebStack API - Paths To 132 Resources Within Applications</h3> 133 <p>On transaction objects, the 134 following method exists to set virtual paths within applications.</p> 135 <dl> 136 <dt><code>set_virtual_path_info</code></dt> 137 <dd>This sets the virtual path, affecting subsequent calls to the <code>get_virtual_path_info</code> 138 method. The path should either contain a leading <code>/</code> 139 character optionally followed by other characters, or an empty string.</dd> 140 </dl> 141 </div> 142 </body></html>