source: TI12-security/trunk/python/Tests/Echo/EchoServer.py @ 1634

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/Tests/Echo/EchoServer.py@1634
Revision 1634, 4.5 KB checked in by pjkersha, 14 years ago (diff)

Tests with WebSphere?.

  • Property svn:executable set to *
Line 
1#!/bin/env python
2#
3# How to build an echo server using the extended code generation
4#
5import sys
6
7# Import the ZSI stuff you'd need no matter what
8from ZSI.ServiceContainer import ServiceContainer, SOAPRequestHandler
9
10# This is a new method imported to show it's value
11from ZSI.ServiceContainer import GetSOAPContext
12
13# Import the generated Server Object
14import EchoServer_interface
15
16# Set security operation by request type
17from EchoServer_messages import *
18
19import wsSecurity
20
21psBodyChildName = lambda ps: \
22    ps.dom.childNodes[1].childNodes[1].childNodes[0].localName
23   
24# Create a Server implementation
25
26#_____________________________________________________________________________
27class EchoSOAPRequestHandler(SOAPRequestHandler):
28     """Add a do_GET method to return the WSDL on HTTP GET requests.
29     Please note that the path to the wsdl file is derived from what
30     the HTTP invocation delivers (which is put into the self.path
31     attribute), so you might want to change this addressing scheme.
32     """
33     def do_GET(self):
34         """Return the WSDL file."""         
35         self.send_xml(EchoServer_interface.EchoServer._wsdl)
36         
37     def do_POST(self):
38          """Fudge to get _Dispatch to pick up the correct address
39          - seems to be necessary when putting proxy redirect for port in
40          the wsdl e.g. http://glue.badc.rl.ac.uk/sessionMgr points to the
41          default port for the Session Manager."""
42          self.path = "/EchoServIn"
43          SOAPRequestHandler.do_POST(self)
44   
45# This using a derived server instead
46class EchoServIn(EchoServer_interface.EchoServer):
47    def __init__(self, post='', **kw):
48        EchoServer_interface.EchoServer.__init__(self, post, **kw)
49
50        # Fudge to copy methods of this class so that equivalent
51        # SimpleCAService class WS stub picks it up
52        self.impl = self
53       
54        self.signatureHandler = wsSecurity.SignatureHandler(\
55                                    certFilePath='../Junk-cert.pem',
56                                    priKeyFilePath='../Junk-key.pem',
57                                    priKeyPwd=open('../tmp2').read().strip())
58
59        self.encryptionHandler = wsSecurity.EncryptionHandler(\
60                                    certFilePath='../Junk-cert.pem',
61                                    priKeyFilePath='../Junk-key.pem',
62                                    priKeyPwd=open('../tmp2').read().strip())
63       
64       
65    def sign(self, sw):
66        '''\
67        Overrides ServiceInterface class method to allow digital signature'''
68        if isinstance(self.request, EchoRequest):
69            # Echo response applies digital signature
70            self.signatureHandler.sign(sw)
71       
72    def verify(self, ps):
73        '''\
74        Overrides ServiceInterface class method to allow signature
75        verification'''     
76        if isinstance(self.request, EchoRequest):
77            # Echo request checks digital signature
78            self.signatureHandler.verify(ps)
79
80    def encrypt(self, sw):
81        if isinstance(self.request, EchoEncrRequest):
82            # Assume EchoEncr response - apply encryption
83            self.encryptionHandler.encrypt(sw)
84           
85    def decrypt(self, ps):
86        if psBodyChildName(ps) == 'EncryptedData':
87            # Assume EchoEncr request
88            self.encryptionHandler.decrypt(ps)
89       
90#
91#       
92#    def authorize(self, auth_info, post, action):
93#        print "Authorizing INHERIT Echo"
94#        ctx = GetSOAPContext()
95#        print dir(ctx)
96#        print "Container: ", ctx.connection
97#        print "Parsed SOAP: ", ctx.parsedsoap
98#        print "Container: ", ctx.container
99#        print "HTTP Headers:\n", ctx.httpheaders
100#        print "----"
101#        print "XML Data:\n", ctx.xmldata
102#        return 1
103
104    def Echo(self, input):
105        return "Input message was: %s" % input
106
107    def EchoEncr(self, input):
108        return "Input secret was: %s" % input
109
110# Here we set up the server
111serviceContainer = ServiceContainer(('localhost', 7100),
112                                    RequestHandlerClass=EchoSOAPRequestHandler)
113
114# Create the TIE version of the server
115#hws = EchoServer()
116#hwsi = EchoServer_interface.EchoServer(impl=hws,
117#                                       auth_method_name="authorize")
118#serviceContainer.setNode(hwsi, url="/EchoServer")
119
120# Create the Inherited version of the server
121import sys
122hws2 = EchoServIn(tracefile=sys.stdout)
123serviceContainer.setNode(hws2, url="/EchoServIn")
124
125try:
126    # Run the service container
127    serviceContainer.serve_forever()
128except KeyboardInterrupt:
129    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.