def check_union_long(self): import time typecode = GED("http://www.pasoa.org/schemas/version024/PStruct.xsd", "localPAssertionId") for value in (1234455, "whatever", "urn:whatever"): sw = ZSI.SoapWriter() sw.serialize(value, typecode) xml = str(sw) ps = ParsedSoap(xml) pyobj = ps.Parse(typecode) # Union Limitation: # currently it tries to parse it sequentially via memberTypes, # so string is going to parse the URI when we want anyURI self.failUnless(value == pyobj, 'Expected equivalent')
def test_local_anyType(self): """serialize an int via anyType, then parse it back. """ import time pyobj = self.client_module.ReadSoapOut() pyobj.RItemList = pyobj.new_RItemList() item = pyobj.RItemList.new_Items() pyobj.RItemList.Items = [ item, ] item.typecode.ofwhat[1].processContents = 'lax' item.Value = 123 s = str(ZSI.SoapWriter().serialize(pyobj)) ps = ZSI.ParsedSoap(s) pyobj = ps.Parse(pyobj.typecode) for item in pyobj.RItemList.Items: item.Value
def serializePrimaryTable(data, service): """returns a SOAP serialization of the DataSet data's primary table. """ table = data.getPrimaryTable() tns = base.getMetaText(service, "identifier") class Row(TC.Struct): def __init__(self): TC.Struct.__init__(self, None, [ sqltypeToTC(f.type)(pname=(tns, f.name)) for f in table.tableDef], pname=(tns, "outRow")) class Table(list): typecode = TC.Array((tns, 'outRow'), Row(), pname=(tns, 'outList')) mapped = Table( base.SerManager(table, mfRegistry=_wsdlMFRegistry).getMappedValues()) sw = ZSI.SoapWriter(nsdict={"tns": tns}) sw.serialize(mapped).close() return str(sw)
def processResult(self, params): try: if self.failureOccurred(): exception = self.currentFailure() error = exception.error() description = exception.description() origin = exception.origin() details = exception.details() raise netsvc.ServiceFailure(error, description, origin, details) params = [params] reply = StringIO.StringIO() ZSI.SoapWriter(reply).serialize( params, ZSI.TC.Any(aslist=1, pname=self._method + 'Response')) self.sendResult(200, reply.getvalue()) except netsvc.ServiceFailure, exception: try: detail = FAULT_DETAIL % (SCHEMA_URI, exception.error, _escape(exception.description), _escape(exception.origin), _escape(exception.details)) if exception.origin == "netsvc" and \ (exception.error == netsvc.SERVER_METHOD_UNAVAILABLE or \ exception.error == netsvc.SERVER_PARAMETERS_INCORRECT or \ exception.error == netsvc.SERVER_REQUEST_DECODING_ERROR): fault = ZSI.Fault(ZSI.Fault.Client, "Request Failed", None, detail, None) else: fault = ZSI.Fault(ZSI.Fault.Server, "Request Failed", None, detail, None) body = fault.AsSOAP() self.sendResult(500, body) except: self.sendError(500, _escape(netsvc.exceptionDetails()))
def execute(self): try: if os.environ["REQUEST_METHOD"] != "POST": self._error(400,"Only POST method supported.") return ct = string.split(os.environ.get("CONTENT_TYPE","text/xml"),';')[0] if ct != "text/xml": self._error(400,"Invalid content type.") return length = int(os.environ["CONTENT_LENGTH"]) content = sys.stdin.read(length) ps = ZSI.ParsedSoap(content) data = ZSI._child_elements(ps.body_root) if len(data) == 0: params = [] else: try: tc = ZSI.TC.Any() params = [] for e in data: params.append(tc.parse(e,ps)) except ZSI.EvaluateException: raise method = ps.body_root.localName result = self.dispatch(method,params) result = [result] reply = StringIO.StringIO() ZSI.SoapWriter(reply).serialize(result, ZSI.TC.Any(aslist=1,pname=method + 'Response')) body = reply.getvalue() print "Status: 200" print "Content-Type: text/xml" print "Content-Length: %d" % len(body) print sys.stdout.write(body) except SystemExit: pass except ServiceFailure, exception: result = {} result["error"] = exception.error result["description"] = exception.description result["origin"] = exception.origin result["details"] = exception.details detail = FAULT_DETAIL % (SCHEMA_URI, exception.error,_escape(exception.description), _escape(exception.origin),_escape(exception.details)) if exception.origin == "netsvc" and \ (exception.error == netrpc.SERVER_METHOD_UNAVAILABLE or \ exception.error == netrpc.SERVER_PARAMETERS_INCORRECT or \ exception.error == netrpc.SERVER_REQUEST_DECODING_ERROR): fault = ZSI.Fault(ZSI.Fault.Client, "Request Failed",None,detail,None) else: fault = ZSI.Fault(ZSI.Fault.Server, "Request Failed",None,detail,None) body = fault.AsSOAP() print "Status: 500" print "Content-Type: text/xml" print "Content-Length: %d" % len(body) print sys.stdout.write(body)
def call(self,method,params,kw): try: if RemoteService._debug: RemoteService._request = "" RemoteService._response = "" if self._type != "http": error = CLIENT_SERVICE_ADDRESS_INVALID description = CLIENT_SERVICE_ADDRESS_INVALID_MESSAGE origin = "netrpc.soap" details = self._url raise AddressInvalid(error,description,origin,details) try: ns = kw.get("ns",self._ns) soapaction = kw.get("soapaction",self._soapaction) if soapaction == None: soapaction = '""' d = {} if ns: d[''] = ns s = StringIO.StringIO() sw = ZSI.SoapWriter(s,nsdict=d) #sw.serialize(params,ZSI.TC.Any(method)) sw.serialize(params,ZSI.TC.Any(method,aslist=1)) sw.close() body = s.getvalue() except ServiceFailure: raise except: error = CLIENT_REQUEST_ENCODING_ERROR description = CLIENT_REQUEST_ENCODING_ERROR_MESSAGE origin = "netrpc.soap" details = exceptionDescription() raise ServiceFailure(error,description,origin,details) try: request = httplib.HTTP(self._host) request.putrequest("POST",self._path) request.putheader("Host",self._host) request.putheader("Connection","Close") request.putheader("Content-Type","text/xml; charset=utf-8") request.putheader("Content-Length",str(len(body))) request.putheader("User-Agent","OSE/SOAP(ZSI)") request.putheader("SOAPAction",soapaction) if self._login != None and self._password != None: info = "%s:%s" % (self._login,self._password) info = base64.encodestring(info) info = "Basic %s" % string.strip(info) request.putheader("Authorization",info) request.endheaders() if RemoteService._debug: headers = "POST %s HTTP/1.0" % self._path headers = "%s\nHost: %s" % (headers,self._host) headers = "%s\nConnection: Close" % headers headers = "%s\nContent-Type: text/xml; charset=utf-8" % headers headers = "%s\nContent-Length: %s" % (headers,str(len(body))) headers = "%s\nUser-Agent: OSE/SOAP(ZSI)" % headers headers = "%s\nSOAPAction: %s" % (headers,soapaction) RemoteService._request = headers + "\n\n" + body request.send(body) except socket.error,exception: error = CLIENT_SERVICE_UNAVAILABLE description = CLIENT_SERVICE_UNAVAILABLE_MESSAGE origin = "netrpc.soap" details = exceptionDescription() raise ServiceUnavailable(error,description,origin,details) error,description,headers = request.getreply() if RemoteService._debug: RemoteService._response = "HTTP/1.0 %d %s\r\n" % (error,description) for line in headers.headers: RemoteService._response = RemoteService._response + line; RemoteService._response = string.replace( RemoteService._response,"\r\n","\n") if error != 200: if error == 500: if headers.has_key("content-type") and \ string.split(headers["content-type"],';')[0] == "text/xml": error = 200 if error != 200: origin = "netrpc.soap" response = request.getfile() # This alone was loosing data # under Mac OS for some reason. #details = response.read() details = "" content = [] while 1: try: # Large read was still loosing # data under Mac OS. Small read # works okay though. Strange!!! data = response.read(64) if data: content.append(data) else: break except: break details = string.join(content,'') if RemoteService._debug: RemoteService._response = RemoteService._response + "\n" + details response.close() if error == 401: raise AuthenticationFailure(error,description,origin,details) elif error == 404: raise ServiceUnavailable(error,description,origin,details) else: raise TransportFailure(error,description,origin,details) error = CLIENT_RESPONSE_HEADERS_INVALID description = CLIENT_RESPONSE_HEADERS_INVALID_MESSAGE origin = "netrpc.soap" if not headers.has_key("content-type"): details = "The Content-Type header does not exist." raise ServiceFailure(error,description,origin,details) if string.split(headers["content-type"],';')[0] != "text/xml": details = "Unexpected value of %s for Content-Type." details = details % `headers["content-type"]` raise ServiceFailure(error,description,origin,details) if not headers.has_key("content-length"): details = "The Content-Length header does not exist." raise ServiceFailure(error,description,origin,details) try: expect = int(headers["content-length"]) except: details = "Content length not an integer, but %s." details = details % `headers["content-length"]` raise ServiceFailure(error,description,origin,details) try: response = request.getfile() actual = 0 data = [] if RemoteService._debug: RemoteService._response = RemoteService._response + "\n" while 1: content = response.read(4096) if not content: break data.append(content) actual = actual + len(content) if RemoteService._debug: RemoteService._response = RemoteService._response + content if expect != actual: error = CLIENT_RESPONSE_HEADERS_INVALID description = CLIENT_RESPONSE_HEADERS_INVALID_MESSAGE origin = "netrpc.soap" details = "Length of content was %d, expected %d." details = details % (actual,expect) raise ServiceFailure(error,description,origin,details) data = string.join(data,"") result = ZSI.ParsedSoap(data) except ServiceFailure: raise except: error = CLIENT_RESPONSE_DECODING_ERROR description = CLIENT_RESPONSE_DECODING_ERROR_MESSAGE origin = "netrpc.soap" details = exceptionDescription() raise ServiceFailure(error,description,origin,details) if result.IsAFault(): try: fault = ZSI.FaultFromFaultMessage(result) error = SERVER_UNKNOWN_ERROR description = fault.string origin = "netrpc.soap" details = "" if type(fault.detail) in ZSI._seqtypes: if len(fault.detail) != 0: elt = fault.detail[0] if elt.nodeName == "OSE:ServiceFailure": tc = ZSI.TC.Struct(None,[ZSI.TC.Integer('error'), ZSI.TC.String('description'),ZSI.TC.String('origin'), ZSI.TC.String('details')],'ServiceFailure') detail = tc.parse(elt,result) error = detail['error'] description = detail['description'] origin = detail['origin'] details = detail['details'] elif elt.nodeName == "ZSI:FaultDetail": tc = ZSI.TC.Struct(None,[ZSI.TC.String('string'), ZSI.TC.String('trace')],'FaultDetail') detail = tc.parse(elt,result) details = detail['string'] + '\n\n' + detail['trace'] elif elt.nodeName == "ZSI:ParseFaultDetail": tc = ZSI.TC.Struct(None,[ZSI.TC.String('string'), ZSI.TC.String('trace')],'FaultDetail') detail = tc.parse(elt,result) details = detail['string'] + '\n\n' + detail['trace'] else: details = fault.detail elif type(fault.detail) in ZSI._stringtypes: details = fault.detail except: error = CLIENT_RESPONSE_DECODING_ERROR description = CLIENT_RESPONSE_DECODING_ERROR_MESSAGE origin = "netrpc.soap" details = exceptionDescription() raise ServiceFailure(error,description,origin,details) raise ServiceFailure(error,description,origin,details) tc = ZSI.TC.Any() data = ZSI._child_elements(result.body_root) if len(data) == 0: return None return tc.parse(data[0],result)