Package pyamf :: Package remoting :: Module amf0

[hide private]
[frames] | no frames]

# Source Code for Module pyamf.remoting.amf0

      1  # Copyright (c) 2007-2009 The PyAMF Project.
      2  # See LICENSE for details.
      3
      4  """
      5  AMF0 Remoting support.
      6
      7  @since: 0.1.0
      8  """
      9
     10  import traceback, sys
     11
     12  from pyamf import remoting
     13  from pyamf.remoting import gateway
     14
     15 -class RequestProcessor(object):
     16 -    def __init__(self, gateway):
     17          self.gateway = gateway
     18
     19 -    def authenticateRequest(self, request, service_request, *args, **kwargs):
     20          """
     21          Authenticates the request against the service.
     22
     23          @param request: The AMF request
     24          @type request: L{Request<pyamf.remoting.Request>}
     25          """
     26          username = password = None
     27
     28          if 'Credentials' in request.headers:
     29              cred = request.headers['Credentials']
     30
     31              username = cred['userid']
     32              password = cred['password']
     33
     34          return self.gateway.authenticateRequest(service_request, username,
     35              password, *args, **kwargs)
     36
     37 -    def buildErrorResponse(self, request, error=None):
     38          """
     39          Builds an error response.
     40
     41          @param request: The AMF request
     42          @type request: L{Request<pyamf.remoting.Request>}
     43          @return: The AMF response
     44          @rtype: L{Response<pyamf.remoting.Response>}
     45          """
     46          if error is not None:
     47              cls, e, tb = error
     48          else:
     49              cls, e, tb = sys.exc_info()
     50
     51          return remoting.Response(build_fault(cls, e, tb),
     52              status=remoting.STATUS_ERROR)
     53
     54 -    def _getBody(self, request, response, service_request, **kwargs):
     55          if 'DescribeService' in request.headers:
     56              return service_request.service.description
     57
     58          return self.gateway.callServiceRequest(service_request, *request.body,
     59              **kwargs)
     60
     61 -    def __call__(self, request, *args, **kwargs):
     62          """
     63          Processes an AMF0 request.
     64
     65          @param request: The request to be processed.
     66          @type request: L{Request<pyamf.remoting.Request>}
     67
     68          @return: The response to the request.
     69          @rtype: L{Response<pyamf.remoting.Response>}
     70          """
     71          response = remoting.Response(None)
     72
     73          try:
     74              service_request = self.gateway.getServiceRequest(request,
     75                  request.target)
     76          except gateway.UnknownServiceError, e:
     77              return self.buildErrorResponse(request)
     78
     79          # we have a valid service, now attempt authentication
     80          try:
     81              authd = self.authenticateRequest(request, service_request, *args,
     82                  **kwargs)
     83          except (SystemExit, KeyboardInterrupt):
     84              raise
     85          except:
     86              return self.buildErrorResponse(request)
     87
     88          if not authd:
     89              # authentication failed
     90              response.status = remoting.STATUS_ERROR
     91              response.body = remoting.ErrorFault(code='AuthenticationError',
     92                  description='Authentication failed')
     93
     94              return response
     95
     96          # authentication succeeded, now fire the preprocessor (if there is one)
     97          try:
     98              self.gateway.preprocessRequest(service_request, *args, **kwargs)
     99          except (SystemExit, KeyboardInterrupt):
    100              raise
    101          except:
    102              return self.buildErrorResponse(request)
    103
    104          try:
    105              response.body = self._getBody(request, response, service_request,
    106                  *args, **kwargs)
    107
    108              return response
    109          except (SystemExit, KeyboardInterrupt):
    110              raise
    111          except:
    112              return self.buildErrorResponse(request)
    113
    114 -def build_fault(cls, e, tb):
    115      """
    116      Builds a L{ErrorFault<pyamf.remoting.ErrorFault>} object based on the last
    117      exception raised.
    118      """
    119      if hasattr(cls, '_amf_code'):
    120          code = cls._amf_code
    121      else:
    122          code = cls.__name__
    123
    124      return remoting.ErrorFault(code=code, description=str(e),
    125          details=str(traceback.format_exception(cls, e, tb)).replace("\\n", ''))
    126