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@91 | 9 | |
paulb@91 | 10 | class Stream: |
paulb@91 | 11 | |
paulb@91 | 12 | """ |
paulb@91 | 13 | Wrapper around java.io.BufferedReader. |
paulb@91 | 14 | """ |
paulb@91 | 15 | |
paulb@91 | 16 | def __init__(self, stream): |
paulb@91 | 17 | |
paulb@91 | 18 | "Initialise the stream with the given underlying 'stream'." |
paulb@91 | 19 | |
paulb@91 | 20 | self.stream = stream |
paulb@91 | 21 | |
paulb@91 | 22 | def read(self): |
paulb@91 | 23 | |
paulb@91 | 24 | "Read the entire message, returning it as a string." |
paulb@91 | 25 | |
paulb@91 | 26 | characters = StringIO() |
paulb@91 | 27 | while 1: |
paulb@91 | 28 | c = self.stream.read() |
paulb@91 | 29 | if c == -1: |
paulb@91 | 30 | return characters.getvalue() |
paulb@91 | 31 | else: |
paulb@91 | 32 | characters.write(chr(c)) |
paulb@91 | 33 | |
paulb@91 | 34 | def readline(self): |
paulb@91 | 35 | |
paulb@91 | 36 | "Read a line from the stream, returning it as a string." |
paulb@91 | 37 | |
paulb@91 | 38 | return self.stream.readLine() |
paulb@91 | 39 | |
paulb@91 | 40 | class Transaction(Generic.Transaction): |
paulb@91 | 41 | |
paulb@91 | 42 | """ |
paulb@91 | 43 | Java Servlet transaction interface. |
paulb@91 | 44 | """ |
paulb@91 | 45 | |
paulb@91 | 46 | def __init__(self, request, response): |
paulb@91 | 47 | |
paulb@91 | 48 | """ |
paulb@91 | 49 | Initialise the transaction using the Java Servlet HTTP 'request' and |
paulb@91 | 50 | 'response'. |
paulb@91 | 51 | """ |
paulb@91 | 52 | |
paulb@91 | 53 | self.request = request |
paulb@91 | 54 | self.response = response |
paulb@91 | 55 | self.status = None |
paulb@91 | 56 | |
paulb@95 | 57 | def commit(self): |
paulb@95 | 58 | |
paulb@95 | 59 | """ |
paulb@95 | 60 | A special method, synchronising the transaction with framework-specific |
paulb@95 | 61 | objects. |
paulb@95 | 62 | """ |
paulb@95 | 63 | |
paulb@95 | 64 | self.get_response_stream().close() |
paulb@95 | 65 | |
paulb@91 | 66 | # Request-related methods. |
paulb@91 | 67 | |
paulb@91 | 68 | def get_request_stream(self): |
paulb@91 | 69 | |
paulb@91 | 70 | """ |
paulb@91 | 71 | A framework-specific method which returns the request stream for |
paulb@91 | 72 | the transaction. |
paulb@91 | 73 | """ |
paulb@91 | 74 | |
paulb@91 | 75 | return Stream(self.request.getReader()) |
paulb@91 | 76 | |
paulb@91 | 77 | def get_request_method(self): |
paulb@91 | 78 | |
paulb@91 | 79 | """ |
paulb@91 | 80 | A framework-specific method which gets the request method. |
paulb@91 | 81 | """ |
paulb@91 | 82 | |
paulb@91 | 83 | return self.request.getMethod() |
paulb@91 | 84 | |
paulb@91 | 85 | def get_headers(self): |
paulb@91 | 86 | |
paulb@91 | 87 | """ |
paulb@108 | 88 | A framework-specific method which returns all request headers as a |
paulb@108 | 89 | dictionary-like object mapping header names to values. |
paulb@91 | 90 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 91 | NOTE: need to change. |
paulb@91 | 92 | """ |
paulb@91 | 93 | |
paulb@91 | 94 | headers = {} |
paulb@91 | 95 | header_names = self.request.getHeaderNames() |
paulb@91 | 96 | if header_names: |
paulb@91 | 97 | for header_name in header_names: |
paulb@91 | 98 | |
paulb@91 | 99 | # NOTE: Retrieve only a single value (not using getHeaders). |
paulb@91 | 100 | |
paulb@91 | 101 | headers[header_name] = self.request.getHeader(header_name) |
paulb@91 | 102 | |
paulb@91 | 103 | return headers |
paulb@91 | 104 | |
paulb@91 | 105 | def get_header_values(self, key): |
paulb@91 | 106 | |
paulb@91 | 107 | """ |
paulb@91 | 108 | A framework-specific method which returns a list of all request header |
paulb@91 | 109 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@91 | 110 | 'key' is treated as a case-insensitive string. |
paulb@91 | 111 | """ |
paulb@91 | 112 | |
paulb@91 | 113 | return self.request.getHeaders(key) |
paulb@91 | 114 | |
paulb@91 | 115 | def get_content_type(self): |
paulb@91 | 116 | |
paulb@91 | 117 | """ |
paulb@91 | 118 | A framework-specific method which gets the content type specified on the |
paulb@91 | 119 | request, along with the charset employed. |
paulb@91 | 120 | """ |
paulb@91 | 121 | |
paulb@91 | 122 | content_types = self.get_header_values("Content-Type") |
paulb@91 | 123 | if len(content_types) >= 1: |
paulb@91 | 124 | return self.parse_content_type(content_types[0]) |
paulb@91 | 125 | else: |
paulb@91 | 126 | return None |
paulb@91 | 127 | |
paulb@91 | 128 | def get_content_charsets(self): |
paulb@91 | 129 | |
paulb@91 | 130 | """ |
paulb@91 | 131 | Returns the character set preferences. |
paulb@91 | 132 | """ |
paulb@91 | 133 | |
paulb@91 | 134 | accept_charsets = self.get_header_values("Accept-Charset") |
paulb@91 | 135 | if len(accept_charsets) >= 1: |
paulb@91 | 136 | return self.parse_content_preferences(accept_charsets[0]) |
paulb@91 | 137 | else: |
paulb@91 | 138 | return None |
paulb@91 | 139 | |
paulb@91 | 140 | def get_content_languages(self): |
paulb@91 | 141 | |
paulb@91 | 142 | """ |
paulb@91 | 143 | A framework-specific method which extracts language information from |
paulb@91 | 144 | the transaction. |
paulb@91 | 145 | """ |
paulb@91 | 146 | |
paulb@91 | 147 | accept_languages = self.get_header_values("Accept-Language") |
paulb@91 | 148 | if len(accept_languages) >= 1: |
paulb@91 | 149 | return self.parse_content_preferences(accept_languages[0]) |
paulb@91 | 150 | else: |
paulb@91 | 151 | return None |
paulb@91 | 152 | |
paulb@91 | 153 | def get_path(self): |
paulb@91 | 154 | |
paulb@91 | 155 | """ |
paulb@91 | 156 | A framework-specific method which gets the entire path from the request. |
paulb@91 | 157 | """ |
paulb@91 | 158 | |
paulb@91 | 159 | return self.request.getServletPath() |
paulb@91 | 160 | |
paulb@91 | 161 | def get_path_info(self): |
paulb@91 | 162 | |
paulb@91 | 163 | """ |
paulb@91 | 164 | A framework-specific method which gets the "path info" (the part of the |
paulb@91 | 165 | URL after the resource name handling the current request) from the |
paulb@91 | 166 | request. |
paulb@91 | 167 | """ |
paulb@91 | 168 | |
paulb@91 | 169 | return self.request.getPathInfo() |
paulb@91 | 170 | |
paulb@91 | 171 | def get_query_string(self): |
paulb@91 | 172 | |
paulb@91 | 173 | """ |
paulb@91 | 174 | A framework-specific method which gets the query string from the path in |
paulb@91 | 175 | the request. |
paulb@91 | 176 | """ |
paulb@91 | 177 | |
paulb@91 | 178 | return self.request.getQueryString() |
paulb@91 | 179 | |
paulb@91 | 180 | # Higher level request-related methods. |
paulb@91 | 181 | |
paulb@91 | 182 | def get_fields_from_path(self): |
paulb@91 | 183 | |
paulb@91 | 184 | """ |
paulb@91 | 185 | A framework-specific method which extracts the form fields from the |
paulb@91 | 186 | path specified in the transaction. The underlying framework may refuse |
paulb@91 | 187 | to supply fields from the path if handling a POST transaction. |
paulb@91 | 188 | |
paulb@91 | 189 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 190 | single value is associated with any given field name). |
paulb@91 | 191 | |
paulb@91 | 192 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 193 | NOTE: from the path. |
paulb@91 | 194 | """ |
paulb@91 | 195 | |
paulb@91 | 196 | return self.get_fields_from_body() |
paulb@91 | 197 | |
paulb@91 | 198 | def get_fields_from_body(self): |
paulb@91 | 199 | |
paulb@91 | 200 | """ |
paulb@91 | 201 | A framework-specific method which extracts the form fields from the |
paulb@91 | 202 | message body in the transaction. |
paulb@91 | 203 | |
paulb@91 | 204 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@91 | 205 | single value is associated with any given field name). |
paulb@91 | 206 | |
paulb@91 | 207 | NOTE: There may not be a reliable means of extracting only the fields |
paulb@91 | 208 | NOTE: from the message body. |
paulb@91 | 209 | """ |
paulb@91 | 210 | |
paulb@91 | 211 | parameter_map = self.request.getParameterMap() |
paulb@91 | 212 | fields = {} |
paulb@91 | 213 | for key in parameter_map.keySet(): |
paulb@91 | 214 | fields[key] = parameter_map[key] |
paulb@91 | 215 | return fields |
paulb@91 | 216 | |
paulb@91 | 217 | def get_user(self): |
paulb@91 | 218 | |
paulb@91 | 219 | """ |
paulb@91 | 220 | A framework-specific method which extracts user information from the |
paulb@91 | 221 | transaction. |
paulb@108 | 222 | |
paulb@108 | 223 | Returns a username as a string or None if no user is defined. |
paulb@91 | 224 | """ |
paulb@91 | 225 | |
paulb@91 | 226 | return self.request.getRemoteUser() |
paulb@91 | 227 | |
paulb@91 | 228 | # Response-related methods. |
paulb@91 | 229 | |
paulb@91 | 230 | def get_response_stream(self): |
paulb@91 | 231 | |
paulb@91 | 232 | """ |
paulb@91 | 233 | A framework-specific method which returns the response stream for |
paulb@91 | 234 | the transaction. |
paulb@91 | 235 | """ |
paulb@91 | 236 | |
paulb@91 | 237 | return self.response.getWriter() |
paulb@91 | 238 | |
paulb@91 | 239 | def get_response_code(self): |
paulb@91 | 240 | |
paulb@91 | 241 | """ |
paulb@91 | 242 | Get the response code associated with the transaction. If no response |
paulb@91 | 243 | code is defined, None is returned. |
paulb@91 | 244 | """ |
paulb@91 | 245 | |
paulb@91 | 246 | return self.status |
paulb@91 | 247 | |
paulb@91 | 248 | def set_response_code(self, response_code): |
paulb@91 | 249 | |
paulb@91 | 250 | """ |
paulb@91 | 251 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@91 | 252 | specification. |
paulb@91 | 253 | """ |
paulb@91 | 254 | |
paulb@91 | 255 | self.status = response_code |
paulb@91 | 256 | self.response.setStatus(self.status) |
paulb@91 | 257 | |
paulb@91 | 258 | def set_header_value(self, header, value): |
paulb@91 | 259 | |
paulb@91 | 260 | """ |
paulb@91 | 261 | Set the HTTP 'header' with the given 'value'. |
paulb@91 | 262 | """ |
paulb@91 | 263 | |
paulb@91 | 264 | self.response.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@91 | 265 | |
paulb@91 | 266 | def set_content_type(self, content_type): |
paulb@91 | 267 | |
paulb@91 | 268 | """ |
paulb@91 | 269 | A framework-specific method which sets the 'content_type' for the |
paulb@91 | 270 | response. |
paulb@91 | 271 | """ |
paulb@91 | 272 | |
paulb@91 | 273 | return self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@91 | 274 | |
paulb@91 | 275 | # vim: tabstop=4 expandtab shiftwidth=4 |