commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7455 - trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc


From: reinhard
Subject: [gnue] r7455 - trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc
Date: Thu, 21 Apr 2005 17:49:57 -0500 (CDT)

Author: reinhard
Date: 2005-04-21 17:49:56 -0500 (Thu, 21 Apr 2005)
New Revision: 7455

Modified:
   trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py
Log:
Transport exceptions happening in XMLRPC code to the front end as Fault object
instead of ProtocolError.


Modified: trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py
===================================================================
--- trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py 
2005-04-21 15:28:28 UTC (rev 7454)
+++ trunk/gnue-common/src/rpc/drivers/xmlrpc/pw_xmlrpc/ServerAdapter.py 
2005-04-21 22:49:56 UTC (rev 7455)
@@ -194,7 +194,7 @@
   # Call the requested method
   # ---------------------------------------------------------------------------
 
-  def call(self, method, params):
+  def call (self, method, params):
     if self._loglevel > 0:
       print _("Dispatching: "), method, params
     
@@ -206,159 +206,160 @@
     ##                     params=""
     ## Call to an method: (of a service=one object)
     ##                     method="DonutPlace.Management.Restart"
-    try:
-      if method [0] == '[':
-        # call to an object
-        # 1. get the object from the objectlibrarian
-        # 2. check, if the object is supported by the gfd
+    if method [0] == '[':
+      # call to an object
+      # 1. get the object from the objectlibrarian
+      # 2. check, if the object is supported by the gfd
 
-        try:
-          i = string.index (method, ']', 1)
-          objhandle = method [1:i]
-          method = method [i+2:]
+      try:
+        i = string.index (method, ']', 1)
+        objhandle = method [1:i]
+        method = method [i+2:]
 
-        except ValueError:
-          raise errors.ApplicationError, \
-              u_("Wrong format of object handle in method call %s") % method
+      except ValueError:
+        raise errors.ApplicationError, \
+            u_("Wrong format of object handle in method call %s") % method
 
-        # TODO check in service dir, if obj is supported or not
-        o = ObjectLibrarian.retrieveObject (objhandle)
+      # TODO check in service dir, if obj is supported or not
+      o = ObjectLibrarian.retrieveObject (objhandle)
 
+      try:
+        server_method = getattr (o, method)
+        server_attribute = None
+
+      except AttributeError:
+        server_method = None
+
         try:
-          server_method = getattr (o, method)
-          server_attribute = None
+          server_attribute = getattr (o, method [4:])
 
         except AttributeError:
-          server_method = None
+          if method != "_close":
+            raise errors.ApplicationError, \
+                u_("Internal XMLRPC server error: method %s can be "
+                   "found in the directory (build out of a .grpc file), "
+                   "but the object doesn't contain this method/attribut. "
+                   "Please check you .grpc file for wrong return types.") \
+                 % method
+        
+      if method != "_close":
+        direntry  = self.getMethodDirEntry (o._type + "." + method)
+        signature = direntry ['signature']
+      else:
+        signature = ('string',)
 
-          try:
-            server_attribute = getattr (o, method [4:])
+    else:
+      # call to a service method or a helper call (get/set) for
+      # a service attribut
+      direntry         = self.getMethodDirEntry (method)
+      server_method    = direntry ['binding']
+      server_attribute = None
+        
+      # check if it is an real method (binding -> method)
+      # or an get/set method for an attribut (binding-> attribut)
 
-          except AttributeError:
-            if method != "_close":
-              raise errors.ApplicationError, \
-                  u_("Internal XMLRPC server error: method %s can be "
-                     "found in the directory (build out of a .grpc file), "
-                     "but the object doesn't contain this method/attribut. "
-                     "Please check you .grpc file for wrong return types.") \
-                   % method
+      if (type (server_method) != type (self.call)):
+        server_attribute = server_method
+        server_method    = None
           
-        if method != "_close":
-          direntry  = self.getMethodDirEntry (o._type + "." + method)
-          signature = direntry ['signature']
-        else:
-          signature = ('string',)
-  
-      else:
-        # call to a service method or a helper call (get/set) for
-        # a service attribut
-        direntry         = self.getMethodDirEntry (method)
-        server_method    = direntry ['binding']
-        server_attribute = None
-          
-        # check if it is an real method (binding -> method)
-        # or an get/set method for an attribut (binding-> attribut)
+      signature = direntry ['signature']
 
