1 | #!/usr/bin/env python |
---|
2 | # CGI Script to support prototype NDG MOLES_Portal (Browse) functionality |
---|
3 | # Bryan Lawrence, April, 2006 |
---|
4 | |
---|
5 | import cgi |
---|
6 | #import cgitb;ctitb.enable() |
---|
7 | |
---|
8 | import os |
---|
9 | import ElementTree as ET |
---|
10 | |
---|
11 | from insecure import * |
---|
12 | from secure import * |
---|
13 | from stubB import * |
---|
14 | |
---|
15 | from renderEntity import renderEntity |
---|
16 | from renderPage import renderPage |
---|
17 | from renderDiscoverySet import renderDiscoverySet |
---|
18 | from htmlUtilities import selector,hyperlink |
---|
19 | from Utilities import * |
---|
20 | from ETxmlView import * |
---|
21 | from DiscoveryWS import * |
---|
22 | |
---|
23 | import Cookie |
---|
24 | |
---|
25 | class BrowseSession: |
---|
26 | |
---|
27 | ''' Holds the browse and select history ''' |
---|
28 | |
---|
29 | def __init__(self,cookie,config): |
---|
30 | |
---|
31 | '''Instantiate with an unsecured browse session (security only |
---|
32 | required if a secure resource is the target) ''' |
---|
33 | self.config=config |
---|
34 | self.rawCookie=cookie |
---|
35 | self.cookie=Cookie.SimpleCookie(cookie) |
---|
36 | self.history=RingBuffer(10) |
---|
37 | self.selected=RingBuffer(5) |
---|
38 | |
---|
39 | self.__load() |
---|
40 | |
---|
41 | def __toXML(self): |
---|
42 | ''' Used to serialise the session into a cookie string ''' |
---|
43 | xml='<ndgCookie><bh>' |
---|
44 | for uri,name in self.getHistory(): |
---|
45 | xml+='<i><u>%s</u><n>%s</n></i>'%(uri,name) |
---|
46 | xml+='</bh><sh>' |
---|
47 | for uri,name in self.getSelected(): |
---|
48 | xml+='<i><u>%s</u><n>%s</n></i>'%(uri,name) |
---|
49 | xml+='</sh></ndgCookie>' |
---|
50 | return xml |
---|
51 | |
---|
52 | def __load(self): |
---|
53 | ''' get lists of URI values out of a cookie ''' |
---|
54 | try: |
---|
55 | e=ET.fromstring(self.cookie['history'].value) |
---|
56 | for se in e: |
---|
57 | for item in se: |
---|
58 | uri,name=item.find('u'),item.find('n') |
---|
59 | self.__addTo(uri.text,name.text,se.tag) |
---|
60 | except KeyError: |
---|
61 | pass |
---|
62 | |
---|
63 | def __addTo(self,uri,name,tag,ignore=False): |
---|
64 | d={'bh':self.history,'sh':self.selected} |
---|
65 | current=d[tag].tolist() |
---|
66 | if (uri,name) not in current or ignore: |
---|
67 | d[tag].append((uri,name)) |
---|
68 | |
---|
69 | def addToHistory(self,uri,name,ignore=False): |
---|
70 | ''' Add a URI to the session history''' |
---|
71 | self.__addTo(uri,name,'bh',ignore) |
---|
72 | |
---|
73 | def getHistory(self): |
---|
74 | ''' Return a list of the items in the history ''' |
---|
75 | return self.history.tolist() |
---|
76 | |
---|
77 | def addToSelected(self,uri,name,ignore=False): |
---|
78 | ''' Add a URI to the selected items ''' |
---|
79 | self.__addTo(uri,name,'sh',ignore) |
---|
80 | self.__addTo(uri,name,'bh',ignore) |
---|
81 | |
---|
82 | def getSelected(self): |
---|
83 | ''' Return a list of selected items ''' |
---|
84 | return self.selected.tolist() |
---|
85 | |
---|
86 | def makeCookie(self,ndgSec=None): |
---|
87 | ''' Create a local cookie ''' |
---|
88 | import md5,time,base64 |
---|
89 | # start by creating a unique session id |
---|
90 | m=md5.new() |
---|
91 | m.update('this is a seed string') |
---|
92 | m.update(str(time.time())) |
---|
93 | cookie=Cookie.SimpleCookie() |
---|
94 | cookie['session']=base64.encodestring(m.digest())[:-3].replace("/", "$") |
---|
95 | cookie['history']=self.__toXML() |
---|
96 | if ndgSec is not None: |
---|
97 | cookie['NDG-ID1']=ndgSec[0] |
---|
98 | cookie['NDG-ID2']=ndgSec[1] |
---|
99 | return cookie |
---|
100 | |
---|
101 | class Request: |
---|
102 | ''' Holds the request URL etc ''' |
---|
103 | def __init__(self,env): |
---|
104 | self.env=env |
---|
105 | self.URL='http://%s:%s%s'%(env.get('SERVER_NAME'),env.get('SERVER_PORT'), |
---|
106 | env.get('SCRIPT_NAME')) |
---|
107 | qs=env.get('QUERY_STRING') |
---|
108 | if qs!='': self.URL+='?'+qs |
---|
109 | |
---|
110 | class CGIcontroller: |
---|
111 | ''' Currently holds the cgi environment and controls ''' |
---|
112 | |
---|
113 | def __init__(self,config): |
---|
114 | ''' Instantiate the CGI environment''' |
---|
115 | # |
---|
116 | # Need to refactor all of this to use the request class and then move |
---|
117 | # the request class out into the utilities ... |
---|
118 | # |
---|
119 | self.env=os.environ |
---|
120 | self.path=self.env.get('PATH_INFO','/') |
---|
121 | self.FieldStorage=getURLdict(cgi.FieldStorage()) |
---|
122 | self.config=config |
---|
123 | self.response=Response() |
---|
124 | self.request=Request(self.env) |
---|
125 | self.requestURL=self.request.URL |
---|
126 | self.selector=selector(self.requestURL,config.get('layout','selectI')) |
---|
127 | |
---|
128 | |
---|
129 | def makeGateway(self,cookie=None): |
---|
130 | ''' Make connection to NDG security and load what is necessary for |
---|
131 | an NDG cookie to be written ''' |
---|
132 | aa=self.config.get('security','localAA',None) |
---|
133 | if 'NDG-ID1' in self.FieldStorage and 'NDG-ID2' in self.FieldStorage: |
---|
134 | #this is a redirect following login ... |
---|
135 | cmdLine=(self.FieldStorage['NDG-ID1'],self.FieldStorage['NDG-ID2']) |
---|
136 | else: cmdLine=None |
---|
137 | self.ndgGate=gateway2NDGsession(self.requestURL,self.config,aa,cookie=cookie,cmdLine=cmdLine) |
---|
138 | self.ndgSec=cmdLine |
---|
139 | |
---|
140 | def goforit(self): |
---|
141 | ''' This method actually responds to the user''' |
---|
142 | |
---|
143 | self.ViewTextOnly,self.ViewXML=0,0 |
---|
144 | |
---|
145 | #Instantiate the Session Environment |
---|
146 | self.cookie=self.env.get('HTTP_COOKIE',None) |
---|
147 | self.session=BrowseSession(self.cookie,self.config) |
---|
148 | if self.config.logfile is not None: self.config.log(self.cookie) |
---|
149 | |
---|
150 | #this is where we invoke NDG security setup, even if we don't |
---|
151 | #need it ... |
---|
152 | self.makeGateway() |
---|
153 | |
---|
154 | #this will do for the moment, although I'd rather the whole |
---|
155 | #URI was self consistent ... |
---|
156 | |
---|
157 | if 'select' in self.FieldStorage: |
---|
158 | #need to sort out the name issue ... |
---|
159 | self.session.addToSelected(self.FieldStorage['select'],'dif') |
---|
160 | #we also need to trim the selector off the current requestURL |
---|
161 | self.requestURL=self.requestURL[0:self.requestURL.find('&select')] |
---|
162 | self.selector.baseURL=self.requestURL |
---|
163 | |
---|
164 | #use name as an error return as well in the following calls ... |
---|
165 | if 'uri' in self.FieldStorage: |
---|
166 | self.uri=self.FieldStorage['uri'] |
---|
167 | content,name=self.__browse() |
---|
168 | elif 'search' in self.FieldStorage: |
---|
169 | self.searchType=self.FieldStorage['search'] |
---|
170 | content,name=self.__search() |
---|
171 | else: |
---|
172 | content,name=self.error('No valid URI'),'Error' |
---|
173 | |
---|
174 | if not self.ViewTextOnly: |
---|
175 | historyHTML='<p>' |
---|
176 | for item in self.session.getHistory(): |
---|
177 | historyHTML+=hyperlink(item[1],item[0])+'<br/>' |
---|
178 | historyHTML+='</p>' |
---|
179 | selectHTML='<p>' |
---|
180 | for item in self.session.getSelected(): |
---|
181 | selectHTML+=hyperlink(item[1],item[0])+'<br/>' |
---|
182 | selectHTML+='</p>' |
---|
183 | self.response.content=renderPage( |
---|
184 | content,historyHTML,selectHTML,name,self.config) |
---|
185 | else: |
---|
186 | self.response.content=content |
---|
187 | self.response.cookie=self.session.makeCookie(ndgSec=self.ndgSec) |
---|
188 | return self.response |
---|
189 | |
---|
190 | def __browse(self): |
---|
191 | ''' Handle orthodox browsing ''' |
---|
192 | |
---|
193 | if self.FieldStorage.has_key('text'): |
---|
194 | self.ViewTextOnly=1 |
---|
195 | elif self.FieldStorage.has_key('xml'): |
---|
196 | self.ViewXML=1 |
---|
197 | |
---|
198 | #get the xml document |
---|
199 | db=self.config.get('db','exist',None) |
---|
200 | xml=insecureGetDoc(self.uri,db) |
---|
201 | |
---|
202 | #create stub-b instance |
---|
203 | self.b=stubB(xml,makeHTML=renderEntity) |
---|
204 | |
---|
205 | if self.b.xml is None: |
---|
206 | content=self.error('Unable to obtain stub-B from database') |
---|
207 | return content,0 |
---|
208 | else: |
---|
209 | self.session.addToHistory(self.b.Burl,self.b.abbreviation) |
---|
210 | if self.b.constraints.exist: |
---|
211 | # we need to evaluate them |
---|
212 | result=self.ndgGate.check(self.b.constraints.SimpleCondition) |
---|
213 | if result=='AccessGranted': |
---|
214 | access=1 |
---|
215 | else: |
---|
216 | access=0 |
---|
217 | else: |
---|
218 | access=1 |
---|
219 | if access: |
---|
220 | name=self.b.name |
---|
221 | if self.ViewTextOnly: |
---|
222 | self.response.contentType='text/plain' |
---|
223 | content=et2text(self.b.tree) |
---|
224 | elif self.ViewXML==1: |
---|
225 | content=et2html(self.b.tree) |
---|
226 | else: |
---|
227 | content=self.b.toHTML() |
---|
228 | else: |
---|
229 | name='NDG Browse' |
---|
230 | content=result |
---|
231 | |
---|
232 | return content,name |
---|
233 | |
---|
234 | def __search(self): |
---|
235 | ''' Handle simple searching within the context of the browse ''' |
---|
236 | if 'SEARCHSTRING' in self.FieldStorage: |
---|
237 | searchString=self.FieldStorage['SEARCHSTRING'] |
---|
238 | try: |
---|
239 | ws=DiscoveryWS() |
---|
240 | except Exception, e: |
---|
241 | return self.error('<p>%s<br/>%s'%(e,'Unable to connect to Search BackEnd')),'Error' |
---|
242 | hits=ws.SearchFullText(searchString) |
---|
243 | state=DiscoveryState(ws.sessID,self.requestURL,hits,stride=10) |
---|
244 | results=ws.GetResults(offset=state.offset,number=state.stride) |
---|
245 | difs=[] |
---|
246 | for result in results: difs.append(result) |
---|
247 | html=renderDiscoverySet(difs,state,selector=self.selector, |
---|
248 | summary=1,spatial=1,temporal=1,services=1) |
---|
249 | title='Search for '+searchString |
---|
250 | return html,title |
---|
251 | else: |
---|
252 | return self.error('No valid search option'),'Error' |
---|
253 | |
---|
254 | def error(self,message): |
---|
255 | ''' Construct a nice formal response, but don't throw a 404 ... ''' |
---|
256 | return '<p>Error: %s</p>'%message |
---|
257 | |
---|