source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/base.py @ 2502

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/base.py@2502
Revision 2502, 3.9 KB checked in by spascoe, 12 years ago (diff)

Some bug fixes to ows_common. OwsController?._load_capabilities implements
discovery of capabilities from a controller definition.

Line 
1from pylons import Response, c, g, cache, request, session
2from pylons.controllers import WSGIController
3from pylons.decorators import jsonify, validate
4from pylons.templating import render, render_response
5from pylons.helpers import abort, redirect_to, etag_cache
6from pylons.i18n import N_, _, ungettext
7import ows_server.models as model
8import ows_server.lib.helpers as h
9
10from ows_common import exceptions as OWS_E
11from ows_common.operations_metadata import OperationsMetadata, Operation
12from ows_common.get_capabilities import ServiceMetadata
13
14class BaseController(WSGIController):
15    def __call__(self, environ, start_response):
16        # Insert any code to be run per request here. The Routes match
17        # is under environ['pylons.routes_dict'] should you want to check
18        # the action or route vars here
19        return WSGIController.__call__(self, environ, start_response)
20   
21class OwsController(BaseController):
22    def __call__(self, environ, start_response):
23
24        # Ensure the capabilities document is loaded
25        self._load_capabilities()
26
27        # All OWS parameter names are case insensitive.
28        req = request._current_obj()
29        self.ows_params = {}
30        for k in req.params:
31            self.ows_params[k.lower()] = req.params[k]       
32
33        try:
34            self._fix_ows_action(environ)
35            return WSGIController.__call__(self, environ, start_response) 
36        except OwsError, e:
37            return render_response('exception_report', report=e.report,
38                                   format='xml')
39           
40 
41    def _fix_ows_action(self, environ):
42        # Override the Routes action from the request query parameter
43        try:
44            action = self.ows_params['request']
45        except KeyError:
46            raise OWS_E.MissingParameterValue('REQUEST parameter not specified', 'REQUEST')
47
48        # Check action is a method in self
49        if not getattr(self, action):
50            raise OWS_E.InvalidParameterValue('request=%s not supported' % action, 'REQUEST')
51
52        # override routes action with request
53        environ['pylons.routes_dict']['action'] = action
54        del self.ows_params['request']
55
56    def _get_method_args(self):
57        """Extends Controller._get_method_args to retrieve arguments
58        from the query string.
59
60        See the docstring for Controller._get_method_args for details.
61
62        """
63
64        # Get the method arguments and add the query string arguments
65        kargs = WSGIController._get_method_args(self)
66        kargs.update(self.ows_params)
67
68        return kargs
69
70    def _load_capabilities(self, ows_endpoint):
71        """
72        creates an ows_common.get_capabilities.ServiceMetadata object
73        by consulting the paste configuration and annotations in the
74        controller definition.  The endpoint must be supplied to allow
75        a controller to serve multiple ows endpoints.
76
77        @param ows_endpoint: The URI of the OWS endpoint being served
78            by this controller.
79
80        """
81        if hasattr(self, '_ows_service_metadata'):
82            return
83
84        parameters = getattr(self, '_ows_parameters', {})
85        constraints = getattr(self, '_ows_constraints', {})
86
87        od = {}
88        for attr in dir(self):
89            op = getattr(self, attr)
90            if hasattr(op, '_ows_name'):
91                p = getattr(op, '_ows_parameters', {})
92                c = getattr(op, '_ows_constraints', {})
93                od[op._ows_name] = Operation(get=ows_endpoint,
94                                                    post=None,
95                                                    parameters=p,
96                                                    constraints=c)
97       
98        om = OperationsMetadata(od, constraints, parameters)
99        return ServiceMetadata(operationsMetadata=om)
100
101# Include the '_' function in the public names
102__all__ = [__name for __name in locals().keys() if not __name.startswith('_') \
103           or __name == '_']
Note: See TracBrowser for help on using the repository browser.