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@115 | 64 | for cookie in self.request.getCookies(): |
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@91 | 106 | header_names = self.request.getHeaderNames() |
paulb@91 | 107 | if header_names: |
paulb@91 | 108 | for header_name in header_names: |
paulb@91 | 109 | |
paulb@91 | 110 | # NOTE: Retrieve only a single value (not using getHeaders). |
paulb@91 | 111 | |
paulb@91 | 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@91 | 124 | return self.request.getHeaders(key) |
paulb@91 | 125 | |
paulb@91 | 126 | def get_content_type(self): |
paulb@91 | 127 | |
paulb@91 | 128 | """ |
paulb@91 | 129 | A framework-specific method which gets the content type specified on the |
paulb@91 | 130 | request, along with the charset employed. |
paulb@91 | 131 | """ |
paulb@91 | 132 | |
paulb@91 | 133 | content_types = self.get_header_values("Content-Type") |
paulb@91 | 134 | if len(content_types) >= 1: |
paulb@91 | 135 | return self.parse_content_type(content_types[0]) |
paulb@91 | 136 | else: |
paulb@91 | 137 | return None |
paulb@91 | 138 | |
paulb@91 | 139 | def get_content_charsets(self): |
paulb@91 | 140 | |
paulb@91 | 141 | """ |
paulb@91 | 142 | Returns the character set preferences. |
paulb@91 | 143 | """ |
paulb@91 | 144 | |
paulb@91 | 145 | accept_charsets = self.get_header_values("Accept-Charset") |
paulb@91 | 146 | if len(accept_charsets) >= 1: |
paulb@91 | 147 | return self.parse_content_preferences(accept_charsets[0]) |
paulb@91 | 148 | else: |
paulb@91 | 149 | return None |
paulb@91 | 150 | |
paulb@91 | 151 | def get_content_languages(self): |
paulb@91 | 152 | |
paulb@91 | 153 | """ |
paulb@91 | 154 | A framework-specific method which extracts language information from |
paulb@91 | 155 | the transaction. |
paulb@91 | 156 | """ |
paulb@91 | 157 | |
paulb@91 | 158 | accept_languages = self.get_header_values("Accept-Language") |
paulb@91 | 159 | if len(accept_languages) >= 1: |
paulb@91 | 160 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 161 | else: |
paulb@91 | 162 | return None |
paulb@91 | 163 | |
paulb@91 | 164 | def get_path(self): |
paulb@91 | 165 | |
paulb@91 | 166 | """ |
paulb@91 | 167 | A framework-specific method which gets the entire path from the request. |
paulb@91 | 168 | """ |
paulb@91 | 169 | |
paulb@91 | 170 | return self.request.getServletPath() |
paulb@91 | 171 | |
paulb@91 | 172 | def get_path_info(self): |
paulb@91 | 173 | |
paulb@91 | 174 | """ |
paulb@91 | 175 | A framework-specific method which gets the "path info" (the part of the |
paulb@91 | 176 | URL after the resource name handling the current request) from the |
paulb@91 | 177 | request. |
paulb@91 | 178 | """ |
paulb@91 | 179 | |
paulb@91 | 180 | return self.request.getPathInfo() |
paulb@91 | 181 | |
paulb@91 | 182 | def get_query_string(self): |
paulb@91 | 183 | |
paulb@91 | 184 | """ |
paulb@91 | 185 | A framework-specific method which gets the query string from the path in |
paulb@91 | 186 | the request. |
paulb@91 | 187 | """ |
paulb@91 | 188 | |
paulb@91 | 189 | return self.request.getQueryString() |
paulb@91 | 190 | |
paulb@91 | 191 | # Higher level request-related methods. |
paulb@91 | 192 | |
paulb@91 | 193 | def get_fields_from_path(self): |
paulb@91 | 194 | |
paulb@91 | 195 | """ |
paulb@91 | 196 | A framework-specific method which extracts the form fields from the |
paulb@91 | 197 | path specified in the transaction. The underlying framework may refuse |
paulb@91 | 198 | to supply fields from the path if handling a POST transaction. |
paulb@91 | 199 | |
paulb@91 | 200 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 201 | single value is associated with any given field name). |
paulb@91 | 202 | |
paulb@91 | 203 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 204 | NOTE: from the path. |
paulb@91 | 205 | """ |
paulb@91 | 206 | |
paulb@91 | 207 | return self.get_fields_from_body() |
paulb@91 | 208 | |
paulb@91 | 209 | def get_fields_from_body(self): |
paulb@91 | 210 | |
paulb@91 | 211 | """ |
paulb@91 | 212 | A framework-specific method which extracts the form fields from the |
paulb@91 | 213 | message body in the transaction. |
paulb@91 | 214 | |
paulb@91 | 215 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 216 | single value is associated with any given field name). |
paulb@91 | 217 | |
paulb@91 | 218 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 219 | NOTE: from the message body. |
paulb@91 | 220 | """ |
paulb@91 | 221 | |
paulb@91 | 222 | parameter_map = self.request.getParameterMap() |
paulb@91 | 223 | fields = {} |
paulb@91 | 224 | for key in parameter_map.keySet(): |
paulb@91 | 225 | fields[key] = parameter_map[key] |
paulb@91 | 226 | return fields |
paulb@91 | 227 | |
paulb@91 | 228 | def get_user(self): |
paulb@91 | 229 | |
paulb@91 | 230 | """ |
paulb@91 | 231 | A framework-specific method which extracts user information from the |
paulb@91 | 232 | transaction. |
paulb@108 | 233 | |
paulb@108 | 234 | Returns a username as a string or None if no user is defined. |
paulb@91 | 235 | """ |
paulb@91 | 236 | |
paulb@128 | 237 | if self.user is not None: |
paulb@128 | 238 | return self.user |
paulb@128 | 239 | else: |
paulb@128 | 240 | return self.request.getRemoteUser() |
paulb@91 | 241 | |
paulb@115 | 242 | def get_cookies(self): |
paulb@115 | 243 | |
paulb@115 | 244 | """ |
paulb@115 | 245 | A framework-specific method which obtains cookie information from the |
paulb@115 | 246 | request. |
paulb@115 | 247 | |
paulb@115 | 248 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 249 | """ |
paulb@115 | 250 | |
paulb@115 | 251 | return self.cookies_in |
paulb@115 | 252 | |
paulb@115 | 253 | def get_cookie(self, cookie_name): |
paulb@115 | 254 | |
paulb@115 | 255 | """ |
paulb@115 | 256 | A framework-specific method which obtains cookie information from the |
paulb@115 | 257 | request. |
paulb@115 | 258 | |
paulb@115 | 259 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 260 | cookie exists. |
paulb@115 | 261 | """ |
paulb@115 | 262 | |
paulb@115 | 263 | return self.cookies_in.get(cookie_name) |
paulb@115 | 264 | |
paulb@91 | 265 | # Response-related methods. |
paulb@91 | 266 | |
paulb@91 | 267 | def get_response_stream(self): |
paulb@91 | 268 | |
paulb@91 | 269 | """ |
paulb@91 | 270 | A framework-specific method which returns the response stream for |
paulb@91 | 271 | the transaction. |
paulb@91 | 272 | """ |
paulb@91 | 273 | |
paulb@91 | 274 | return self.response.getWriter() |
paulb@91 | 275 | |
paulb@91 | 276 | def get_response_code(self): |
paulb@91 | 277 | |
paulb@91 | 278 | """ |
paulb@91 | 279 | Get the response code associated with the transaction. If no response |
paulb@91 | 280 | code is defined, None is returned. |
paulb@91 | 281 | """ |
paulb@91 | 282 | |
paulb@91 | 283 | return self.status |
paulb@91 | 284 | |
paulb@91 | 285 | def set_response_code(self, response_code): |
paulb@91 | 286 | |
paulb@91 | 287 | """ |
paulb@91 | 288 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 289 | specification. |
paulb@91 | 290 | """ |
paulb@91 | 291 | |
paulb@91 | 292 | self.status = response_code |
paulb@91 | 293 | self.response.setStatus(self.status) |
paulb@91 | 294 | |
paulb@91 | 295 | def set_header_value(self, header, value): |
paulb@91 | 296 | |
paulb@91 | 297 | """ |
paulb@91 | 298 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 299 | """ |
paulb@91 | 300 | |
paulb@91 | 301 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 302 | |
paulb@91 | 303 | def set_content_type(self, content_type): |
paulb@91 | 304 | |
paulb@91 | 305 | """ |
paulb@91 | 306 | A framework-specific method which sets the 'content_type' for the |
paulb@91 | 307 | response. |
paulb@91 | 308 | """ |
paulb@91 | 309 | |
paulb@91 | 310 | return self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@91 | 311 | |
paulb@115 | 312 | # Higher level response-related methods. |
paulb@115 | 313 | |
paulb@115 | 314 | def set_cookie(self, cookie): |
paulb@115 | 315 | |
paulb@115 | 316 | """ |
paulb@115 | 317 | A framework-specific method which stores the given 'cookie' object in |
paulb@115 | 318 | the response. |
paulb@115 | 319 | """ |
paulb@115 | 320 | |
paulb@115 | 321 | new_cookie = javax.servlet.http.Cookie(cookie.name, cookie.value) |
paulb@115 | 322 | self.response.addCookie(new_cookie) |
paulb@115 | 323 | |
paulb@115 | 324 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 325 | |
paulb@115 | 326 | """ |
paulb@115 | 327 | A framework-specific method which stores a cookie with the given 'name' |
paulb@115 | 328 | and 'value' in the response. |
paulb@115 | 329 | |
paulb@115 | 330 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 331 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 332 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 333 | """ |
paulb@115 | 334 | |
paulb@115 | 335 | cookie = javax.servlet.http.Cookie(name, value) |
paulb@115 | 336 | if path is not None: |
paulb@115 | 337 | cookie.setPath(path) |
paulb@115 | 338 | |
paulb@115 | 339 | # NOTE: The expires parameter seems not to be supported. |
paulb@115 | 340 | |
paulb@115 | 341 | self.response.addCookie(cookie) |
paulb@115 | 342 | |
paulb@115 | 343 | def delete_cookie(self, cookie_name): |
paulb@115 | 344 | |
paulb@115 | 345 | """ |
paulb@115 | 346 | A framework-specific method which adds to the response a request that |
paulb@115 | 347 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@115 | 348 | client. |
paulb@115 | 349 | """ |
paulb@115 | 350 | |
paulb@115 | 351 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 352 | # has been sent the cookie or not. |
paulb@115 | 353 | # NOTE: Magic discovered in Webware. |
paulb@115 | 354 | |
paulb@115 | 355 | cookie = javax.servlet.http.Cookie(cookie_name, "") |
paulb@115 | 356 | cookie.setPath("/") |
paulb@115 | 357 | cookie.setMaxAge(0) |
paulb@115 | 358 | self.response.addCookie(cookie) |
paulb@115 | 359 | |
paulb@128 | 360 | # Application-specific methods. |
paulb@128 | 361 | |
paulb@128 | 362 | def set_user(self, username): |
paulb@128 | 363 | |
paulb@128 | 364 | """ |
paulb@128 | 365 | An application-specific method which sets the user information with |
paulb@128 | 366 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 367 | 'get_user'. |
paulb@128 | 368 | """ |
paulb@128 | 369 | |
paulb@128 | 370 | self.user = username |
paulb@128 | 371 | |
paulb@91 | 372 | # vim: tabstop=4 expandtab shiftwidth=4 |