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

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

Interim commit to support common cache. At this point
it's implemented in retrieve (although not obviously
working in all cases).

Line 
1import DocumentRetrieve, ndgSearch, ndgObject, xmlHandler2
2import cgi,time
3from Utilities import myConfig
4
5
6debug=0
7def ndgRetrieve(uri,config,logger=0,format=''):
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' or uri.schema=='NumSim':
34        if not debug: 
35            status,ws=getws(config,uri)
36            if not status: return status,ws
37    elif uri.schema=='NDG-A0':
38        status,ws=getws(config,uri)
39        if not status: return status,ws
40    #try:
41    if uri.schema=='NDG-A0':
42        target='/db/ndg_A_metadata'
43    elif uri.schema=='NumSim':
44        target='/db/NumSim'
45    else:
46        target='/db/ndg_B_metadata'
47    if format=='': format=uri.schema
48    #do the actual search:
49    time1=time.time()
50    print uri.uri
51    if debug:
52        if uri.uri in ['badc.nerc.ac.uk__NDG-B1__dataent_COAPEC',
53                        'badc.nerc.ac.uk__NumSim__HiGEM_6.1_CodeBase',
54                        'badc.nerc.ac.uk__NumSim__HiGEM_6.1_Control']:
55            f=str('ows_server/examples/%s.xml'%uri.uri)
56            r=file(f,'r').read()
57    else:
58        r=ws.get(uri.repository,uri.schema,uri.localID,format=format,targetCollection=target)
59    time2=time.time()-time1
60    logString='Document retrieve [%s] took [%s]'%(uri,time2)
61    if logger: logger.info(logString)
62
63    # did we get a sensible answer?
64    if isinstance(r,int):
65        return 0,'<p> There are %s identifiers matching your request! </p>'%r
66       
67    #except Exception,e:
68    #    return 0,'<p> Unable to retrieve [%s], reason was [%s]</p>'%(uri,cgi.escape(str(e)))
69   
70    #temporary hack for empty docs from non-existent DIFs ...
71    #if r=='': return 0,'No matching DIF [%s]'%uri.uri
72    #if we got this far, we should have valid xml
73    if debug: file('ndgRetrieve.%s.debug.xml'%uri.uri,'w').write(r)
74       
75    x=xmlHandler2.xmlHandler(r,string=1)
76   
77    return 1,x
78   
79import unittest
80
81class TestCase(unittest.TestCase):
82   
83    def setup(self):
84        self.c=myConfig('../config/ndgDiscovery.config')
85   
86    def testSOAP(self):
87        ''' test link to SOAP web service for DIF'''
88
89        self.setup()
90        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
91        uri=ndgObject.ndgObject(doc)
92        status,xml=ndgRetrieve(uri,self.c)
93        if not status: raise ValueError(xml)
94       
95    def testOriSOAP(self):
96        ''' test link to SOAP web service for DIF (original version)'''
97
98        self.setup()
99        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
100        uri=ndgObject.ndgObject(doc)
101        status,xml=ndgRetrieve(uri,self.c,format='original')
102        if not status: raise ValueError(xml)
103
104    def testEXIST(self):
105        ''' test link to local exist via xml rpc.
106                users will need to ensure that the document is actually
107                in your exist database! '''
108       
109        self.setup()
110        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
111        doc='neodc.nerc.ac.uk__NDG-B1__NEODC_NCAVEO_LCM2000'
112        uri=ndgObject.ndgObject(doc)
113        status,xml=ndgRetrieve(uri,self.c)
114        if not status: raise ValueError(xml)
115       
116    def testNoSOAP(self):
117        ''' Test that we get a sensible error if a DIF doesn't exist '''
118       
119        self.setup()
120        doc='badc.nerc.ac.uk__DIF__blah'
121        uri=ndgObject.ndgObject(doc)
122        status,xml=ndgRetrieve(uri,self.c)
123        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
124       
125    def testNoB(self):
126        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
127       
128        self.setup()
129        doc='badc.nerc.ac.uk__NDG-B0__blah'
130        uri=ndgObject.ndgObject(doc)
131        status,xml=ndgRetrieve(uri,self.c)
132        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
133       
134       
135    def testXMLdif(self):
136        ''' Make sure we can encode and decode at least one DIF properly '''
137        self.setup()
138        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
139        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
140        uri=ndgObject.ndgObject(doc)
141        status,xml=ndgRetrieve(uri,self.c)
142        if status: s=str(xml)
143
144       
145       
146if __name__=="__main__":
147
148    unittest.main()
Note: See TracBrowser for help on using the repository browser.