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