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

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

First step to remote ndg document retrieval

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,output='',remote=0):
8   
9    ''' Given an ndgObject, uri, retrieve it, with security in place.
10            If logger is non-zero it should be a python log class
11            If output is not '' it should be desired output schema (normally
12        to be used for discovery documents in a variety of outputs or
13        to indicate that an original document is required.
14            If remote is non zero, then attempt to get the document
15        from a remote site via a (secured) restful http call'''
16   
17    def getws(config,uri,remote):
18        ''' Get a ws connection to the local exist database '''
19        # The logic here is that
20        #    this code is running on server, which supports repositories sr
21        #    this particular object needs the repository for uri.repository
22        #    which had better be the same as sr, and then we use the
23        #    actual exist repository er.
24        #    I'm sure this can be cleaned up further :-)
25        r=config.get('NDG_B_SERVICE',uri.repository)
26        sr=config.get('DEFAULT','repository')
27        er=config.get('NDG_EXIST',uri.repository)
28        if r<> sr:
29            if not remote:
30                return 0,'The uri [%s] is not available on [%s]'%(uri,server)
31            else:
32                ps=config.get('DEFAULT','proxyServer')
33                ws=DocumentRetrieve.RestfulGet(r,proxyServer=ps)
34        else:
35            pwf=config.get('NDG_EXIST','passwordFile')
36            ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
37        return 1,ws
38   
39    server=config.get('DEFAULT','server')
40    if uri.schema in ['DIF','MDIP']:
41        r=server
42        status,ws=1,ndgSearch.ndgSearch()
43    elif uri.schema[0:5]=='NDG-B' or uri.schema=='NumSim':
44        status,ws=getws(config,uri,remote)
45    elif uri.schema=='NDG-A0':
46        status,ws=getws(config,uri,remote)
47     
48    if not status: return status,ws
49           
50    #try:
51    if uri.schema=='NDG-A0':
52        target='/db/ndg_A_metadata'
53    elif uri.schema=='NumSim':
54        target='/db/NumSim'
55    else:
56        target='/db/ndg_B_metadata'
57     
58    if output=='': output=uri.schema
59   
60    #do the actual retrieve:
61    e=None
62    try:
63        time1=time.time()
64        r=ws.get(uri.repository,uri.schema,uri.localID,format=output,targetCollection=target)
65        time2=time.time()-time1
66        logString='Document retrieve [%s] took [%s]'%(uri,time2)
67    except Exception,e:
68        e=str(e)
69        r=e
70        logString='Document retrieve [%s] failed [%s]'%(uri,e)
71   
72    if logger: logger.info(logString)
73
74    # did we get a sensible answer?
75    if isinstance(r,int):
76        return 0,'<p> There are %s identifiers matching your request! </p>'%r
77    if e is not None:
78        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
79   
80    try:
81        x=xmlHandler2.xmlHandler(r,string=1)
82        return 1,x
83    except Exception,e:
84        logString='Document parse for [%s] failed [%s]'%(uri,e)
85        if logger: logger.info(logString)
86        return 0,'''<p>Unable to parse documment [%s]<br/>
87                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
88   
89   
90import unittest
91
92class TestCase(unittest.TestCase):
93   
94    def setup(self):
95        self.c=myConfig('../../ndgDiscovery.config')
96   
97    def testSOAP(self):
98        ''' test link to SOAP web service for DIF'''
99
100        self.setup()
101        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
102        uri=ndgObject.ndgObject(doc)
103        status,xml=ndgRetrieve(uri,self.c)
104        if not status: raise ValueError(xml)
105       
106    def testOriSOAP(self):
107        ''' test link to SOAP web service for DIF (original version)'''
108
109        self.setup()
110        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
111        uri=ndgObject.ndgObject(doc)
112        status,xml=ndgRetrieve(uri,self.c,output='original')
113        if not status: raise ValueError(xml)
114
115    def testEXIST(self):
116        ''' test link to local exist via xml rpc.
117                users will need to ensure that the document is actually
118                in your exist database! '''
119       
120        self.setup()
121        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
122        doc='neodc.nerc.ac.uk__NDG-B1__NEODC_NCAVEO_LCM2000'
123        uri=ndgObject.ndgObject(doc)
124        status,xml=ndgRetrieve(uri,self.c)
125        if not status: raise ValueError(xml)
126       
127    def testNoSOAP(self):
128        ''' Test that we get a sensible error if a DIF doesn't exist '''
129       
130        self.setup()
131        doc='badc.nerc.ac.uk__DIF__blah'
132        uri=ndgObject.ndgObject(doc)
133        status,xml=ndgRetrieve(uri,self.c)
134        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
135       
136    def testNoB(self):
137        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
138       
139        self.setup()
140        doc='badc.nerc.ac.uk__NDG-B0__blah'
141        uri=ndgObject.ndgObject(doc)
142        status,xml=ndgRetrieve(uri,self.c)
143        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
144       
145       
146    def testXMLdif(self):
147        ''' Make sure we can encode and decode at least one DIF properly '''
148        self.setup()
149        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
150        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
151        uri=ndgObject.ndgObject(doc)
152        status,xml=ndgRetrieve(uri,self.c)
153        if status: s=str(xml)
154
155    def testRemoteGet(self):
156        ''' Attempt a remote get '''
157        self.setup()
158        doc='www.npm.ac.uk__NDG-B1__data_entity.692'
159        uri=ndgObject.ndgObject(doc)
160        status,xml=ndgRetrieve(uri,self.c,remote=1)
161        if not status: print xml
162        self.assertEqual(status,1)
163       
164       
165if __name__=="__main__":
166
167    unittest.main()
Note: See TracBrowser for help on using the repository browser.