source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/browseCGI.py @ 783

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/browseCGI.py@783
Revision 783, 3.6 KB checked in by lawrence, 13 years ago (diff)

Scaffolding for connecting the browse portal to NDG security.

(Also includes a placeholder for putting the browse history by
using an xpath expression inside an elementtree of the output
from the xslt).

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2# CGI Script to support prototype NDG MOLES_Portal (Browse) functionality
3# Bryan Lawrence, April, 2006
4
5import cgi
6#import cgitb;ctitb.enable()
7import os
8import ElementTree
9from insecure import *
10from collections import deque # python 2.4
11from secure import *
12
13def EvaluateCredentials(ac,securityMetadata):
14        ''' Take Moles security metadata and compare with attribute certificate '''
15        return 1
16
17class RingBuffer(deque):
18    #deque is a python 2.4 class!
19    #credit http://www.onlamp.com/pub/a/python/excerpt/pythonckbk_chap1/index1.html
20    def __init__(self, size_max):
21        deque.__init__(self)
22        self.size_max = size_max
23    def append(self, datum):
24        deque.append(self, datum)
25        if len(self) > self.size_max:
26            self.popleft( )
27    def tolist(self):
28        return list(self)
29
30class BrowseSession:
31        ''' Holds the browse history and contact details for the NDG session manager '''
32        def __init__(self,cookie,url):
33                '''Instantiate and get security environment'''
34                # we should have two cookies, the NDG security cookie, and our browse history cookie!
35                self.cookie=cookie
36                self.history=RingBuffer(10)
37                self.ndgGate=gateway2NDGsession(cookie,url)
38                self.__loadBrowseHistory()
39        def __loadBrowseHistory(self):
40                ''' get the string of URI values out of the cookie (if it's there)'''
41                try:
42                        strHistory=self.cookie['BrowseHistory'].value
43                except:
44                        strHistory='[]'
45                for uri in strHistory[1:-1].split(','): self.addToHistory(uri)
46        def addToHistory(self,uri):
47                ''' Add a URI to the session history'''
48                self.history.append(uri)
49        def getHistory(self):
50                ''' Return a list of the items in the history '''
51                return self.history.tolist()
52        def getCredentials(self):
53                '''Obtain the attribute certificate from the wallet '''
54                return 'empty attribute certificate'
55
56class StubB:
57        ''' Holds the stub-b document and provides methods which get and manipulate it '''
58       
59        def __init__(self,uri,xsltMethod='JavaXalan'):
60                '''Instantiate by getting URI from exist database'''
61                #try:
62                self.xml=insecureGetDoc(uri)
63                #self.xml=uri
64                #except:
65                #       raise 'NotFound'
66                self.xsltMethod=xsltMethod
67       
68        def getSecurityDetails(self):
69                '''Obtain the roles and attribute authority address'''
70                #issue an xquery on the stub-b
71                #parse for roles
72                #parse for AA WSDL
73                #tree=ElementTree.fromstring(self.xml)
74                #sec=tree.findall('//ndgSecurity')
75                roles=[]
76                AAaddress=''
77                return roles, AAaddress
78       
79        def toHTML(self):
80                x=doXSLT(self.xml,xsltMethod=self.xsltMethod)
81                #return '<p>%s</p>'%x
82                return x
83       
84class CGIcontroller:
85        ''' Currently holds the cgi environment and controls '''
86       
87        def __init__(self):
88                ''' Instantiate the CGI environment'''
89                self.env=os.environ
90                self.path=self.env.get('PATH_INFO','/')
91                self.FieldStorage=cgi.FieldStorage()
92
93        def goforit(self):
94                ''' This method actually responds to the user'''
95               
96                #Instantiate the Session Environment
97                self.cookie=self.env.get('HTTP_COOKIE',None)
98
99                self.session=BrowseSession(self.cookie,self.path)
100               
101                #Handle authorisation
102                self.ac=self.session.getCredentials()
103       
104                #this will do for the moment, although I'd rather the whole
105                #URI was self consistent ...
106                if self.FieldStorage.has_key('uri'):
107                        self.uri=self.FieldStorage['uri'].value
108                else: 
109                        return self.error('No valid URI')
110                try:
111                        self.b=StubB(self.uri)
112                except 'NotFound':
113                        return self.error('URI cannot be retrieved from DataBase')
114                       
115                if EvaluateCredentials(self.ac,self.uri):
116                        #we can show the user the page
117                        return self.b.toHTML()
118                else:
119                        return self.error('Not Allowed')
120               
121        def error(self,message):
122                ''' Construct a nice formal response with appropriate HTML headers '''
123                return '<error>%s</error>'%message
124               
Note: See TracBrowser for help on using the repository browser.