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