paulb@91 | 1 | #!/usr/bin/env python |
paulb@91 | 2 | |
paulb@91 | 3 | """ |
paulb@91 | 4 | Java Servlet classes. |
paulb@91 | 5 | """ |
paulb@91 | 6 | |
paulb@91 | 7 | import Generic |
paulb@91 | 8 | from StringIO import StringIO |
paulb@91 | 9 | |
paulb@91 | 10 | class Stream: |
paulb@91 | 11 | |
paulb@91 | 12 | """ |
paulb@91 | 13 | Wrapper around java.io.BufferedReader. |
paulb@91 | 14 | """ |
paulb@91 | 15 | |
paulb@91 | 16 | def __init__(self, stream): |
paulb@91 | 17 | |
paulb@91 | 18 | "Initialise the stream with the given underlying 'stream'." |
paulb@91 | 19 | |
paulb@91 | 20 | self.stream = stream |
paulb@91 | 21 | |
paulb@91 | 22 | def read(self): |
paulb@91 | 23 | |
paulb@91 | 24 | "Read the entire message, returning it as a string." |
paulb@91 | 25 | |
paulb@91 | 26 | characters = StringIO() |
paulb@91 | 27 | while 1: |
paulb@91 | 28 | c = self.stream.read() |
paulb@91 | 29 | if c == -1: |
paulb@91 | 30 | return characters.getvalue() |
paulb@91 | 31 | else: |
paulb@91 | 32 | characters.write(chr(c)) |
paulb@91 | 33 | |
paulb@91 | 34 | def readline(self): |
paulb@91 | 35 | |
paulb@91 | 36 | "Read a line from the stream, returning it as a string." |
paulb@91 | 37 | |
paulb@91 | 38 | return self.stream.readLine() |
paulb@91 | 39 | |
paulb@91 | 40 | class Transaction(Generic.Transaction): |
paulb@91 | 41 | |
paulb@91 | 42 | """ |
paulb@91 | 43 | Java Servlet transaction interface. |
paulb@91 | 44 | """ |
paulb@91 | 45 | |
paulb@91 | 46 | def __init__(self, request, response): |
paulb@91 | 47 | |
paulb@91 | 48 | """ |
paulb@91 | 49 | Initialise the transaction using the Java Servlet HTTP 'request' and |
paulb@91 | 50 | 'response'. |
paulb@91 | 51 | """ |
paulb@91 | 52 | |
paulb@91 | 53 | self.request = request |
paulb@91 | 54 | self.response = response |
paulb@91 | 55 | self.status = None |
paulb@91 | 56 | |
paulb@95 | 57 | def commit(self): |
paulb@95 | 58 | |
paulb@95 | 59 | """ |
paulb@95 | 60 | A special method, synchronising the transaction with framework-specific |
paulb@95 | 61 | objects. |
paulb@95 | 62 | """ |
paulb@95 | 63 | |
paulb@95 | 64 | self.get_response_stream().close() |
paulb@95 | 65 | |
paulb@91 | 66 | # Request-related methods. |
paulb@91 | 67 | |
paulb@91 | 68 | def get_request_stream(self): |
paulb@91 | 69 | |
paulb@91 | 70 | """ |
paulb@91 | 71 | A framework-specific method which returns the request stream for |
paulb@91 | 72 | the transaction. |
paulb@91 | 73 | """ |
paulb@91 | 74 | |
paulb@91 | 75 | return Stream(self.request.getReader()) |
paulb@91 | 76 | |
paulb@91 | 77 | def get_request_method(self): |
paulb@91 | 78 | |
paulb@91 | 79 | """ |
paulb@91 | 80 | A framework-specific method which gets the request method. |
paulb@91 | 81 | """ |
paulb@91 | 82 | |
paulb@91 | 83 | return self.request.getMethod() |
paulb@91 | 84 | |
paulb@91 | 85 | def get_headers(self): |
paulb@91 | 86 | |
paulb@91 | 87 | """ |
paulb@91 | 88 | A framework-specific method which returns all request headers. |
paulb@91 | 89 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 90 | NOTE: need to change. |
paulb@91 | 91 | """ |
paulb@91 | 92 | |
paulb@91 | 93 | headers = {} |
paulb@91 | 94 | header_names = self.request.getHeaderNames() |
paulb@91 | 95 | if header_names: |
paulb@91 | 96 | for header_name in header_names: |
paulb@91 | 97 | |
paulb@91 | 98 | # NOTE: Retrieve only a single value (not using getHeaders). |
paulb@91 | 99 | |
paulb@91 | 100 | headers[header_name] = self.request.getHeader(header_name) |
paulb@91 | 101 | |
paulb@91 | 102 | return headers |
paulb@91 | 103 | |
paulb@91 | 104 | def get_header_values(self, key): |
paulb@91 | 105 | |
paulb@91 | 106 | """ |
paulb@91 | 107 | A framework-specific method which returns a list of all request header |
paulb@91 | 108 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@91 | 109 | 'key' is treated as a case-insensitive string. |
paulb@91 | 110 | """ |
paulb@91 | 111 | |
paulb@91 | 112 | return self.request.getHeaders(key) |
paulb@91 | 113 | |
paulb@91 | 114 | def get_content_type(self): |
paulb@91 | 115 | |
paulb@91 | 116 | """ |
paulb@91 | 117 | A framework-specific method which gets the content type specified on the |
paulb@91 | 118 | request, along with the charset employed. |
paulb@91 | 119 | """ |
paulb@91 | 120 | |
paulb@91 | 121 | content_types = self.get_header_values("Content-Type") |
paulb@91 | 122 | if len(content_types) >= 1: |
paulb@91 | 123 | return self.parse_content_type(content_types[0]) |
paulb@91 | 124 | else: |
paulb@91 | 125 | return None |
paulb@91 | 126 | |
paulb@91 | 127 | def get_content_charsets(self): |
paulb@91 | 128 | |
paulb@91 | 129 | """ |
paulb@91 | 130 | Returns the character set preferences. |
paulb@91 | 131 | """ |
paulb@91 | 132 | |
paulb@91 | 133 | accept_charsets = self.get_header_values("Accept-Charset") |
paulb@91 | 134 | if len(accept_charsets) >= 1: |
paulb@91 | 135 | return self.parse_content_preferences(accept_charsets[0]) |
paulb@91 | 136 | else: |
paulb@91 | 137 | return None |
paulb@91 | 138 | |
paulb@91 | 139 | def get_content_languages(self): |
paulb@91 | 140 | |
paulb@91 | 141 | """ |
paulb@91 | 142 | A framework-specific method which extracts language information from |
paulb@91 | 143 | the transaction. |
paulb@91 | 144 | """ |
paulb@91 | 145 | |
paulb@91 | 146 | accept_languages = self.get_header_values("Accept-Language") |
paulb@91 | 147 | if len(accept_languages) >= 1: |
paulb@91 | 148 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 149 | else: |
paulb@91 | 150 | return None |
paulb@91 | 151 | |
paulb@91 | 152 | def get_path(self): |
paulb@91 | 153 | |
paulb@91 | 154 | """ |
paulb@91 | 155 | A framework-specific method which gets the entire path from the request. |
paulb@91 | 156 | """ |
paulb@91 | 157 | |
paulb@91 | 158 | return self.request.getServletPath() |
paulb@91 | 159 | |
paulb@91 | 160 | def get_path_info(self): |
paulb@91 | 161 | |
paulb@91 | 162 | """ |
paulb@91 | 163 | A framework-specific method which gets the "path info" (the part of the |
paulb@91 | 164 | URL after the resource name handling the current request) from the |
paulb@91 | 165 | request. |
paulb@91 | 166 | """ |
paulb@91 | 167 | |
paulb@91 | 168 | return self.request.getPathInfo() |
paulb@91 | 169 | |
paulb@91 | 170 | def get_query_string(self): |
paulb@91 | 171 | |
paulb@91 | 172 | """ |
paulb@91 | 173 | A framework-specific method which gets the query string from the path in |
paulb@91 | 174 | the request. |
paulb@91 | 175 | """ |
paulb@91 | 176 | |
paulb@91 | 177 | return self.request.getQueryString() |
paulb@91 | 178 | |
paulb@91 | 179 | # Higher level request-related methods. |
paulb@91 | 180 | |
paulb@91 | 181 | def get_fields_from_path(self): |
paulb@91 | 182 | |
paulb@91 | 183 | """ |
paulb@91 | 184 | A framework-specific method which extracts the form fields from the |
paulb@91 | 185 | path specified in the transaction. The underlying framework may refuse |
paulb@91 | 186 | to supply fields from the path if handling a POST transaction. |
paulb@91 | 187 | |
paulb@91 | 188 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 189 | single value is associated with any given field name). |
paulb@91 | 190 | |
paulb@91 | 191 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 192 | NOTE: from the path. |
paulb@91 | 193 | """ |
paulb@91 | 194 | |
paulb@91 | 195 | return self.get_fields_from_body() |
paulb@91 | 196 | |
paulb@91 | 197 | def get_fields_from_body(self): |
paulb@91 | 198 | |
paulb@91 | 199 | """ |
paulb@91 | 200 | A framework-specific method which extracts the form fields from the |
paulb@91 | 201 | message body in the transaction. |
paulb@91 | 202 | |
paulb@91 | 203 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 204 | single value is associated with any given field name). |
paulb@91 | 205 | |
paulb@91 | 206 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 207 | NOTE: from the message body. |
paulb@91 | 208 | """ |
paulb@91 | 209 | |
paulb@91 | 210 | parameter_map = self.request.getParameterMap() |
paulb@91 | 211 | fields = {} |
paulb@91 | 212 | for key in parameter_map.keySet(): |
paulb@91 | 213 | fields[key] = parameter_map[key] |
paulb@91 | 214 | return fields |
paulb@91 | 215 | |
paulb@91 | 216 | def get_user(self): |
paulb@91 | 217 | |
paulb@91 | 218 | """ |
paulb@91 | 219 | A framework-specific method which extracts user information from the |
paulb@91 | 220 | transaction. |
paulb@91 | 221 | """ |
paulb@91 | 222 | |
paulb@91 | 223 | return self.request.getRemoteUser() |
paulb@91 | 224 | |
paulb@91 | 225 | # Response-related methods. |
paulb@91 | 226 | |
paulb@91 | 227 | def get_response_stream(self): |
paulb@91 | 228 | |
paulb@91 | 229 | """ |
paulb@91 | 230 | A framework-specific method which returns the response stream for |
paulb@91 | 231 | the transaction. |
paulb@91 | 232 | """ |
paulb@91 | 233 | |
paulb@91 | 234 | return self.response.getWriter() |
paulb@91 | 235 | |
paulb@91 | 236 | def get_response_code(self): |
paulb@91 | 237 | |
paulb@91 | 238 | """ |
paulb@91 | 239 | Get the response code associated with the transaction. If no response |
paulb@91 | 240 | code is defined, None is returned. |
paulb@91 | 241 | """ |
paulb@91 | 242 | |
paulb@91 | 243 | return self.status |
paulb@91 | 244 | |
paulb@91 | 245 | def set_response_code(self, response_code): |
paulb@91 | 246 | |
paulb@91 | 247 | """ |
paulb@91 | 248 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 249 | specification. |
paulb@91 | 250 | """ |
paulb@91 | 251 | |
paulb@91 | 252 | self.status = response_code |
paulb@91 | 253 | self.response.setStatus(self.status) |
paulb@91 | 254 | |
paulb@91 | 255 | def set_header_value(self, header, value): |
paulb@91 | 256 | |
paulb@91 | 257 | """ |
paulb@91 | 258 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 259 | """ |
paulb@91 | 260 | |
paulb@91 | 261 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 262 | |
paulb@91 | 263 | def set_content_type(self, content_type): |
paulb@91 | 264 | |
paulb@91 | 265 | """ |
paulb@91 | 266 | A framework-specific method which sets the 'content_type' for the |
paulb@91 | 267 | response. |
paulb@91 | 268 | """ |
paulb@91 | 269 | |
paulb@91 | 270 | return self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@91 | 271 | |
paulb@91 | 272 | # vim: tabstop=4 expandtab shiftwidth=4 |