示例#1
0
    def _dispatch(self, method, args, dispatch_dict):
        """Custom XML-RPC dispatcher for components.

        method -- XML-RPC method name
        args -- tuple of paramaters to method

        """
        if method in dispatch_dict:
            method_func = dispatch_dict[method]
        else:
            try:
                method_func = self._resolve_exposed_method(method)
            except NoExposedMethod:
                self.logger.error("Unknown method %s" % (method))
                raise xmlrpclib.Fault(xmlrpclib.METHOD_NOT_FOUND,
                                      "Unknown method %s" % method)

        try:
            method_start = time.time()
            try:
                result = method_func(*args)
            finally:
                Bcfg2.Statistics.stats.add_value(method,
                                                 time.time() - method_start)
        except xmlrpclib.Fault:
            raise
        except Exception:
            err = sys.exc_info()[1]
            if getattr(err, "log", True):
                self.logger.error(err, exc_info=True)
            raise xmlrpclib.Fault(getattr(err, "fault_code", 1), str(err))
        return result
示例#2
0
 def _marshaled_dispatch(self, address, data):
     params, method = xmlrpclib.loads(data)
     if not self.instance.check_acls(address, method):
         raise XMLRPCACLCheckException
     try:
         if '.' not in method:
             params = (address, ) + params
         response = self.instance._dispatch(method, params, self.funcs)
         # py3k compatibility
         if type(response) not in [bool, str, list, dict]:
             response = (response.decode('utf-8'), )
         else:
             response = (response, )
         raw_response = xmlrpclib.dumps(response, methodresponse=True,
                                        allow_none=self.allow_none,
                                        encoding=self.encoding)
     except xmlrpclib.Fault:
         fault = sys.exc_info()[1]
         raw_response = xmlrpclib.dumps(fault, methodresponse=True,
                                        allow_none=self.allow_none,
                                        encoding=self.encoding)
     except:
         err = sys.exc_info()
         self.logger.error("Unexpected handler error", exc_info=1)
         # report exception back to server
         raw_response = xmlrpclib.dumps(
             xmlrpclib.Fault(1, "%s:%s" % (err[0].__name__, err[1])),
             methodresponse=True, allow_none=self.allow_none,
             encoding=self.encoding)
     return raw_response
示例#3
0
 def _marshaled_dispatch(self, address, data):
     method_func = None
     params, method = xmlrpclib.loads(data)
     try:
         if '.' not in method:
             params = (address, ) + params
         response = self.instance._dispatch(method, params, self.funcs)
         # py3k compatibility
         if type(response) not in [bool, str, list, dict]:
             response = (response.decode('utf-8'), )
         else:
             response = (response, )
         raw_response = xmlrpclib.dumps(response,
                                        methodresponse=1,
                                        allow_none=self.allow_none,
                                        encoding=self.encoding)
     except xmlrpclib.Fault:
         fault = sys.exc_info()[1]
         raw_response = xmlrpclib.dumps(fault,
                                        allow_none=self.allow_none,
                                        encoding=self.encoding)
     except:
         self.logger.error("Unexpected handler error", exc_info=1)
         # report exception back to server
         raw_response = xmlrpclib.dumps(xmlrpclib.Fault(
             1, "%s:%s" % (sys.exc_type, sys.exc_value)),
                                        allow_none=self.allow_none,
                                        encoding=self.encoding)
     return raw_response
示例#4
0
def on_error(*args, **kwargs):  # pylint: disable=W0613
    """ define our own error handler that handles xmlrpclib.Fault
    objects and so allows for the possibility of returning proper
    error codes. this obviates the need to use the builtin CherryPy
    xmlrpc tool """
    err = sys.exc_info()[1]
    if not isinstance(err, xmlrpclib.Fault):
        err = xmlrpclib.Fault(xmlrpclib.INTERNAL_ERROR, str(err))
    xmlrpcutil._set_response(xmlrpclib.dumps(err))  # pylint: disable=W0212
示例#5
0
def on_error(*args, **kwargs):  # pylint: disable=W0613
    """ CherryPy error handler that handles :class:`xmlrpclib.Fault`
    objects and so allows for the possibility of returning proper
    error codes. This obviates the need to use
    :func:`cherrypy.lib.xmlrpc.on_error`, the builtin CherryPy xmlrpc
    tool, which does not handle xmlrpclib.Fault objects and returns
    the same error code for every error."""
    err = sys.exc_info()[1]
    if not isinstance(err, xmlrpclib.Fault):
        err = xmlrpclib.Fault(xmlrpclib.INTERNAL_ERROR, str(err))
    xmlrpcutil._set_response(xmlrpclib.dumps(err))  # pylint: disable=W0212
示例#6
0
    def _dispatch(self, method, args, dispatch_dict):
        """ Dispatch XML-RPC method calls

        :param method: XML-RPC method name
        :type method: string
        :param args: Paramaters to pass to the method
        :type args: tuple
        :param dispatch_dict: A dict of method name -> function that
                              can be used to provide custom mappings
        :type dispatch_dict: dict
        :returns: The return value of the method call
        :raises: :exc:`xmlrpclib.Fault`
        """
        if method in dispatch_dict:
            method_func = dispatch_dict[method]
        else:
            try:
                method_func = self._resolve_exposed_method(method)
            except NoExposedMethod:
                self.logger.error("Unknown method %s" % (method))
                raise xmlrpclib.Fault(xmlrpclib.METHOD_NOT_FOUND,
                                      "Unknown method %s" % method)

        try:
            method_start = time.time()
            try:
                return method_func(*args)
            finally:
                Bcfg2.Server.Statistics.stats.add_value(
                    method,
                    time.time() - method_start)
        except xmlrpclib.Fault:
            raise
        except Exception:
            err = sys.exc_info()[1]
            if getattr(err, "log", True):
                self.logger.error(err, exc_info=True)
            raise xmlrpclib.Fault(getattr(err, "fault_code", 1), str(err))
示例#7
0
 def critical_error(self, operation):
     """Log and err, traceback and return an xmlrpc fault to client."""
     self.logger.error(operation, exc_info=1)
     raise xmlrpclib.Fault(xmlrpclib.APPLICATION_ERROR,
                           "Critical failure: %s" % operation)