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