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

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

A whole raft of modifications

  • changing to the NERC icons
  • search support within Browse and NumSim
  • NumSim V10 support in viewing
  • older numsim docs will now not be found and displayed properly (which means a lot of updating is needed)
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.ndgHTTP(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    else:
48        status=0
49        ws='<p> Invalid scheme in NDG format URI </p>'
50     
51    if not status: return status,ws
52           
53    #try:
54    if uri.schema=='NDG-A0':
55        target='/db/ndg_A_metadata'
56    elif uri.schema=='NumSim':
57        target='/db/NumSim'
58    else:
59        target='/db/ndg_B_metadata'
60     
61    if output=='': output=uri.schema
62   
63    #do the actual retrieve:
64    e=None
65    try:
66        time1=time.time()
67        r=ws.get(uri.repository,uri.schema,uri.localID,format=output,targetCollection=target)
68        time2=time.time()-time1
69        logString='Document retrieve [%s] took [%s]'%(uri,time2)
70    except Exception,e:
71        e=str(e)
72        r=e
73        logString='Document retrieve [%s] failed [%s]'%(uri,e)
74   
75    if logger: logger.info(logString)
76
77    # did we get a sensible answer?
78    if isinstance(r,int):
79        return 0,'<p> There are %s identifiers matching your request! </p>'%r
80    if e is not None:
81        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
82   
83    try:
84        x=xmlHandler2.xmlHandler(r,string=1)
85        return 1,x
86    except Exception,e:
87        logString='Document parse for [%s] failed [%s]'%(uri,e)
88        if logger: logger.info(logString)
89        return 0,'''<p>Unable to parse documment [%s]<br/>
90                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
91   
92   
93import unittest
94
95class TestCase(unittest.TestCase):
96   
97    def setup(self):
98        self.c=myConfig('../../ndgDiscovery.config')
99   
100    def testSOAP(self):
101        ''' test link to SOAP web service for DIF'''
102
103        self.setup()
104        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
105        uri=ndgObject.ndgObject(doc)
106        status,xml=ndgRetrieve(uri,self.c)
107        if not status: raise ValueError(xml)
108       
109    def testOriSOAP(self):
110        ''' test link to SOAP web service for DIF (original version)'''
111
112        self.setup()
113        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
114        uri=ndgObject.ndgObject(doc)
115        status,xml=ndgRetrieve(uri,self.c,output='original')
116        if not status: raise ValueError(xml)
117
118    def testEXIST(self):
119        ''' test link to local exist via xml rpc.
120                users will need to ensure that the document is actually
121                in your exist database! '''
122       
123        self.setup()
124        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
125        doc='neodc.nerc.ac.uk__NDG-B1__NEODC_NCAVEO_LCM2000'
126        uri=ndgObject.ndgObject(doc)
127        status,xml=ndgRetrieve(uri,self.c)
128        if not status: raise ValueError(xml)
129       
130    def testNoSOAP(self):
131        ''' Test that we get a sensible error if a DIF doesn't exist '''
132       
133        self.setup()
134        doc='badc.nerc.ac.uk__DIF__blah'
135        uri=ndgObject.ndgObject(doc)
136        status,xml=ndgRetrieve(uri,self.c)
137        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
138       
139    def testNoB(self):
140        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
141       
142        self.setup()
143        doc='badc.nerc.ac.uk__NDG-B0__blah'
144        uri=ndgObject.ndgObject(doc)
145        status,xml=ndgRetrieve(uri,self.c)
146        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
147       
148       
149    def testXMLdif(self):
150        ''' Make sure we can encode and decode at least one DIF properly '''
151        self.setup()
152        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
153        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
154        uri=ndgObject.ndgObject(doc)
155        status,xml=ndgRetrieve(uri,self.c)
156        if status: s=str(xml)
157
158    def testRemoteGet(self):
159        ''' Attempt a remote get '''
160        self.setup()
161        doc='www.npm.ac.uk__NDG-B1__data_entity.692'
162        uri=ndgObject.ndgObject(doc)
163        status,xml=ndgRetrieve(uri,self.c,remote=1)
164        if not status: print xml
165        self.assertEqual(status,1)
166       
167    def testNumSim(self):
168        '''Get a NumSim document '''
169        self.setup()
170        doc='badc.nerc.ac.uk__NumSim__HadAM2_CodeBase'
171        uri=ndgObject.ndgObject(doc)
172        status,xml=ndgRetrieve(uri,self.c)
173        print status,xml
174       
175       
176if __name__=="__main__":
177
178    unittest.main()
Note: See TracBrowser for help on using the repository browser.