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