source: TI07-MOLES/trunk/PythonCode/wsgi/ndgGateKeeper.py @ 2046

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/ndgGateKeeper.py@2046
Revision 2046, 2.7 KB checked in by lawrence, 12 years ago (diff)

Mainly test code to understand browse deployment environment (plus tiny
bug fix in browse docuemnt retrieval).

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