コード例 #1
0
ファイル: easy_client.py プロジェクト: gbhuvneshwar/debexpo
    def __call__(self, data, raw=False):
        e = data.to_xml(data, data.__class__.__name__)
        envelope = make_soap_envelope(e)
        body = et.tostring(envelope)
        methodName = '\"%s\"' % self.method
        httpHeaders = {
            "Content-Length": len(body),
            "Content-type": 'text/xml; charset="UTF-8"',
            "Accept":
            "application/soap+xml, application/dime, multipart/related, text/*",
            'User-Agent': 'Soaplib/1.0',
            'SOAPAction': methodName
        }
        scheme, host, path = split_url(self.host)
        if scheme == "http":
            conn = httplib.HTTPConnection(host)
        elif scheme == "https":
            conn = httplib.HTTPSConnection(host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                               scheme)

        conn.request("POST", path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        raw_data = response.read()

        retval = et.fromstring(raw_data)
        d = retval.find(
            '{http://schemas.xmlsoap.org/soap/envelope/}Body').getchildren()[0]

        if raw:
            return d
        return objectify(d)
コード例 #2
0
ファイル: easy_client.py プロジェクト: Manduka/soaplib
 def __call__(self, data, raw=False):
     e = data.to_xml(data, data.__class__.__name__)
     envelope = make_soap_envelope(e)
     body = et.tostring(envelope)
     methodName = '\"%s\"' % self.method
     httpHeaders = {
         'Content-Length': len(body),
         'Content-type': 'text/xml; charset="UTF-8"',
         'Accept': ('application/soap+xml, application/dime, '
                    'multipart/related, text/*'),
         'User-Agent': 'Soaplib/1.0',
         'SOAPAction': methodName,
     }
     scheme, host, path = split_url(self.host)
     if scheme == "http":
         conn = httplib.HTTPConnection(host)
     elif scheme == "https":
         conn = httplib.HTTPSConnection(host)
     else:
         raise RuntimeError("Unsupported URI connection scheme: %s" %
             scheme)
     conn.request("POST", path, body=body, headers=httpHeaders)
     response = conn.getresponse()
     raw_data = response.read()
     retval = et.fromstring(raw_data)
     pattern = '{http://schemas.xmlsoap.org/soap/envelope/}Body'
     node = retval.find(pattern).getchildren()[0]
     if raw:
         return node
     return objectify(node)
コード例 #3
0
ファイル: soap_test.py プロジェクト: certik/debexpo
 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')
コード例 #4
0
ファイル: primitive.py プロジェクト: diegoug/soaplib
 def from_xml(cls, element):
     code = _element_to_string(element.find('faultcode'))
     string = _element_to_string(element.find('faultstring'))
     detail_element = element.find('detail')
     if detail_element is not None:
         if len(detail_element.getchildren()):
             detail = ElementTree.tostring(detail_element)
         else:
             detail = _element_to_string(element.find('detail'))
     else:
         detail = ''
     return Fault(faultcode=code, faultstring=string, detail=detail)
コード例 #5
0
ファイル: primitive.py プロジェクト: esauro/SingularMS
 def from_xml(cls, element):
     code = _element_to_string(element.find('faultcode'))
     string = _element_to_string(element.find('faultstring'))
     detail_element = element.find('detail')
     if detail_element:
         if len(detail_element.getchildren()):
             detail = ElementTree.tostring(detail_element)
         else:
             detail = _element_to_string(element.find('detail'))
     else:
         detail = ''
     return Fault(faultcode = code, faultstring = string, detail = detail)
コード例 #6
0
    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')
コード例 #7
0
ファイル: soap_test.py プロジェクト: certik/debexpo
    def test_soap_envelope(self):
        m = Message('myMessage',[('p',Person)])
        env = make_soap_envelope(m.to_xml(Person()))
        
        self.assertTrue(env.tag.endswith('Envelope'))
        self.assertTrue(env.getchildren()[0].tag.endswith('Body'))

        m = Message('myMessage',[('p',Person)])
        env = make_soap_envelope(m.to_xml(Person()), header_elements=[et.Element('header1'),et.Element('header2')])

        env = et.fromstring(et.tostring(env))
        
        self.assertTrue(env.getchildren()[0].tag.endswith('Header'))
        self.assertEquals(len(env.getchildren()[0].getchildren()),2)
        self.assertTrue(env.getchildren()[1].tag.endswith('Body'))
コード例 #8
0
    def test_soap_envelope(self):
        m = Message('myMessage', [('p', Person)])
        env = make_soap_envelope(m.to_xml(Person()))

        self.assertTrue(env.tag.endswith('Envelope'))
        self.assertTrue(env.getchildren()[0].tag.endswith('Body'))

        m = Message('myMessage', [('p', Person)])
        env = make_soap_envelope(
            m.to_xml(Person()),
            header_elements=[et.Element('header1'),
                             et.Element('header2')])

        env = et.fromstring(et.tostring(env))

        self.assertTrue(env.getchildren()[0].tag.endswith('Header'))
        self.assertEquals(len(env.getchildren()[0].getchildren()), 2)
        self.assertTrue(env.getchildren()[1].tag.endswith('Body'))
コード例 #9
0
ファイル: service.py プロジェクト: gbhuvneshwar/debexpo
    def wsdl(self, url):
        '''
        This method generates and caches the wsdl for this object based
        on the soap methods designated by the soapmethod or soapdocument
        descriptors
        @param url the url that this service can be found at.  This must be 
        passed in by the caller because this object has no notion of the
        server environment in which it runs.
        @returns the string of the wsdl
        '''
        if not self.__wsdl__ == None:
            # return the cached __wsdl__
            return self.__wsdl__
        url = url.replace('.wsdl','')
        # otherwise build it
        serviceName = self.__class__.__name__.split('.')[-1]

        tns = self.__tns__

        root = ElementTree.Element("definitions")

        root.set('targetNamespace',tns)

        root.set('xmlns:tns',tns)
        root.set('xmlns:typens',tns)

        root.set('xmlns','http://schemas.xmlsoap.org/wsdl/')
        root.set('xmlns:soap','http://schemas.xmlsoap.org/wsdl/soap/')
        root.set('xmlns:xs','http://www.w3.org/2001/XMLSchema')
        root.set('xmlns:plnk','http://schemas.xmlsoap.org/ws/2003/05/partner-link/')
        root.set('xmlns:SOAP-ENC',"http://schemas.xmlsoap.org/soap/encoding/")
        root.set('xmlns:wsdl',"http://schemas.xmlsoap.org/wsdl/")
        root.set('name',serviceName)
        
        types = ElementTree.SubElement(root,"types")

        methods = self.methods()
        hasCallbacks = self._hasCallbacks()

        self._add_schema(types,methods)
        self._add_messages_for_methods(root,methods)

        # add necessary async headers
        # WS-Addressing -> RelatesTo ReplyTo MessageID
        # callback porttype
        if hasCallbacks:
            root.set('xmlns:wsa','http://schemas.xmlsoap.org/ws/2003/03/addressing')

            wsaSchemaNode = ElementTree.SubElement(types, "schema")
            wsaSchemaNode.set("targetNamespace", tns+'Callback')
            wsaSchemaNode.set("xmlns", "http://www.w3.org/2001/XMLSchema")

            importNode = ElementTree.SubElement(wsaSchemaNode, "import")
            importNode.set("namespace", "http://schemas.xmlsoap.org/ws/2003/03/addressing")
            importNode.set("schemaLocation", "http://schemas.xmlsoap.org/ws/2003/03/addressing/")

        
            reltMessage = ElementTree.SubElement(root,'message')
            reltMessage.set('name','RelatesToHeader')
            reltPart = ElementTree.SubElement(reltMessage,'part')
            reltPart.set('name','RelatesTo')
            reltPart.set('element','wsa:RelatesTo')

            replyMessage = ElementTree.SubElement(root,'message')
            replyMessage.set('name','ReplyToHeader')
            replyPart = ElementTree.SubElement(replyMessage,'part')
            replyPart.set('name','ReplyTo')
            replyPart.set('element','wsa:ReplyTo')

            idHeader = ElementTree.SubElement(root,'message')
            idHeader.set('name','MessageIDHeader')
            idPart = ElementTree.SubElement(idHeader,'part')
            idPart.set('name','MessageID')
            idPart.set('element','wsa:MessageID')

            # make portTypes
            callbackPortType = ElementTree.SubElement(root,'portType')
            callbackPortType.set('name','%sCallback'%serviceName)
            
            cbServiceName = '%sCallback'%serviceName
            cbService = ElementTree.SubElement(root,'service')
            cbService.set('name',cbServiceName)
            cbWsdlPort = ElementTree.SubElement(cbService,'port')
            cbWsdlPort.set('name',cbServiceName)
            cbWsdlPort.set('binding','tns:%s'%cbServiceName)
            cbAddr = ElementTree.SubElement(cbWsdlPort,'soap:address')
            cbAddr.set('location',url)
            
            
        serviceName = self.__class__.__name__.split('.')[-1] 
        portType = ElementTree.SubElement(root,'portType')
        portType.set('name',serviceName)
        for method in methods:
            if method.isCallback:
                operation = ElementTree.SubElement(callbackPortType,'operation')
            else:
                operation = ElementTree.SubElement(portType,'operation')
                
            operation.set('name',method.name)
            params = []
            for name,param in method.inMessage.params:
                params.append(name)

            documentation = ElementTree.SubElement(operation,'documentation')
            documentation.text = method.doc
            operation.set('parameterOrder',method.inMessage.typ)
            opInput = ElementTree.SubElement(operation,'input')
            opInput.set('name',method.inMessage.typ)
            opInput.set('message','tns:%s'%method.inMessage.typ)

            if method.outMessage.params != None and not method.isCallback and not method.isAsync:
                opOutput = ElementTree.SubElement(operation,'output')
                opOutput.set('name',method.outMessage.typ)
                opOutput.set('message','tns:%s'%method.outMessage.typ)
        
        # make partner link
        plink = ElementTree.SubElement(root,'plnk:partnerLinkType')
        plink.set('name',serviceName)
        role = ElementTree.SubElement(plink,'plnk:role')
        role.set('name', serviceName)
        plinkPortType = ElementTree.SubElement(role,'plnk:portType')
        plinkPortType.set('name','tns:%s'%serviceName) 

        if hasCallbacks:
            role = ElementTree.SubElement(plink,'plnk:role')
            role.set('name', '%sCallback'%serviceName)
            plinkPortType = ElementTree.SubElement(role,'plnk:portType')
            plinkPortType.set('name','tns:%sCallback'%serviceName) 

        self._add_bindings_for_methods(root,serviceName,methods)

        service = ElementTree.SubElement(root,'service')
        service.set('name',serviceName)
        wsdlPort = ElementTree.SubElement(service,'port')
        wsdlPort.set('name',serviceName)
        wsdlPort.set('binding','tns:%s'%serviceName)
        addr = ElementTree.SubElement(wsdlPort,'soap:address')
        addr.set('location',url)

        wsdl = ElementTree.tostring(root)
        wsdl = "<?xml version='1.0' encoding='utf-8' ?>%s"%(wsdl)

        #cache the wsdl for next time
        self.__wsdl__ = wsdl 
        return self.__wsdl__
コード例 #10
0
ファイル: wsgi_soap.py プロジェクト: Proga/soaplib
    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
コード例 #11
0
ファイル: wsgi_soap.py プロジェクト: Proga/soaplib
            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
コード例 #12
0
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). 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

            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 'CONTENT_LENGTH' in environ:
                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]
