def formatFault(exc, service): if isinstance(exc, base.ValidationError): val = ZSI.Fault(ZSI.Fault.Client, unicodeXML(exc)) else: val = ZSI.Fault(ZSI.Fault.Server, unicodeXML(exc)) return val.AsSOAP( nsdict={"tns": base.getMetaText(service, "identifier")})
def _raise_exception(self, code, msg): if ZSI_AVAILABLE: msg = _propagate_stack_trace(self._cfg_manager, msg) raise ZSI.Fault('ZSI:' + code, msg) else: msg = "Optional library 'ZSI' is not available, so SOAP clients will not be supported. However, AbstractZSI is being used, so problems will arise" log.log(self, log.level.Error, msg) print >> sys.stderr, msg
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)
class RpcServlet(netsvc.HttpServlet, netsvc.Service): def __init__(self, session, binding): netsvc.HttpServlet.__init__(self, session) netsvc.Service.__init__(self, "", "", netsvc.SERVICE_HIDDEN) self._binding = binding self._method = None self._content = [] self._contentLength = 0 def destroyServlet(self): netsvc.HttpServlet.destroyServlet(self) netsvc.Service.destroyReferences(self) def processRequest(self): try: if self.requestMethod() != "POST": self.sendError(400, "Only POST method supported.") return if self.contentLength() <= 0: self.sendError(400, "Invalid content length.") return except: self.sendError(500, _escape(netsvc.exceptionDetails())) def processContent(self, content): try: try: self._content.append(content) self._contentLength = self._contentLength + len(content) if self._contentLength < self.contentLength(): return if self._contentLength > self.contentLength(): error = netsvc.SERVER_REQUEST_DECODING_ERROR description = netsvc.SERVER_REQUEST_DECODING_ERROR_MESSAGE origin = "netsvc.soap" details = "Invalid content length." raise netsvc.ServiceFailure(error, description, origin, details) content = string.join(self._content, "") 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 self._method = ps.body_root.localName except netsvc.ServiceFailure: raise except ZSI.EvaluateException: raise except: error = netsvc.SERVER_REQUEST_DECODING_ERROR description = netsvc.SERVER_REQUEST_DECODING_ERROR_MESSAGE origin = "netsvc.soap" details = netsvc.exceptionDescription() raise netsvc.ServiceFailure(error, description, origin, details) service = self.serviceEndPoint(self._binding) id = apply(service.__getattr__(self._method), params) self.processResponse(self.processResult, id) self.processFailure(self.processResult, id) except ZSI.EvaluateException, e: fault = ZSI.FaultFromZSIException(e) self.sendResult(500, fault.AsSOAP()) except netsvc.ServiceFailure, exception: try: detail = FAULT_DETAIL % (SCHEMA_URI, exception.error, _escape(exception.description), _escape(exception.origin), _escape(exception.details)) fault = ZSI.Fault(ZSI.Fault.Client, "Request Failed", None, detail, None) body = fault.AsSOAP() self.sendResult(500, body) except: self.sendError(500, _escape(netsvc.exceptionDetails()))