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