コード例 #13
0
    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
コード例 #14
0
            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
コード例 #15
0
ファイル: easy_client.py プロジェクト: gbhuvneshwar/debexpo
    serializer = new.classobj(__type_name, (ClassSerializer, ), {})
    namespace = None

    setattr(serializer, 'soap_members', {})
    setattr(serializer, 'namespace', namespace)
    for k, v in kwargs.items():
        serializer.soap_members[k] = get_serializer(v)
    o = serializer()
    for k, v in kwargs.items():
        setattr(o, k, v)
    return o


if __name__ == '__main__':
    echoInteger = make('echoInteger', i=34)
    print et.tostring(echoInteger.to_xml(echoInteger, 'echoInteger'))

    c = DocumentClient(
        'http://localhost:9753/',
        ['echoInteger', 'echoSimpleClass', 'echoSimpleClassArray'])
    print c.echoInteger(make('echoInteger', i=3)).retval

    print c.echoSimpleClass(
        make('echoSimpleClass', sc=make('SimpleClass', i=34,
                                        s='bobo'))).retval.s

    d = c.echoSimpleClassArray(
        make('echoSimpleClassArray',
             sca=[
                 make('SimpleClass', i=34, s='jasdf'),
                 make('SimpleClass', i=34, s='bobo'),
コード例 #16
0
ファイル: soaphandler.py プロジェクト: yangkf1985/torweb
class SOAPService(BaseHandler, SoapServiceBase):
    '''
    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 __init__(self, application, request, **kwargs):
        setattr(self, "request", request)
        setattr(self, "application", application)
        BaseHandler.__init__(self, application, request, **kwargs)
        SoapServiceBase.__init__(self)

    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
        '''
        return self

    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 payload is not None:
                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]

            # call the method
            func = getattr(service, methodname)

            # retrieve the method descriptor
            descriptor = func(_soap_descriptor=True, klazz=service.__class__)
            if payload is not None:
                params = descriptor.inMessage.from_xml(*[payload])
            else:
                params = ()
            # implementation hook
            self.onMethodExec(environ, body, params,
                              descriptor.inMessage.params)

            # call the method
            if len(_unmentioned_attachs) > 0:
                retval = func(*params,
                              unmentioned_attachs=_unmentioned_attachs)
            else:
                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)

            # construct the soap response, and serialize it
            envelope = make_soap_envelope(results, tns=service.__tns__)
            #ElementTree.cleanup_namespaces(envelope)
            resp = ElementTree.tostring(envelope, encoding=string_encoding)
            headers = {'Content-Type': 'text/xml;charset=utf-8'}

            if descriptor.mtom:
                headers, resp = apply_mtom(headers, resp,
                                           descriptor.outMessage.params,
                                           (retval, ))

            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)
