source: cows/trunk/cows/pylons/wxs_fetchcontroller.py @ 4438

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/wxs_fetchcontroller.py@4438
Revision 4438, 2.7 KB checked in by domlowe, 12 years ago (diff)

Adding fetch controller for retrieving netcdf (or other) files after wfs/wcs subsetting operations

Line 
1import os, logging
2from pylons.controllers import WSGIController
3from pylons import request, response, config, session, c
4
5log=logging.getLogger(__name__)
6class FetchController(WSGIController):
7    ''' returns a file from the filestore. Used for the WCS store parameter in WCS 1.1.0, and also used for
8    fetching of documents referenced by CSML storagedescriptors in the WFS. Could also be used for other services.
9    checks the security information in the text file that accompanies the file and checks ndg security - TODO, this will need
10    revising when we have new security integration'''
11    def _getMimeType(self, file):
12        if os.path.splitext(file)[1]=='.nc':
13            return 'application/cf-netcdf'
14        else:
15            return 'application/unknown'
16       
17       
18    def fetchFile(self, file):
19        logging.info("Fetching file, '%s'" %file)
20       
21        #given the filename, these are the paths to the file, and to the accompanying text file
22        filePath=config['publish_dir'] + '/' + file
23       
24        #just return the file, no security check implemented for now.
25        fileToReturn=open(filePath, 'r')
26        mType=self._getMimeType(file)
27        response.headers['Content-Type']=mType
28        return response.write(fileToReturn.read())
29       
30        #textFilePath=config['publish_dir'] +'/'+os.path.splitext(os.path.basename(file))[0]+'.txt'
31
32#        This is all to handle ndg security, which isn't implemented here. Leaving the code here for now for future reference.
33#        #open the text file and read security credentials
34#        input =open(textFilePath, 'r')
35#        sec=input.read()
36#        log.debug('This is the security information in the text file: %s'%len(sec))
37#        #check current users credentials
38#        #if they match, return the file
39#        #TODO, check this properly
40#        if sec=='No Security':
41#            match=True #allow
42#            log.debug('There is no security on this file')     
43#        elif 'ndgSec' in session:
44#            #if username matches
45#            if sec == str(session['ndgSec']['u']):
46#                match=True #allow         
47#            else:
48#                match=False #deny
49#        else:
50#             match=False  #deny     
51#        if match:
52#            #return the file (netcdf)
53#            fileToReturn=open(filePath, 'r')
54#            mType=self._getMimeType(file)
55#                     
56#            response.headers['Content-Type']=mType
57#            return response.write(fileToReturn.read())
58#        else:
59#            #return access denied message
60#            c.xml='<div class="error">%s</div>'%'<p> Access Denied </p><p>Not Logged in</p>'
61#            return render('error')
Note: See TracBrowser for help on using the repository browser.