source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/utils/clientbase.py @ 4892

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/utils/clientbase.py@4892
Revision 4892, 4.3 KB checked in by pjkersha, 12 years ago (diff)

Fixed typo - requestedURI

Line 
1"""NDG Security
2
3Functionality for client interface to WSGI based applications. 
4
5NERC DataGrid Project
6"""
7__author__ = "P J Kershaw"
8__date__ = "30/01/09"
9__copyright__ = "(C) 2009 Science and Technology Facilities Council"
10__license__ = "BSD - see LICENSE file in top-level directory"
11__contact__ = "Philip.Kershaw@stfc.ac.uk"
12__revision__ = "$Id$"
13import logging
14log = logging.getLogger(__name__)
15
16class WSGIClientBase(object):
17    '''Base class for client interface to WSGI based applications.  The client
18    can access the service via a key in the WSGI environ dictionary or by
19    instantiating a proxy to some remote service.  By wrapping this choice,
20    clients can potentially avoid calls over the wire to services that are
21    otherwise available locally.  At the same time, the client can be
22    agnostic as to whether the call was made locally or over the network.
23    '''
24
25    environKey = ''
26   
27    def __init__(self,
28                 environKey=None, 
29                 environ={}, 
30                 **clientKw):
31 
32        self._environKey = environKey or WSGICLientBase.environKey
33                       
34        # Standard WSGI environment dict
35        self._environ = environ   
36       
37        # Derived class could instantiate required client type if a 'uri'
38        # key is set in clientKw   
39        self._client = None
40       
41
42    def _setEnviron(self, environ):
43        if not isinstance(environ, dict):
44            raise TypeError("Expecting dict type for 'environ' property")
45        self._environ = environ
46       
47    def _getEnviron(self, environ):
48        return self._environ
49   
50    environ = property(fget=_getEnviron, 
51                       fset=_setEnviron, 
52                       doc="WSGI environ dictionary")
53
54    def _getRef(self):
55        """Get reference to WSGI service instance in environ"""
56        raise NotImplementedError()
57   
58    ref = property(fget=_getRef, doc="local instance")
59   
60    def _refInEnviron(self):
61        '''Check whether a reference is present in the WSGI environ to the
62        service to be queried.  Check also that if a URI was specified by the
63        client, it matches the URI the local WSGI service is published under.
64       
65        This method is critical to the purpose of this class ie. enables
66        clients to optimize calls to local services by avoiding calling them
67        over the network and instead accessing them locally through the WSGI
68        stack.
69       
70        The client class must have a uri attribute and the WSGI service
71        referenced must have a published URI attribute
72        '''
73        if self._environKey not in self._environ:
74            log.debug("Checking for referenced WSGI service in environ: "
75                      "the given key was not found in the environ dictionary")
76            return False
77       
78        if self._client:
79            # A SOAP client was initialised - check to see if its URI matches
80            # the URI for the service referenced in environ
81            requestedURI = getattr(self._client, 'uri', None)
82            if requestedURI is None:
83                log.debug("Checking for referenced WSGI service in environ: "
84                          "No URI was set in the client request - assuming "
85                          "call to local service")
86                return True
87           
88            serviceURI = getattr(self._environ[self._environKey], 
89                                 'publishedURI',
90                                 None)
91            if serviceURI is None:
92                log.debug("Checking for referenced WSGI service in environ: "
93                          "no service URI was set")
94                return False
95            else:
96                log.debug("Checking for referenced WSGI service in environ: "
97                          "testing requested URI equals the referenced WSGI "
98                          "service's URI")
99                return requestedURI == serviceURI
100        else:
101            log.debug("Checking for referenced WSGI service in environ: "
102                      "no client is set - a local instance must be referenced")
103            return True
104   
105    # Define as property for convenient call syntax
106    refInEnviron = property(fget=_refInEnviron,
107                            doc="return True if referenced instance is "
108                                "available in WSGI environ")
Note: See TracBrowser for help on using the repository browser.