Ejemplo n.º 1
0
    def process_request(self, request):
        # Abort early if we don't have any work to do
        params_json = request.body
        if not params_json:
            return

        # Reject unrecognized content types. Empty string indicates
        # the client did not explicitly set the header
        if request.content_type not in ('application/json', ''):
            e = exception.ValidationError(attribute='application/json',
                                          target='Content-Type header')
            return wsgi.render_exception(e)

        params_parsed = {}
        try:
            params_parsed = jsonutils.loads(params_json)
        except ValueError:
            e = exception.ValidationError(attribute='valid JSON',
                                          target='request body')
            return wsgi.render_exception(e)
        finally:
            if not params_parsed:
                params_parsed = {}

        params = {}
        for k, v in params_parsed.iteritems():
            if k in ('self', 'context'):
                continue
            if k.startswith('_'):
                continue
            params[k] = v

        request.environ[PARAMS_ENV] = params
Ejemplo n.º 2
0
 def process_request(self, request):
     """Transform the request from XML to JSON."""
     incoming_xml = 'application/xml' in str(request.content_type)
     if incoming_xml and request.body:
         request.content_type = 'application/json'
         try:
             request.body = jsonutils.dumps(
                 serializer.from_xml(request.body))
         except Exception:
             LOG.exception('Serializer failed')
             e = exception.ValidationError(attribute='valid XML',
                                           target='request body')
             return wsgi.render_exception(e)