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