-        if (type (server_method) != type (self.call)):
-          server_attribute = server_method
-          server_method    = None
+      if (server_method is None) and (server_attribute is None):
+        raise errors.SystemError, \
+            u_("Server XML-RPC method '%s' is not bound to real method") \
+            % method
+
+    try:
+      # TODO:  Compare submitted attributs with signature
+      pass
+
+    except KeyError:
+      raise errors.ApplicationError, \
+          u_("Server XML-RPC procedure %(method)s accepts just %(attr)s "
+             "as attributes") \
+          % {'method': method,
+             'attr'  : attr}
+    
+
+    # replace object handles in param with the real object
+    counter = 0
+    while counter < len (params):
+      p = params [counter]
+      if type (p) == type (""):
+        if (len (p) == 42) and (p [0] == "[") and (p [41] == "]"):
+          try:
+            p = p [1:41]
+            obj = ObjectLibrarian.retrieveObject (p)
+            newp = params [0:counter-1] + (obj,) + params [counter+1:]
+            params = newp
+
+          except:
+            pass
+
+      counter += 1
+
+    # check if it is an real method (binding -> method)
+    # or an get/set method for an attribut (binding-> attribut)
+
+    __params = typeconv.rpc_to_python (params, server.InvalidParameter)
+
+    if (server_method is not None):
             
-        signature = direntry ['signature']
-  
-        if (server_method is None) and (server_attribute is None):
-          raise errors.SystemError, \
-              u_("Server XML-RPC method '%s' is not bound to real method") \
-              % method
-  
+      # call method with params
       try:
-        # TODO:  Compare submitted attributs with signature
-        pass
+        result = server_method (*__params)
+      except:
+        # Conserve traceback
+        (group, name, message, detail) = errors.getException (1)
+        raise errors.RemoteError (group, name, message, detail)
 
-      except KeyError:
-        raise errors.ApplicationError, \
-            u_("Server XML-RPC procedure %(method)s accepts just %(attr)s "
-               "as attributes") \
-            % {'method': method,
-               'attr'  : attr}
+      result = typeconv.python_to_rpc (result, server.InvalidParameter)
+
+    # check if it's the close object method
+    elif (method == "_close"):
+      result = self.releaseDynamicObject (o)
       
-  
-      # replace object handles in param with the real object
-      counter = 0
-      while counter < len (params):
-        p = params [counter]
-        if type (p) == type (""):
-          if (len (p) == 42) and (p [0] == "[") and (p [41] == "]"):
-            try:
-              p = p [1:41]
-              obj = ObjectLibrarian.retrieveObject (p)
-              newp = params [0:counter-1] + (obj,) + params [counter+1:]
-              params = newp
+    else:
+      
+      ## check wether it's the set or the get method for the attribute
+      mparts = string.splitfields (method, '.')
+      mparts.reverse ()
+      calltype = mparts [0]
+      calltype = calltype [:4]
+      gDebug (9, 'method %s has calling type %s' % (method, calltype))
 
-            except:
-              pass
+      if calltype == 'set_':
+        # setAttribut method
+        server_attribute = params [0]
 
-        counter += 1
-  
-      # check if it is an real method (binding -> method)
-      # or an get/set method for an attribut (binding-> attribut)
-  
-      __params = typeconv.rpc_to_python (params, server.InvalidParameter)
-  
-      if (server_method is not None):
-              
-        # call method with params
-        result = server_method (*__params)
-        result = typeconv.python_to_rpc (result, server.InvalidParameter)
-  
-      # check if it's the close object method
-      elif (method == "_close"):
-        result = self.releaseDynamicObject (o)
-        
+      elif calltype == 'get_':
+        # getAttribut method
+        result = server_attribute
       else:
-        
-        ## check wether it's the set or the get method for the attribute
-        mparts = string.splitfields (method, '.')
-        mparts.reverse ()
-        calltype = mparts [0]
-        calltype = calltype [:4]
-        gDebug (9, 'method %s has calling type %s' % (method, calltype))
+        raise errors.SystemError, \
+            u_("Internal Server XML-RPC error: method type (get/set "
+               "attribute) couldn't be detected (method %s)") % method
+    
 
