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