source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgRetrieve.py @ 2643

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgRetrieve.py@2643
Revision 2643, 5.6 KB checked in by lawrence, 12 years ago (diff)

Starting to put stubB handling into ows/browse.
Starting to add trackback support.

Line 
1import DocumentRetrieve, ndgSearch, ndgObject, xmlHandler2
2import cgi,time
3from Utilities import myConfig
4from ndgSecurity import HandleSecurity
5
6debug=1
7def ndgRetrieve(uri,config,logger=0,requestor='test',format='',securityTokens=None):
8   
9    ''' Given an ndgObject, uri, retrieve it, with security in place '''
10   
11   
12    def getws(config,uri,):
13        ''' Get a ws connection to the local exist database '''
14        # The logic here is that
15        #    this code is running on server, which supports repositories sr
16        #    this particular object needs the repository for uri.repository
17        #    which had better be the same as sr, and then we use the
18        #    actual exist repository er.
19        #    I'm sure this can be cleaned up further :-)
20        r=config.get('NDG_B_SERVICE',uri.repository)
21        sr=config.get('DEFAULT','repository')
22        if r<> sr:
23            return 0,'The uri [%s] is not available on [%s]'%(uri,server)
24        er=config.get('NDG_EXIST',uri.repository)
25        pwf=config.get('DEFAULT','passwordFile')
26        ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
27        return 1,ws
28   
29    server=config.get('DEFAULT','server')
30    if uri.schema=='DIF':
31        r=server
32        ws=ndgSearch.ndgSearch()
33    elif uri.schema[0:5]=='NDG-B':
34        status,ws=getws(config,uri)
35        if not status: return status,ws
36    elif uri.schema=='NDG-A0':
37        status,ws=getws(config,uri)
38        if not status: return status,ws
39    try:
40        if uri.schema=='NDG-A0':
41            target='/db/ndg_A_metadata'
42        else:
43            target='/db/ndg_B_metadata'
44        if format=='': format=uri.schema
45        #do the actual search:
46        time1=time.time()
47        r=ws.get(uri.repository,uri.schema,uri.localID,format=format,targetCollection=target)
48        time2=time.time()-time1
49        logString='Document retrieve [%s] for [%s] took [%ss]'%(uri,requestor,time2)
50        if logger: logger.info(logString)
51
52        # did we get a sensible answer?
53        if isinstance(r,int):
54            return 0,'<p> There are %s identifiers matching your request! </p>'%r
55           
56    except Exception,e:
57        return 0,'<p> Unable to retrieve [%s], reason was [%s]</p>'%(uri,cgi.escape(str(e)))
58   
59    #temporary hack for empty docs from non-existent DIFs ...
60    #if r=='': return 0,'No matching DIF [%s]'%uri.uri
61    #if we got this far, we should have valid xml
62    if debug: file('ndgRetrieve.%s.debug.xml'%uri.uri,'w').write(r)
63       
64    x=xmlHandler2.xmlHandler(r,string=1)
65    if uri.schema=='DIF':
66        pass # no access control
67    elif uri.schema =='NDG-B0':
68        #cred=x.find('dgSecurityCondition/simpleCondition')
69        #if cred:
70        #    return 0,'<p> Access Control: <br/>[<![CDAT[%s]]> </p>'
71        pass
72    elif uri.schema =='NDG-B1':
73        pass
74    elif uri.schema =='NDG-A0':
75       
76        s=x.tree.find('{http://ndg.nerc.ac.uk/csml}AccessControlPolicy/{http://ndg.nerc.ac.uk/csml}dgSecurityCondition')
77        if s is not None:
78            status,message=HandleSecurity(s,securityTokens)
79            if not status: return 0,'<p> Access Denied </p><p>%s</p>'%message
80    return 1,x
81   
82import unittest
83
84class TestCase(unittest.TestCase):
85   
86    def setup(self):
87        self.c=myConfig('../config/ndgDiscovery.config')
88   
89    def testSOAP(self):
90        ''' test link to SOAP web service for DIF'''
91
92        self.setup()
93        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
94        uri=ndgObject.ndgObject(doc)
95        status,xml=ndgRetrieve(uri,self.c)
96        if not status: raise ValueError(xml)
97       
98    def testOriSOAP(self):
99        ''' test link to SOAP web service for DIF (original version)'''
100
101        self.setup()
102        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
103        uri=ndgObject.ndgObject(doc)
104        status,xml=ndgRetrieve(uri,self.c,format='original')
105        if not status: raise ValueError(xml)
106
107    def testEXIST(self):
108        ''' test link to local exist via xml rpc.
109                users will need to ensure that the document is actually
110                in your exist database! '''
111       
112        self.setup()
113        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
114        uri=ndgObject.ndgObject(doc)
115        status,xml=ndgRetrieve(uri,self.c)
116        if not status: raise ValueError(xml)
117       
118    def testNoSOAP(self):
119        ''' Test that we get a sensible error if a DIF doesn't exist '''
120       
121        self.setup()
122        doc='badc.nerc.ac.uk__DIF__blah'
123        uri=ndgObject.ndgObject(doc)
124        status,xml=ndgRetrieve(uri,self.c)
125        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
126       
127    def testNoB(self):
128        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
129       
130        self.setup()
131        doc='badc.nerc.ac.uk__NDG-B0__blah'
132        uri=ndgObject.ndgObject(doc)
133        status,xml=ndgRetrieve(uri,self.c)
134        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
135       
136       
137    def testXMLdif(self):
138        ''' Make sure we can encode and decode at least one DIF properly '''
139        self.setup()
140        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
141        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
142        uri=ndgObject.ndgObject(doc)
143        status,xml=ndgRetrieve(uri,self.c)
144        if status: s=str(xml)
145       
146   
147    def testSecurityCSML(self):
148        ''' Make sure that CSML security is respected '''
149        securityTokens=None
150        self.setup()
151        doc='badc.nerc.ac.uk__NDG-A0__AfEj15o6'
152        uri=ndgObject.ndgObject(doc)
153        status,xml=ndgRetrieve(uri,self.c)
154        self.assertEqual(status,0)
155       
156if __name__=="__main__":
157    unittest.main()
Note: See TracBrowser for help on using the repository browser.