1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/JavaServlet.py Thu Mar 25 22:20:25 2004 +0000
1.3 @@ -0,0 +1,263 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Java Servlet classes.
1.8 +"""
1.9 +
1.10 +import Generic
1.11 +from StringIO import StringIO
1.12 +
1.13 +class Stream:
1.14 +
1.15 + """
1.16 + Wrapper around java.io.BufferedReader.
1.17 + """
1.18 +
1.19 + def __init__(self, stream):
1.20 +
1.21 + "Initialise the stream with the given underlying 'stream'."
1.22 +
1.23 + self.stream = stream
1.24 +
1.25 + def read(self):
1.26 +
1.27 + "Read the entire message, returning it as a string."
1.28 +
1.29 + characters = StringIO()
1.30 + while 1:
1.31 + c = self.stream.read()
1.32 + if c == -1:
1.33 + return characters.getvalue()
1.34 + else:
1.35 + characters.write(chr(c))
1.36 +
1.37 + def readline(self):
1.38 +
1.39 + "Read a line from the stream, returning it as a string."
1.40 +
1.41 + return self.stream.readLine()
1.42 +
1.43 +class Transaction(Generic.Transaction):
1.44 +
1.45 + """
1.46 + Java Servlet transaction interface.
1.47 + """
1.48 +
1.49 + def __init__(self, request, response):
1.50 +
1.51 + """
1.52 + Initialise the transaction using the Java Servlet HTTP 'request' and
1.53 + 'response'.
1.54 + """
1.55 +
1.56 + self.request = request
1.57 + self.response = response
1.58 + self.status = None
1.59 +
1.60 + # Request-related methods.
1.61 +
1.62 + def get_request_stream(self):
1.63 +
1.64 + """
1.65 + A framework-specific method which returns the request stream for
1.66 + the transaction.
1.67 + """
1.68 +
1.69 + return Stream(self.request.getReader())
1.70 +
1.71 + def get_request_method(self):
1.72 +
1.73 + """
1.74 + A framework-specific method which gets the request method.
1.75 + """
1.76 +
1.77 + return self.request.getMethod()
1.78 +
1.79 + def get_headers(self):
1.80 +
1.81 + """
1.82 + A framework-specific method which returns all request headers.
1.83 + NOTE: If duplicate header names are permitted, then this interface will
1.84 + NOTE: need to change.
1.85 + """
1.86 +
1.87 + headers = {}
1.88 + header_names = self.request.getHeaderNames()
1.89 + if header_names:
1.90 + for header_name in header_names:
1.91 +
1.92 + # NOTE: Retrieve only a single value (not using getHeaders).
1.93 +
1.94 + headers[header_name] = self.request.getHeader(header_name)
1.95 +
1.96 + return headers
1.97 +
1.98 + def get_header_values(self, key):
1.99 +
1.100 + """
1.101 + A framework-specific method which returns a list of all request header
1.102 + values associated with the given 'key'. Note that according to RFC 2616,
1.103 + 'key' is treated as a case-insensitive string.
1.104 + """
1.105 +
1.106 + return self.request.getHeaders(key)
1.107 +
1.108 + def get_content_type(self):
1.109 +
1.110 + """
1.111 + A framework-specific method which gets the content type specified on the
1.112 + request, along with the charset employed.
1.113 + """
1.114 +
1.115 + content_types = self.get_header_values("Content-Type")
1.116 + if len(content_types) >= 1:
1.117 + return self.parse_content_type(content_types[0])
1.118 + else:
1.119 + return None
1.120 +
1.121 + def get_content_charsets(self):
1.122 +
1.123 + """
1.124 + Returns the character set preferences.
1.125 + """
1.126 +
1.127 + accept_charsets = self.get_header_values("Accept-Charset")
1.128 + if len(accept_charsets) >= 1:
1.129 + return self.parse_content_preferences(accept_charsets[0])
1.130 + else:
1.131 + return None
1.132 +
1.133 + def get_content_languages(self):
1.134 +
1.135 + """
1.136 + A framework-specific method which extracts language information from
1.137 + the transaction.
1.138 + """
1.139 +
1.140 + accept_languages = self.get_header_values("Accept-Language")
1.141 + if len(accept_languages) >= 1:
1.142 + return self.parse_content_preferences(accept_languages[0])
1.143 + else:
1.144 + return None
1.145 +
1.146 + def get_path(self):
1.147 +
1.148 + """
1.149 + A framework-specific method which gets the entire path from the request.
1.150 + """
1.151 +
1.152 + return self.request.getServletPath()
1.153 +
1.154 + def get_path_info(self):
1.155 +
1.156 + """
1.157 + A framework-specific method which gets the "path info" (the part of the
1.158 + URL after the resource name handling the current request) from the
1.159 + request.
1.160 + """
1.161 +
1.162 + return self.request.getPathInfo()
1.163 +
1.164 + def get_query_string(self):
1.165 +
1.166 + """
1.167 + A framework-specific method which gets the query string from the path in
1.168 + the request.
1.169 + """
1.170 +
1.171 + return self.request.getQueryString()
1.172 +
1.173 + # Higher level request-related methods.
1.174 +
1.175 + def get_fields_from_path(self):
1.176 +
1.177 + """
1.178 + A framework-specific method which extracts the form fields from the
1.179 + path specified in the transaction. The underlying framework may refuse
1.180 + to supply fields from the path if handling a POST transaction.
1.181 +
1.182 + Returns a dictionary mapping field names to lists of values (even if a
1.183 + single value is associated with any given field name).
1.184 +
1.185 + NOTE: There may not be a reliable means of extracting only the fields
1.186 + NOTE: from the path.
1.187 + """
1.188 +
1.189 + return self.get_fields_from_body()
1.190 +
1.191 + def get_fields_from_body(self):
1.192 +
1.193 + """
1.194 + A framework-specific method which extracts the form fields from the
1.195 + message body in the transaction.
1.196 +
1.197 + Returns a dictionary mapping field names to lists of values (even if a
1.198 + single value is associated with any given field name).
1.199 +
1.200 + NOTE: There may not be a reliable means of extracting only the fields
1.201 + NOTE: from the message body.
1.202 + """
1.203 +
1.204 + parameter_map = self.request.getParameterMap()
1.205 + fields = {}
1.206 + for key in parameter_map.keySet():
1.207 + fields[key] = parameter_map[key]
1.208 + return fields
1.209 +
1.210 + def get_user(self):
1.211 +
1.212 + """
1.213 + A framework-specific method which extracts user information from the
1.214 + transaction.
1.215 + """
1.216 +
1.217 + return self.request.getRemoteUser()
1.218 +
1.219 + # Response-related methods.
1.220 +
1.221 + def get_response_stream(self):
1.222 +
1.223 + """
1.224 + A framework-specific method which returns the response stream for
1.225 + the transaction.
1.226 + """
1.227 +
1.228 + return self.response.getWriter()
1.229 +
1.230 + def get_response_code(self):
1.231 +
1.232 + """
1.233 + Get the response code associated with the transaction. If no response
1.234 + code is defined, None is returned.
1.235 + """
1.236 +
1.237 + return self.status
1.238 +
1.239 + def set_response_code(self, response_code):
1.240 +
1.241 + """
1.242 + Set the 'response_code' using a numeric constant defined in the HTTP
1.243 + specification.
1.244 + """
1.245 +
1.246 + self.status = response_code
1.247 + self.response.setStatus(self.status)
1.248 +
1.249 + def set_header_value(self, header, value):
1.250 +
1.251 + """
1.252 + Set the HTTP 'header' with the given 'value'.
1.253 + """
1.254 +
1.255 + self.response.setHeader(self.format_header_value(header), self.format_header_value(value))
1.256 +
1.257 + def set_content_type(self, content_type):
1.258 +
1.259 + """
1.260 + A framework-specific method which sets the 'content_type' for the
1.261 + response.
1.262 + """
1.263 +
1.264 + return self.response.setHeader("Content-Type", self.format_content_type(content_type))
1.265 +
1.266 +# vim: tabstop=4 expandtab shiftwidth=4