source: TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/pylons/ows_controller.py @ 3216

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/pylons/ows_controller.py@3216
Revision 3216, 4.8 KB checked in by spascoe, 12 years ago (diff)

Removing all except 1 of the references to ows_server. The last one will go away when we abstract away security.

Line 
1"""
2Base controller for OGC Web Services (OWS).
3
4@author: Stephen Pascoe
5"""
6
7
8from pylons import Response, c, g, cache, request, session
9from pylons.controllers import WSGIController
10from pylons.decorators import jsonify, validate
11from pylons.templating import render, render_response
12from pylons.helpers import abort, redirect_to, etag_cache
13
14import webhelpers as h
15
16#!TODO: Replace this with a pluggable security system
17from ows_server.lib.security_util import setSecuritySession, LoginServiceQuery
18
19from ows_common import exceptions as OWS_E
20from ows_common.operations_metadata import OperationsMetadata, Operation, RequestMethod
21from ows_common.get_capabilities import ServiceMetadata
22import ows_common.xml
23
24
25try:
26    from xml.etree import ElementTree as ET
27except ImportError:
28    from elementtree import ElementTree as ET
29
30import logging
31logger = logging.getLogger(__name__)
32
33# Configure
34#!TODO: rename this configuration object to something non-NDG specific
35EXCEPTION_TYPE = request.environ['ndgConfig'].get('OWS_SERVER', 'exception_type', 'ogc').lower()
36
37
38
39class OwsController(WSGIController):
40    def __call__(self, environ, start_response):
41
42        # All OWS parameter names are case insensitive.
43        req = request._current_obj()
44        self.ows_params = {}
45        for k in req.params:
46            self.ows_params[k.lower()] = req.params[k]       
47
48        # If the EXCEPTION_TYPE is 'pylons' let Pylons catch any exceptions.
49        # Otherwise send an OGC exception report for any OWS_E.OwsError
50        if 'pylons' in EXCEPTION_TYPE:
51            self._fixOwsAction(environ)
52            return super(OwsController, self).__call__(environ, start_response)
53        else:
54            try:
55                self._fixOwsAction(environ)
56                return super(OwsController, self).__call__(environ, start_response)
57            except OWS_E.OwsError, e:
58                logger.exception(e)
59
60                r=render_response('exception_report', report=e.report, format='xml')
61                r.headers['content-type'] = 'text/xml'
62                return r
63
64
65    def _fixOwsAction(self, environ):
66        # Override the Routes action from the request query parameter
67        try:
68            action = self.ows_params['request']
69        except KeyError:
70            raise OWS_E.MissingParameterValue('REQUEST parameter not specified', 'REQUEST')
71
72        # Check action is a method in self
73        if not getattr(self, action):
74            raise OWS_E.InvalidParameterValue('request=%s not supported' % action, 'REQUEST')
75
76        # override routes action with request
77        environ['pylons.routes_dict']['action'] = action
78        del self.ows_params['request']
79
80    def _loadCapabilities(self):
81        """
82        creates an ows_common.get_capabilities.ServiceMetadata object
83        by consulting the paste configuration and annotations in the
84        controller definition.
85
86        """
87        # Deduce ows_endpoint from routes
88        ows_endpoint = h.url_for(controller=request.environ['pylons.routes_dict']['controller'])
89       
90        #Deduce base_url from config
91        base_url =request.environ['ndgConfig'].get('DEFAULT','server')
92
93       
94        # Get the server-level configuration data from an XML file
95        config = request.environ['paste.config']
96        sm_tree = ET.parse(config['ows_common_config'])
97        sm = ows_common.xml.service_metadata(sm_tree.getroot())
98       
99        # Extract service-level parameters and constraint
100        parameters = getattr(self, '_ows_parameters', {})
101        constraints = getattr(self, '_ows_constraints', {})
102        versions = getattr(self, '_ows_versions', [])
103       
104        # Extract operation-level parameters and constraints
105        od = {}
106        for attr in dir(self):
107            op = getattr(self, attr)
108            if hasattr(op, '_ows_name'):
109                p = getattr(op, '_ows_parameters', {})
110                c = getattr(op, '_ows_constraints', {})
111                od[op._ows_name] = Operation(get=RequestMethod(href=base_url+ows_endpoint),
112                                             post=None,
113                                             parameters=p,
114                                             constraints=c,
115                                             name=op._ows_name)
116       
117        sm.operationsMetadata = OperationsMetadata(od, constraints, parameters)
118        sm.serviceIdentification.serviceTypeVersions = versions
119        return sm
120
121    def _renderCapabilities(self, template='ows/get_capabilities'):
122        """
123        The standard way of returning a Capabilities document.
124
125        Each subclass should implement self._load_capabilities() and call
126        this method to return a response object.
127
128        """
129        c.service_metadata = self._loadCapabilities()       
130        r = render_response(template, format='xml')
131        r.headers['content-type'] = 'text/xml'
132        return r
133
Note: See TracBrowser for help on using the repository browser.