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 # Higher level request-related methods. 116 117 def get_fields(self): 118 119 """ 120 A framework-specific method which extracts the form fields from the 121 transaction. 122 """ 123 124 return self.trans.request().fields() 125 126 def get_user(self): 127 128 """ 129 A framework-specific method which extracts user information from the 130 transaction. 131 """ 132 133 # NOTE: Webware relies entirely on a CGI-style environment where the 134 # NOTE: actual headers are not available. Therefore, the Web server must 135 # NOTE: itself be set up to provide user support. 136 137 try: 138 return self.trans.request().remoteUser() 139 except KeyError, exc: 140 return None 141 142 # Response-related methods. 143 144 def get_response_stream(self): 145 146 """ 147 A framework-specific method which returns the response stream for 148 the transaction. 149 """ 150 151 return self.trans.response() 152 153 def get_response_code(self): 154 155 """ 156 Get the response code associated with the transaction. If no response 157 code is defined, None is returned. 158 """ 159 160 # NOTE: Webware treats the response code as just another header. 161 162 status = self.trans.response().header("Status", None) 163 try: 164 if status is not None: 165 return int(status) 166 else: 167 return None 168 except ValueError: 169 return None 170 171 def set_response_code(self, response_code): 172 173 """ 174 Set the 'response_code' using a numeric constant defined in the HTTP 175 specification. 176 """ 177 178 self.trans.response().setStatus(response_code) 179 180 def set_header(self, header, value): 181 182 """ 183 Set the HTTP 'header' with the given 'value'. 184 """ 185 186 self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) 187 188 def set_content_type(self, content_type): 189 190 """ 191 A framework-specific method which sets the 'content_type' for the 192 response. 193 """ 194 195 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) 196 197 # vim: tabstop=4 expandtab shiftwidth=4