/usr/share/pyshared/soaplib/wsgi_soap.py is in python-soaplib 0.8.1-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | import cStringIO
import traceback
from soaplib.soap import make_soap_envelope, make_soap_fault, from_soap, collapse_swa, apply_mtom
from soaplib.service import SoapServiceBase
from soaplib.util import reconstruct_url
from soaplib.serializers.primitive import string_encoding, Fault
from soaplib.etimport import ElementTree
from threading import local
request = local()
_exceptions = False
_exception_logger = None
_debug = False
_debug_logger = None
###################################################################
# Logging / Debugging Utilities #
# replace with python logger? #
###################################################################
def _dump(e): # util?
print e
def log_exceptions(on, out=_dump):
global _exceptions
global _exception_logger
_exceptions=on
_exception_logger=out
def log_debug(on,out=_dump):
global _debug
global _debug_logger
_debug=on
_debug_logger=out
def debug(msg):
global _debug
global _debug_logger
if _debug:
_debug_logger(msg)
def exceptions(msg):
global _exceptions
global _exception_logger
if _exceptions:
_exception_logger(msg)
def reset_request():
'''
This method clears the data stored in the threadlocal
request object
'''
request.environ = None
request.header = None
request.additional = {}
class WSGISoapApp(object):
'''
This is the base object representing a soap web application, and conforms
to the WSGI specification (PEP 333). This object should be overridden
and getHandler(environ) overridden to provide the object implementing
the specified functionality. Hooks have been added so that the subclass
can react to various events that happen durring the execution of the
request.
'''
def onCall(self,environ):
'''
This is the first method called when this WSGI app is invoked
@param the wsgi environment
'''
pass
def onWsdl(self,environ,wsdl):
'''
This is called when a wsdl is requested
@param the wsgi environment
@param the wsdl string
'''
pass
def onWsdlException(self,environ,exc,resp):
'''
Called when an exception occurs durring wsdl generation
@param the wsgi environment
@param exc the exception
@param the fault response string
'''
pass
def onMethodExec(self,environ,body,py_params,soap_params):
'''
Called BEFORE the service implementing the functionality is called
@param the wsgi environment
@param the body element of the soap request
@param the tuple of python params being passed to the method
@param the soap elements for each params
'''
pass
def onResults(self,environ,py_results,soap_results):
'''
Called AFTER the service implementing the functionality is called
@param the wsgi environment
@param the python results from the method
@param the xml serialized results of the method
'''
pass
def onException(self,environ,exc,resp):
'''
Called when an error occurs durring execution
@param the wsgi environment
@param the exception
@param the response string
'''
pass
def onReturn(self,environ,returnString):
'''
Called before the application returns
@param the wsgi environment
@param return string of the soap request
'''
pass
def getHandler(self,environ):
'''
This method returns the object responsible for processing a given request, and
needs to be overridden by a subclass to handle the application specific
mapping of the request to the appropriate handler.
@param the wsgi environment
@returns the object to be called for the soap operation
'''
raise Exception("Not implemented")
def __call__(self, environ, start_response, address_url=None):
'''
This method conforms to the WSGI spec for callable wsgi applications (PEP 333).
This method looks in environ['wsgi.input'] for a fully formed soap request envelope,
will deserialize the request parameters and call the method on the object returned
by the getHandler() method.
@param the http environment
@param a callable that begins the response message
@returns the string representation of the soap call
'''
methodname = ''
try:
reset_request()
request.environ = environ
# implementation hook
self.onCall(environ)
serviceName = environ['PATH_INFO'].split('/')[-1]
service = self.getHandler(environ)
if (environ['QUERY_STRING'].endswith('wsdl') or environ['PATH_INFO'].endswith('wsdl')) and environ['REQUEST_METHOD'].lower() == 'get':
# get the wsdl for the service
#
# Assume path_info matches pattern
# /stuff/stuff/stuff/serviceName.wsdl or ?WSDL
#
serviceName = serviceName.split('.')[0]
if address_url:
url = address_url
else:
url = reconstruct_url(environ).split('.wsdl')[0]
start_response('200 OK',[('Content-type','text/xml')])
try:
wsdl_content = service.wsdl(url)
# implementation hook
self.onWsdl(environ,wsdl_content)
except Exception, e:
# implementation hook
buffer = cStringIO.StringIO()
traceback.print_exc(file=buffer)
buffer.seek(0)
stacktrace = str(buffer.read())
faultStr = ElementTree.tostring(make_soap_fault( str(e), detail=stacktrace), encoding=string_encoding)
exceptions(faultStr)
self.onWsdlException(environ,e,faultStr)
# initiate the response
start_response('500',[('Content-type','text/xml'),('Content-length',str(len(faultStr)))])
return [faultStr]
reset_request()
return [wsdl_content]
if environ['REQUEST_METHOD'].lower() != 'post':
start_response('405 Method Not Allowed',[('Allow','POST')])
return ''
input = environ.get('wsgi.input')
length = environ.get("CONTENT_LENGTH")
body = input.read(int(length))
debug(body)
body = collapse_swa( environ.get("CONTENT_TYPE"), body)
# deserialize the body of the message
try:
payload, header = from_soap(body)
except SyntaxError,e:
payload = None
header = None
if payload:
methodname = payload.tag.split('}')[-1]
else:
# check HTTP_SOAPACTION
methodname = environ.get("HTTP_SOAPACTION")
if methodname.startswith('"') and methodname.endswith('"'):
methodname = methodname[1:-1]
if methodname.find('/') >0:
methodname = methodname.split('/')[1]
request.header = header
# call the method
func = getattr(service, methodname)
# retrieve the method descriptor
descriptor = func(_soap_descriptor=True, klazz=service.__class__)
if payload:
params = descriptor.inMessage.from_xml(*[payload])
else:
params = ()
# implementation hook
self.onMethodExec(environ,body,params,descriptor.inMessage.params)
# call the method
retval = func(*params)
# transform the results into an element
# only expect a single element
results = None
if not (descriptor.isAsync or descriptor.isCallback):
results = descriptor.outMessage.to_xml(*[retval])
# implementation hook
self.onResults(environ,results,retval)
# grab any headers that were included in the request
response_headers = None
if hasattr(request,'response_headers'):
response_headers = request.response_headers
# construct the soap response, and serialize it
envelope = make_soap_envelope(results,tns=service.__tns__,header_elements=response_headers)
ElementTree.cleanup_namespaces(envelope)
resp = ElementTree.tostring(envelope, encoding=string_encoding)
headers = {'Content-Type': 'text/xml'}
if descriptor.mtom:
headers, resp = apply_mtom( headers, resp,
descriptor.outMessage.params,
(retval,) )
if environ.has_key('CONTENT_LENGTH'):
del(environ['CONTENT_LENGTH'])
# initiate the response
start_response('200 OK',headers.items())
self.onReturn(environ,resp)
debug(resp)
# return the serialized results
reset_request()
return [resp]
except Fault,e:
# grab any headers that were included in the request
response_headers = None
if hasattr(request,'response_headers'):
response_headers = request.response_headers
# The user issued a Fault, so handle it just like an exception!
fault = make_soap_fault(
e.faultstring,
e.faultcode,
e.detail,
header_elements = response_headers)
faultStr = ElementTree.tostring(fault, encoding=string_encoding)
exceptions(faultStr)
self.onException(environ,e,faultStr)
reset_request()
# initiate the response
start_response('500 Internal Server Error',[('Content-type','text/xml')])
return [faultStr]
except Exception, e:
# Dump the stack trace to a buffer to be sent
# back to the caller
# capture stacktrace
buffer = cStringIO.StringIO()
traceback.print_exc(file=buffer)
buffer.seek(0)
stacktrace = str(buffer.read())
faultstring = str(e)
if methodname:
faultcode = faultCode='%sFault'%methodname
else:
faultcode = 'Server'
detail = stacktrace
faultStr = ElementTree.tostring(make_soap_fault(faultstring,faultcode,detail), encoding=string_encoding)
exceptions(faultStr)
self.onException(environ,e,faultStr)
reset_request()
# initiate the response
start_response('500 Internal Server Error',[('Content-type','text/xml')])
return [faultStr]
class SimpleWSGISoapApp(WSGISoapApp, SoapServiceBase):
'''
This object is a VERY simple extention of the base WSGISoapApp.
It subclasses both WSGISoapApp, and SoapServiceBase, so that
an object can simply subclass this single object, and it will
be both a wsgi application and a soap service. This is convenient
if you want to only expose some functionality, and dont need
complex handler mapping, and all of the functionality can be put
in a single class.
'''
def __init__(self):
WSGISoapApp.__init__(self)
SoapServiceBase.__init__(self)
def getHandler(self,environ):
return self
|