Changeset 329f61d


Ignore:
Timestamp:
Jun 27, 2008 6:34:17 PM (16 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
Children:
0a47d52
Parents:
4f4b977
Message:

basic XMLRPC

Location:
fedd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd.py

    r4f4b977 r329f61d  
    1010from M2Crypto import SSL
    1111from M2Crypto.SSL.SSLServer import SSLServer
     12import xmlrpclib
    1213
    1314from optparse import OptionParser
     
    2728        self.impl = impl
    2829
    29 class fedd_handler(BaseHTTPRequestHandler):
     30class fedd_soap_handler(BaseHTTPRequestHandler):
    3031    server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version
    31 
    32     namespace = 'http://www.isi.edu/faber/fedd.wsdl'
    33     methods = {
    34             'RequestAccess': 'soap_RequestAccess',
    35             }
    3632
    3733
     
    5955        """Confirm that this class implements the namespace and SOAP method"""
    6056        root = ps.body_root
    61         if root.namespaceURI != fedd_handler.namespace:
     57        if root.namespaceURI != self.server.impl.soap_namespace:
    6258            self.send_fault(Fault(Fault.Client,
    6359                'Unknown namespace "%s"' % root.namespaceURI))
    6460            return False
    65         if not fedd_handler.methods.has_key(root.localName):
    66             self.send_fault(Fault(Fault.Client, 'Unknown operation "%s"' % \
    67                     root.localName))
     61
     62        if getattr(root, 'localName', None) == None:
     63            self.send_fault(Fault(Fault.Client, 'No method"'))
    6864            return False
    6965        return True
     
    8480        if not self.check_method(ps): return
    8581        try:
    86             # We know this will work because check_method passed above
    87             method = getattr(self.server.impl,
    88                     fedd_handler.methods[ps.body_root.localName])
    89             resp = method(ps, fedid(cert=self.request.get_peer_cert()))
     82            resp = self.server.impl.soap_dispatch(ps.body_root.localName, ps,
     83                    fedid(cert=self.request.get_peer_cert()))
    9084        except Fault, f:
    9185            self.send_fault(f)
     
    9791            self.send_xml(str(sw))
    9892
     93class fedd_xmlrpc_handler(BaseHTTPRequestHandler):
     94    server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version
     95
     96    def send_xml(self, text, code=200):
     97        """Send an XML document as reply"""
     98        self.send_response(code)
     99        self.send_header('Content-type', 'text/xml; charset="utf-8"')
     100        self.send_header('Content-Length', str(len(text)))
     101        self.end_headers()
     102        self.wfile.write(text)
     103        self.wfile.flush()
     104
     105    def do_POST(self):
     106        """Treat an HTTP POST request as an XMLRPC service call"""
     107
     108        resp = None
     109        data = None
     110        cl = int(self.headers['content-length'])
     111        data = self.rfile.read(cl)
     112        params, method = xmlrpclib.loads(data)
     113
     114        try:
     115            resp = self.server.impl.xmlrpc_dispatch(method, params,
     116                        fedid(cert=self.request.get_peer_cert()))
     117            data = xmlrpclib.dumps((resp,), methodresponse=True)
     118        except Fault, f:
     119            xf = xmlrpclib.Fault(f.code, f.string)
     120            data = xmlrpclib.dumps(xf, methodresponse=True)
     121            resp = None
     122        except xmlrpclib.Fault, f:
     123            data = xmlrpclib.dumps(f, methodresponse=True)
     124            resp = None
     125
     126        self.send_xml(data)
     127
     128
    99129class fedd_opts(OptionParser):
    100130    """Encapsulate option processing in this class, rather than in main"""
     
    103133                version="0.1")
    104134
    105         self.set_defaults(host="localhost", port=23235, debug=0)
     135        self.set_defaults(host="localhost", port=23235,
     136                transport="soap",debug=0)
    106137
    107138        self.add_option("-d", "--debug", action="count", dest="debug",
     
    113144        self.add_option("-p", "--port", action="store", type="int",
    114145                dest="port", help="Port to listen on (default %default)")
     146        self.add_option("-x","--transport", action="store", type="choice",
     147                choices=("xmlrpc", "soap"),
     148                help="Transport for request (xmlrpc|soap) (Default: %default)")
    115149        self.add_option("--trace", action="store_const", dest="tracefile",
    116150                const=sys.stderr, help="Print SOAP exchange to stderr")
     
    140174            raise
    141175
    142 s = fedd_server(SOAP_port, fedd_handler, ctx, impl)
     176if opts.transport == "soap":
     177    s = fedd_server(SOAP_port, fedd_soap_handler, ctx, impl)
     178elif opts.transport == "xmlrpc":
     179    s = fedd_server(SOAP_port, fedd_xmlrpc_handler, ctx, impl)
     180else:
     181    s = None
     182
    143183s.serve_forever()
  • fedd/fedd_client.py

    r4f4b977 r329f61d  
    88
    99from M2Crypto import SSL, X509
     10from M2Crypto.m2xmlrpclib import SSL_Transport
    1011import M2Crypto.httpslib
     12
     13from xmlrpclib import ServerProxy, Error, dumps, loads
    1114from ZSI import SoapWriter
    1215
     
    6972
    7073        self.set_defaults(url="https://localhost:23235", anonymous=False,
    71                 serialize_only=False, use_fedid=False, debug=0)
     74                serialize_only=False, transport="soap", use_fedid=False,
     75                debug=0)
    7276
    7377        self.add_option("-a","--anonymous", action="store_true",
     
    108112        self.add_option("-U", "--username", action="store", dest="user",
    109113                type="string", help="Use this username instead of the uid")
     114        self.add_option("-x","--transport", action="store", type="choice",
     115                choices=("xmlrpc", "soap"),
     116                help="Transport for request (xmlrpc|soap) (Default: %default)")
    110117        self.add_option("--trace", action="store_const", dest="tracefile",
    111118                const=sys.stderr, help="Print SOAP exchange to stderr")
     
    230237        else: raise
    231238
    232 loc = feddServiceLocator();
    233 port = loc.getfeddPortType(opts.url,
    234         transport=M2Crypto.httpslib.HTTPSConnection,
    235         transdict={ 'ssl_context' : context },
    236         tracefile=opts.tracefile)
    237 
    238 req = RequestAccessRequestMessage()
    239 
    240239msg = {
    241240        'allocID': pack_id('test alloc'),
     
    268267        msg['user'] = [];
    269268
    270 req.set_element_RequestAccessRequestBody(
    271         pack_soap(req, "RequestAccessRequestBody", msg))
    272 
    273269if opts.debug > 1: print >>sys.stderr, msg
    274270
    275 if opts.serialize_only:
    276     sw = SoapWriter()
    277     sw.serialize(req)
    278     print str(sw)
    279     sys.exit(0)
    280 
    281 try:
    282     resp = port.RequestAccess(req)
    283 except ZSI.FaultException, e:
    284     sys.exit("Fault: %s" % e)
    285 
    286 
    287 if (resp != None):
    288     resp_body = resp.get_element_RequestAccessResponseBody()
    289     if ( resp_body != None):
     271if opts.transport == "soap":
     272    loc = feddServiceLocator();
     273    port = loc.getfeddPortType(opts.url,
     274            transport=M2Crypto.httpslib.HTTPSConnection,
     275            transdict={ 'ssl_context' : context },
     276            tracefile=opts.tracefile)
     277
     278    req = RequestAccessRequestMessage()
     279
     280    req.set_element_RequestAccessRequestBody(
     281            pack_soap(req, "RequestAccessRequestBody", msg))
     282
     283    if opts.serialize_only:
     284        sw = SoapWriter()
     285        sw.serialize(req)
     286        print str(sw)
     287        sys.exit(0)
     288
     289    try:
     290        resp = port.RequestAccess(req)
     291    except ZSI.FaultException, e:
     292        sys.exit("Fault: %s" % e)
     293
     294
     295    if (resp != None):
     296        resp_body = resp.get_element_RequestAccessResponseBody()
     297        if ( resp_body != None):
     298            try:
     299                resp_dict = unpack_soap(resp_body)
     300                if opts.debug > 1: print >>sys.stderr, resp_dict
     301                print_response_as_testbed(resp_dict, opts.label)
     302            except RuntimeError, e:
     303                sys.exit("Bad response. %s" % e.messgae)
     304        else: sys.exit("No body in resonpse!?")
     305    else: sys.exit("No response?!?")
     306elif opts.transport == "xmlrpc":
     307    if opts.serialize_only:
     308        ser = dumps((msg,))
     309        sys.exit(0)
     310
     311    transport = SSL_Transport(context)
     312    port = ServerProxy(opts.url, transport=transport)
     313
     314    try:
     315        resp = port.RequestAccess({ 'RequestAccessRequestBody': msg})
     316        resp, method = loads(resp)
     317    except Error, e:
     318        sys.exit("Fault: %s" % e)
     319
     320
     321    if (resp != None):
    290322        try:
    291             resp_dict = unpack_soap(resp_body)
     323            print resp
     324            resp_dict = resp[0]['RequestAccessResponseBody']
    292325            if opts.debug > 1: print >>sys.stderr, resp_dict
    293326            print_response_as_testbed(resp_dict, opts.label)
    294327        except RuntimeError, e:
    295328            sys.exit("Bad response. %s" % e.messgae)
    296     else: sys.exit("No body in resonpse!?")
    297 else: sys.exit("No response?!?")
     329    else: sys.exit("No response?!?")
  • fedd/fedd_proj.py

    r4f4b977 r329f61d  
    88from M2Crypto.SSL.SSLServer import SSLServer
    99import M2Crypto.httpslib
     10import xmlrpclib
    1011
    1112import re
     
    3031    id_attrs = ("testbed", "cert_file", "trusted_certs", "proxy",
    3132            "proxy_trusted_certs", "cert_pwd")
     33
     34    # Used by the SOAP caller
     35    soap_namespace = 'http://www.isi.edu/faber/fedd.wsdl'
     36    soap_methods = { 'RequestAccess': 'soap_RequestAccess' }
     37    xmlrpc_methods = { 'RequestAccess': 'xmlrpc_RequestAccess' }
    3238
    3339    class access_project:
     
    407413        return msg
    408414
    409 
    410     def soap_RequestAccess(self, ps, fid):
    411         req = ps.Parse(RequestAccessRequestMessage.typecode)
    412 
    413 
    414         #if req == None: raise Fault(Fault.Client, "No request??")
    415         #req = req.get_element_RequestAccessRequestBody()
    416         # if req == None: raise Fault(Fault.Client, "No request body??")
    417 
    418         msg = self.RequestAccess(unpack_soap(req), fedid)
    419         resp = RequestAccessResponseMessage()
    420 
    421         resp.set_element_RequestAccessResponseBody(
    422                 pack_soap(resp, "RequestAccessResponseBody", msg))
    423         return resp
    424 
    425415    def RequestAccess(self, req, fid):
    426416
     
    466456            # Proxy the request
    467457            return self.proxy_request(dt, req)
     458
     459    def soap_RequestAccess(self, ps, fid):
     460        req = ps.Parse(RequestAccessRequestMessage.typecode)
     461
     462        msg = self.RequestAccess(unpack_soap(req), fedid)
     463        resp = RequestAccessResponseMessage()
     464
     465        resp.set_element_RequestAccessResponseBody(
     466                pack_soap(resp, "RequestAccessResponseBody", msg))
     467        return resp
     468
     469    def xmlrpc_RequestAccess(self, params, fid):
     470        msg = self.RequestAccess(params[0], fedid)
     471        if msg != None:
     472            return xmlrpclib.dumps(({ "RequestAccessResponseBody": msg },))
    468473
    469474    def read_trust(self, trust):
     
    634639        f.close()
    635640
     641    def soap_dispatch(self, method, req, fid):
     642        if fedd_proj.soap_methods.has_key(method):
     643            return getattr(self, fedd_proj.soap_methods[method])(req, fid)
     644        else:
     645            raise Fault(Fault.Client, "Unknown method: %s" % method)
     646
     647    def xmlrpc_dispatch(self, method, req, fid):
     648        if fedd_proj.xmlrpc_methods.has_key(method):
     649            return getattr(self, fedd_proj.xmlrpc_methods[method])(req, fid)
     650        else:
     651            raise xmlrpclib.Fault(100, "Unknown method: %s" % method)
     652
    636653def new_feddservice(configfile):
    637654    return fedd_proj(configfile)
Note: See TracChangeset for help on using the changeset viewer.