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