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

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

Fixing the error returning in ndgRetrieve ...

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