1.1 --- a/WebStack/JavaServlet.py Thu Sep 02 19:31:31 2004 +0000
1.2 +++ b/WebStack/JavaServlet.py Thu Sep 02 19:32:22 2004 +0000
1.3 @@ -6,7 +6,7 @@
1.4
1.5 import Generic
1.6 from StringIO import StringIO
1.7 -from Helpers.Request import Cookie, get_body_fields, get_storage_items
1.8 +from Helpers.Request import Cookie, get_body_fields, get_storage_items, get_fields_from_query_string
1.9 import javax.servlet.http.Cookie
1.10
1.11 # Form data decoding.
1.12 @@ -14,6 +14,7 @@
1.13 import javax.mail.internet.MimeMessage
1.14 import javax.mail.Session
1.15 import java.util.Properties
1.16 +import java.net.URLDecoder
1.17
1.18 class Stream:
1.19
1.20 @@ -212,7 +213,7 @@
1.21 Returns the query string from the path in the request.
1.22 """
1.23
1.24 - return self.request.getQueryString()
1.25 + return self.request.getQueryString() or ""
1.26
1.27 # Higher level request-related methods.
1.28
1.29 @@ -225,12 +226,12 @@
1.30
1.31 Returns a dictionary mapping field names to lists of values (even if a
1.32 single value is associated with any given field name).
1.33 -
1.34 - NOTE: There may not be a reliable means of extracting only the fields
1.35 - NOTE: from the path.
1.36 """
1.37
1.38 - return self.get_fields_from_body()
1.39 + # There may not be a reliable means of extracting only the fields from
1.40 + # the path using the API.
1.41 +
1.42 + return get_fields_from_query_string(self.get_query_string(), java.net.URLDecoder().decode)
1.43
1.44 def get_fields_from_body(self, encoding=None):
1.45
1.46 @@ -244,25 +245,53 @@
1.47 single value is associated with any given field name). Each value is
1.48 either a Unicode object (representing a simple form field, for example)
1.49 or a plain string (representing a file upload form field, for example).
1.50 -
1.51 - NOTE: There may not be a reliable means of extracting only the fields
1.52 - NOTE: from the message body. Moreover, the encoding of the fields may
1.53 - NOTE: not be pertinent.
1.54 """
1.55
1.56 # Where the content type is "multipart/form-data", we use javax.mail
1.57 # functionality. Otherwise, we use the Servlet API's parameter access
1.58 # methods.
1.59
1.60 - if self.get_content_type() and self.get_content_type().content_type == "multipart/form-data":
1.61 + if self.get_content_type() and self.get_content_type().media_type == "multipart/form-data":
1.62 fields = self._get_fields_from_message()
1.63
1.64 else:
1.65 + # Override the default encoding if requested.
1.66 +
1.67 + if encoding is not None:
1.68 + self.request.setCharacterEncoding(encoding)
1.69 +
1.70 + # There may not be a reliable means of extracting only the fields
1.71 + # the message body using the API. Remove fields originating from the
1.72 + # path in the mixture provided by the API.
1.73 +
1.74 + fields_from_path = self.get_fields_from_path()
1.75 +
1.76 fields = {}
1.77 parameter_map = self.request.getParameterMap()
1.78 if parameter_map:
1.79 - for key in parameter_map.keySet():
1.80 - fields[key] = parameter_map[key]
1.81 + for field_name in parameter_map.keySet():
1.82 +
1.83 + # Find path values for this field.
1.84 +
1.85 + if fields_from_path.has_key(field_name):
1.86 + field_from_path_values = fields_from_path[field_name]
1.87 + if type(field_from_path_values) != type([]):
1.88 + field_from_path_values = [field_from_path_values]
1.89 + else:
1.90 + field_from_path_values = []
1.91 +
1.92 +
1.93 + # Filter out path values.
1.94 +
1.95 + fields[field_name] = []
1.96 + for field_value in parameter_map[field_name]:
1.97 + if field_value not in field_from_path_values:
1.98 + fields[field_name].append(field_value)
1.99 +
1.100 + # Remove filtered entries.
1.101 +
1.102 + if fields[field_name] == []:
1.103 + del fields[field_name]
1.104
1.105 return fields
1.106
1.107 @@ -308,7 +337,7 @@
1.108 Returns the response stream for the transaction.
1.109 """
1.110
1.111 - return self.response.getOutputStream()
1.112 + return self.response.getWriter()
1.113
1.114 def get_response_code(self):
1.115
1.116 @@ -343,7 +372,7 @@
1.117 Sets the 'content_type' for the response.
1.118 """
1.119
1.120 - return self.response.setHeader("Content-Type", str(content_type))
1.121 + self.response.setContentType(str(content_type))
1.122
1.123 # Higher level response-related methods.
1.124