コード例 #17
0
ファイル: easy_client.py プロジェクト: Manduka/soaplib
    serializer = new.classobj(__type_name, (ClassSerializer, ), {})
    namespace = None

    setattr(serializer, 'soap_members', {})
    setattr(serializer, 'namespace', namespace)
    for k, v in kwargs.items():
        serializer.soap_members[k] = get_serializer(v)
    o = serializer()
    for k, v in kwargs.items():
        setattr(o, k, v)
    return o


if __name__ == '__main__':
    echoInteger = make('echoInteger', i=34)
    print et.tostring(echoInteger.to_xml(echoInteger, 'echoInteger'))

    c = DocumentClient('http://localhost:9753/',
        ['echoInteger', 'echoSimpleClass', 'echoSimpleClassArray'])
    print c.echoInteger(make('echoInteger', i=3)).retval

    print c.echoSimpleClass(make('echoSimpleClass',
        sc=make('SimpleClass', i=34, s='bobo'))).retval.s

    d = c.echoSimpleClassArray(make('echoSimpleClassArray',
        sca=[
            make('SimpleClass', i=34, s='jasdf'),
            make('SimpleClass', i=34, s='bobo'),
            make('SimpleClass', i=34, s='poo'),
        ]))
    print '*' * 80
コード例 #18
0
ファイル: soaphandler.py プロジェクト: yangkf1985/torweb
    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
