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