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><dt><code>get_processed_virtual_path_info</code></dt> 59 <dd>This gets the virtual path information which is considered to 60 have been processed or traversed, and consists of the part of the path 61 info which does not appear in the virtual path info. In other words, 62 when components at the start of the virtual path info are removed, such 63 components will appear at the end of the processed virtual path info.<br /> 64 65 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> 66 67 </dl> 68 </div> 69 <h2>Choosing the Right Path Value</h2> 70 <p>Given that the path may change depending on where an 71 application is deployed in a server environment, it may not be very 72 easy to use when determining which resources are being requested or 73 accessed within your application. Conversely, given that the "path 74 info" does not mention the full path to where the resources are, 75 it may be difficult to use that to provide references or links to those 76 resources. Here is a summary of how you might use the different path 77 values:</p> 78 <table style="text-align: left; width: 80%;" align="center" border="1" cellpadding="5" cellspacing="0" width="80%"> 79 <tbody> 80 <tr> 81 <th style="text-align: center;">Type of information</th> 82 <th style="text-align: center;">Possible uses</th> 83 </tr> 84 <tr> 85 <td align="undefined" valign="undefined">Path</td> 86 <td align="undefined" valign="undefined">Building links to 87 resources within an application - subtract the "path info" from 88 the end and you should get the location of the application.</td> 89 </tr> 90 <tr> 91 <td align="undefined" valign="undefined">Path info</td> 92 <td align="undefined" valign="undefined">Determining which 93 resources are being accessed within an application.</td> 94 </tr> 95 <tr> 96 <td align="undefined" valign="undefined">Virtual path info</td> 97 <td align="undefined" valign="undefined">This is an 98 application-defined version of "path info" and is discussed below.</td> 99 </tr> 100 </tbody> 101 </table> 102 <h2>Using the Virtual Path</h2> 103 <p>Although WebStack sets the "path info" so that applications 104 know which part of themselves are being accessed, you may decide 105 that upon 106 processing the request, these different parts of your application 107 should be 108 presented with different path information. For example, in a 109 hierarchical 110 structure of resources, each resource might use the first part of the 111 "path info" as an input to some kind of processing, but then have the 112 need to remove the 113 part they used, passing on a modified path to the other resources. For 114 such approaches, the "virtual path info" may be used instead, since it 115 permits modification within an application.</p> 116 <p>So starting with a virtual path like this (which would be the same 117 as the "path info")...</p> 118 <pre>/company/department/employee</pre> 119 <p>...a resource might extract <code>company</code> from the start 120 of the path as follows:</p> 121 <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> 122 <p>Then, having processed the first non-empty part (remembering that 123 the first part will be an empty string)...</p> 124 <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> 125 <p>...it will reconstruct the path, removing the processed part (but 126 remembering to preserve a leading <code>/</code> character)...</p> 127 <pre> trans.set_virtual_path_info("/" + "/".join(parts[2:]))</pre> 128 <p>...and hand over control to another resource which would do the same 129 thing with the first of the other path components (<code>department</code> 130 and <code>employee</code>), and so on.</p> 131 <p>The compelling thing about this strategy is the way that each 132 resource would only need to take the "virtual path info" into 133 consideration, and that each resource would believe that it is running 134 independently from any "parent" resource. Moreover, such resources 135 could be deployed independently and still operate in the same way 136 without being "hardcoded" into assuming that they always reside at a 137 particular level in a resource hierarchy.</p> 138 <div class="WebStack"> 139 <h3>WebStack API - Paths To 140 Resources Within Applications</h3> 141 <p>On transaction objects, the 142 following method exists to set virtual paths within applications.</p> 143 <dl> 144 <dt><code>set_virtual_path_info</code></dt> 145 <dd>This sets the virtual path, affecting subsequent calls to the <code>get_virtual_path_info</code> 146 method. The path should either contain a leading <code>/</code> 147 character optionally followed by other characters, or an empty string.</dd> 148 </dl> 149 </div> 150 </body></html>