source: TI07-MOLES/tags/wsgi-beta/ndgGateKeeper.py @ 2371

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/tags/wsgi-beta/ndgGateKeeper.py@5134
Revision 2371, 3.0 KB checked in by lawrence, 13 years ago (diff)

Interim check in browse update ... (browse still not working properly)

Line 
1from Utilities import myConfig
2from paste.request import parse_querystring
3from ndgRetrieve import showDoc
4from ndgObject import ndgObject
5
6def keeperFactory(global_config,**local_conf):
7   
8    ''' This factory is intended to be used by paste-deploy to return
9    an ndg Gatekeeper aplication (designed to protect access to specific
10    ndg resources) '''
11   
12    if 'configDir' in local_conf.keys():
13        configDir=local_conf[configDir]
14    else:
15        try:
16            configDir=global_config['configDir']
17        except:
18            configDir=None
19           
20    def filter(app):
21        return ndgGateKeeper(app,configDir)
22   
23    return filter
24       
25def query_replace(inputs,arg,value):
26    ''' Replaces a particular argument in the query string and reloads it into
27    the wsgi environment '''
28    q=''
29    for i in inputs: q+='%s=%s&'%(i,inputs[i])
30    q=q[0:-1]
31    return q
32   
33   
34def check_access():
35    return 1
36   
37class ndgGateKeeper(object):
38   
39    ''' This is the ndg Gatekeeper application, it works in partnership with the
40    ndg retrieve application and a user defined application '''
41   
42    def __init__(self,wrapTarget,configDir):
43       
44        ''' Instantiate a wsgi ndgDiscovery GUI application '''
45        self.configDir=configDir
46        self.wrapTarget=wrapTarget
47        self.ndgSession=None
48        self.config=myConfig(configDir+'ndgDiscovery.config')
49        self.shower=showDoc(self.config)
50       
51    def __call__(self,environ,start_response):
52       
53        ''' This is the function which implements the gatekeeper '''
54       
55        environ['ndgSession']=self.ndgSession
56        inputs=dict(parse_querystring(environ))
57        if 'type' in inputs.keys(): 
58            outputType=inputs['type']
59        else:
60            outputType='xml'
61        inputs['type']='xml'
62        # modify the query string to enforce only getting the xml version at
63        # this stage, since we need that to decide whether it can be shown.
64
65        environ['QUERY_STRING']=query_replace(inputs,'type','xml')
66
67        ctype='text/xml'
68        if outputType!='xml': ctype='text/html'
69
70        # provide an alternative start_response to ensure that
71        # the output headers are what we want
72        def wrapped_start_response(status,headers,errorhandler=None):
73           pass
74        try:
75            # now look at the document and decide whether the user can look at it ...
76            r=self.wrapTarget(environ,wrapped_start_response)
77            if r[0][0:3]!='<p>':
78                # Currently assuming error messages from the wrapped Target are coming back as <p>...
79                # yes they can see it ...
80                if 'format' not in inputs:
81                    n=ndgObject(inputs['uri'])
82                    inputs['format']=n.schema
83                print 'Desired format',inputs['format']
84                r=self.shower.get(r[0],inputs['format'],otype=outputType)
85        except ValueError,e:
86            r=e
87
88        # now do the real start response
89        start_response('200 OK', [('Content-Type', ctype)])
90        xml=r[0]
91
92        return r
Note: See TracBrowser for help on using the repository browser.