Commit 3f407d3d authored by Jim Fulton's avatar Jim Fulton

Changed to use delegation rather than inheritence to customize responses

for XML-RPC.
parent eb56a836
...@@ -39,25 +39,27 @@ def parse_input(data): ...@@ -39,25 +39,27 @@ def parse_input(data):
method = replace(method, '.', '/') method = replace(method, '.', '/')
return method, params return method, params
def response(anHTTPResponse): # See below
"""Return a valid ZPublisher response object #
# def response(anHTTPResponse):
Use data already gathered by the existing response. # """Return a valid ZPublisher response object
The new response will replace the existing response. #
""" # Use data already gathered by the existing response.
# As a first cut, lets just clone the response and # The new response will replace the existing response.
# put all of the logic in our refined response class below. # """
r=Response() # # As a first cut, lets just clone the response and
r.__dict__.update(anHTTPResponse.__dict__) # # put all of the logic in our refined response class below.
return r # r=Response()
# r.__dict__.update(anHTTPResponse.__dict__)
# return r
######################################################################## ########################################################################
# Possible implementation helpers: # Possible implementation helpers:
class Response(HTTPResponse): class Response:
"""Customized HTTPResponse that handles XML-RPC-specific details. """Customized Response that handles XML-RPC-specific details.
We override setBody to marhsall Python objects into XML-RPC. We We override setBody to marhsall Python objects into XML-RPC. We
also override exception to convert errors to XML-RPC faults. also override exception to convert errors to XML-RPC faults.
...@@ -70,6 +72,16 @@ class Response(HTTPResponse): ...@@ -70,6 +72,16 @@ class Response(HTTPResponse):
The current implementation, however, should suffice for now. The current implementation, however, should suffice for now.
""" """
# Because we can't predict what kind of thing we're customizing,
# we have to use delegation, rather than inheritence to do the
# customization.
def __init__(self, real): self.__dict__['_real']=real
def __getattr__(self, name): return getattr(self._real, name)
def __setattr__(self, name, v): return setattr(self._real, name, v)
def __delattr__(self, name): return delattr(self._real, name)
def setBody(self, body, title='', is_error=0, bogus_str_search=None): def setBody(self, body, title='', is_error=0, bogus_str_search=None):
if isinstance(body, xmlrpclib.Fault): if isinstance(body, xmlrpclib.Fault):
# Convert Fault object to XML-RPC response. # Convert Fault object to XML-RPC response.
...@@ -80,9 +92,8 @@ class Response(HTTPResponse): ...@@ -80,9 +92,8 @@ class Response(HTTPResponse):
# everything to a string first. # everything to a string first.
body = xmlrpclib.dumps((body,), methodresponse=1) body = xmlrpclib.dumps((body,), methodresponse=1)
# Set our body to the XML-RPC message, and fix our MIME type. # Set our body to the XML-RPC message, and fix our MIME type.
HTTPResponse.setBody(self, body) self._real.setBody(body)
self.setHeader('content-type', 'text/xml') self._real.setHeader('content-type', 'text/xml')
print 'setBody', body
return self return self
def exception(self, fatal=0, info=None, def exception(self, fatal=0, info=None,
...@@ -113,7 +124,8 @@ class Response(HTTPResponse): ...@@ -113,7 +124,8 @@ class Response(HTTPResponse):
# Do the damage. # Do the damage.
self.setBody(f) self.setBody(f)
print 'Exception', f self._real.setStatus(200)
self.setStatus(200)
return tb return tb
response=Response
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment