source: MILK/trunk/milk_server/milk_server/config/milkMiddleware.py @ 5261

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/config/milkMiddleware.py@5261
Revision 5261, 8.5 KB checked in by cbyrom, 11 years ago (diff)

Lots of tidy ups to MILK codebase:

Implement new input search filters - with javascript datapickers to
pick the date ranges + add the vocab search ahead text input and
combine this with the text input.

Refactor discovery controller to tidy it up significantly - making more
structured and improving error handling and logging. Improve
templates for the search filter, splitting into multiple files to
organise better.

Various tidying up and tweaks of other codebase - e.g. standardising
use of global variables across app.

Line 
1'''
2Class defining the config params for the milk server
3'''
4from paste.deploy import CONFIG
5from ndg.common.src.models.myconfig import myConfig
6from ndg.common.src.lib.atomvalidator import AtomValidator
7from ndg.common.src.clients.http.vocabserverclient import VocabServerClient
8import milk_server.lib.helpers as h
9
10class NDGConfigError(Exception): 
11    '''Errors related to reading from ndg config file'''
12     
13class milkMiddleware:
14   
15    def __init__(self, app, g, app_conf):
16       
17        #this is the next application in the wsgi stack
18        self.app=app
19       
20        #set up the ndg configuration file
21        cf= CONFIG['app_conf']['configfile']
22        cf=myConfig(cf)
23     
24        self.globals=g
25        self.globals.htdocs=cf.get('DEFAULT','htdocs',None)
26        self.globals.localLink=cf.get('layout','localLink',None)
27        self.globals.ndgLink=cf.get('layout','ndgLink','http://ndg.nerc.ac.uk')
28        self.globals.ndgImage=cf.get('layout','ndgImage',None)
29        self.globals.ndgAlt=cf.get('layout','ndgAlt','Visit NDG')
30        self.globals.stfcLink=cf.get('layout','stfcLink')
31        self.globals.stfcImage=cf.get('layout','stfcImage')
32        self.globals.helpIcon=cf.get('layout','helpIcon')
33        self.globals.navPlus=cf.get('layout','navPlus')
34        self.globals.loadingIcon=cf.get('layout','loadingIcon')
35        self.globals.navMinus=cf.get('layout','navMinus')
36        self.globals.LeftAlt=cf.get('layout','HdrLeftAlt')
37        self.globals.LeftLogo=cf.get('layout','HdrLeftLogo')
38        self.globals.icons_xml=cf.get('layout','Xicon')
39        self.globals.icons_plot=cf.get('layout','plot')
40        self.globals.icons_prn=cf.get('layout','printer')
41        self.globals.icons_A=cf.get('NDG_A_SERVICE','icon')
42        self.globals.icons_B=cf.get('NDG_B_SERVICE','icon')
43        self.globals.icons_D=cf.get('DISCOVERY','icon')
44        self.globals.icons_R=cf.get('RELATED','icon')
45        self.globals.icons_key=cf.get('layout','key')
46        self.globals.debugModeOn = \
47            self.__getTrueFalseValue(cf.get('DEFAULT','debug')) 
48       
49        self.globals.atomEditorEnabled = \
50            self.__getTrueFalseValue(cf.get('MILK_SERVER','editorOn')) 
51
52        self.globals.browseEnabled = \
53            self.__getTrueFalseValue(cf.get('MILK_SERVER','browseOn')) 
54
55        self.globals.discoveryEnabled = \
56            self.__getTrueFalseValue(cf.get('MILK_SERVER','discoveryOn')) 
57
58        self.globals.wmcClientURL = cf.get('WMC_CLIENT','url')
59       
60        self.globals.disclaimer=cf.get('default','disclaimer')
61       
62        self.globals.discoveryURL=cf.get('SEARCH','discoveryURL')
63       
64        self.globals.server=cf.get('DEFAULT','server','')
65
66        self.globals.proxyServer = cf.get('DEFAULT','proxyServer')
67
68        self.globals.mailServer = cf.get('DEFAULT','mailServer')
69        self.globals.tbRecipient = cf.get('DEFAULT', 'tbrecipient')
70        self.globals.metadataMaintainer = cf.get('DEFAULT','metadataMaintainer')
71               
72        # for standalone discovery
73        self.globals.standalone = cf.config.getboolean('DISCOVERY','standalone')
74
75        # NB, without this set, the default host on ndg.badc.rl.ac.uk will be
76        # used by discoveryServiceClient
77        self.globals.discoveryServiceURL = cf.get('DISCOVERY', 'discoveryServiceURL')
78               
79        self.globals.pwFile = cf.get('NDG_EXIST','passwordFile')
80        self.globals.localEXist = cf.get('NDG_EXIST','local')
81        # set up the validator for atoms - to allow re-use across the app
82        self.globals.validator = AtomValidator(None,
83                                               localMILKServer = self.globals.server, 
84                                               dbConfigFile = self.globals.pwFile, 
85                                               dbHostName = self.globals.localEXist,
86                                               newLineChar="<br/>",
87                                               isDebug = self.globals.debugModeOn,
88                                               loadAllCollections = False)
89
90        # initialise client to vocab server - to do vocab term searches
91        self.globals.vocabServerClient = VocabServerClient(proxyServer = self.globals.proxyServer)
92
93        # initialise collection to store re-usable connections to the eXist DB
94        self.globals.eXistDBCons = {}
95       
96        # Security Related
97
98        # Single Sign On settings - check for mode of operation:
99        # 1) act as a client to a separate Single Sign On Service
100        # or
101        # 2) Single Sign On service is integrated into THIS service
102        securityEnabled = not self.globals.standalone
103        isSSOClient = cf.config.has_section('NDG_SECURITY.ssoClient') and \
104            securityEnabled
105           
106        isSSOService = cf.config.has_section('NDG_SECURITY.ssoService') and \
107            securityEnabled
108           
109        if isSSOClient and isSSOService:
110            raise NDGConfigError(\
111                "NDG_SECURITY.ssoClient and NDG_SECURITY.ssoService " + \
112                "sections are present in the NDG Config file: " + \
113                "only one or the other may be set")
114 
115        if isSSOClient:
116            try:
117                from \
118            ndg.security.client.ssoclient.ssoclient.config.ssoClientMiddleware\
119                    import SSOMiddleware
120            except ImportError, e:
121                # If standalone flag is not present security must be enabled
122                raise NDGConfigError(\
123                    '%s: importing Single Sign On Client SSOMiddleware: %s' % \
124                        (__name__, e))
125                       
126               
127            self.app = SSOMiddleware(app, cf.config, g, app_conf,
128                                     defSection='NDG_SECURITY.ssoClient')
129           
130            self.globals.sslServer = g.ndg.security.common.sso.cfg.sslServer
131            self.globals.wayfuri=g.ndg.security.common.sso.cfg.wayfuri
132            self.globals.logout=g.ndg.security.common.sso.cfg.logoutURI
133           
134        elif isSSOService:
135            try:
136                from ndg.security.server.sso.sso.config.ssoServiceMiddleware \
137                    import SSOMiddleware
138            except ImportError, e:
139                # If standalone flag is not present security must be enabled
140                raise NDGConfigError(\
141                    '%s: importing Single Sign On Service SSOMiddleware: %s' %\
142                        (__name__, e))
143                       
144               
145            self.app = SSOMiddleware(app, g, app_conf,
146                                     defSection='NDG_SECURITY.ssoService',
147                                     wssSection='NDG_SECURITY.wssecurity')
148           
149            self.globals.sslServer=g.ndg.security.server.sso.cfg.sslServer
150            self.globals.wayfuri=g.ndg.security.server.sso.cfg.wayfuri
151            self.globals.logout=g.ndg.security.server.sso.cfg.logoutURI
152            self.globals.getCredentials=g.ndg.security.server.sso.cfg.getCredentials
153
154        # Policy Enforcement Point initialisation
155        if securityEnabled:
156            try:
157                from ndg.security.common.authz.pep import PEP
158            except ImportError, e:
159                # If standalone flag is not present security must be enabled
160                raise NDGConfigError('%s: expecting standalone config ' % \
161                        __name__ + 
162                        'flag set to False for Policy Enforcement Point ' + \
163                        'import: %s' % e)
164           
165            self.globals.pep = PEP(cfg=cf.config, 
166                                   cfgSection='NDG_SECURITY.gatekeeper')
167           
168        self.config=cf
169       
170        self.__setUpHelpText(g)
171
172    def __getTrueFalseValue(self, val):
173        '''
174        Check for config values that could be true/false in a consistent manner
175        @param val: value read from config object
176        @return True, if val is some form of 'true'
177        '''
178        if val and val.lower() == 'true':
179            return True
180        return False
181
182    def __setUpHelpText(self, g):
183       
184        cf = CONFIG['app_conf']['helpConfigfile']
185        cf = myConfig(cf)
186        # NB, the cf.config class lowercases everything.
187        for (section, val) in cf.config.items('ATOM_EDITOR'):
188            setattr(g, section, val)
189           
190        # extend the granulite example link - to add a valid location
191        g.example_granulite = g.example_granulite.replace('HREF', \
192            h.url_for('example'))
193       
194       
195    def __call__(self,environ,start_response):
196       
197        environ['ndgConfig']=self.config
198        return self.app(environ,start_response)
199   
Note: See TracBrowser for help on using the repository browser.