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@91 | 82 | A framework-specific method which returns the request stream for |
paulb@91 | 83 | the transaction. |
paulb@91 | 84 | """ |
paulb@91 | 85 | |
paulb@91 | 86 | return Stream(self.request.getReader()) |
paulb@91 | 87 | |
paulb@91 | 88 | def get_request_method(self): |
paulb@91 | 89 | |
paulb@91 | 90 | """ |
paulb@91 | 91 | A framework-specific method which gets the request method. |
paulb@91 | 92 | """ |
paulb@91 | 93 | |
paulb@91 | 94 | return self.request.getMethod() |
paulb@91 | 95 | |
paulb@91 | 96 | def get_headers(self): |
paulb@91 | 97 | |
paulb@91 | 98 | """ |
paulb@108 | 99 | A framework-specific method which returns all request headers as a |
paulb@108 | 100 | dictionary-like object mapping header names to values. |
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@91 | 119 | A framework-specific method which returns a list of all request header |
paulb@91 | 120 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@91 | 121 | 'key' is treated as a 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@91 | 133 | A framework-specific method which gets the content type specified on the |
paulb@91 | 134 | request, along with the 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@91 | 158 | A framework-specific method which extracts language information from |
paulb@91 | 159 | the transaction. |
paulb@91 | 160 | """ |
paulb@91 | 161 | |
paulb@176 | 162 | accept_languages = self.get_header_values("Accept-Language") or [] |
paulb@91 | 163 | if len(accept_languages) >= 1: |
paulb@91 | 164 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 165 | else: |
paulb@91 | 166 | return None |
paulb@91 | 167 | |
paulb@91 | 168 | def get_path(self): |
paulb@91 | 169 | |
paulb@91 | 170 | """ |
paulb@91 | 171 | A framework-specific method which gets the entire path from the request. |
paulb@91 | 172 | """ |
paulb@91 | 173 | |
paulb@162 | 174 | # NOTE: To be verified. |
paulb@162 | 175 | |
paulb@162 | 176 | path = self.get_path_without_query() |
paulb@162 | 177 | qs = self.get_query_string() |
paulb@162 | 178 | if qs: |
paulb@162 | 179 | path += "?" |
paulb@162 | 180 | path += qs |
paulb@162 | 181 | return path |
paulb@162 | 182 | |
paulb@162 | 183 | def get_path_without_query(self): |
paulb@162 | 184 | |
paulb@162 | 185 | """ |
paulb@162 | 186 | A framework-specific method which gets the entire path from the request |
paulb@162 | 187 | minus the query string. |
paulb@162 | 188 | """ |
paulb@162 | 189 | |
paulb@91 | 190 | return self.request.getServletPath() |
paulb@91 | 191 | |
paulb@91 | 192 | def get_path_info(self): |
paulb@91 | 193 | |
paulb@91 | 194 | """ |
paulb@91 | 195 | A framework-specific method which gets the "path info" (the part of the |
paulb@91 | 196 | URL after the resource name handling the current request) from the |
paulb@91 | 197 | request. |
paulb@91 | 198 | """ |
paulb@91 | 199 | |
paulb@91 | 200 | return self.request.getPathInfo() |
paulb@91 | 201 | |
paulb@91 | 202 | def get_query_string(self): |
paulb@91 | 203 | |
paulb@91 | 204 | """ |
paulb@91 | 205 | A framework-specific method which gets the query string from the path in |
paulb@91 | 206 | the request. |
paulb@91 | 207 | """ |
paulb@91 | 208 | |
paulb@91 | 209 | return self.request.getQueryString() |
paulb@91 | 210 | |
paulb@91 | 211 | # Higher level request-related methods. |
paulb@91 | 212 | |
paulb@91 | 213 | def get_fields_from_path(self): |
paulb@91 | 214 | |
paulb@91 | 215 | """ |
paulb@91 | 216 | A framework-specific method which extracts the form fields from the |
paulb@91 | 217 | path specified in the transaction. The underlying framework may refuse |
paulb@91 | 218 | to supply fields from the path if handling a POST transaction. |
paulb@91 | 219 | |
paulb@91 | 220 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 221 | single value is associated with any given field name). |
paulb@91 | 222 | |
paulb@91 | 223 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 224 | NOTE: from the path. |
paulb@91 | 225 | """ |
paulb@91 | 226 | |
paulb@91 | 227 | return self.get_fields_from_body() |
paulb@91 | 228 | |
paulb@167 | 229 | def get_fields_from_body(self, encoding=None): |
paulb@91 | 230 | |
paulb@91 | 231 | """ |
paulb@91 | 232 | A framework-specific method which extracts the form fields from the |
paulb@167 | 233 | message body in the transaction. The optional 'encoding' parameter |
paulb@167 | 234 | specifies the character encoding of the message body for cases where no |
paulb@167 | 235 | such information is available, but where the default encoding is to be |
paulb@167 | 236 | overridden. |
paulb@91 | 237 | |
paulb@91 | 238 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 239 | single value is associated with any given field name). |
paulb@91 | 240 | |
paulb@91 | 241 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@167 | 242 | NOTE: from the message body. Moreover, the encoding of the fields may |
paulb@167 | 243 | NOTE: not be pertinent. |
paulb@91 | 244 | """ |
paulb@91 | 245 | |
paulb@91 | 246 | parameter_map = self.request.getParameterMap() |
paulb@91 | 247 | fields = {} |
paulb@176 | 248 | if parameter_map: |
paulb@176 | 249 | for key in parameter_map.keySet(): |
paulb@176 | 250 | fields[key] = parameter_map[key] |
paulb@91 | 251 | return fields |
paulb@91 | 252 | |
paulb@91 | 253 | def get_user(self): |
paulb@91 | 254 | |
paulb@91 | 255 | """ |
paulb@91 | 256 | A framework-specific method which extracts user information from the |
paulb@91 | 257 | transaction. |
paulb@108 | 258 | |
paulb@108 | 259 | Returns a username as a string or None if no user is defined. |
paulb@91 | 260 | """ |
paulb@91 | 261 | |
paulb@128 | 262 | if self.user is not None: |
paulb@128 | 263 | return self.user |
paulb@128 | 264 | else: |
paulb@128 | 265 | return self.request.getRemoteUser() |
paulb@91 | 266 | |
paulb@115 | 267 | def get_cookies(self): |
paulb@115 | 268 | |
paulb@115 | 269 | """ |
paulb@115 | 270 | A framework-specific method which obtains cookie information from the |
paulb@115 | 271 | request. |
paulb@115 | 272 | |
paulb@115 | 273 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 274 | """ |
paulb@115 | 275 | |
paulb@115 | 276 | return self.cookies_in |
paulb@115 | 277 | |
paulb@115 | 278 | def get_cookie(self, cookie_name): |
paulb@115 | 279 | |
paulb@115 | 280 | """ |
paulb@115 | 281 | A framework-specific method which obtains cookie information from the |
paulb@115 | 282 | request. |
paulb@115 | 283 | |
paulb@115 | 284 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 285 | cookie exists. |
paulb@115 | 286 | """ |
paulb@115 | 287 | |
paulb@115 | 288 | return self.cookies_in.get(cookie_name) |
paulb@115 | 289 | |
paulb@91 | 290 | # Response-related methods. |
paulb@91 | 291 | |
paulb@91 | 292 | def get_response_stream(self): |
paulb@91 | 293 | |
paulb@91 | 294 | """ |
paulb@91 | 295 | A framework-specific method which returns the response stream for |
paulb@91 | 296 | the transaction. |
paulb@91 | 297 | """ |
paulb@91 | 298 | |
paulb@176 | 299 | return self.response.getOutputStream() |
paulb@91 | 300 | |
paulb@91 | 301 | def get_response_code(self): |
paulb@91 | 302 | |
paulb@91 | 303 | """ |
paulb@91 | 304 | Get the response code associated with the transaction. If no response |
paulb@91 | 305 | code is defined, None is returned. |
paulb@91 | 306 | """ |
paulb@91 | 307 | |
paulb@91 | 308 | return self.status |
paulb@91 | 309 | |
paulb@91 | 310 | def set_response_code(self, response_code): |
paulb@91 | 311 | |
paulb@91 | 312 | """ |
paulb@91 | 313 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 314 | specification. |
paulb@91 | 315 | """ |
paulb@91 | 316 | |
paulb@91 | 317 | self.status = response_code |
paulb@91 | 318 | self.response.setStatus(self.status) |
paulb@91 | 319 | |
paulb@91 | 320 | def set_header_value(self, header, value): |
paulb@91 | 321 | |
paulb@91 | 322 | """ |
paulb@91 | 323 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 324 | """ |
paulb@91 | 325 | |
paulb@91 | 326 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 327 | |
paulb@91 | 328 | def set_content_type(self, content_type): |
paulb@91 | 329 | |
paulb@91 | 330 | """ |
paulb@91 | 331 | A framework-specific method which sets the 'content_type' for the |
paulb@91 | 332 | response. |
paulb@91 | 333 | """ |
paulb@91 | 334 | |
paulb@91 | 335 | return self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@91 | 336 | |
paulb@115 | 337 | # Higher level response-related methods. |
paulb@115 | 338 | |
paulb@115 | 339 | def set_cookie(self, cookie): |
paulb@115 | 340 | |
paulb@115 | 341 | """ |
paulb@115 | 342 | A framework-specific method which stores the given 'cookie' object in |
paulb@115 | 343 | the response. |
paulb@115 | 344 | """ |
paulb@115 | 345 | |
paulb@115 | 346 | new_cookie = javax.servlet.http.Cookie(cookie.name, cookie.value) |
paulb@115 | 347 | self.response.addCookie(new_cookie) |
paulb@115 | 348 | |
paulb@115 | 349 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 350 | |
paulb@115 | 351 | """ |
paulb@115 | 352 | A framework-specific method which stores a cookie with the given 'name' |
paulb@115 | 353 | and 'value' in the response. |
paulb@115 | 354 | |
paulb@115 | 355 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 356 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 357 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 358 | """ |
paulb@115 | 359 | |
paulb@115 | 360 | cookie = javax.servlet.http.Cookie(name, value) |
paulb@115 | 361 | if path is not None: |
paulb@115 | 362 | cookie.setPath(path) |
paulb@115 | 363 | |
paulb@115 | 364 | # NOTE: The expires parameter seems not to be supported. |
paulb@115 | 365 | |
paulb@115 | 366 | self.response.addCookie(cookie) |
paulb@115 | 367 | |
paulb@115 | 368 | def delete_cookie(self, cookie_name): |
paulb@115 | 369 | |
paulb@115 | 370 | """ |
paulb@115 | 371 | A framework-specific method which adds to the response a request that |
paulb@115 | 372 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@115 | 373 | client. |
paulb@115 | 374 | """ |
paulb@115 | 375 | |
paulb@115 | 376 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 377 | # has been sent the cookie or not. |
paulb@115 | 378 | # NOTE: Magic discovered in Webware. |
paulb@115 | 379 | |
paulb@115 | 380 | cookie = javax.servlet.http.Cookie(cookie_name, "") |
paulb@115 | 381 | cookie.setPath("/") |
paulb@115 | 382 | cookie.setMaxAge(0) |
paulb@115 | 383 | self.response.addCookie(cookie) |
paulb@115 | 384 | |
paulb@128 | 385 | # Application-specific methods. |
paulb@128 | 386 | |
paulb@128 | 387 | def set_user(self, username): |
paulb@128 | 388 | |
paulb@128 | 389 | """ |
paulb@128 | 390 | An application-specific method which sets the user information with |
paulb@128 | 391 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 392 | 'get_user'. |
paulb@128 | 393 | """ |
paulb@128 | 394 | |
paulb@128 | 395 | self.user = username |
paulb@128 | 396 | |
paulb@91 | 397 | # vim: tabstop=4 expandtab shiftwidth=4 |