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

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

moving the passwordFile and configFile variables so they respect %(here)s - needed to start paster from /etc/init.d at boot up

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