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