Ejemplo n.º 1
0
    def login_user(self, user, remember=False):
        user_dict = user.to_dict()
        cookie_user = CookieUser(**user_dict)

        expires = None
        if remember:
            expires = 90

        self.set_secure_cookie(USER_COOKIE_NAME, helper.to_json(cookie_user.to_dict()), expires_days=expires)
Ejemplo n.º 2
0
 def method(*args, **kargs):
     url = self.__url + "/" + name
     # Put positional arguments into keyword arguments as we want all the
     # parameters to go through POST
     if args:
         kargs["__dwsargs__"] = args
     for key in kargs.keys():
         kargs[key] = to_json(kargs[key])
     result = callservice(url, headers__=self.__headers, **kargs)
     result = from_json(result)
     return result
Ejemplo n.º 3
0
 def write(self, chunk):
     if isinstance(chunk, dict):
         #The JSON Serializer does not like Python dates so we must encode them properly.
         super(BaseHandler, self).write(helper.to_json(chunk))
     else:
         super(BaseHandler, self).write(chunk)
Ejemplo n.º 4
0
    def __call__(self, environ, start_response):
        base_url = self.config['controller']
        req_url = environ.get('PATH_INFO')
        if not req_url.startswith(base_url + '/') and req_url != base_url:
            return self.not_found(start_response)

        proc_start = time.time()

        try:
            assert self.access_key == environ.get('HTTP_AUTHORIZATION')
            log.debug('Access granted with key %s' % \
                environ.get('HTTP_AUTHORIZATION'))
        except:
            log.warn('Access unauthorized. Environment:\n%s' % environ)
            return self.auth_required(start_response)

        environ['SCRIPT_NAME'] = base_url
        environ['PATH_INFO'] = environ['PATH_INFO'].split(base_url)[1]

        servicename = path_info_pop(environ)
        # Describe myself
        if not servicename:
            desc = self.describe(start_response)
            log.info('Describing esb')
            log.debug('Returning %s' % desc[0])
            return desc

        try:
            service = self.services[servicename]['service']
        except:
            # There is no such service
            log.warn('Service %s not found or not working' % servicename)
            return self.not_found(start_response)
        
        methodname = path_info_pop(environ)
        if not methodname:
            # The request about the service itself
            desc = self.describe_service(servicename, start_response)
            log.info('Describing %s' % servicename)
            log.debug('Returning %s' % desc[0])
            return desc

        # A method was called
        try:
            method = getattr(service, methodname)
            assert getattr(method, 'webmethod', False)
        except:
            # The service does not have such a webmethod
            log.warn('A webmethod %s.%s not found' % (servicename, methodname))
            return self.not_found(start_response)

        kargs = dict()
        try:
            kargs = dict(parse_formvars(environ, include_get_vars=True))
            for karg in kargs.keys():
                kargs[karg] = from_json(kargs[karg])
        except:
            log.warn('Failed to parse parameters for %s.%s: %s' % (servicename,
                methodname, kargs))
            return self.request_error(start_response, 'Invalid parameters')

        # Extract the positional arguments:
        args = kargs.pop('__dwsargs__', [])

        # Call the method
        if is_testing():
            result = method(*args, **kargs)
            result = to_json(result)
        else:
            try:
                exec_start = time.time()
                result = method(*args, **kargs)
                exec_time = (time.time() - exec_start) * 1000

                log.info('%s.%s finished in %.2f ms' % (servicename,
                    methodname, exec_time))
                log.debug('\nArgs: %s\nKargs: %s\nResult: %s' % (args, kargs,
                    result))

                result = to_json(result)
                elixir.session.close()
            except Exception, e:
                exc = format_exc(e)
                start_response('500 Internal Server Error',
                    [('Content-Type', 'text/html')])

                log.error('%s.%s\n%s' % (servicename, methodname, exc))
                elixir.session.close()
                return ['Internal server error']
Ejemplo n.º 5
0
 def describe_service(self, service, start_response):
     start_response('200 OK', [('Content-Type', 'text/html')])
     service = self.services[service]['service']
     methods = filter(lambda m: getattr(getattr(service, m),
         'webmethod', None), dir(service))
     return [to_json(sorted(methods))]
Ejemplo n.º 6
0
 def describe(self, start_response):
     start_response('200 OK', [('Content-Type', 'text/html')])
     return [to_json(sorted(self.services.keys()))]