1 #!/usr/bin/env python 2 3 """ 4 Webware classes. 5 """ 6 7 import Generic 8 9 class Transaction(Generic.Transaction): 10 11 """ 12 Webware transaction interface. 13 """ 14 15 def __init__(self, trans): 16 17 "Initialise the transaction using the Webware transaction 'trans'." 18 19 self.trans = trans 20 21 # Request-related methods. 22 23 def get_request_stream(self): 24 25 """ 26 A framework-specific method which returns the request stream for 27 the transaction. 28 """ 29 30 stream = self.trans.request().rawInput(rewind=1) 31 if stream is None: 32 return StringIO.StringIO("") 33 34 return stream 35 36 def get_request_method(self): 37 38 """ 39 A framework-specific method which gets the request method. 40 """ 41 42 return self.trans.request().method() 43 44 def get_headers(self): 45 46 """ 47 A framework-specific method which returns all request headers. 48 """ 49 50 # NOTE: Webware doesn't really provide access to headers in the request. 51 52 return {} 53 54 def get_header_values(self, key): 55 56 """ 57 A framework-specific method which returns a list of all request header 58 values associated with the given 'key'. Note that according to RFC 2616, 59 'key' is treated as a case-insensitive string. 60 """ 61 62 # NOTE: Webware doesn't really provide access to headers in the request. 63 64 return [] 65 66 def get_content_type(self): 67 68 """ 69 A framework-specific method which gets the content type specified on the 70 request, along with the charset employed. 71 """ 72 73 return self.parse_content_type(self.trans.request().contentType()) 74 75 def get_content_charsets(self): 76 77 """ 78 Returns the character set preferences. 79 NOTE: Requires enhancements to HTTPRequest. 80 """ 81 82 return self.trans.request().contentCharsets() 83 84 def get_content_languages(self): 85 86 """ 87 A framework-specific method which extracts language information from 88 the transaction. 89 NOTE: Requires enhancements to HTTPRequest. 90 """ 91 92 return self.trans.request().contentLanguages() 93 94 def get_path(self): 95 96 """ 97 A framework-specific method which gets the entire path from the request. 98 """ 99 100 return self.trans.request().uri() 101 102 def get_path_info(self): 103 104 """ 105 A framework-specific method which gets the "path info" (the part of the 106 URL after the resource name handling the current request) from the 107 request. 108 """ 109 110 # NOTE: Webware does not seem to prefix the "path info" in a way 111 # NOTE: consistent with the other frameworks. 112 113 return "/" + self.trans.request().pathInfo() 114 115 def get_query_string(self): 116 117 """ 118 A framework-specific method which gets the query string from the path in 119 the request. 120 """ 121 122 return self.trans.request().queryString() 123 124 # Higher level request-related methods. 125 126 def get_fields_from_path(self): 127 128 """ 129 A framework-specific method which extracts the form fields from the 130 path specified in the transaction. The underlying framework may refuse 131 to supply fields from the path if handling a POST transaction. 132 133 The returned object should employ the cgi.FieldStorage interface. 134 135 The result of this method is dependent on the WebUtils.FieldStorage 136 module's FieldStorage class. 137 """ 138 139 return self.trans.request().fields() 140 141 def get_fields_from_body(self): 142 143 """ 144 A framework-specific method which extracts the form fields from the 145 message body in the transaction. 146 147 The returned object should employ the cgi.FieldStorage interface. 148 149 The result of this method is dependent on the WebUtils.FieldStorage 150 module's FieldStorage class. 151 """ 152 153 return self.trans.request().fields() 154 155 def get_fields(self): 156 157 """ 158 A framework-specific method which extracts the form fields from the 159 transaction. Typically, the origin of the form fields will be affected 160 by the method specified in the transaction. 161 162 The returned object should employ the cgi.FieldStorage interface. 163 """ 164 165 return self.trans.request().fields() 166 167 def get_user(self): 168 169 """ 170 A framework-specific method which extracts user information from the 171 transaction. 172 """ 173 174 # NOTE: Webware relies entirely on a CGI-style environment where the 175 # NOTE: actual headers are not available. Therefore, the Web server must 176 # NOTE: itself be set up to provide user support. 177 178 try: 179 return self.trans.request().remoteUser() 180 except KeyError, exc: 181 return None 182 183 # Response-related methods. 184 185 def get_response_stream(self): 186 187 """ 188 A framework-specific method which returns the response stream for 189 the transaction. 190 """ 191 192 return self.trans.response() 193 194 def get_response_code(self): 195 196 """ 197 Get the response code associated with the transaction. If no response 198 code is defined, None is returned. 199 """ 200 201 # NOTE: Webware treats the response code as just another header. 202 203 status = self.trans.response().header("Status", None) 204 try: 205 if status is not None: 206 return int(status) 207 else: 208 return None 209 except ValueError: 210 return None 211 212 def set_response_code(self, response_code): 213 214 """ 215 Set the 'response_code' using a numeric constant defined in the HTTP 216 specification. 217 """ 218 219 self.trans.response().setStatus(response_code) 220 221 def set_header_value(self, header, value): 222 223 """ 224 Set the HTTP 'header' with the given 'value'. 225 """ 226 227 self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) 228 229 def set_content_type(self, content_type): 230 231 """ 232 A framework-specific method which sets the 'content_type' for the 233 response. 234 """ 235 236 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) 237 238 # vim: tabstop=4 expandtab shiftwidth=4