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 the request headers. 48 NOTE: Experimental, since framework support varies somewhat. 49 """ 50 51 # NOTE: Webware doesn't really provide access to headers in the request. 52 53 return {} 54 55 def get_content_type(self): 56 57 """ 58 A framework-specific method which gets the content type specified on the 59 request, along with the charset employed. 60 """ 61 62 return self.parse_content_type(self.trans.request().contentType()) 63 64 def get_content_charsets(self): 65 66 """ 67 Returns the character set preferences. 68 NOTE: Requires enhancements to HTTPRequest. 69 """ 70 71 return self.trans.request().contentCharsets() 72 73 def get_content_languages(self): 74 75 """ 76 A framework-specific method which extracts language information from 77 the transaction. 78 NOTE: Requires enhancements to HTTPRequest. 79 """ 80 81 return self.trans.request().contentLanguages() 82 83 def get_path(self): 84 85 """ 86 A framework-specific method which gets the entire path from the request. 87 """ 88 89 return self.trans.request().uri() 90 91 def get_path_info(self): 92 93 """ 94 A framework-specific method which gets the "path info" (the part of the 95 URL after the resource name handling the current request) from the 96 request. 97 """ 98 99 # NOTE: Webware does not seem to prefix the "path info" in a way 100 # NOTE: consistent with the other frameworks. 101 102 return "/" + self.trans.request().pathInfo() 103 104 # Higher level request-related methods. 105 106 def get_fields(self): 107 108 """ 109 A framework-specific method which extracts the form fields from the 110 transaction. 111 """ 112 113 return self.trans.request().fields() 114 115 def get_user(self): 116 117 """ 118 A framework-specific method which extracts user information from the 119 transaction. 120 """ 121 122 # NOTE: Webware relies entirely on a CGI-style environment where the 123 # NOTE: actual headers are not available. Therefore, the Web server must 124 # NOTE: itself be set up to provide user support. 125 126 try: 127 return self.trans.request().remoteUser() 128 except KeyError, exc: 129 return None 130 131 # Response-related methods. 132 133 def get_response_stream(self): 134 135 """ 136 A framework-specific method which returns the response stream for 137 the transaction. 138 """ 139 140 return self.trans.response() 141 142 def get_response_code(self): 143 144 """ 145 Get the response code associated with the transaction. If no response 146 code is defined, None is returned. 147 """ 148 149 # NOTE: Webware treats the response code as just another header. 150 151 status = self.trans.response().header("Status", None) 152 try: 153 if status is not None: 154 return int(status) 155 else: 156 return None 157 except ValueError: 158 return None 159 160 def set_response_code(self, response_code): 161 162 """ 163 Set the 'response_code' using a numeric constant defined in the HTTP 164 specification. 165 """ 166 167 self.trans.response().setStatus(response_code) 168 169 def set_header(self, header, value): 170 171 """ 172 Set the HTTP 'header' with the given 'value'. 173 """ 174 175 self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) 176 177 def set_content_type(self, content_type): 178 179 """ 180 A framework-specific method which sets the 'content_type' for the 181 response. 182 """ 183 184 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) 185 186 # vim: tabstop=4 expandtab shiftwidth=4