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@311 | 214 | if self.path_info is not None: |
paulb@311 | 215 | return self.path_info |
paulb@311 | 216 | else: |
paulb@311 | 217 | return self.request.getPathInfo() or "" |
paulb@91 | 218 | |
paulb@91 | 219 | def get_query_string(self): |
paulb@91 | 220 | |
paulb@91 | 221 | """ |
paulb@186 | 222 | Returns the query string from the path in the request. |
paulb@91 | 223 | """ |
paulb@91 | 224 | |
paulb@228 | 225 | return self.request.getQueryString() or "" |
paulb@91 | 226 | |
paulb@91 | 227 | # Higher level request-related methods. |
paulb@91 | 228 | |
paulb@91 | 229 | def get_fields_from_path(self): |
paulb@91 | 230 | |
paulb@91 | 231 | """ |
paulb@249 | 232 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 233 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 234 | the path if handling a POST transaction. |
paulb@91 | 235 | |
paulb@91 | 236 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 237 | single value is associated with any given field name). |
paulb@91 | 238 | """ |
paulb@91 | 239 | |
paulb@228 | 240 | # There may not be a reliable means of extracting only the fields from |
paulb@235 | 241 | # the path using the API. Moreover, any access to the request parameters |
paulb@235 | 242 | # disrupts the proper extraction and decoding of the request parameters |
paulb@235 | 243 | # which originated in the request body. |
paulb@228 | 244 | |
paulb@228 | 245 | return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode) |
paulb@91 | 246 | |
paulb@167 | 247 | def get_fields_from_body(self, encoding=None): |
paulb@91 | 248 | |
paulb@91 | 249 | """ |
paulb@249 | 250 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 251 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 252 | encoding of the message body for cases where no such information is |
paulb@249 | 253 | available, but where the default encoding is to be overridden. |
paulb@91 | 254 | |
paulb@91 | 255 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 256 | single value is associated with any given field name). Each value is |
paulb@193 | 257 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 258 | or a plain string (representing a file upload form field, for example). |
paulb@91 | 259 | """ |
paulb@91 | 260 | |
paulb@249 | 261 | # There may not be a reliable means of extracting only the fields |
paulb@249 | 262 | # the message body using the API. Remove fields originating from the |
paulb@249 | 263 | # path in the mixture provided by the API. |
paulb@249 | 264 | |
paulb@249 | 265 | all_fields = self._get_fields(encoding) |
paulb@249 | 266 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 267 | return filter_fields(all_fields, fields_from_path) |
paulb@249 | 268 | |
paulb@249 | 269 | def _get_fields(self, encoding=None): |
paulb@249 | 270 | |
paulb@235 | 271 | # Override the default encoding if requested. |
paulb@235 | 272 | |
paulb@235 | 273 | if encoding is not None: |
paulb@235 | 274 | self.request.setCharacterEncoding(encoding) |
paulb@235 | 275 | |
paulb@201 | 276 | # Where the content type is "multipart/form-data", we use javax.mail |
paulb@201 | 277 | # functionality. Otherwise, we use the Servlet API's parameter access |
paulb@201 | 278 | # methods. |
paulb@193 | 279 | |
paulb@228 | 280 | if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data": |
paulb@249 | 281 | if self.message_fields is not None: |
paulb@249 | 282 | return self.message_fields |
paulb@249 | 283 | else: |
paulb@249 | 284 | fields = self.message_fields = self._get_fields_from_message() |
paulb@193 | 285 | else: |
paulb@198 | 286 | fields = {} |
paulb@193 | 287 | parameter_map = self.request.getParameterMap() |
paulb@193 | 288 | if parameter_map: |
paulb@228 | 289 | for field_name in parameter_map.keySet(): |
paulb@249 | 290 | fields[field_name] = parameter_map[field_name] |
paulb@228 | 291 | |
paulb@249 | 292 | return fields |
paulb@249 | 293 | |
paulb@249 | 294 | def get_fields(self, encoding=None): |
paulb@228 | 295 | |
paulb@249 | 296 | """ |
paulb@249 | 297 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 298 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 299 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 300 | where no such information is available, but where the default encoding |
paulb@249 | 301 | is to be overridden. |
paulb@249 | 302 | |
paulb@249 | 303 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 304 | single value is associated with any given field name). Each value is |
paulb@249 | 305 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 306 | or a plain string (representing a file upload form field, for example). |
paulb@228 | 307 | |
paulb@249 | 308 | Where a given field name is used in both the path and message body to |
paulb@249 | 309 | specify values, the values from both sources will be combined into a |
paulb@249 | 310 | single list associated with that field name. |
paulb@249 | 311 | """ |
paulb@249 | 312 | |
paulb@249 | 313 | # NOTE: The Java Servlet API (like Zope) seems to provide only body |
paulb@249 | 314 | # NOTE: fields upon POST requests. |
paulb@228 | 315 | |
paulb@249 | 316 | if self.get_request_method() == "GET": |
paulb@249 | 317 | return self._get_fields(encoding) |
paulb@249 | 318 | else: |
paulb@249 | 319 | fields = {} |
paulb@249 | 320 | fields.update(self.get_fields_from_path()) |
paulb@249 | 321 | for name, values in self._get_fields(encoding).items(): |
paulb@249 | 322 | if not fields.has_key(name): |
paulb@249 | 323 | fields[name] = values |
paulb@249 | 324 | else: |
paulb@249 | 325 | fields[name] += values |
paulb@91 | 326 | return fields |
paulb@91 | 327 | |
paulb@91 | 328 | def get_user(self): |
paulb@91 | 329 | |
paulb@91 | 330 | """ |
paulb@186 | 331 | Extracts user information from the transaction. |
paulb@108 | 332 | |
paulb@108 | 333 | Returns a username as a string or None if no user is defined. |
paulb@91 | 334 | """ |
paulb@91 | 335 | |
paulb@128 | 336 | if self.user is not None: |
paulb@128 | 337 | return self.user |
paulb@128 | 338 | else: |
paulb@128 | 339 | return self.request.getRemoteUser() |
paulb@91 | 340 | |
paulb@115 | 341 | def get_cookies(self): |
paulb@115 | 342 | |
paulb@115 | 343 | """ |
paulb@186 | 344 | Obtains cookie information from the request. |
paulb@115 | 345 | |
paulb@115 | 346 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 347 | """ |
paulb@115 | 348 | |
paulb@115 | 349 | return self.cookies_in |
paulb@115 | 350 | |
paulb@115 | 351 | def get_cookie(self, cookie_name): |
paulb@115 | 352 | |
paulb@115 | 353 | """ |
paulb@186 | 354 | Obtains cookie information from the request. |
paulb@115 | 355 | |
paulb@115 | 356 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 357 | cookie exists. |
paulb@115 | 358 | """ |
paulb@115 | 359 | |
paulb@115 | 360 | return self.cookies_in.get(cookie_name) |
paulb@115 | 361 | |
paulb@91 | 362 | # Response-related methods. |
paulb@91 | 363 | |
paulb@91 | 364 | def get_response_stream(self): |
paulb@91 | 365 | |
paulb@91 | 366 | """ |
paulb@186 | 367 | Returns the response stream for the transaction. |
paulb@91 | 368 | """ |
paulb@91 | 369 | |
paulb@228 | 370 | return self.response.getWriter() |
paulb@91 | 371 | |
paulb@252 | 372 | def get_response_stream_encoding(self): |
paulb@252 | 373 | |
paulb@252 | 374 | """ |
paulb@252 | 375 | Returns the response stream encoding. |
paulb@252 | 376 | """ |
paulb@252 | 377 | |
paulb@252 | 378 | return self.response.getCharacterEncoding() |
paulb@252 | 379 | |
paulb@91 | 380 | def get_response_code(self): |
paulb@91 | 381 | |
paulb@91 | 382 | """ |
paulb@91 | 383 | Get the response code associated with the transaction. If no response |
paulb@91 | 384 | code is defined, None is returned. |
paulb@91 | 385 | """ |
paulb@91 | 386 | |
paulb@91 | 387 | return self.status |
paulb@91 | 388 | |
paulb@91 | 389 | def set_response_code(self, response_code): |
paulb@91 | 390 | |
paulb@91 | 391 | """ |
paulb@91 | 392 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 393 | specification. |
paulb@91 | 394 | """ |
paulb@91 | 395 | |
paulb@91 | 396 | self.status = response_code |
paulb@91 | 397 | self.response.setStatus(self.status) |
paulb@91 | 398 | |
paulb@91 | 399 | def set_header_value(self, header, value): |
paulb@91 | 400 | |
paulb@91 | 401 | """ |
paulb@91 | 402 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 403 | """ |
paulb@91 | 404 | |
paulb@91 | 405 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 406 | |
paulb@91 | 407 | def set_content_type(self, content_type): |
paulb@91 | 408 | |
paulb@91 | 409 | """ |
paulb@186 | 410 | Sets the 'content_type' for the response. |
paulb@91 | 411 | """ |
paulb@91 | 412 | |
paulb@228 | 413 | self.response.setContentType(str(content_type)) |
paulb@91 | 414 | |
paulb@115 | 415 | # Higher level response-related methods. |
paulb@115 | 416 | |
paulb@115 | 417 | def set_cookie(self, cookie): |
paulb@115 | 418 | |
paulb@115 | 419 | """ |
paulb@186 | 420 | Stores the given 'cookie' object in the response. |
paulb@115 | 421 | """ |
paulb@115 | 422 | |
paulb@258 | 423 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@115 | 424 | |
paulb@115 | 425 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 426 | |
paulb@115 | 427 | """ |
paulb@186 | 428 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@115 | 429 | |
paulb@115 | 430 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 431 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 432 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 433 | """ |
paulb@115 | 434 | |
paulb@258 | 435 | cookie = javax.servlet.http.Cookie(self.encode_cookie_value(name), |
paulb@258 | 436 | self.encode_cookie_value(value)) |
paulb@115 | 437 | if path is not None: |
paulb@115 | 438 | cookie.setPath(path) |
paulb@115 | 439 | |
paulb@115 | 440 | # NOTE: The expires parameter seems not to be supported. |
paulb@115 | 441 | |
paulb@115 | 442 | self.response.addCookie(cookie) |
paulb@115 | 443 | |
paulb@115 | 444 | def delete_cookie(self, cookie_name): |
paulb@115 | 445 | |
paulb@115 | 446 | """ |
paulb@186 | 447 | Adds to the response a request that the cookie with the given |
paulb@186 | 448 | 'cookie_name' be deleted/discarded by the client. |
paulb@115 | 449 | """ |
paulb@115 | 450 | |
paulb@115 | 451 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 452 | # has been sent the cookie or not. |
paulb@115 | 453 | # NOTE: Magic discovered in Webware. |
paulb@115 | 454 | |
paulb@258 | 455 | cookie = javax.servlet.http.Cookie(self.encode_cookie_value(cookie_name), "") |
paulb@115 | 456 | cookie.setPath("/") |
paulb@115 | 457 | cookie.setMaxAge(0) |
paulb@115 | 458 | self.response.addCookie(cookie) |
paulb@115 | 459 | |
paulb@183 | 460 | # Session-related methods. |
paulb@183 | 461 | |
paulb@183 | 462 | def get_session(self, create=1): |
paulb@183 | 463 | |
paulb@183 | 464 | """ |
paulb@186 | 465 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 466 | transaction. |
paulb@183 | 467 | |
paulb@183 | 468 | If no session has yet been established according to information |
paulb@183 | 469 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 470 | determines whether a new session will be established. |
paulb@183 | 471 | |
paulb@183 | 472 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 473 | then None is returned. In all other cases, a session object is created |
paulb@183 | 474 | (where appropriate) and returned. |
paulb@183 | 475 | """ |
paulb@183 | 476 | |
paulb@235 | 477 | session = self.request.getSession(create) |
paulb@235 | 478 | if session: |
paulb@235 | 479 | return Session(session) |
paulb@235 | 480 | else: |
paulb@235 | 481 | return None |
paulb@183 | 482 | |
paulb@183 | 483 | def expire_session(self): |
paulb@183 | 484 | |
paulb@183 | 485 | """ |
paulb@186 | 486 | Expires any session established according to information provided in the |
paulb@186 | 487 | transaction. |
paulb@183 | 488 | """ |
paulb@183 | 489 | |
paulb@235 | 490 | session = self.request.getSession(0) |
paulb@183 | 491 | if session: |
paulb@183 | 492 | session.invalidate() |
paulb@183 | 493 | |
paulb@201 | 494 | # Special Java-specific methods. |
paulb@201 | 495 | |
paulb@201 | 496 | def _get_fields_from_message(self): |
paulb@201 | 497 | |
paulb@201 | 498 | "Get fields from a multipart message." |
paulb@201 | 499 | |
paulb@201 | 500 | session = javax.mail.Session.getDefaultInstance(java.util.Properties()) |
paulb@203 | 501 | |
paulb@203 | 502 | # Fake a multipart message. |
paulb@203 | 503 | |
paulb@203 | 504 | str_buffer = java.io.StringWriter() |
paulb@203 | 505 | fp = self.get_request_stream() |
paulb@203 | 506 | boundary = fp.readline() |
paulb@203 | 507 | str_buffer.write('Content-Type: multipart/mixed; boundary="%s"\n\n' % boundary[2:-1]) |
paulb@203 | 508 | str_buffer.write(boundary) |
paulb@203 | 509 | str_buffer.write(fp.read()) |
paulb@203 | 510 | str_buffer.close() |
paulb@203 | 511 | |
paulb@203 | 512 | # Re-read that message. |
paulb@203 | 513 | |
paulb@203 | 514 | input_stream = java.io.StringBufferInputStream(str_buffer.toString()) |
paulb@203 | 515 | message = javax.mail.internet.MimeMessage(session, input_stream) |
paulb@201 | 516 | content = message.getContent() |
paulb@201 | 517 | return self._get_fields_from_multipart(content) |
paulb@201 | 518 | |
paulb@201 | 519 | def _get_fields_from_multipart(self, content): |
paulb@201 | 520 | |
paulb@201 | 521 | "Get fields from multipart 'content'." |
paulb@201 | 522 | |
paulb@201 | 523 | fields = {} |
paulb@201 | 524 | for i in range(0, content.getCount()): |
paulb@201 | 525 | part = content.getBodyPart(i) |
paulb@201 | 526 | subcontent = part.getContent() |
paulb@201 | 527 | |
paulb@203 | 528 | # Convert input stream content. |
paulb@203 | 529 | |
paulb@203 | 530 | if isinstance(subcontent, java.io.InputStream): |
paulb@203 | 531 | subcontent = Stream(subcontent).read() |
paulb@203 | 532 | |
paulb@201 | 533 | # Record string content. |
paulb@201 | 534 | |
paulb@201 | 535 | if type(subcontent) == type(""): |
paulb@203 | 536 | |
paulb@203 | 537 | # Should get: form-data; name="x" |
paulb@203 | 538 | |
paulb@238 | 539 | disposition = self.parse_header_value(HeaderValue, part.getHeader("Content-Disposition")[0]) |
paulb@203 | 540 | |
paulb@235 | 541 | # Store and optionally convert the field. |
paulb@203 | 542 | |
paulb@238 | 543 | if disposition.name is not None: |
paulb@238 | 544 | if not fields.has_key(disposition.name[1:-1]): |
paulb@238 | 545 | fields[disposition.name[1:-1]] = [] |
paulb@238 | 546 | fields[disposition.name[1:-1]].append(subcontent) |
paulb@203 | 547 | |
paulb@235 | 548 | # Otherwise, descend deeper into the multipart hierarchy. |
paulb@203 | 549 | |
paulb@201 | 550 | else: |
paulb@201 | 551 | fields.update(self._get_fields_from_multipart(subcontent)) |
paulb@201 | 552 | |
paulb@201 | 553 | return fields |
paulb@201 | 554 | |
paulb@183 | 555 | class Session: |
paulb@183 | 556 | |
paulb@183 | 557 | """ |
paulb@183 | 558 | A simple session class with behaviour more similar to the Python framework |
paulb@183 | 559 | session classes. |
paulb@183 | 560 | """ |
paulb@183 | 561 | |
paulb@183 | 562 | def __init__(self, session): |
paulb@183 | 563 | |
paulb@183 | 564 | "Initialise the session object with the framework 'session' object." |
paulb@183 | 565 | |
paulb@183 | 566 | self.session = session |
paulb@183 | 567 | |
paulb@183 | 568 | def keys(self): |
paulb@183 | 569 | keys = [] |
paulb@183 | 570 | keys_enum = self.session.getAttributeNames() |
paulb@183 | 571 | while keys_enum.hasMoreElements(): |
paulb@183 | 572 | keys.append(keys_enum.nextElement()) |
paulb@183 | 573 | return keys |
paulb@183 | 574 | |
paulb@183 | 575 | def values(self): |
paulb@183 | 576 | values = [] |
paulb@183 | 577 | for key in self.keys(): |
paulb@183 | 578 | values.append(self[key]) |
paulb@183 | 579 | return values |
paulb@183 | 580 | |
paulb@183 | 581 | def items(self): |
paulb@183 | 582 | items = [] |
paulb@183 | 583 | for key in self.keys(): |
paulb@183 | 584 | items.append((key, self[key])) |
paulb@183 | 585 | return items |
paulb@183 | 586 | |
paulb@183 | 587 | def __getitem__(self, key): |
paulb@183 | 588 | return self.session.getAttribute(key) |
paulb@183 | 589 | |
paulb@183 | 590 | def __setitem__(self, key, value): |
paulb@183 | 591 | self.session.setAttribute(key, value) |
paulb@183 | 592 | |
paulb@183 | 593 | def __delitem__(self, key): |
paulb@183 | 594 | self.session.removeAttribute(key) |
paulb@183 | 595 | |
paulb@91 | 596 | # vim: tabstop=4 expandtab shiftwidth=4 |