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@249 | 9 | from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string, filter_fields |
paulb@246 | 10 | import javax.servlet.http |
paulb@201 | 11 | |
paulb@201 | 12 | # Form data decoding. |
paulb@201 | 13 | |
paulb@246 | 14 | import javax.mail.internet |
paulb@246 | 15 | import javax.mail |
paulb@246 | 16 | import java.util |
paulb@246 | 17 | import java.net |
paulb@238 | 18 | from WebStack.Generic import HeaderValue |
paulb@91 | 19 | |
paulb@91 | 20 | class Stream: |
paulb@91 | 21 | |
paulb@91 | 22 | """ |
paulb@91 | 23 | Wrapper around java.io.BufferedReader. |
paulb@91 | 24 | """ |
paulb@91 | 25 | |
paulb@91 | 26 | def __init__(self, stream): |
paulb@91 | 27 | |
paulb@91 | 28 | "Initialise the stream with the given underlying 'stream'." |
paulb@91 | 29 | |
paulb@91 | 30 | self.stream = stream |
paulb@91 | 31 | |
paulb@91 | 32 | def read(self): |
paulb@91 | 33 | |
paulb@91 | 34 | "Read the entire message, returning it as a string." |
paulb@91 | 35 | |
paulb@91 | 36 | characters = StringIO() |
paulb@91 | 37 | while 1: |
paulb@91 | 38 | c = self.stream.read() |
paulb@91 | 39 | if c == -1: |
paulb@91 | 40 | return characters.getvalue() |
paulb@91 | 41 | else: |
paulb@91 | 42 | characters.write(chr(c)) |
paulb@91 | 43 | |
paulb@91 | 44 | def readline(self): |
paulb@91 | 45 | |
paulb@91 | 46 | "Read a line from the stream, returning it as a string." |
paulb@91 | 47 | |
paulb@249 | 48 | line = self.stream.readLine() |
paulb@249 | 49 | if line is not None: |
paulb@249 | 50 | return line + "\n" |
paulb@249 | 51 | else: |
paulb@249 | 52 | return "" |
paulb@91 | 53 | |
paulb@91 | 54 | class Transaction(Generic.Transaction): |
paulb@91 | 55 | |
paulb@91 | 56 | """ |
paulb@91 | 57 | Java Servlet transaction interface. |
paulb@91 | 58 | """ |
paulb@91 | 59 | |
paulb@91 | 60 | def __init__(self, request, response): |
paulb@91 | 61 | |
paulb@91 | 62 | """ |
paulb@91 | 63 | Initialise the transaction using the Java Servlet HTTP 'request' and |
paulb@91 | 64 | 'response'. |
paulb@91 | 65 | """ |
paulb@91 | 66 | |
paulb@91 | 67 | self.request = request |
paulb@91 | 68 | self.response = response |
paulb@91 | 69 | self.status = None |
paulb@128 | 70 | self.user = None |
paulb@91 | 71 | |
paulb@115 | 72 | # Remember the cookies received in the request. |
paulb@115 | 73 | # NOTE: Discarding much of the information received. |
paulb@115 | 74 | |
paulb@115 | 75 | self.cookies_in = {} |
paulb@176 | 76 | for cookie in self.request.getCookies() or []: |
paulb@258 | 77 | cookie_name = self.decode_cookie_value(cookie.getName()) |
paulb@258 | 78 | self.cookies_in[cookie_name] = Cookie(cookie_name, self.decode_cookie_value(cookie.getValue())) |
paulb@115 | 79 | |
paulb@193 | 80 | # Cached information. |
paulb@193 | 81 | |
paulb@249 | 82 | self.message_fields = None |
paulb@193 | 83 | |
paulb@95 | 84 | def commit(self): |
paulb@95 | 85 | |
paulb@95 | 86 | """ |
paulb@95 | 87 | A special method, synchronising the transaction with framework-specific |
paulb@95 | 88 | objects. |
paulb@95 | 89 | """ |
paulb@95 | 90 | |
paulb@95 | 91 | self.get_response_stream().close() |
paulb@95 | 92 | |
paulb@91 | 93 | # Request-related methods. |
paulb@91 | 94 | |
paulb@91 | 95 | def get_request_stream(self): |
paulb@91 | 96 | |
paulb@91 | 97 | """ |
paulb@186 | 98 | Returns the request stream for the transaction. |
paulb@91 | 99 | """ |
paulb@91 | 100 | |
paulb@91 | 101 | return Stream(self.request.getReader()) |
paulb@91 | 102 | |
paulb@91 | 103 | def get_request_method(self): |
paulb@91 | 104 | |
paulb@91 | 105 | """ |
paulb@186 | 106 | Returns the request method. |
paulb@91 | 107 | """ |
paulb@91 | 108 | |
paulb@91 | 109 | return self.request.getMethod() |
paulb@91 | 110 | |
paulb@91 | 111 | def get_headers(self): |
paulb@91 | 112 | |
paulb@91 | 113 | """ |
paulb@186 | 114 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 115 | names to values. |
paulb@186 | 116 | |
paulb@91 | 117 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 118 | NOTE: need to change. |
paulb@91 | 119 | """ |
paulb@91 | 120 | |
paulb@91 | 121 | headers = {} |
paulb@176 | 122 | header_names_enum = self.request.getHeaderNames() |
paulb@176 | 123 | while header_names_enum.hasMoreElements(): |
paulb@91 | 124 | |
paulb@176 | 125 | # NOTE: Retrieve only a single value (not using getHeaders). |
paulb@91 | 126 | |
paulb@176 | 127 | header_name = header_names_enum.nextElement() |
paulb@176 | 128 | headers[header_name] = self.request.getHeader(header_name) |
paulb@91 | 129 | |
paulb@91 | 130 | return headers |
paulb@91 | 131 | |
paulb@91 | 132 | def get_header_values(self, key): |
paulb@91 | 133 | |
paulb@91 | 134 | """ |
paulb@186 | 135 | Returns a list of all request header values associated with the given |
paulb@186 | 136 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 137 | case-insensitive string. |
paulb@91 | 138 | """ |
paulb@91 | 139 | |
paulb@176 | 140 | values = [] |
paulb@176 | 141 | headers_enum = self.request.getHeaders(key) |
paulb@176 | 142 | while headers_enum.hasMoreElements(): |
paulb@176 | 143 | values.append(headers_enum.nextElement()) |
paulb@176 | 144 | return values |
paulb@91 | 145 | |
paulb@91 | 146 | def get_content_type(self): |
paulb@91 | 147 | |
paulb@91 | 148 | """ |
paulb@186 | 149 | Returns the content type specified on the request, along with the |
paulb@186 | 150 | charset employed. |
paulb@91 | 151 | """ |
paulb@91 | 152 | |
paulb@176 | 153 | content_types = self.get_header_values("Content-Type") or [] |
paulb@91 | 154 | if len(content_types) >= 1: |
paulb@91 | 155 | return self.parse_content_type(content_types[0]) |
paulb@91 | 156 | else: |
paulb@91 | 157 | return None |
paulb@91 | 158 | |
paulb@91 | 159 | def get_content_charsets(self): |
paulb@91 | 160 | |
paulb@91 | 161 | """ |
paulb@91 | 162 | Returns the character set preferences. |
paulb@91 | 163 | """ |
paulb@91 | 164 | |
paulb@176 | 165 | accept_charsets = self.get_header_values("Accept-Charset") or [] |
paulb@91 | 166 | if len(accept_charsets) >= 1: |
paulb@91 | 167 | return self.parse_content_preferences(accept_charsets[0]) |
paulb@91 | 168 | else: |
paulb@91 | 169 | return None |
paulb@91 | 170 | |
paulb@91 | 171 | def get_content_languages(self): |
paulb@91 | 172 | |
paulb@91 | 173 | """ |
paulb@186 | 174 | Returns extracted language information from the transaction. |
paulb@91 | 175 | """ |
paulb@91 | 176 | |
paulb@176 | 177 | accept_languages = self.get_header_values("Accept-Language") or [] |
paulb@91 | 178 | if len(accept_languages) >= 1: |
paulb@91 | 179 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 180 | else: |
paulb@91 | 181 | return None |
paulb@91 | 182 | |
paulb@91 | 183 | def get_path(self): |
paulb@91 | 184 | |
paulb@91 | 185 | """ |
paulb@186 | 186 | Returns the entire path from the request. |
paulb@91 | 187 | """ |
paulb@91 | 188 | |
paulb@162 | 189 | # NOTE: To be verified. |
paulb@162 | 190 | |
paulb@162 | 191 | path = self.get_path_without_query() |
paulb@162 | 192 | qs = self.get_query_string() |
paulb@162 | 193 | if qs: |
paulb@162 | 194 | path += "?" |
paulb@162 | 195 | path += qs |
paulb@162 | 196 | return path |
paulb@162 | 197 | |
paulb@162 | 198 | def get_path_without_query(self): |
paulb@162 | 199 | |
paulb@162 | 200 | """ |
paulb@186 | 201 | Returns the entire path from the request minus the query string. |
paulb@162 | 202 | """ |
paulb@162 | 203 | |
paulb@216 | 204 | return self.request.getContextPath() + self.request.getServletPath() + self.get_path_info() |
paulb@91 | 205 | |
paulb@91 | 206 | def get_path_info(self): |
paulb@91 | 207 | |
paulb@91 | 208 | """ |
paulb@186 | 209 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 210 | handling the current request) from the request. |
paulb@91 | 211 | """ |
paulb@91 | 212 | |
paulb@216 | 213 | return self.request.getPathInfo() or "" |
paulb@91 | 214 | |
paulb@91 | 215 | def get_query_string(self): |
paulb@91 | 216 | |
paulb@91 | 217 | """ |
paulb@186 | 218 | Returns the query string from the path in the request. |
paulb@91 | 219 | """ |
paulb@91 | 220 | |
paulb@228 | 221 | return self.request.getQueryString() or "" |
paulb@91 | 222 | |
paulb@91 | 223 | # Higher level request-related methods. |
paulb@91 | 224 | |
paulb@91 | 225 | def get_fields_from_path(self): |
paulb@91 | 226 | |
paulb@91 | 227 | """ |
paulb@249 | 228 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 229 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 230 | the path if handling a POST transaction. |
paulb@91 | 231 | |
paulb@91 | 232 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 233 | single value is associated with any given field name). |
paulb@91 | 234 | """ |
paulb@91 | 235 | |
paulb@228 | 236 | # There may not be a reliable means of extracting only the fields from |
paulb@235 | 237 | # the path using the API. Moreover, any access to the request parameters |
paulb@235 | 238 | # disrupts the proper extraction and decoding of the request parameters |
paulb@235 | 239 | # which originated in the request body. |
paulb@228 | 240 | |
paulb@228 | 241 | return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode) |
paulb@91 | 242 | |
paulb@167 | 243 | def get_fields_from_body(self, encoding=None): |
paulb@91 | 244 | |
paulb@91 | 245 | """ |
paulb@249 | 246 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 247 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 248 | encoding of the message body for cases where no such information is |
paulb@249 | 249 | available, but where the default encoding is to be overridden. |
paulb@91 | 250 | |
paulb@91 | 251 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 252 | single value is associated with any given field name). Each value is |
paulb@193 | 253 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 254 | or a plain string (representing a file upload form field, for example). |
paulb@91 | 255 | """ |
paulb@91 | 256 | |
paulb@249 | 257 | # There may not be a reliable means of extracting only the fields |
paulb@249 | 258 | # the message body using the API. Remove fields originating from the |
paulb@249 | 259 | # path in the mixture provided by the API. |
paulb@249 | 260 | |
paulb@249 | 261 | all_fields = self._get_fields(encoding) |
paulb@249 | 262 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 263 | return filter_fields(all_fields, fields_from_path) |
paulb@249 | 264 | |
paulb@249 | 265 | def _get_fields(self, encoding=None): |
paulb@249 | 266 | |
paulb@235 | 267 | # Override the default encoding if requested. |
paulb@235 | 268 | |
paulb@235 | 269 | if encoding is not None: |
paulb@235 | 270 | self.request.setCharacterEncoding(encoding) |
paulb@235 | 271 | |
paulb@201 | 272 | # Where the content type is "multipart/form-data", we use javax.mail |
paulb@201 | 273 | # functionality. Otherwise, we use the Servlet API's parameter access |
paulb@201 | 274 | # methods. |
paulb@193 | 275 | |
paulb@228 | 276 | if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data": |
paulb@249 | 277 | if self.message_fields is not None: |
paulb@249 | 278 | return self.message_fields |
paulb@249 | 279 | else: |
paulb@249 | 280 | fields = self.message_fields = self._get_fields_from_message() |
paulb@193 | 281 | else: |
paulb@198 | 282 | fields = {} |
paulb@193 | 283 | parameter_map = self.request.getParameterMap() |
paulb@193 | 284 | if parameter_map: |
paulb@228 | 285 | for field_name in parameter_map.keySet(): |
paulb@249 | 286 | fields[field_name] = parameter_map[field_name] |
paulb@228 | 287 | |
paulb@249 | 288 | return fields |
paulb@249 | 289 | |
paulb@249 | 290 | def get_fields(self, encoding=None): |
paulb@228 | 291 | |
paulb@249 | 292 | """ |
paulb@249 | 293 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 294 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 295 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 296 | where no such information is available, but where the default encoding |
paulb@249 | 297 | is to be overridden. |
paulb@249 | 298 | |
paulb@249 | 299 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 300 | single value is associated with any given field name). Each value is |
paulb@249 | 301 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 302 | or a plain string (representing a file upload form field, for example). |
paulb@228 | 303 | |
paulb@249 | 304 | Where a given field name is used in both the path and message body to |
paulb@249 | 305 | specify values, the values from both sources will be combined into a |
paulb@249 | 306 | single list associated with that field name. |
paulb@249 | 307 | """ |
paulb@249 | 308 | |
paulb@249 | 309 | # NOTE: The Java Servlet API (like Zope) seems to provide only body |
paulb@249 | 310 | # NOTE: fields upon POST requests. |
paulb@228 | 311 | |
paulb@249 | 312 | if self.get_request_method() == "GET": |
paulb@249 | 313 | return self._get_fields(encoding) |
paulb@249 | 314 | else: |
paulb@249 | 315 | fields = {} |
paulb@249 | 316 | fields.update(self.get_fields_from_path()) |
paulb@249 | 317 | for name, values in self._get_fields(encoding).items(): |
paulb@249 | 318 | if not fields.has_key(name): |
paulb@249 | 319 | fields[name] = values |
paulb@249 | 320 | else: |
paulb@249 | 321 | fields[name] += values |
paulb@91 | 322 | return fields |
paulb@91 | 323 | |
paulb@91 | 324 | def get_user(self): |
paulb@91 | 325 | |
paulb@91 | 326 | """ |
paulb@186 | 327 | Extracts user information from the transaction. |
paulb@108 | 328 | |
paulb@108 | 329 | Returns a username as a string or None if no user is defined. |
paulb@91 | 330 | """ |
paulb@91 | 331 | |
paulb@128 | 332 | if self.user is not None: |
paulb@128 | 333 | return self.user |
paulb@128 | 334 | else: |
paulb@128 | 335 | return self.request.getRemoteUser() |
paulb@91 | 336 | |
paulb@115 | 337 | def get_cookies(self): |
paulb@115 | 338 | |
paulb@115 | 339 | """ |
paulb@186 | 340 | Obtains cookie information from the request. |
paulb@115 | 341 | |
paulb@115 | 342 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 343 | """ |
paulb@115 | 344 | |
paulb@115 | 345 | return self.cookies_in |
paulb@115 | 346 | |
paulb@115 | 347 | def get_cookie(self, cookie_name): |
paulb@115 | 348 | |
paulb@115 | 349 | """ |
paulb@186 | 350 | Obtains cookie information from the request. |
paulb@115 | 351 | |
paulb@115 | 352 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 353 | cookie exists. |
paulb@115 | 354 | """ |
paulb@115 | 355 | |
paulb@115 | 356 | return self.cookies_in.get(cookie_name) |
paulb@115 | 357 | |
paulb@91 | 358 | # Response-related methods. |
paulb@91 | 359 | |
paulb@91 | 360 | def get_response_stream(self): |
paulb@91 | 361 | |
paulb@91 | 362 | """ |
paulb@186 | 363 | Returns the response stream for the transaction. |
paulb@91 | 364 | """ |
paulb@91 | 365 | |
paulb@228 | 366 | return self.response.getWriter() |
paulb@91 | 367 | |
paulb@252 | 368 | def get_response_stream_encoding(self): |
paulb@252 | 369 | |
paulb@252 | 370 | """ |
paulb@252 | 371 | Returns the response stream encoding. |
paulb@252 | 372 | """ |
paulb@252 | 373 | |
paulb@252 | 374 | return self.response.getCharacterEncoding() |
paulb@252 | 375 | |
paulb@91 | 376 | def get_response_code(self): |
paulb@91 | 377 | |
paulb@91 | 378 | """ |
paulb@91 | 379 | Get the response code associated with the transaction. If no response |
paulb@91 | 380 | code is defined, None is returned. |
paulb@91 | 381 | """ |
paulb@91 | 382 | |
paulb@91 | 383 | return self.status |
paulb@91 | 384 | |
paulb@91 | 385 | def set_response_code(self, response_code): |
paulb@91 | 386 | |
paulb@91 | 387 | """ |
paulb@91 | 388 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 389 | specification. |
paulb@91 | 390 | """ |
paulb@91 | 391 | |
paulb@91 | 392 | self.status = response_code |
paulb@91 | 393 | self.response.setStatus(self.status) |
paulb@91 | 394 | |
paulb@91 | 395 | def set_header_value(self, header, value): |
paulb@91 | 396 | |
paulb@91 | 397 | """ |
paulb@91 | 398 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 399 | """ |
paulb@91 | 400 | |
paulb@91 | 401 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 402 | |
paulb@91 | 403 | def set_content_type(self, content_type): |
paulb@91 | 404 | |
paulb@91 | 405 | """ |
paulb@186 | 406 | Sets the 'content_type' for the response. |
paulb@91 | 407 | """ |
paulb@91 | 408 | |
paulb@228 | 409 | self.response.setContentType(str(content_type)) |
paulb@91 | 410 | |
paulb@115 | 411 | # Higher level response-related methods. |
paulb@115 | 412 | |
paulb@115 | 413 | def set_cookie(self, cookie): |
paulb@115 | 414 | |
paulb@115 | 415 | """ |
paulb@186 | 416 | Stores the given 'cookie' object in the response. |
paulb@115 | 417 | """ |
paulb@115 | 418 | |
paulb@258 | 419 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@115 | 420 | |
paulb@115 | 421 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 422 | |
paulb@115 | 423 | """ |
paulb@186 | 424 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@115 | 425 | |
paulb@115 | 426 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 427 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 428 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 429 | """ |
paulb@115 | 430 | |
paulb@258 | 431 | cookie = javax.servlet.http.Cookie(self.encode_cookie_value(name), |
paulb@258 | 432 | self.encode_cookie_value(value)) |
paulb@115 | 433 | if path is not None: |
paulb@115 | 434 | cookie.setPath(path) |
paulb@115 | 435 | |
paulb@115 | 436 | # NOTE: The expires parameter seems not to be supported. |
paulb@115 | 437 | |
paulb@115 | 438 | self.response.addCookie(cookie) |
paulb@115 | 439 | |
paulb@115 | 440 | def delete_cookie(self, cookie_name): |
paulb@115 | 441 | |
paulb@115 | 442 | """ |
paulb@186 | 443 | Adds to the response a request that the cookie with the given |
paulb@186 | 444 | 'cookie_name' be deleted/discarded by the client. |
paulb@115 | 445 | """ |
paulb@115 | 446 | |
paulb@115 | 447 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 448 | # has been sent the cookie or not. |
paulb@115 | 449 | # NOTE: Magic discovered in Webware. |
paulb@115 | 450 | |
paulb@258 | 451 | cookie = javax.servlet.http.Cookie(self.encode_cookie_value(cookie_name), "") |
paulb@115 | 452 | cookie.setPath("/") |
paulb@115 | 453 | cookie.setMaxAge(0) |
paulb@115 | 454 | self.response.addCookie(cookie) |
paulb@115 | 455 | |
paulb@183 | 456 | # Session-related methods. |
paulb@183 | 457 | |
paulb@183 | 458 | def get_session(self, create=1): |
paulb@183 | 459 | |
paulb@183 | 460 | """ |
paulb@186 | 461 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 462 | transaction. |
paulb@183 | 463 | |
paulb@183 | 464 | If no session has yet been established according to information |
paulb@183 | 465 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 466 | determines whether a new session will be established. |
paulb@183 | 467 | |
paulb@183 | 468 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 469 | then None is returned. In all other cases, a session object is created |
paulb@183 | 470 | (where appropriate) and returned. |
paulb@183 | 471 | """ |
paulb@183 | 472 | |
paulb@235 | 473 | session = self.request.getSession(create) |
paulb@235 | 474 | if session: |
paulb@235 | 475 | return Session(session) |
paulb@235 | 476 | else: |
paulb@235 | 477 | return None |
paulb@183 | 478 | |
paulb@183 | 479 | def expire_session(self): |
paulb@183 | 480 | |
paulb@183 | 481 | """ |
paulb@186 | 482 | Expires any session established according to information provided in the |
paulb@186 | 483 | transaction. |
paulb@183 | 484 | """ |
paulb@183 | 485 | |
paulb@235 | 486 | session = self.request.getSession(0) |
paulb@183 | 487 | if session: |
paulb@183 | 488 | session.invalidate() |
paulb@183 | 489 | |
paulb@128 | 490 | # Application-specific methods. |
paulb@128 | 491 | |
paulb@128 | 492 | def set_user(self, username): |
paulb@128 | 493 | |
paulb@128 | 494 | """ |
paulb@128 | 495 | An application-specific method which sets the user information with |
paulb@128 | 496 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 497 | 'get_user'. |
paulb@128 | 498 | """ |
paulb@128 | 499 | |
paulb@128 | 500 | self.user = username |
paulb@128 | 501 | |
paulb@201 | 502 | # Special Java-specific methods. |
paulb@201 | 503 | |
paulb@201 | 504 | def _get_fields_from_message(self): |
paulb@201 | 505 | |
paulb@201 | 506 | "Get fields from a multipart message." |
paulb@201 | 507 | |
paulb@201 | 508 | session = javax.mail.Session.getDefaultInstance(java.util.Properties()) |
paulb@203 | 509 | |
paulb@203 | 510 | # Fake a multipart message. |
paulb@203 | 511 | |
paulb@203 | 512 | str_buffer = java.io.StringWriter() |
paulb@203 | 513 | fp = self.get_request_stream() |
paulb@203 | 514 | boundary = fp.readline() |
paulb@203 | 515 | str_buffer.write('Content-Type: multipart/mixed; boundary="%s"\n\n' % boundary[2:-1]) |
paulb@203 | 516 | str_buffer.write(boundary) |
paulb@203 | 517 | str_buffer.write(fp.read()) |
paulb@203 | 518 | str_buffer.close() |
paulb@203 | 519 | |
paulb@203 | 520 | # Re-read that message. |
paulb@203 | 521 | |
paulb@203 | 522 | input_stream = java.io.StringBufferInputStream(str_buffer.toString()) |
paulb@203 | 523 | message = javax.mail.internet.MimeMessage(session, input_stream) |
paulb@201 | 524 | content = message.getContent() |
paulb@201 | 525 | return self._get_fields_from_multipart(content) |
paulb@201 | 526 | |
paulb@201 | 527 | def _get_fields_from_multipart(self, content): |
paulb@201 | 528 | |
paulb@201 | 529 | "Get fields from multipart 'content'." |
paulb@201 | 530 | |
paulb@201 | 531 | fields = {} |
paulb@201 | 532 | for i in range(0, content.getCount()): |
paulb@201 | 533 | part = content.getBodyPart(i) |
paulb@201 | 534 | subcontent = part.getContent() |
paulb@201 | 535 | |
paulb@203 | 536 | # Convert input stream content. |
paulb@203 | 537 | |
paulb@203 | 538 | if isinstance(subcontent, java.io.InputStream): |
paulb@203 | 539 | subcontent = Stream(subcontent).read() |
paulb@203 | 540 | |
paulb@201 | 541 | # Record string content. |
paulb@201 | 542 | |
paulb@201 | 543 | if type(subcontent) == type(""): |
paulb@203 | 544 | |
paulb@203 | 545 | # Should get: form-data; name="x" |
paulb@203 | 546 | |
paulb@238 | 547 | disposition = self.parse_header_value(HeaderValue, part.getHeader("Content-Disposition")[0]) |
paulb@203 | 548 | |
paulb@235 | 549 | # Store and optionally convert the field. |
paulb@203 | 550 | |
paulb@238 | 551 | if disposition.name is not None: |
paulb@238 | 552 | if not fields.has_key(disposition.name[1:-1]): |
paulb@238 | 553 | fields[disposition.name[1:-1]] = [] |
paulb@238 | 554 | fields[disposition.name[1:-1]].append(subcontent) |
paulb@203 | 555 | |
paulb@235 | 556 | # Otherwise, descend deeper into the multipart hierarchy. |
paulb@203 | 557 | |
paulb@201 | 558 | else: |
paulb@201 | 559 | fields.update(self._get_fields_from_multipart(subcontent)) |
paulb@201 | 560 | |
paulb@201 | 561 | return fields |
paulb@201 | 562 | |
paulb@183 | 563 | class Session: |
paulb@183 | 564 | |
paulb@183 | 565 | """ |
paulb@183 | 566 | A simple session class with behaviour more similar to the Python framework |
paulb@183 | 567 | session classes. |
paulb@183 | 568 | """ |
paulb@183 | 569 | |
paulb@183 | 570 | def __init__(self, session): |
paulb@183 | 571 | |
paulb@183 | 572 | "Initialise the session object with the framework 'session' object." |
paulb@183 | 573 | |
paulb@183 | 574 | self.session = session |
paulb@183 | 575 | |
paulb@183 | 576 | def keys(self): |
paulb@183 | 577 | keys = [] |
paulb@183 | 578 | keys_enum = self.session.getAttributeNames() |
paulb@183 | 579 | while keys_enum.hasMoreElements(): |
paulb@183 | 580 | keys.append(keys_enum.nextElement()) |
paulb@183 | 581 | return keys |
paulb@183 | 582 | |
paulb@183 | 583 | def values(self): |
paulb@183 | 584 | values = [] |
paulb@183 | 585 | for key in self.keys(): |
paulb@183 | 586 | values.append(self[key]) |
paulb@183 | 587 | return values |
paulb@183 | 588 | |
paulb@183 | 589 | def items(self): |
paulb@183 | 590 | items = [] |
paulb@183 | 591 | for key in self.keys(): |
paulb@183 | 592 | items.append((key, self[key])) |
paulb@183 | 593 | return items |
paulb@183 | 594 | |
paulb@183 | 595 | def __getitem__(self, key): |
paulb@183 | 596 | return self.session.getAttribute(key) |
paulb@183 | 597 | |
paulb@183 | 598 | def __setitem__(self, key, value): |
paulb@183 | 599 | self.session.setAttribute(key, value) |
paulb@183 | 600 | |
paulb@183 | 601 | def __delitem__(self, key): |
paulb@183 | 602 | self.session.removeAttribute(key) |
paulb@183 | 603 | |
paulb@91 | 604 | # vim: tabstop=4 expandtab shiftwidth=4 |