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