def Download(self, xml, req): try: username = req.Username password = req.Password except: username = password = None try: (code, starttime, endtime) = self.impl.Download(command_key=req.CommandKey, file_type=req.FileType, url=req.URL, username=username, password=password, file_size=int(req.FileSize), target_filename=req.TargetFileName, delay_seconds=int(req.DelaySeconds), success_url=req.SuccessURL, failure_url=req.FailureURL) except core.ResourcesExceededError as e: return soap.SimpleFault(xml, soap.CpeFault.RESOURCES_EXCEEDED, str(e)) except core.FileTransferProtocolError as e: return soap.SimpleFault(xml, soap.CpeFault.FILE_TRANSFER_PROTOCOL, str(e)) with xml['cwmp:DownloadResponse']: xml.Status(str(code)) xml.StartTime(cwmpdate.format(starttime)) xml.CompleteTime(cwmpdate.format(endtime)) return xml
def Handle(self, body): body = str(body) obj = soap.Parse(body) request_id = obj.Header.get('ID', None) req = obj.Body[0] method = req.name with soap.Envelope(request_id, None) as xml: try: responder = self._GetResponder(method) result = responder(xml, req) except api.SetParameterErrors as e: faults = self._ExceptionListToFaultList(e.error_list) result = soap.SetParameterValuesFault(xml, faults) except KeyError as e: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INVALID_PARAM_NAME, faultstring='No such parameter: %s' % e.args[0]) except IndexError as e: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INVALID_ARGUMENTS, faultstring=str(e)) except NotImplementedError: cpefault = soap.CpeFault.METHOD_NOT_SUPPORTED faultstring = 'Unsupported RPC method: %s' % method result = soap.SimpleFault(xml, cpefault, faultstring) except: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INTERNAL_ERROR, faultstring=traceback.format_exc()) if result is not None: return xml else: return None
def Handle(self, body): """Dispatch the given XML request to the implementation. Args: body: the xml string of the request. Returns: an xml string for the response, or None if no response is expected. """ # Data arriving from tornado web server should be non-decoded utf-8 body = bytes(body) obj = soap.Parse(body) # decodes as utf-8, via ElementTree request_id = obj.Header.get('ID', None) req = obj.Body[0] method = req.name with soap.Envelope(request_id, None) as xml: try: responder = self._GetResponder(method) result = responder(xml, req) except api.SetParameterErrors as e: faults = self._ExceptionListToFaultList(e.error_list) result = soap.SetParameterValuesFault(xml, faults) except api.AddObjectsErrors as e: faults = self._ExceptionListToFaultList(e.error_list) result = soap.AddObjectsFault(xml, faults) except api.ParameterNameError as e: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INVALID_PARAM_NAME, faultstring='No such parameter: %s' % unicode(e.parameter)) except KeyError as e: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INVALID_PARAM_NAME, faultstring='No such parameter: %s' % unicode(e.args[0])) except IndexError as e: result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INVALID_ARGUMENTS, faultstring=unicode(e)) except NotImplementedError: cpefault = soap.CpeFault.METHOD_NOT_SUPPORTED faultstring = 'Unsupported RPC method: %s' % method result = soap.SimpleFault(xml, cpefault, faultstring) except: # pylint:disable=bare-except result = soap.SimpleFault( xml, cpefault=soap.CpeFault.INTERNAL_ERROR, faultstring=traceback.format_exc()) traceback.print_exc() if result is not None: return bytes(xml) # the utf-8 encoded XML response (pass or fail) else: return None # no response generated
def GetParameterNames(self, xml, req): """Process a GetParameterNames request.""" path = unicode(req.ParameterPath) nextlevel = cwmpbool.parse(req.NextLevel) # Spec: If NextLevel is true and ParameterPath is a Parameter name # rather than apartial path, the CPE MUST return a fault response # with the Invalid Arguments fault code(9003). if nextlevel is True and path and not path.endswith('.'): return soap.SimpleFault(xml, soap.CpeFault.INVALID_ARGUMENTS, faultstring='No such parameter: %s' % unicode(path)) names = list(self.impl.GetParameterNames(path, nextlevel)) soaptype = 'ParameterInfoStruct[{0}]'.format(len(names)) parameter_list_attrs = {'soap-enc:arrayType': soaptype} with xml['cwmp:GetParameterNamesResponse']: with xml.ParameterList(**parameter_list_attrs): for name in names: with xml['ParameterInfoStruct']: xml.Name(name) xml.Writable( '1' ) # TODO(apenwarr): detect true writability here return xml
def CancelTransfer(self, xml, req): try: self.impl.CancelTransfer(req.CommandKey) except core.CancelNotPermitted as e: return soap.SimpleFault(xml, soap.CpeFault.DOWNLOAD_CANCEL_NOTPERMITTED, str(e)) xml['cwmp:CancelTransferResponse'](None) return xml