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 return self.trans.request().remoteUser() 123 124 # Response-related methods. 125 126 def get_response_stream(self): 127 128 """ 129 A framework-specific method which returns the response stream for 130 the transaction. 131 """ 132 133 return self.trans.response() 134 135 def get_response_code(self): 136 137 """ 138 Get the response code associated with the transaction. If no response 139 code is defined, None is returned. 140 """ 141 142 # NOTE: Webware treats the response code as just another header. 143 144 status = self.trans.response().header("Status", None) 145 try: 146 if status is not None: 147 return int(status) 148 else: 149 return None 150 except ValueError: 151 return None 152 153 def set_response_code(self, response_code): 154 155 """ 156 Set the 'response_code' using a numeric constant defined in the HTTP 157 specification. 158 """ 159 160 self.trans.response().setStatus(response_code) 161 162 def set_content_type(self, content_type): 163 164 """ 165 A framework-specific method which sets the 'content_type' for the 166 response. 167 """ 168 169 return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) 170 171 # vim: tabstop=4 expandtab shiftwidth=4