def index_html(self, REQUEST, RESPONSE): """Handle an incoming SOAP request or a non-SOAP WSDL query.""" if REQUEST.get('SOAPXML', None) == None: # Not a SOAP Request, return WSDL return self.service_description(REQUEST, RESPONSE) try: # Deserialize the Body of the SOAP Request payload, header = from_soap(REQUEST.SOAPXML) methodname = payload.tag.split('}')[-1] # Look-up the method within our class and obtain its SOAP descriptor # specifying its arguments signature and return type. try: func = getattr(self, methodname) descriptor = func(_soap_descriptor=True) except AttributeError: faultstring = 'No Such SOAP Method: %s' % repr(methodname) faultcode = 'Server' fault = make_soap_fault(faultstring, faultcode, detail=None) resp = ElementTree.tostring(fault, encoding=string_encoding) RESPONSE.setStatus('NotImplemented', reason=faultstring) RESPONSE.setHeader('Content-Type', 'text/xml') return resp except TypeError, e: if "unexpected keyword argument '_soap_descriptor'" not in str(e): raise faultstring = 'Method %s Exists But Not SOAP-Callable' % repr(methodname) faultcode = 'Server' fault = make_soap_fault(faultstring, faultcode, detail=None) resp = ElementTree.tostring(fault, encoding=string_encoding) RESPONSE.setStatus('BadRequest', reason=faultstring) RESPONSE.setHeader('Content-Type', 'text/xml') return resp # Run the supplied arguments through the arguments signature and catch # any argument type or count mismatches. try: params = descriptor.inMessage.from_xml(payload) except Exception, e: faultstring = "%s: Argument Parse Error" % str(e) faultcode = '%sFault' % methodname fault = make_soap_fault(faultstring, faultcode, detail=None) resp = ElementTree.tostring(fault, encoding=string_encoding) RESPONSE.setStatus('BadRequest', reason=faultstring) RESPONSE.setHeader('Content-Type', 'text/xml') return resp
def __call__(self): faultstring = self.request['faultexc'].__class__.__name__ self.request.response.setStatus('InternalServerError', reason=faultstring) faultcode = 'Server' fault = make_soap_fault(faultstring, faultcode, detail=None) self.request.response.setHeader('Content-Type', 'text/xml') return ElementTree.tostring(fault, encoding=string_encoding)
def test_soap_fault(self): fault = make_soap_fault('something happened') fault = et.fromstring(et.tostring(fault)) self.assertTrue(fault.getchildren()[0].tag.endswith,'Body') self.assertTrue(fault.getchildren()[0].getchildren()[0].tag.endswith('Fault')) f = fault.getchildren()[0].getchildren()[0] self.assertEquals(f.find('faultstring').text,'something happened') self.assertEquals(f.find('faultcode').text,'Server') self.assertEquals(f.find('detail').text,None) fault = make_soap_fault('something happened','DatabaseError','error on line 12') fault = et.fromstring(et.tostring(fault)) f = fault.getchildren()[0].getchildren()[0] self.assertEquals(f.find('faultstring').text,'something happened') self.assertEquals(f.find('faultcode').text,'DatabaseError') self.assertEquals(f.find('detail').text,'error on line 12')
def test_soap_fault(self): fault = make_soap_fault('something happened') fault = et.fromstring(et.tostring(fault)) self.assertTrue(fault.getchildren()[0].tag.endswith, 'Body') self.assertTrue( fault.getchildren()[0].getchildren()[0].tag.endswith('Fault')) f = fault.getchildren()[0].getchildren()[0] self.assertEquals(f.find('faultstring').text, 'something happened') self.assertEquals(f.find('faultcode').text, 'Server') self.assertEquals(f.find('detail').text, None) fault = make_soap_fault('something happened', 'DatabaseError', 'error on line 12') fault = et.fromstring(et.tostring(fault)) f = fault.getchildren()[0].getchildren()[0] self.assertEquals(f.find('faultstring').text, 'something happened') self.assertEquals(f.find('faultcode').text, 'DatabaseError') self.assertEquals(f.find('detail').text, 'error on line 12')
def __call__(self, environ, start_response, address_url=None): ''' This method conforms to the WSGI spec for callable wsgi applications (PEP 333). It 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 Internal Server Error', [('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)) methodname = environ.get("HTTP_SOAPACTION") debug('\033[92m'+ methodname +'\033[0m') 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
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]
def __call__(self, environ, start_response, address_url=None): ''' This method conforms to the WSGI spec for callable wsgi applications (PEP 333). It 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
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
def __call__(self, environ, start_response): ''' 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 serviceName.lower().endswith('wsdl'): # get the wsdl for the service # # Assume path_info matches pattern # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL # serviceName = serviceName.split('.')[0] 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 payload, header = from_soap(body) 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] request.header = header # call the method func = getattr(service, methodname) # retrieve the method descriptor descriptor = func(_soap_descriptor=True) 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) 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]
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): ''' 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 serviceName.lower().endswith('wsdl'): # get the wsdl for the service # # Assume path_info matches pattern # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL # serviceName = serviceName.split('.')[0] 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 payload, header = from_soap(body) methodname = payload.tag.split('}')[-1] request.header = header # call the method func = getattr(service, methodname) # retrieve the method descriptor descriptor = func(_soap_descriptor=True) params = descriptor.inMessage.from_xml(*[payload]) # 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) 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 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]
def __call__(self, environ, start_response): ''' 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 serviceName.lower().endswith('wsdl'): # get the wsdl for the service # # Assume path_info matches pattern # /stuff/stuff/stuff/serviceName.wsdl or ?WSDL # serviceName = serviceName.split('.')[0] 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 payload, header = from_soap(body) methodname = payload.tag.split('}')[-1] request.header = header # call the method func = getattr(service, methodname) # retrieve the method descriptor descriptor = func(_soap_descriptor=True) params = descriptor.inMessage.from_xml(*[payload]) # 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) 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]
results = descriptor.outMessage.to_xml(*[retval]) envelope = make_soap_envelope(results, tns=self.__tns__) resp = ElementTree.tostring(envelope, encoding=string_encoding) RESPONSE.setHeader('Content-Type', 'text/xml') return resp except Exception, e: faultstring = str(e) if methodname: faultcode = '%sFault' % methodname else: faultcode = 'Server' fault = make_soap_fault(faultstring, faultcode, detail=None) resp = ElementTree.tostring(fault, encoding=string_encoding) RESPONSE.setStatus('InternalServerError', reason=faultstring) RESPONSE.setHeader('Content-Type', 'text/xml') return resp class ISOAPException(IException): pass class SOAPException(Exception): """Base exception class for all derived exceptions for SOAP""" implements(ISOAPException)
def post(self): ''' 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 = '' container = tornado.wsgi.WSGIContainer(self.application) environ = container.environ(self.request) try: # 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] url = reconstruct_url(environ).split('.wsdl')[0] 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 #return Response(faultStr, '500 Internal Server Error', [('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(faultStr) #return Response(wsdl_content, '200 OK', [('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(wsdl_content) if environ['REQUEST_METHOD'].lower() != 'post': #return Response('', '405 Method Not Allowed',[('Allow','POST')]) self.set_header('Content-Type', 'text/html;charset=utf-8') return self.write_error(status_code=405) input = environ.get('wsgi.input') length = environ.get("CONTENT_LENGTH") body = input.read(int(length)) debug(body) # body, _unmentioned_attachs = collapse_swa( environ.get("CONTENT_TYPE"), body) # collapse_swa has some problem try: body, _unmentioned_attachs = collapse_swa( environ.get("CONTENT_TYPE"), body) except: body = collapse_swa(environ.get("CONTENT_TYPE"), body) _unmentioned_attachs = [] pass # deserialize the body of the message try: payload, header = from_soap(body) except SyntaxError, e: payload = None header = None
resp = '<?xml version="1.0" encoding="utf-8"?>' + resp if environ.has_key('CONTENT_LENGTH'): del (environ['CONTENT_LENGTH']) self.onReturn(environ, resp) debug(resp) # return the serialized results #return Response(resp, '200 OK', headers.items()) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(resp) except Fault, e: # The user issued a Fault, so handle it just like an exception! fault = make_soap_fault(e.faultstring, e.faultcode, e.detail) faultStr = ElementTree.tostring(fault, encoding=string_encoding) exceptions(faultStr) self.onException(environ, e, faultStr) # initiate the response #return Response(faultStr, '500 Internal Server Error',[('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(faultStr) except Exception, e: # Dump the stack trace to a buffer to be sent # back to the caller
def post(self): ''' 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 = '' container = tornado.wsgi.WSGIContainer(self.application) environ = container.environ(self.request) try: # 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] url = reconstruct_url(environ).split('.wsdl')[0] 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 #return Response(faultStr, '500 Internal Server Error', [('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(faultStr) #return Response(wsdl_content, '200 OK', [('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(wsdl_content) if environ['REQUEST_METHOD'].lower() != 'post': #return Response('', '405 Method Not Allowed',[('Allow','POST')]) self.set_header('Content-Type', 'text/html;charset=utf-8') return self.write_error(status_code=405) input = environ.get('wsgi.input') length = environ.get("CONTENT_LENGTH") body = input.read(int(length)) debug(body) # body, _unmentioned_attachs = collapse_swa( environ.get("CONTENT_TYPE"), body) # collapse_swa has some problem try: body, _unmentioned_attachs = collapse_swa( environ.get("CONTENT_TYPE"), body) except: body = collapse_swa( environ.get("CONTENT_TYPE"), body) _unmentioned_attachs = [] pass # deserialize the body of the message try: payload, header = from_soap(body) except SyntaxError,e: payload = None header = None
if environ.has_key('CONTENT_LENGTH'): del(environ['CONTENT_LENGTH']) self.onReturn(environ,resp) debug(resp) # return the serialized results #return Response(resp, '200 OK', headers.items()) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(resp) except Fault,e: # The user issued a Fault, so handle it just like an exception! fault = make_soap_fault( e.faultstring, e.faultcode, e.detail) faultStr = ElementTree.tostring(fault, encoding=string_encoding) exceptions(faultStr) self.onException(environ,e,faultStr) # initiate the response #return Response(faultStr, '500 Internal Server Error',[('Content-type','text/xml;charset=utf-8')]) self.set_header('Content-Type', 'text/xml;charset=utf-8') return self.write(faultStr) except Exception, e: # Dump the stack trace to a buffer to be sent # back to the caller