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