paulb@91 | 1 | #!/usr/bin/env python |
paulb@91 | 2 | |
paulb@91 | 3 | """ |
paulb@91 | 4 | Java Servlet classes. |
paulb@91 | 5 | """ |
paulb@91 | 6 | |
paulb@91 | 7 | import Generic |
paulb@91 | 8 | from StringIO import StringIO |
paulb@115 | 9 | from Helpers.Request import Cookie |
paulb@115 | 10 | import javax.servlet.http.Cookie |
paulb@91 | 11 | |
paulb@91 | 12 | class Stream: |
paulb@91 | 13 | |
paulb@91 | 14 | """ |
paulb@91 | 15 | Wrapper around java.io.BufferedReader. |
paulb@91 | 16 | """ |
paulb@91 | 17 | |
paulb@91 | 18 | def __init__(self, stream): |
paulb@91 | 19 | |
paulb@91 | 20 | "Initialise the stream with the given underlying 'stream'." |
paulb@91 | 21 | |
paulb@91 | 22 | self.stream = stream |
paulb@91 | 23 | |
paulb@91 | 24 | def read(self): |
paulb@91 | 25 | |
paulb@91 | 26 | "Read the entire message, returning it as a string." |
paulb@91 | 27 | |
paulb@91 | 28 | characters = StringIO() |
paulb@91 | 29 | while 1: |
paulb@91 | 30 | c = self.stream.read() |
paulb@91 | 31 | if c == -1: |
paulb@91 | 32 | return characters.getvalue() |
paulb@91 | 33 | else: |
paulb@91 | 34 | characters.write(chr(c)) |
paulb@91 | 35 | |
paulb@91 | 36 | def readline(self): |
paulb@91 | 37 | |
paulb@91 | 38 | "Read a line from the stream, returning it as a string." |
paulb@91 | 39 | |
paulb@91 | 40 | return self.stream.readLine() |
paulb@91 | 41 | |
paulb@91 | 42 | class Transaction(Generic.Transaction): |
paulb@91 | 43 | |
paulb@91 | 44 | """ |
paulb@91 | 45 | Java Servlet transaction interface. |
paulb@91 | 46 | """ |
paulb@91 | 47 | |
paulb@91 | 48 | def __init__(self, request, response): |
paulb@91 | 49 | |
paulb@91 | 50 | """ |
paulb@91 | 51 | Initialise the transaction using the Java Servlet HTTP 'request' and |
paulb@91 | 52 | 'response'. |
paulb@91 | 53 | """ |
paulb@91 | 54 | |
paulb@91 | 55 | self.request = request |
paulb@91 | 56 | self.response = response |
paulb@91 | 57 | self.status = None |
paulb@128 | 58 | self.user = None |
paulb@91 | 59 | |
paulb@115 | 60 | # Remember the cookies received in the request. |
paulb@115 | 61 | # NOTE: Discarding much of the information received. |
paulb@115 | 62 | |
paulb@115 | 63 | self.cookies_in = {} |
paulb@176 | 64 | for cookie in self.request.getCookies() or []: |
paulb@115 | 65 | cookie_name = cookie.getName() |
paulb@115 | 66 | self.cookies_in[cookie_name] = Cookie(cookie_name, cookie.getValue()) |
paulb@115 | 67 | |
paulb@95 | 68 | def commit(self): |
paulb@95 | 69 | |
paulb@95 | 70 | """ |
paulb@95 | 71 | A special method, synchronising the transaction with framework-specific |
paulb@95 | 72 | objects. |
paulb@95 | 73 | """ |
paulb@95 | 74 | |
paulb@95 | 75 | self.get_response_stream().close() |
paulb@95 | 76 | |
paulb@91 | 77 | # Request-related methods. |
paulb@91 | 78 | |
paulb@91 | 79 | def get_request_stream(self): |
paulb@91 | 80 | |
paulb@91 | 81 | """ |
paulb@186 | 82 | Returns the request stream for the transaction. |
paulb@91 | 83 | """ |
paulb@91 | 84 | |
paulb@91 | 85 | return Stream(self.request.getReader()) |
paulb@91 | 86 | |
paulb@91 | 87 | def get_request_method(self): |
paulb@91 | 88 | |
paulb@91 | 89 | """ |
paulb@186 | 90 | Returns the request method. |
paulb@91 | 91 | """ |
paulb@91 | 92 | |
paulb@91 | 93 | return self.request.getMethod() |
paulb@91 | 94 | |
paulb@91 | 95 | def get_headers(self): |
paulb@91 | 96 | |
paulb@91 | 97 | """ |
paulb@186 | 98 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 99 | names to values. |
paulb@186 | 100 | |
paulb@91 | 101 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 102 | NOTE: need to change. |
paulb@91 | 103 | """ |
paulb@91 | 104 | |
paulb@91 | 105 | headers = {} |
paulb@176 | 106 | header_names_enum = self.request.getHeaderNames() |
paulb@176 | 107 | while header_names_enum.hasMoreElements(): |
paulb@91 | 108 | |
paulb@176 | 109 | # NOTE: Retrieve only a single value (not using getHeaders). |
paulb@91 | 110 | |
paulb@176 | 111 | header_name = header_names_enum.nextElement() |
paulb@176 | 112 | headers[header_name] = self.request.getHeader(header_name) |
paulb@91 | 113 | |
paulb@91 | 114 | return headers |
paulb@91 | 115 | |
paulb@91 | 116 | def get_header_values(self, key): |
paulb@91 | 117 | |
paulb@91 | 118 | """ |
paulb@186 | 119 | Returns a list of all request header values associated with the given |
paulb@186 | 120 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 121 | case-insensitive string. |
paulb@91 | 122 | """ |
paulb@91 | 123 | |
paulb@176 | 124 | values = [] |
paulb@176 | 125 | headers_enum = self.request.getHeaders(key) |
paulb@176 | 126 | while headers_enum.hasMoreElements(): |
paulb@176 | 127 | values.append(headers_enum.nextElement()) |
paulb@176 | 128 | return values |
paulb@91 | 129 | |
paulb@91 | 130 | def get_content_type(self): |
paulb@91 | 131 | |
paulb@91 | 132 | """ |
paulb@186 | 133 | Returns the content type specified on the request, along with the |
paulb@186 | 134 | charset employed. |
paulb@91 | 135 | """ |
paulb@91 | 136 | |
paulb@176 | 137 | content_types = self.get_header_values("Content-Type") or [] |
paulb@91 | 138 | if len(content_types) >= 1: |
paulb@91 | 139 | return self.parse_content_type(content_types[0]) |
paulb@91 | 140 | else: |
paulb@91 | 141 | return None |
paulb@91 | 142 | |
paulb@91 | 143 | def get_content_charsets(self): |
paulb@91 | 144 | |
paulb@91 | 145 | """ |
paulb@91 | 146 | Returns the character set preferences. |
paulb@91 | 147 | """ |
paulb@91 | 148 | |
paulb@176 | 149 | accept_charsets = self.get_header_values("Accept-Charset") or [] |
paulb@91 | 150 | if len(accept_charsets) >= 1: |
paulb@91 | 151 | return self.parse_content_preferences(accept_charsets[0]) |
paulb@91 | 152 | else: |
paulb@91 | 153 | return None |
paulb@91 | 154 | |
paulb@91 | 155 | def get_content_languages(self): |
paulb@91 | 156 | |
paulb@91 | 157 | """ |
paulb@186 | 158 | Returns extracted language information from the transaction. |
paulb@91 | 159 | """ |
paulb@91 | 160 | |
paulb@176 | 161 | accept_languages = self.get_header_values("Accept-Language") or [] |
paulb@91 | 162 | if len(accept_languages) >= 1: |
paulb@91 | 163 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 164 | else: |
paulb@91 | 165 | return None |
paulb@91 | 166 | |
paulb@91 | 167 | def get_path(self): |
paulb@91 | 168 | |
paulb@91 | 169 | """ |
paulb@186 | 170 | Returns the entire path from the request. |
paulb@91 | 171 | """ |
paulb@91 | 172 | |
paulb@162 | 173 | # NOTE: To be verified. |
paulb@162 | 174 | |
paulb@162 | 175 | path = self.get_path_without_query() |
paulb@162 | 176 | qs = self.get_query_string() |
paulb@162 | 177 | if qs: |
paulb@162 | 178 | path += "?" |
paulb@162 | 179 | path += qs |
paulb@162 | 180 | return path |
paulb@162 | 181 | |
paulb@162 | 182 | def get_path_without_query(self): |
paulb@162 | 183 | |
paulb@162 | 184 | """ |
paulb@186 | 185 | Returns the entire path from the request minus the query string. |
paulb@162 | 186 | """ |
paulb@162 | 187 | |
paulb@91 | 188 | return self.request.getServletPath() |
paulb@91 | 189 | |
paulb@91 | 190 | def get_path_info(self): |
paulb@91 | 191 | |
paulb@91 | 192 | """ |
paulb@186 | 193 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 194 | handling the current request) from the request. |
paulb@91 | 195 | """ |
paulb@91 | 196 | |
paulb@91 | 197 | return self.request.getPathInfo() |
paulb@91 | 198 | |
paulb@91 | 199 | def get_query_string(self): |
paulb@91 | 200 | |
paulb@91 | 201 | """ |
paulb@186 | 202 | Returns the query string from the path in the request. |
paulb@91 | 203 | """ |
paulb@91 | 204 | |
paulb@91 | 205 | return self.request.getQueryString() |
paulb@91 | 206 | |
paulb@91 | 207 | # Higher level request-related methods. |
paulb@91 | 208 | |
paulb@91 | 209 | def get_fields_from_path(self): |
paulb@91 | 210 | |
paulb@91 | 211 | """ |
paulb@186 | 212 | Extracts the form fields from the path specified in the transaction. The |
paulb@186 | 213 | underlying framework may refuse to supply fields from the path if |
paulb@186 | 214 | handling a POST transaction. |
paulb@91 | 215 | |
paulb@91 | 216 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 217 | single value is associated with any given field name). |
paulb@91 | 218 | |
paulb@91 | 219 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 220 | NOTE: from the path. |
paulb@91 | 221 | """ |
paulb@91 | 222 | |
paulb@91 | 223 | return self.get_fields_from_body() |
paulb@91 | 224 | |
paulb@167 | 225 | def get_fields_from_body(self, encoding=None): |
paulb@91 | 226 | |
paulb@91 | 227 | """ |
paulb@186 | 228 | Extracts the form fields from the message body in the transaction. The |
paulb@186 | 229 | optional 'encoding' parameter specifies the character encoding of the |
paulb@186 | 230 | message body for cases where no such information is available, but where |
paulb@186 | 231 | the default encoding is to be overridden. |
paulb@91 | 232 | |
paulb@91 | 233 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 234 | single value is associated with any given field name). |
paulb@91 | 235 | |
paulb@91 | 236 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@167 | 237 | NOTE: from the message body. Moreover, the encoding of the fields may |
paulb@167 | 238 | NOTE: not be pertinent. |
paulb@91 | 239 | """ |
paulb@91 | 240 | |
paulb@91 | 241 | parameter_map = self.request.getParameterMap() |
paulb@91 | 242 | fields = {} |
paulb@176 | 243 | if parameter_map: |
paulb@176 | 244 | for key in parameter_map.keySet(): |
paulb@176 | 245 | fields[key] = parameter_map[key] |
paulb@91 | 246 | return fields |
paulb@91 | 247 | |
paulb@91 | 248 | def get_user(self): |
paulb@91 | 249 | |
paulb@91 | 250 | """ |
paulb@186 | 251 | Extracts user information from the transaction. |
paulb@108 | 252 | |
paulb@108 | 253 | Returns a username as a string or None if no user is defined. |
paulb@91 | 254 | """ |
paulb@91 | 255 | |
paulb@128 | 256 | if self.user is not None: |
paulb@128 | 257 | return self.user |
paulb@128 | 258 | else: |
paulb@128 | 259 | return self.request.getRemoteUser() |
paulb@91 | 260 | |
paulb@115 | 261 | def get_cookies(self): |
paulb@115 | 262 | |
paulb@115 | 263 | """ |
paulb@186 | 264 | Obtains cookie information from the request. |
paulb@115 | 265 | |
paulb@115 | 266 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 267 | """ |
paulb@115 | 268 | |
paulb@115 | 269 | return self.cookies_in |
paulb@115 | 270 | |
paulb@115 | 271 | def get_cookie(self, cookie_name): |
paulb@115 | 272 | |
paulb@115 | 273 | """ |
paulb@186 | 274 | Obtains cookie information from the request. |
paulb@115 | 275 | |
paulb@115 | 276 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 277 | cookie exists. |
paulb@115 | 278 | """ |
paulb@115 | 279 | |
paulb@115 | 280 | return self.cookies_in.get(cookie_name) |
paulb@115 | 281 | |
paulb@91 | 282 | # Response-related methods. |
paulb@91 | 283 | |
paulb@91 | 284 | def get_response_stream(self): |
paulb@91 | 285 | |
paulb@91 | 286 | """ |
paulb@186 | 287 | Returns the response stream for the transaction. |
paulb@91 | 288 | """ |
paulb@91 | 289 | |
paulb@176 | 290 | return self.response.getOutputStream() |
paulb@91 | 291 | |
paulb@91 | 292 | def get_response_code(self): |
paulb@91 | 293 | |
paulb@91 | 294 | """ |
paulb@91 | 295 | Get the response code associated with the transaction. If no response |
paulb@91 | 296 | code is defined, None is returned. |
paulb@91 | 297 | """ |
paulb@91 | 298 | |
paulb@91 | 299 | return self.status |
paulb@91 | 300 | |
paulb@91 | 301 | def set_response_code(self, response_code): |
paulb@91 | 302 | |
paulb@91 | 303 | """ |
paulb@91 | 304 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 305 | specification. |
paulb@91 | 306 | """ |
paulb@91 | 307 | |
paulb@91 | 308 | self.status = response_code |
paulb@91 | 309 | self.response.setStatus(self.status) |
paulb@91 | 310 | |
paulb@91 | 311 | def set_header_value(self, header, value): |
paulb@91 | 312 | |
paulb@91 | 313 | """ |
paulb@91 | 314 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 315 | """ |
paulb@91 | 316 | |
paulb@91 | 317 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 318 | |
paulb@91 | 319 | def set_content_type(self, content_type): |
paulb@91 | 320 | |
paulb@91 | 321 | """ |
paulb@186 | 322 | Sets the 'content_type' for the response. |
paulb@91 | 323 | """ |
paulb@91 | 324 | |
paulb@91 | 325 | return self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@91 | 326 | |
paulb@115 | 327 | # Higher level response-related methods. |
paulb@115 | 328 | |
paulb@115 | 329 | def set_cookie(self, cookie): |
paulb@115 | 330 | |
paulb@115 | 331 | """ |
paulb@186 | 332 | Stores the given 'cookie' object in the response. |
paulb@115 | 333 | """ |
paulb@115 | 334 | |
paulb@115 | 335 | new_cookie = javax.servlet.http.Cookie(cookie.name, cookie.value) |
paulb@115 | 336 | self.response.addCookie(new_cookie) |
paulb@115 | 337 | |
paulb@115 | 338 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 339 | |
paulb@115 | 340 | """ |
paulb@186 | 341 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@115 | 342 | |
paulb@115 | 343 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 344 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 345 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 346 | """ |
paulb@115 | 347 | |
paulb@115 | 348 | cookie = javax.servlet.http.Cookie(name, value) |
paulb@115 | 349 | if path is not None: |
paulb@115 | 350 | cookie.setPath(path) |
paulb@115 | 351 | |
paulb@115 | 352 | # NOTE: The expires parameter seems not to be supported. |
paulb@115 | 353 | |
paulb@115 | 354 | self.response.addCookie(cookie) |
paulb@115 | 355 | |
paulb@115 | 356 | def delete_cookie(self, cookie_name): |
paulb@115 | 357 | |
paulb@115 | 358 | """ |
paulb@186 | 359 | Adds to the response a request that the cookie with the given |
paulb@186 | 360 | 'cookie_name' be deleted/discarded by the client. |
paulb@115 | 361 | """ |
paulb@115 | 362 | |
paulb@115 | 363 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 364 | # has been sent the cookie or not. |
paulb@115 | 365 | # NOTE: Magic discovered in Webware. |
paulb@115 | 366 | |
paulb@115 | 367 | cookie = javax.servlet.http.Cookie(cookie_name, "") |
paulb@115 | 368 | cookie.setPath("/") |
paulb@115 | 369 | cookie.setMaxAge(0) |
paulb@115 | 370 | self.response.addCookie(cookie) |
paulb@115 | 371 | |
paulb@183 | 372 | # Session-related methods. |
paulb@183 | 373 | |
paulb@183 | 374 | def get_session(self, create=1): |
paulb@183 | 375 | |
paulb@183 | 376 | """ |
paulb@186 | 377 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 378 | transaction. |
paulb@183 | 379 | |
paulb@183 | 380 | If no session has yet been established according to information |
paulb@183 | 381 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 382 | determines whether a new session will be established. |
paulb@183 | 383 | |
paulb@183 | 384 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 385 | then None is returned. In all other cases, a session object is created |
paulb@183 | 386 | (where appropriate) and returned. |
paulb@183 | 387 | """ |
paulb@183 | 388 | |
paulb@183 | 389 | return self.request.getSession(create) |
paulb@183 | 390 | |
paulb@183 | 391 | def expire_session(self): |
paulb@183 | 392 | |
paulb@183 | 393 | """ |
paulb@186 | 394 | Expires any session established according to information provided in the |
paulb@186 | 395 | transaction. |
paulb@183 | 396 | """ |
paulb@183 | 397 | |
paulb@183 | 398 | session = self.get_session(create=0) |
paulb@183 | 399 | if session: |
paulb@183 | 400 | session.invalidate() |
paulb@183 | 401 | |
paulb@128 | 402 | # Application-specific methods. |
paulb@128 | 403 | |
paulb@128 | 404 | def set_user(self, username): |
paulb@128 | 405 | |
paulb@128 | 406 | """ |
paulb@128 | 407 | An application-specific method which sets the user information with |
paulb@128 | 408 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 409 | 'get_user'. |
paulb@128 | 410 | """ |
paulb@128 | 411 | |
paulb@128 | 412 | self.user = username |
paulb@128 | 413 | |
paulb@183 | 414 | class Session: |
paulb@183 | 415 | |
paulb@183 | 416 | """ |
paulb@183 | 417 | A simple session class with behaviour more similar to the Python framework |
paulb@183 | 418 | session classes. |
paulb@183 | 419 | """ |
paulb@183 | 420 | |
paulb@183 | 421 | def __init__(self, session): |
paulb@183 | 422 | |
paulb@183 | 423 | "Initialise the session object with the framework 'session' object." |
paulb@183 | 424 | |
paulb@183 | 425 | self.session = session |
paulb@183 | 426 | |
paulb@183 | 427 | def keys(self): |
paulb@183 | 428 | keys = [] |
paulb@183 | 429 | keys_enum = self.session.getAttributeNames() |
paulb@183 | 430 | while keys_enum.hasMoreElements(): |
paulb@183 | 431 | keys.append(keys_enum.nextElement()) |
paulb@183 | 432 | return keys |
paulb@183 | 433 | |
paulb@183 | 434 | def values(self): |
paulb@183 | 435 | values = [] |
paulb@183 | 436 | for key in self.keys(): |
paulb@183 | 437 | values.append(self[key]) |
paulb@183 | 438 | return values |
paulb@183 | 439 | |
paulb@183 | 440 | def items(self): |
paulb@183 | 441 | items = [] |
paulb@183 | 442 | for key in self.keys(): |
paulb@183 | 443 | items.append((key, self[key])) |
paulb@183 | 444 | return items |
paulb@183 | 445 | |
paulb@183 | 446 | def __getitem__(self, key): |
paulb@183 | 447 | return self.session.getAttribute(key) |
paulb@183 | 448 | |
paulb@183 | 449 | def __setitem__(self, key, value): |
paulb@183 | 450 | self.session.setAttribute(key, value) |
paulb@183 | 451 | |
paulb@183 | 452 | def __delitem__(self, key): |
paulb@183 | 453 | self.session.removeAttribute(key) |
paulb@183 | 454 | |
paulb@91 | 455 | # vim: tabstop=4 expandtab shiftwidth=4 |