コード例 #19
0
ファイル: wsgi_soap.py プロジェクト: tubav/teagle
    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]
コード例 #20
0
    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]
コード例 #21
0
ファイル: client.py プロジェクト: edevil/soaplib
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(['    ' + str(p[0])
                for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {'Content-Length': len(body),
                      'Content-type': 'text/xml; charset="UTF-8"',
                      'Accept': ('application/soap+xml, application/dime, '
                                'multipart/related, text/*'),
                      'User-Agent': 'Soaplib/1.0',
                      'SOAPAction': methodName,
                      }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                self.descriptor.inMessage.params, args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                        "%s %s \n %s \n %s" %
                        (response.status, response.reason, trace.getvalue(),
                         data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        return results[0]
コード例 #22
0
ファイル: soaphandler.py プロジェクト: finalbattle/torweb
 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
コード例 #23
0
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(
                ['    ' + str(p[0]) for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {
            'Content-Length':
            len(body),
            'Content-type':
            'text/xml; charset="UTF-8"',
            'Accept': ('application/soap+xml, application/dime, '
                       'multipart/related, text/*'),
            'User-Agent':
            'Soaplib/1.0',
            'SOAPAction':
            methodName,
        }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                                           self.descriptor.inMessage.params,
                                           args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                               self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in ['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                                      "%s %s \n %s \n %s" %
                                      (response.status, response.reason,
                                       trace.getvalue(), data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        #TODO: consider supporting multiple return types in a better manner
        if len(results) > 1:
            return results
        return results[0]