-        if calltype == 'set_':
-          # setAttribut method
-          server_attribute = params [0]
+    # replace real object in param with an object handle
+    if type (result) == type (self):  ## both should be instances
+       ObjectLibrarian.archiveObject (result)
 
-        elif calltype == 'get_':
-          # getAttribut method
-          result = server_attribute
-        else:
-          raise errors.SystemError, \
-              u_("Internal Server XML-RPC error: method type (get/set "
-                 "attribute) couldn't be detected (method %s)") % method
-      
-  
-      # replace real object in param with an object handle
-      if type (result) == type (self):  ## both should be instances
-         ObjectLibrarian.archiveObject (result)
-  
-         # get the type of the result
-         rtype = signature [0]
-         # delete the surrounding brackets < >
-         rtype = rtype [1:len(rtype)-1]
-         # store typeinfo in new object
-         result._type = rtype
-  
-         result = ObjectLibrarian.getObjectReference (result)
-         self.registerDynamicObject (result, rtype)
-  
-      # check for empty results (not allowed for XMLRPC)
-      if (result is None) or (result == [None]):
-        gDebug (9, 'Transform result None into 1')
-        result = 1
+       # get the type of the result
+       rtype = signature [0]
+       # delete the surrounding brackets < >
+       rtype = rtype [1:len(rtype)-1]
+       # store typeinfo in new object
+       result._type = rtype
 
-    except:
-      stack  = string.join (errors.getException (1), u'\x91')
-      result = xmlrpclib.Fault (1, stack)
+       result = ObjectLibrarian.getObjectReference (result)
+       self.registerDynamicObject (result, rtype)
 
+    # check for empty results (not allowed for XMLRPC)
+    if (result is None) or (result == [None]):
+      gDebug (9, 'Transform result None into 1')
+      result = 1
+
     return result
   
   
@@ -434,47 +435,38 @@
   #
   # Handle the HTTP POST command
   #
-  # This method is based largely on the
-  # sample code in xmlrpcserver.
-  #
   def do_POST(self):
     try:
-      # get arguments
-      data = self.rfile.read(int(self.headers["content-length"]))
+      # Read the request
+      data = self.rfile.read (int (self.headers ["content-length"]))
+      params, method = xmlrpclib.loads (data)
       
-      params, method = xmlrpclib.loads(data)
-      
-      # generate response
-      response = self.server._parent.call(method, params)
+      # Execute the function
+      response = self.server._parent.call (method, params)
 
-      if isinstance (response, xmlrpclib.Fault):
-        response = xmlrpclib.dumps (response)
-      else:
-        if response is None:
-          response = ()
-        elif not isinstance (response, types.TupleType):
-          response = (response,)
-        response = xmlrpclib.dumps (response, methodresponse = 1)
-        
+      # Generate response as a tuple
+      if response is None:
+        response = ()
+      elif not isinstance (response, types.TupleType):
+        response = (response,)
+      response = xmlrpclib.dumps (response, methodresponse = 1)
     except:
-      # internal error, report as HTTP server error
-      gDebug (1, 'Unexpected Exception in XML-RPC code: %s:%s' % \
-                 (sys.exc_type, sys.exc_value))
-      # TODO: try to be a bit more informative (on client side)
-      self.send_response(500)
-      self.end_headers()
-    else:
-      # got a valid XML RPC response
-      self.send_response(200)
-      self.send_header("Content-type", "text/xml")
-      self.send_header("Content-length", str(len(response)))
-      self.end_headers()
-      self.wfile.write(response)
+      # Send back exceptions as Fault responses
+      stack  = string.join (errors.getException (), u'\x91')
+      response = xmlrpclib.Fault (1, stack)
+      response = xmlrpclib.dumps (response, methodresponse = 1)
 
-      # shut down the connection
-      self.wfile.flush()
-      self.connection.shutdown(1)
+    self.send_response (200)
+    self.send_header ("Content-type", "text/xml")
+    self.send_header ("Content-length", str (len (response)))
+    self.end_headers ()
+    self.wfile.write (response)
 
+    # shut down the connection
+    self.wfile.flush ()
+    self.connection.shutdown (1)
+
+
 ##############################################################################
 #
 # Enhanced HTTP Request Handler serving XMLRPC Post requests and allows to





reply via email to

[Prev in Thread] Current Thread [Next in Thread]